Skip to content

Commit

Permalink
Fixed resolution of generic types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Jul 7, 2015
1 parent 2c22b5a commit 83d0df8
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 19 deletions.
Expand Up @@ -1159,7 +1159,7 @@ abstract class AbstractBase<S> implements Builder<S> {
/**
* The interface types to implement as specified for this builder.
*/
protected final List<TypeDescription> interfaceTypes;
protected final List<GenericTypeDescription> interfaceTypes;

/**
* The modifiers specified for this builder.
Expand Down Expand Up @@ -1252,7 +1252,7 @@ protected AbstractBase(ClassFileVersion classFileVersion,
NamingStrategy namingStrategy,
AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
TypeDescription targetType,
List<TypeDescription> interfaceTypes,
List<GenericTypeDescription> interfaceTypes,
int modifiers,
TypeAttributeAppender attributeAppender,
ElementMatcher<? super MethodDescription> ignoredMethods,
Expand Down Expand Up @@ -1769,7 +1769,7 @@ protected abstract Builder<S> materialize(ClassFileVersion classFileVersion,
NamingStrategy namingStrategy,
AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
TypeDescription targetType,
List<TypeDescription> interfaceTypes,
List<GenericTypeDescription> interfaceTypes,
int modifiers,
TypeAttributeAppender attributeAppender,
ElementMatcher<? super MethodDescription> ignoredMethods,
Expand Down
Expand Up @@ -433,7 +433,9 @@ public TypeDescription getDeclaringType() {

@Override
public GenericTypeDescription getSuperType() {
return superType.accept(GenericTypeDescription.Visitor.Substitutor.ForAttachment.of(this));
return superType == null
? null
: superType.accept(GenericTypeDescription.Visitor.Substitutor.ForAttachment.of(this));
}

@Override
Expand Down
Expand Up @@ -17,7 +17,6 @@
import net.bytebuddy.implementation.auxiliary.AuxiliaryType;
import net.bytebuddy.matcher.ElementMatcher;

import java.util.Collections;
import java.util.List;

import static net.bytebuddy.matcher.ElementMatchers.is;
Expand Down Expand Up @@ -85,7 +84,7 @@ public RebaseDynamicTypeBuilder(ClassFileVersion classFileVersion,
namingStrategy,
auxiliaryTypeNamingStrategy,
levelType,
joinUniqueRaw(interfaceTypes, levelType.getInterfaces().asRawTypes()),
joinUniqueRaw(interfaceTypes, levelType.getInterfaces()),
modifiers,
attributeAppender,
ignoredMethods,
Expand Down Expand Up @@ -133,7 +132,7 @@ protected RebaseDynamicTypeBuilder(ClassFileVersion classFileVersion,
NamingStrategy namingStrategy,
AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
TypeDescription levelType,
List<TypeDescription> interfaceTypes,
List<GenericTypeDescription> interfaceTypes,
int modifiers,
TypeAttributeAppender attributeAppender,
ElementMatcher<? super MethodDescription> ignoredMethods,
Expand Down Expand Up @@ -174,7 +173,7 @@ protected DynamicType.Builder<T> materialize(ClassFileVersion classFileVersion,
NamingStrategy namingStrategy,
AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
TypeDescription levelType,
List<TypeDescription> interfaceTypes,
List<GenericTypeDescription> interfaceTypes,
int modifiers,
TypeAttributeAppender attributeAppender,
ElementMatcher<? super MethodDescription> ignoredMethods,
Expand Down
Expand Up @@ -77,7 +77,7 @@ public RedefinitionDynamicTypeBuilder(ClassFileVersion classFileVersion,
namingStrategy,
auxiliaryTypeNamingStrategy,
levelType,
joinUniqueRaw(interfaceTypes, levelType.getInterfaces().asRawTypes()),
joinUniqueRaw(interfaceTypes, levelType.getInterfaces()),
modifiers,
attributeAppender,
ignoredMethods,
Expand Down Expand Up @@ -123,7 +123,7 @@ protected RedefinitionDynamicTypeBuilder(ClassFileVersion classFileVersion,
NamingStrategy namingStrategy,
AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
TypeDescription levelType,
List<TypeDescription> interfaceTypes,
List<GenericTypeDescription> interfaceTypes,
int modifiers,
TypeAttributeAppender attributeAppender,
ElementMatcher<? super MethodDescription> ignoredMethods,
Expand Down Expand Up @@ -162,7 +162,7 @@ protected DynamicType.Builder<T> materialize(ClassFileVersion classFileVersion,
NamingStrategy namingStrategy,
AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
TypeDescription levelType,
List<TypeDescription> interfaceTypes,
List<GenericTypeDescription> interfaceTypes,
int modifiers,
TypeAttributeAppender attributeAppender,
ElementMatcher<? super MethodDescription> ignoredMethods,
Expand Down Expand Up @@ -198,7 +198,10 @@ protected DynamicType.Builder<T> materialize(ClassFileVersion classFileVersion,
@Override
public DynamicType.Unloaded<T> make() {
MethodRegistry.Compiled compiledMethodRegistry = methodRegistry.prepare(new InstrumentedType.Default(namingStrategy.name(new NamingStrategy
.UnnamedType.Default(targetType.getSuperType().asRawType(), interfaceTypes, modifiers, classFileVersion)),
.UnnamedType.Default(targetType.getSuperType() == null ? null : targetType.getSuperType().asRawType(),
interfaceTypes,
modifiers,
classFileVersion)),
modifiers,
targetType.getTypeVariables().accept(new GenericTypeDescription.Visitor.Substitutor.ForDetachment(is(targetType))),
targetType.getSuperType(),
Expand Down
Expand Up @@ -18,6 +18,7 @@
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.LatentMethodMatcher;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -78,7 +79,7 @@ public SubclassDynamicTypeBuilder(ClassFileVersion classFileVersion,
namingStrategy,
auxiliaryTypeNamingStrategy,
superType,
interfaceTypes,
new ArrayList<GenericTypeDescription>(interfaceTypes),
modifiers,
attributeAppender,
ignoredMethods,
Expand Down Expand Up @@ -125,7 +126,7 @@ protected SubclassDynamicTypeBuilder(ClassFileVersion classFileVersion,
NamingStrategy namingStrategy,
AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
TypeDescription superType,
List<TypeDescription> interfaceTypes,
List<GenericTypeDescription> interfaceTypes,
int modifiers,
TypeAttributeAppender attributeAppender,
ElementMatcher<? super MethodDescription> ignoredMethods,
Expand Down Expand Up @@ -164,7 +165,7 @@ protected DynamicType.Builder<T> materialize(ClassFileVersion classFileVersion,
NamingStrategy namingStrategy,
AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
TypeDescription targetType,
List<TypeDescription> interfaceTypes,
List<GenericTypeDescription> interfaceTypes,
int modifiers,
TypeAttributeAppender attributeAppender,
ElementMatcher<? super MethodDescription> ignoredMethods,
Expand Down
Expand Up @@ -25,6 +25,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

public class MethodRebaseResolverEnabledTest {
Expand Down Expand Up @@ -69,9 +70,11 @@ public void setUp() throws Exception {
when(constructor.getParameters()).thenReturn(constructorParameterList);
when(methodNameTransformer.transform(method)).thenReturn(BAR);
when(returnType.asRawType()).thenReturn(returnType); // REFACTOR
when(returnType.getSort()).thenReturn(GenericTypeDescription.Sort.RAW);
when(parameterType.asRawType()).thenReturn(parameterType); // REFACTOR
when(parameterType.accept(Mockito.any(GenericTypeDescription.Visitor.class))).thenReturn(parameterType);
when(parameterType.accept(any(GenericTypeDescription.Visitor.class))).thenReturn(parameterType);
when(placeholderType.asRawType()).thenReturn(placeholderType); // REFACTOR
when(placeholderType.accept(any(GenericTypeDescription.Visitor.class))).thenReturn(placeholderType);
}

@Test
Expand Down Expand Up @@ -100,7 +103,7 @@ public void testResolutionInstrumentedConstructor() throws Exception {
assertThat(resolution.isRebased(), is(true));
assertThat(resolution.getAdditionalArguments(), is((StackManipulation) NullConstant.INSTANCE));
assertThat(resolution.getResolvedMethod().getInternalName(), is(MethodDescription.CONSTRUCTOR_INTERNAL_NAME));
assertThat(resolution.getResolvedMethod().getReturnType(), is((GenericTypeDescription) returnType));
assertThat(resolution.getResolvedMethod().getReturnType(), is((GenericTypeDescription) TypeDescription.VOID));
assertThat(resolution.getResolvedMethod().getParameters().asTypeList(),
is((GenericTypeList) new GenericTypeList.Explicit(Arrays.asList(parameterType, placeholderType))));
assertThat(resolution.getResolvedMethod().isSynthetic(), is(true));
Expand Down
Expand Up @@ -63,8 +63,9 @@ public void setUp() throws Exception {
when(placeholderType.getDescriptor()).thenReturn(BAZ);
when(otherPlaceHolderType.getDescriptor()).thenReturn(FOO);
when(returnType.asRawType()).thenReturn(returnType); // REFACTOR
when(parameterType.accept(any(GenericTypeDescription.Visitor.class))).thenReturn(parameterType);
when(returnType.accept(any(GenericTypeDescription.Visitor.class))).thenReturn(returnType); // REFACTOR
when(parameterType.asRawType()).thenReturn(parameterType); // REFACTOR
when(parameterType.accept(any(GenericTypeDescription.Visitor.class))).thenReturn(parameterType);
}

@Test
Expand All @@ -74,7 +75,7 @@ public void testPreservation() throws Exception {
assertThat(resolution.getResolvedMethod().getDeclaringType(), is(typeDescription));
assertThat(resolution.getResolvedMethod().getInternalName(), is(MethodDescription.CONSTRUCTOR_INTERNAL_NAME));
assertThat(resolution.getResolvedMethod().getModifiers(), is(MethodRebaseResolver.REBASED_METHOD_MODIFIER));
assertThat(resolution.getResolvedMethod().getReturnType(), is((GenericTypeDescription) returnType));
assertThat(resolution.getResolvedMethod().getReturnType(), is((GenericTypeDescription) TypeDescription.VOID));
assertThat(resolution.getResolvedMethod().getParameters(), is(ParameterList.Explicit.latent(resolution.getResolvedMethod(),
Arrays.asList(parameterType, placeholderType))));
StackManipulation.Size size = resolution.getAdditionalArguments().apply(methodVisitor, implementationContext);
Expand Down
Expand Up @@ -49,6 +49,7 @@ public class MethodRebaseResolverResolutionForRebasedMethodTest {
private Implementation.Context implementationContext;

@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
when(methodDescription.getDeclaringType()).thenReturn(typeDescription);
when(methodDescription.getReturnType()).thenReturn(returnType);
Expand All @@ -62,7 +63,9 @@ public void setUp() throws Exception {
ParameterList parameterList = ParameterList.Explicit.latent(methodDescription, Collections.singletonList(parameterType));
when(methodDescription.getParameters()).thenReturn(parameterList);
when(returnType.asRawType()).thenReturn(returnType); // REFACTOR
when(returnType.accept(any(GenericTypeDescription.Visitor.class))).thenReturn(returnType); // REFACTOR
when(parameterType.asRawType()).thenReturn(parameterType); // REFACTOR
when(parameterType.accept(any(GenericTypeDescription.Visitor.class))).thenReturn(parameterType); // REFACTOR
}

@Test
Expand Down

0 comments on commit 83d0df8

Please sign in to comment.