Skip to content

Commit

Permalink
Added tests for properly testing bridges of Java 8 interfaces. Remove…
Browse files Browse the repository at this point in the history
…d bridge matchers as these are not longer required for matching methods that were resolved by a method graph.
  • Loading branch information
Rafael Winterhalter committed Aug 3, 2015
1 parent 74ebf9f commit 34df71c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 403 deletions.
2 changes: 1 addition & 1 deletion byte-buddy-dep/src/main/java/net/bytebuddy/ByteBuddy.java
Expand Up @@ -155,7 +155,7 @@ public ByteBuddy(ClassFileVersion classFileVersion) {
new NamingStrategy.Unbound.Default(BYTE_BUDDY_DEFAULT_PREFIX),
new AuxiliaryType.NamingStrategy.SuffixingRandom(BYTE_BUDDY_DEFAULT_SUFFIX),
new TypeList.Empty(),
isDefaultFinalizer().or(isSynthetic().and(not(isVisibilityBridge()))),
isSynthetic().or(isDefaultFinalizer()),
new ClassVisitorWrapper.Chain(),
new MethodRegistry.Default(),
new Definable.Undefined<Integer>(),
Expand Down
Expand Up @@ -1134,14 +1134,6 @@ public static <T extends MethodDescription> ElementMatcher.Junction<T> isTypeIni
return new MethodSortMatcher<T>(MethodSortMatcher.Sort.TYPE_INITIALIZER);
}

public static <T extends MethodDescription> ElementMatcher.Junction<T> isTypeBridge() {
return new MethodSortMatcher<T>(MethodSortMatcher.Sort.TYPE_BRIDGE);
}

public static <T extends MethodDescription> ElementMatcher.Junction<T> isVisibilityBridge() {
return new MethodSortMatcher<T>(MethodSortMatcher.Sort.VISIBILITY_BRIDGE);
}

public static <T extends MethodDescription> ElementMatcher.Junction<T> isVirtual() {
return new MethodSortMatcher<T>(MethodSortMatcher.Sort.VIRTUAL);
}
Expand Down
@@ -1,9 +1,6 @@
package net.bytebuddy.matcher;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.generic.GenericTypeDescription;

import static net.bytebuddy.matcher.ElementMatchers.*;

/**
* Matches a method description by its general characteristics which are represented as a
Expand Down Expand Up @@ -94,40 +91,6 @@ protected boolean isSort(MethodDescription target) {
}
},

/**
* Matches method descriptions that represent bridge methods that are implemented in order to increase
* a method's visibility in a subtype.
*/
VISIBILITY_BRIDGE("isVisibilityBridge()") {
@Override
protected boolean isSort(MethodDescription target) {
if (target.isBridge()) {
if (target.getDeclaringType().asRawType().isInterface()) {
return false;
}
for (GenericTypeDescription currentType : target.getDeclaringType().getSuperType()) {
for (MethodDescription methodDescription : currentType.getDeclaredMethods()) {
if (methodDescription.asDefined().asToken().equals(target.asToken())) {
return !methodDescription.isBridge() && target.getDeclaringType().asRawType().getDeclaredMethods()
.filter(not(isBridge())
.and(hasMethodName(methodDescription.getInternalName()))
.and(takesArguments(methodDescription.getParameters().asTypeList().asRawTypes())))
.isEmpty();
}
}
}
}
return false;
}
},

TYPE_BRIDGE("isTypeBridge()") {
@Override
protected boolean isSort(MethodDescription target) {
return target.isBridge() && !VISIBILITY_BRIDGE.isSort(target);
}
},

/**
* Matches method descriptions that represent Java 8 default methods.
*/
Expand Down
Expand Up @@ -3,8 +3,11 @@
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.test.utility.JavaVersionRule;
import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;

import static net.bytebuddy.matcher.ElementMatchers.*;
import static org.hamcrest.CoreMatchers.is;
Expand All @@ -14,6 +17,13 @@

public class MethodGraphCompilerDefaultTest {

private static final String TYPE_VARIABLE_INTERFACE_BRIDGE = "net.bytebuddy.test.precompiled.TypeVariableInterfaceBridge";

private static final String RETURN_TYPE_INTERFACE_BRIDGE = "net.bytebuddy.test.precompiled.ReturnTypeInterfaceBridge";

@Rule
public MethodRule javaVersionRule = new JavaVersionRule();

@Test
public void testTrivialJavaHierarchy() throws Exception {
MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(TypeDescription.OBJECT);
Expand Down Expand Up @@ -510,6 +520,36 @@ public void testGenericNonOverriddenInterfaceImplementation() throws Exception {
.locate(methodDescription.asToken())));
}

@Test
@JavaVersionRule.Enforce(8)
public void testTypeVariableInterfaceBridge() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(TYPE_VARIABLE_INTERFACE_BRIDGE));
MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
assertThat(methodGraph.listNodes().size(), is(1));
MethodDescription methodDescription = typeDescription.getDeclaredMethods().filter(takesArguments(String.class)).getOnly();
MethodGraph.Node node = methodGraph.locate(methodDescription.asToken());
assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
assertThat(node.getRepresentative(), is(methodDescription));
assertThat(node.getBridges().size(), is(1));
assertThat(node.getBridges().contains(typeDescription.getDeclaredMethods().filter(takesArguments(Object.class)).getOnly().asTypeToken()), is(true));
assertThat(node.getVisibility(), is(MethodGraph.Node.Visibility.PLAIN));
}

@Test
@JavaVersionRule.Enforce(8)
public void testReturnTypeInterfaceBridge() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(RETURN_TYPE_INTERFACE_BRIDGE));
MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
assertThat(methodGraph.listNodes().size(), is(1));
MethodDescription methodDescription = typeDescription.getDeclaredMethods().filter(returns(String.class)).getOnly();
MethodGraph.Node node = methodGraph.locate(methodDescription.asToken());
assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
assertThat(node.getRepresentative(), is(methodDescription));
assertThat(node.getBridges().size(), is(1));
assertThat(node.getBridges().contains(typeDescription.getDeclaredMethods().filter(returns(Object.class)).getOnly().asTypeToken()), is(true));
assertThat(node.getVisibility(), is(MethodGraph.Node.Visibility.PLAIN));
}

@Test
public void testDuplicateNameClass() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(DuplicateNameClass.class);
Expand Down

0 comments on commit 34df71c

Please sign in to comment.