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.bind;
17
18 import net.bytebuddy.description.method.MethodDescription;
19 import net.bytebuddy.description.type.TypeDescription;
20
21 /**
22  * This ambiguity resolver matches that method out of two methods that is declared by the more specific type. If two
23  * methods are declared by the same type or by two unrelated types, this resolver returns an ambiguous result.
24  */

25 public enum DeclaringTypeResolver implements MethodDelegationBinder.AmbiguityResolver {
26
27     /**
28      * The singleton instance.
29      */

30     INSTANCE;
31
32     /**
33      * {@inheritDoc}
34      */

35     public Resolution resolve(MethodDescription source,
36                               MethodDelegationBinder.MethodBinding left,
37                               MethodDelegationBinder.MethodBinding right) {
38         TypeDescription leftType = left.getTarget().getDeclaringType().asErasure();
39         TypeDescription rightType = right.getTarget().getDeclaringType().asErasure();
40         if (leftType.equals(rightType)) {
41             return Resolution.AMBIGUOUS;
42         } else if (leftType.isAssignableFrom(rightType)) {
43             return Resolution.RIGHT;
44         } else if (leftType.isAssignableTo(rightType)) {
45             return Resolution.LEFT;
46         } else {
47             return Resolution.AMBIGUOUS;
48         }
49     }
50 }
51