Skip to content

Commit

Permalink
Added documentation and test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Jul 12, 2015
1 parent 4977c1c commit 5468146
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 31 deletions.
Expand Up @@ -66,10 +66,6 @@ public interface TypeDescription extends GenericTypeDescription, TypeVariableSou
*/ */
GenericTypeList ARRAY_INTERFACES = new GenericTypeList.ForLoadedType(Cloneable.class, Serializable.class); GenericTypeList ARRAY_INTERFACES = new GenericTypeList.ForLoadedType(Cloneable.class, Serializable.class);


GenericTypeDescription getDeclaredSuperType();

GenericTypeList getDeclaredInterfaces();

/** /**
* Checks if {@code value} is an instance of the type represented by this instance. * Checks if {@code value} is an instance of the type represented by this instance.
* *
Expand Down Expand Up @@ -296,14 +292,28 @@ abstract class AbstractTypeDescription extends AbstractModifierReviewable implem


@Override @Override
public GenericTypeDescription getSuperType() { public GenericTypeDescription getSuperType() {
return LazyProjection.OfUntransformedType.of(getDeclaredSuperType(), GenericTypeDescription.Visitor.NoOp.INSTANCE); return LazyProjection.OfPotentiallyRawType.of(getDeclaredSuperType(), GenericTypeDescription.Visitor.NoOp.INSTANCE);
} }


/**
* Returns the declared super type in the form it is declared in the class file.
*
* @return The declared super type.
*/
protected abstract GenericTypeDescription getDeclaredSuperType();

@Override @Override
public GenericTypeList getInterfaces() { public GenericTypeList getInterfaces() {
return new GenericTypeList.OfUntransformedType(getDeclaredInterfaces(), GenericTypeDescription.Visitor.NoOp.INSTANCE); return new GenericTypeList.OfPotentiallyRawType(getDeclaredInterfaces(), GenericTypeDescription.Visitor.NoOp.INSTANCE);
} }


/**
* Returns the declared interface types in the form they are declared in the class file.
*
* @return The declared super type.
*/
protected abstract GenericTypeList getDeclaredInterfaces();

@Override @Override
public Sort getSort() { public Sort getSort() {
return Sort.NON_GENERIC; return Sort.NON_GENERIC;
Expand Down Expand Up @@ -1119,12 +1129,12 @@ public boolean isPrimitive() {
} }


@Override @Override
public GenericTypeDescription getDeclaredSuperType() { protected GenericTypeDescription getDeclaredSuperType() {
return TypeDescription.OBJECT; return TypeDescription.OBJECT;
} }


@Override @Override
public GenericTypeList getDeclaredInterfaces() { protected GenericTypeList getDeclaredInterfaces() {
return ARRAY_INTERFACES; return ARRAY_INTERFACES;
} }


Expand Down Expand Up @@ -1276,12 +1286,12 @@ public Latent(String name, int modifiers, GenericTypeDescription superType, List
} }


@Override @Override
public GenericTypeDescription getDeclaredSuperType() { protected GenericTypeDescription getDeclaredSuperType() {
return superType; return superType;
} }


@Override @Override
public GenericTypeList getDeclaredInterfaces() { protected GenericTypeList getDeclaredInterfaces() {
return new GenericTypeList.Explicit(interfaces); return new GenericTypeList.Explicit(interfaces);
} }


Expand Down Expand Up @@ -1360,21 +1370,31 @@ public GenericTypeList getTypeVariables() {
} }
} }


/**
* A type representation of a package description.
*/
class ForPackageDescription extends AbstractTypeDescription.OfSimpleType { class ForPackageDescription extends AbstractTypeDescription.OfSimpleType {


/**
* The package to be described as a type.
*/
private final PackageDescription packageDescription; private final PackageDescription packageDescription;


/**
* Creates a new type description of a package description.
* @param packageDescription The package to be described as a type.
*/
public ForPackageDescription(PackageDescription packageDescription) { public ForPackageDescription(PackageDescription packageDescription) {
this.packageDescription = packageDescription; this.packageDescription = packageDescription;
} }


@Override @Override
public GenericTypeDescription getDeclaredSuperType() { protected GenericTypeDescription getDeclaredSuperType() {
return TypeDescription.OBJECT; return TypeDescription.OBJECT;
} }


@Override @Override
public GenericTypeList getDeclaredInterfaces() { protected GenericTypeList getDeclaredInterfaces() {
return new GenericTypeList.Empty(); return new GenericTypeList.Empty();
} }


Expand Down
Expand Up @@ -1427,12 +1427,12 @@ public Sort getSort() {


@Override @Override
public GenericTypeDescription getSuperType() { public GenericTypeDescription getSuperType() {
return LazyProjection.OfUntransformedType.of(asRawType().getDeclaredSuperType(), Visitor.Substitutor.ForTypeVariableBinding.bind(this)); return LazyProjection.OfPotentiallyRawType.of(asRawType().getSuperType(), Visitor.Substitutor.ForTypeVariableBinding.bind(this));
} }


@Override @Override
public GenericTypeList getInterfaces() { public GenericTypeList getInterfaces() {
return new GenericTypeList.OfUntransformedType(asRawType().getDeclaredInterfaces(), Visitor.Substitutor.ForTypeVariableBinding.bind(this)); return new GenericTypeList.OfPotentiallyRawType(asRawType().getInterfaces(), Visitor.Substitutor.ForTypeVariableBinding.bind(this));
} }


@Override @Override
Expand Down Expand Up @@ -1652,7 +1652,7 @@ public static class Raw implements GenericTypeDescription {
* @param transformer A transformer to apply to a non-raw types. * @param transformer A transformer to apply to a non-raw types.
* @return Either a raw type, i * @return Either a raw type, i
*/ */
public static GenericTypeDescription resolve(GenericTypeDescription typeDescription, Visitor<? extends GenericTypeDescription> transformer) { public static GenericTypeDescription check(GenericTypeDescription typeDescription, Visitor<? extends GenericTypeDescription> transformer) {
if (typeDescription == null) { if (typeDescription == null) {
return null; return null;
} }
Expand All @@ -1677,15 +1677,15 @@ protected Raw(TypeDescription typeDescription) {


@Override @Override
public GenericTypeDescription getSuperType() { public GenericTypeDescription getSuperType() {
GenericTypeDescription superType = typeDescription.getDeclaredSuperType(); GenericTypeDescription superType = typeDescription.getSuperType();
return superType == null return superType == null
? null ? null
: superType.accept(Visitor.TypeVariableErasing.INSTANCE); : superType.accept(Visitor.TypeVariableErasing.INSTANCE);
} }


@Override @Override
public GenericTypeList getInterfaces() { public GenericTypeList getInterfaces() {
return typeDescription.getDeclaredInterfaces().accept(Visitor.TypeVariableErasing.INSTANCE); return typeDescription.getInterfaces().accept(Visitor.TypeVariableErasing.INSTANCE);
} }


@Override @Override
Expand Down Expand Up @@ -2067,27 +2067,27 @@ public String toString() {
return resolve().toString(); return resolve().toString();
} }


public static class OfUntransformedType extends LazyProjection { public static class OfPotentiallyRawType extends LazyProjection {


public static GenericTypeDescription of(GenericTypeDescription unresolvedType, Visitor<? extends GenericTypeDescription> transformer) { public static GenericTypeDescription of(GenericTypeDescription unresolvedType, Visitor<? extends GenericTypeDescription> transformer) {
if (unresolvedType == null) { if (unresolvedType == null) {
return null; return null;
} }
return new OfUntransformedType(unresolvedType, transformer); return new OfPotentiallyRawType(unresolvedType, transformer);
} }


private final GenericTypeDescription unresolvedType; private final GenericTypeDescription unresolvedType;


private final GenericTypeDescription.Visitor<? extends GenericTypeDescription> transformer; private final GenericTypeDescription.Visitor<? extends GenericTypeDescription> transformer;


public OfUntransformedType(GenericTypeDescription unresolvedType, Visitor<? extends GenericTypeDescription> transformer) { public OfPotentiallyRawType(GenericTypeDescription unresolvedType, Visitor<? extends GenericTypeDescription> transformer) {
this.unresolvedType = unresolvedType; this.unresolvedType = unresolvedType;
this.transformer = transformer; this.transformer = transformer;
} }


@Override @Override
protected GenericTypeDescription resolve() { protected GenericTypeDescription resolve() {
return GenericTypeDescription.ForParameterizedType.Raw.resolve(unresolvedType, transformer); return GenericTypeDescription.ForParameterizedType.Raw.check(unresolvedType, transformer);
} }


@Override @Override
Expand Down
Expand Up @@ -247,21 +247,21 @@ public int getStackSize() {
} }
} }


class OfUntransformedType extends AbstractBase { class OfPotentiallyRawType extends AbstractBase {


private final List<? extends GenericTypeDescription> typeDescriptions; private final List<? extends GenericTypeDescription> typeDescriptions;


private final GenericTypeDescription.Visitor<? extends GenericTypeDescription> transformer; private final GenericTypeDescription.Visitor<? extends GenericTypeDescription> transformer;


public OfUntransformedType(List<? extends GenericTypeDescription> typeDescriptions, public OfPotentiallyRawType(List<? extends GenericTypeDescription> typeDescriptions,
GenericTypeDescription.Visitor<? extends GenericTypeDescription> transformer) { GenericTypeDescription.Visitor<? extends GenericTypeDescription> transformer) {
this.typeDescriptions = typeDescriptions; this.typeDescriptions = typeDescriptions;
this.transformer = transformer; this.transformer = transformer;
} }


@Override @Override
public GenericTypeDescription get(int index) { public GenericTypeDescription get(int index) {
return new GenericTypeDescription.LazyProjection.OfUntransformedType(typeDescriptions.get(index), transformer); return new GenericTypeDescription.LazyProjection.OfPotentiallyRawType(typeDescriptions.get(index), transformer);
} }


@Override @Override
Expand Down
Expand Up @@ -490,14 +490,14 @@ public TypeDescription getDeclaringType() {
} }


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


@Override @Override
public GenericTypeList getDeclaredInterfaces() { protected GenericTypeList getDeclaredInterfaces() {
return GenericTypeList.ForDetachedTypes.attach(this, interfaceTypes); return GenericTypeList.ForDetachedTypes.attach(this, interfaceTypes);
} }


Expand Down
8 changes: 4 additions & 4 deletions byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java
Expand Up @@ -3145,14 +3145,14 @@ protected LazyTypeDescription(TypePool typePool,
} }


@Override @Override
public GenericTypeDescription getDeclaredSuperType() { protected GenericTypeDescription getDeclaredSuperType() {
return superTypeDescriptor == null || isInterface() return superTypeDescriptor == null || isInterface()
? null ? null
: signatureResolution.resolveSuperType(superTypeDescriptor, typePool, this); : signatureResolution.resolveSuperType(superTypeDescriptor, typePool, this);
} }


@Override @Override
public GenericTypeList getDeclaredInterfaces() { protected GenericTypeList getDeclaredInterfaces() {
return signatureResolution.resolveInterfaceTypes(interfaceTypeDescriptors, typePool, this); return signatureResolution.resolveInterfaceTypes(interfaceTypeDescriptors, typePool, this);
} }


Expand Down Expand Up @@ -4388,7 +4388,7 @@ public GenericTypeList resolveTypeVariables(TypePool typePool, TypeVariableSourc


@Override @Override
public String toString() { public String toString() {
return "TypePool.LazyTypeDescription.GenericTypeToken.Resolution.Raw." + none(); return "TypePool.LazyTypeDescription.GenericTypeToken.Resolution.Raw." + name();
} }
} }


Expand Down Expand Up @@ -4439,7 +4439,7 @@ public GenericTypeList resolveTypeVariables(TypePool typePool, TypeVariableSourc


@Override @Override
public String toString() { public String toString() {
return "TypePool.LazyTypeDescription.GenericTypeToken.Resolution.Malformed." + none(); return "TypePool.LazyTypeDescription.GenericTypeToken.Resolution.Malformed." + name();
} }
} }
} }
Expand Down
@@ -0,0 +1,81 @@
package net.bytebuddy.description.type;

import net.bytebuddy.description.annotation.AnnotationList;
import net.bytebuddy.description.type.generic.GenericTypeDescription;
import net.bytebuddy.test.utility.MockitoRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.mockito.Mock;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TypeDescriptionForPackageDescriptionTest {

private static final String FOO = "foo";

@Rule
public TestRule mockitoRule = new MockitoRule(this);

private TypeDescription typeDescription;

@Mock
private PackageDescription packageDescription;

@Before
public void setUp() throws Exception {
typeDescription = new TypeDescription.ForPackageDescription(packageDescription);
}

@Test
public void testName() throws Exception {
when(packageDescription.getName()).thenReturn(FOO);
assertThat(typeDescription.getName(), is(FOO + "." + PackageDescription.PACKAGE_CLASS_NAME));
}

@Test
public void testModifiers() throws Exception {
assertThat(typeDescription.getModifiers(), is(PackageDescription.PACKAGE_MODIFIERS));
}

@Test
public void testInterfaces() throws Exception {
assertThat(typeDescription.getInterfaces().size(), is(0));
}

@Test
public void testAnnotations() throws Exception {
AnnotationList annotationList = mock(AnnotationList.class);
when(packageDescription.getDeclaredAnnotations()).thenReturn(annotationList);
assertThat(typeDescription.getDeclaredAnnotations(), is(annotationList));
}

@Test
public void testTypeVariables() throws Exception {
assertThat(typeDescription.getTypeVariables().size(), is(0));
}

@Test
public void testFields() throws Exception {
assertThat(typeDescription.getDeclaredFields().size(), is(0));
}

@Test
public void testMethods() throws Exception {
assertThat(typeDescription.getDeclaredMethods().size(), is(0));
}

@Test
public void testPackage() throws Exception {
assertThat(typeDescription.getPackage(), is(packageDescription));
}

@Test
public void testSuperType() throws Exception {
assertThat(typeDescription.getSuperType(), is((GenericTypeDescription) TypeDescription.OBJECT));
}
}

0 comments on commit 5468146

Please sign in to comment.