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
20 /**
21 * Implementation of an
22 * {@link net.bytebuddy.implementation.bind.MethodDelegationBinder.AmbiguityResolver}
23 * that resolves conflicting bindings by considering equality of a target method's internalName as an indicator for a dominant
24 * binding.
25 * <p> </p>
26 * For example, if method {@code source.foo} can be bound to methods {@code targetA.foo} and {@code targetB.bar},
27 * {@code targetA.foo} will be considered as dominant.
28 */
29 public enum MethodNameEqualityResolver implements MethodDelegationBinder.AmbiguityResolver {
30
31 /**
32 * The singleton instance.
33 */
34 INSTANCE;
35
36 /**
37 * {@inheritDoc}
38 */
39 public Resolution resolve(MethodDescription source,
40 MethodDelegationBinder.MethodBinding left,
41 MethodDelegationBinder.MethodBinding right) {
42 boolean leftEquals = left.getTarget().getName().equals(source.getName());
43 boolean rightEquals = right.getTarget().getName().equals(source.getName());
44 if (leftEquals ^ rightEquals) {
45 return leftEquals ? Resolution.LEFT : Resolution.RIGHT;
46 } else {
47 return Resolution.AMBIGUOUS;
48 }
49 }
50 }
51