Skip to content

Commit

Permalink
Modified generic signature that is written by method descriptions to …
Browse files Browse the repository at this point in the history
…not include generic types if no exception type is generic.
  • Loading branch information
raphw committed Oct 14, 2015
1 parent c647b30 commit 04e4a52
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
Expand Up @@ -28,8 +28,7 @@
import java.util.Iterator;
import java.util.List;

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

/**
* Implementations of this interface describe a Java method, i.e. a method or a constructor. Implementations of this
Expand Down Expand Up @@ -360,9 +359,12 @@ public String getGenericSignature() {
GenericTypeDescription returnType = getReturnType();
returnType.accept(new GenericTypeDescription.Visitor.ForSignatureVisitor(signatureWriter.visitReturnType()));
generic = generic || !returnType.getSort().isNonGeneric();
for (GenericTypeDescription exceptionType : getExceptionTypes()) {
exceptionType.accept(new GenericTypeDescription.Visitor.ForSignatureVisitor(signatureWriter.visitExceptionType()));
generic = generic || !exceptionType.getSort().isNonGeneric();
GenericTypeList exceptionTypes = getExceptionTypes();
if (!exceptionTypes.filter(not(ofSort(GenericTypeDescription.Sort.NON_GENERIC))).isEmpty()) {
for (GenericTypeDescription exceptionType : exceptionTypes) {
exceptionType.accept(new GenericTypeDescription.Visitor.ForSignatureVisitor(signatureWriter.visitExceptionType()));
generic = generic || !exceptionType.getSort().isNonGeneric();
}
}
return generic
? signatureWriter.toString()
Expand Down
Expand Up @@ -19,7 +19,7 @@

public class GenericSignatureResolutionTest {

private static final String FOO = "foo";
private static final String FOO = "foo", BAR = "bar";

@Test
public void testGenericType() throws Exception {
Expand Down Expand Up @@ -49,7 +49,7 @@ public void testGenericField() throws Exception {
public void testGenericMethod() throws Exception {
DynamicType.Unloaded<?> unloaded = new ByteBuddy()
.redefine(GenericMethod.class)
.method(named("foo"))
.method(named(FOO))
.intercept(FixedValue.nullValue())
.make();
Class<?> type = unloaded.load(null, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
Expand All @@ -61,6 +61,22 @@ public void testGenericMethod() throws Exception {
assertThat(createdMethod.getExceptionTypes().getOnly(), is(originalMethod.getExceptionTypes().getOnly()));
}

@Test
public void testGenericMethodWithoutGenericExceptionTypes() throws Exception {
DynamicType.Unloaded<?> unloaded = new ByteBuddy()
.redefine(GenericMethod.class)
.method(named(BAR))
.intercept(FixedValue.nullValue())
.make();
Class<?> type = unloaded.load(null, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
MethodDescription createdMethod = new MethodDescription.ForLoadedMethod(type.getDeclaredMethod(BAR, Object.class));
MethodDescription originalMethod = new MethodDescription.ForLoadedMethod(GenericMethod.class.getDeclaredMethod(BAR, Object.class));
assertThat(createdMethod.getTypeVariables(), is(originalMethod.getTypeVariables()));
assertThat(createdMethod.getReturnType(), is(originalMethod.getReturnType()));
assertThat(createdMethod.getParameters().getOnly().getType(), is(originalMethod.getParameters().getOnly().getType()));
assertThat(createdMethod.getExceptionTypes().getOnly(), is(originalMethod.getExceptionTypes().getOnly()));
}

@Test
public void testNoSuperType() throws Exception {
assertThat(new ByteBuddy().redefine(Object.class).make(), notNullValue(DynamicType.class));
Expand Down Expand Up @@ -178,11 +194,16 @@ public static abstract class GenericType<T extends ArrayList<T> & Callable<T>,

}

@SuppressWarnings("unused")
public static class GenericMethod {

<T extends Exception & Callable<T>> T foo(T arg) throws T {
return null;
}

<T> T bar(T arg) throws Exception {
return null;
}
}

public static class GenericField<T> {
Expand Down

0 comments on commit 04e4a52

Please sign in to comment.