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.reference;
17
18 import net.bytebuddy.description.type.TypeDescription;
19 import net.bytebuddy.implementation.bytecode.StackManipulation;
20 import net.bytebuddy.implementation.bytecode.assign.Assigner;
21 import net.bytebuddy.implementation.bytecode.assign.TypeCasting;
22
23 /**
24 * A simple assigner that is capable of handling the casting of reference types. Primitives can only be assigned to
25 * each other if they represent the same type.
26 */
27 public enum ReferenceTypeAwareAssigner implements Assigner {
28
29 /**
30 * The singleton instance.
31 */
32 INSTANCE;
33
34 /**
35 * {@inheritDoc}
36 */
37 public StackManipulation assign(TypeDescription.Generic source, TypeDescription.Generic target, Typing typing) {
38 if (source.isPrimitive() || target.isPrimitive()) {
39 return source.equals(target)
40 ? StackManipulation.Trivial.INSTANCE
41 : StackManipulation.Illegal.INSTANCE;
42 } else if (source.asErasure().isAssignableTo(target.asErasure())) {
43 return StackManipulation.Trivial.INSTANCE;
44 } else if (typing.isDynamic()) {
45 return TypeCasting.to(target);
46 } else {
47 return StackManipulation.Illegal.INSTANCE;
48 }
49 }
50 }
51