1 /*
2  * Copyright 2014 - 2020 Rafael Winterhalter
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.bytebuddy.implementation.bytecode.assign.primitive;
17
18 import net.bytebuddy.build.HashCodeAndEqualsPlugin;
19 import net.bytebuddy.description.type.TypeDescription;
20 import net.bytebuddy.implementation.bytecode.StackManipulation;
21 import net.bytebuddy.implementation.bytecode.assign.Assigner;
22
23 /**
24  * This assigner is able to handle non-{@code void}, primitive types. This means:
25  * <ol>
26  * <li>If a primitive type is assigned to a non-primitive type, it will attempt to widen the source type into the
27  * target type.</li>
28  * <li>If a primitive type is assigned to a non-primitive type, it will attempt to box the source type and then
29  * query the chained assigner for assigning the boxed type to the target type.</li>
30  * <li>If a non-primitive type is assigned to a primitive type, it will unbox the source type and then attempt a
31  * widening of the unboxed type into the target type. If the source type does not represent a wrapper type, it will
32  * attempt to infer the boxing type from the target type and check if the source type is assignable to this wrapper
33  * type.</li>
34  * <li>If two non-primitive types are subject of the assignment, it will delegate the assignment to its chained
35  * assigner.</li>
36  * </ol>
37  */

38 @HashCodeAndEqualsPlugin.Enhance
39 public class PrimitiveTypeAwareAssigner implements Assigner {
40
41     /**
42      * Another assigner that is aware of assigning reference types. This assigner is queried for assigning
43      * non-primitive types or for assigning a boxed type to another non-primitive type.
44      */

45     private final Assigner referenceTypeAwareAssigner;
46
47     /**
48      * Creates a new assigner with the given delegate.
49      *
50      * @param referenceTypeAwareAssigner A chained assigner that is queried for assignments not involving primitive
51      *                                   types.
52      */

53     public PrimitiveTypeAwareAssigner(Assigner referenceTypeAwareAssigner) {
54         this.referenceTypeAwareAssigner = referenceTypeAwareAssigner;
55     }
56
57     /**
58      * {@inheritDoc}
59      */

60     public StackManipulation assign(TypeDescription.Generic source, TypeDescription.Generic target, Typing typing) {
61         if (source.isPrimitive() && target.isPrimitive()) {
62             return PrimitiveWideningDelegate.forPrimitive(source).widenTo(target);
63         } else if (source.isPrimitive() /* && !target.isPrimitive() */) {
64             return PrimitiveBoxingDelegate.forPrimitive(source).assignBoxedTo(target, referenceTypeAwareAssigner, typing);
65         } else if (/* !source.isPrimitive() && */ target.isPrimitive()) {
66             return PrimitiveUnboxingDelegate.forReferenceType(source).assignUnboxedTo(target, referenceTypeAwareAssigner, typing);
67         } else /* !source.isPrimitive() && !target.isPrimitive()) */ {
68             return referenceTypeAwareAssigner.assign(source, target, typing);
69         }
70     }
71 }
72