Skip to content

Commit

Permalink
Improved layz resolution for super and interface types.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Oct 25, 2016
1 parent cdd2be6 commit 3f5efd1
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 38 deletions.
Expand Up @@ -5794,16 +5794,6 @@ public Sort getSort() {
return resolve().getSort(); return resolve().getSort();
} }


@Override
public TypeList.Generic getInterfaces() {
return resolve().getInterfaces();
}

@Override
public Generic getSuperClass() {
return resolve().getSuperClass();
}

@Override @Override
public FieldList<FieldDescription.InGenericShape> getDeclaredFields() { public FieldList<FieldDescription.InGenericShape> getDeclaredFields() {
return resolve().getDeclaredFields(); return resolve().getDeclaredFields();
Expand Down Expand Up @@ -5884,11 +5874,6 @@ public boolean represents(java.lang.reflect.Type type) {
return resolve().represents(type); return resolve().represents(type);
} }


@Override
public Iterator<TypeDefinition> iterator() {
return resolve().iterator();
}

@Override @Override
public int hashCode() { public int hashCode() {
return resolve().hashCode(); return resolve().hashCode();
Expand All @@ -5904,29 +5889,163 @@ public String toString() {
return resolve().toString(); return resolve().toString();
} }


/** public abstract static class WithLazyNavigation extends LazyProjection {
* A base implementation of a lazy type projection of an annotated element that resolves its type annotations
* via an {@link AnnotationReader}.
*/
protected abstract static class OfAnnotatedElement extends LazyProjection {


/** @Override
* Returns the current type's annotation reader. public Generic getSuperClass() {
* return LazySuperClass.of(this);
* @return The current type's annotation reader. }
*/
protected abstract AnnotationReader getAnnotationReader();


@Override @Override
public AnnotationList getDeclaredAnnotations() { public TypeList.Generic getInterfaces() {
return getAnnotationReader().asList(); return LazyInterfaceList.of(this);
}

@Override
public Iterator<TypeDefinition> iterator() {
return new TypeDefinition.SuperClassIterator(this);
}

protected static class LazySuperClass extends LazyProjection.WithLazyNavigation {

private final LazyProjection delegate;

protected LazySuperClass(LazyProjection delegate) {
this.delegate = delegate;
}

protected static Generic of(LazyProjection delegate) {
return delegate.asErasure().getSuperClass() == null
? Generic.UNDEFINED
: new LazySuperClass(delegate);
}

@Override
public AnnotationList getDeclaredAnnotations() {
return resolve().getDeclaredAnnotations();
}

@Override
public TypeDescription asErasure() {
return delegate.asErasure().getSuperClass().asErasure();
}

@Override
protected Generic resolve() {
return delegate.resolve().getSuperClass();
}
}

protected static class LazyInterfaceType extends LazyProjection.WithLazyNavigation {

private final LazyProjection delegate;

private final int index;

private final TypeDescription.Generic rawInterface;

protected LazyInterfaceType(LazyProjection delegate, int index, Generic rawInterface) {
this.delegate = delegate;
this.index = index;
this.rawInterface = rawInterface;
}

@Override
public AnnotationList getDeclaredAnnotations() {
return resolve().getDeclaredAnnotations();
}

@Override
public TypeDescription asErasure() {
return rawInterface.asErasure();
}

@Override
protected Generic resolve() {
return delegate.resolve().getInterfaces().get(index);
}
}


protected static class LazyInterfaceList extends TypeList.Generic.AbstractBase {

private final LazyProjection delegate;

private final TypeList.Generic rawInterfaces;

protected LazyInterfaceList(LazyProjection delegate, TypeList.Generic rawInterfaces) {
this.delegate = delegate;
this.rawInterfaces = rawInterfaces;
}

protected static TypeList.Generic of(LazyProjection delegate) {
return new LazyInterfaceList(delegate, delegate.asErasure().getInterfaces());
}

@Override
public Generic get(int index) {
return new LazyInterfaceType(delegate, index, rawInterfaces.get(index));
}

@Override
public int size() {
return rawInterfaces.size();
}
}

protected abstract static class OfAnnotatedElement extends WithLazyNavigation {

/**
* Returns the current type's annotation reader.
*
* @return The current type's annotation reader.
*/
protected abstract AnnotationReader getAnnotationReader();

@Override
public AnnotationList getDeclaredAnnotations() {
return getAnnotationReader().asList();
}
}
}

public abstract static class WithEagerNavigation extends LazyProjection {

@Override
public Generic getSuperClass() {
return resolve().getSuperClass();
}

@Override
public TypeList.Generic getInterfaces() {
return resolve().getInterfaces();
}

@Override
public Iterator<TypeDefinition> iterator() {
return resolve().iterator();
}

protected abstract static class OfAnnotatedElement extends WithEagerNavigation {

/**
* Returns the current type's annotation reader.
*
* @return The current type's annotation reader.
*/
protected abstract AnnotationReader getAnnotationReader();

@Override
public AnnotationList getDeclaredAnnotations() {
return getAnnotationReader().asList();
}
} }
} }


/** /**
* A lazy projection of a generic super type. * A lazy projection of a generic super type.
*/ */
public static class ForLoadedSuperClass extends LazyProjection.OfAnnotatedElement { public static class ForLoadedSuperClass extends LazyProjection.WithLazyNavigation.OfAnnotatedElement {


/** /**
* The type of which the super class is represented. * The type of which the super class is represented.
Expand Down Expand Up @@ -5967,7 +6086,7 @@ protected AnnotationReader getAnnotationReader() {
/** /**
* A lazy projection of a field's type. * A lazy projection of a field's type.
*/ */
public static class ForLoadedFieldType extends LazyProjection.OfAnnotatedElement { public static class ForLoadedFieldType extends LazyProjection.WithEagerNavigation.OfAnnotatedElement {


/** /**
* The field of which the type is represented. * The field of which the type is represented.
Expand Down Expand Up @@ -6002,7 +6121,7 @@ protected AnnotationReader getAnnotationReader() {
/** /**
* A lazy projection of a method's generic return type. * A lazy projection of a method's generic return type.
*/ */
public static class ForLoadedReturnType extends LazyProjection.OfAnnotatedElement { public static class ForLoadedReturnType extends LazyProjection.WithEagerNavigation.OfAnnotatedElement {


/** /**
* The method which defines the return type. * The method which defines the return type.
Expand Down Expand Up @@ -6037,7 +6156,7 @@ protected AnnotationReader getAnnotationReader() {
/** /**
* A lazy projection of the parameter type of a {@link Constructor}. * A lazy projection of the parameter type of a {@link Constructor}.
*/ */
public static class OfConstructorParameter extends LazyProjection.OfAnnotatedElement { public static class OfConstructorParameter extends LazyProjection.WithEagerNavigation.OfAnnotatedElement {


/** /**
* The constructor of which a parameter type is represented. * The constructor of which a parameter type is represented.
Expand Down Expand Up @@ -6090,7 +6209,7 @@ protected AnnotationReader getAnnotationReader() {
/** /**
* A lazy projection of the parameter type of a {@link Method}. * A lazy projection of the parameter type of a {@link Method}.
*/ */
public static class OfMethodParameter extends LazyProjection.OfAnnotatedElement { public static class OfMethodParameter extends LazyProjection.WithEagerNavigation.OfAnnotatedElement {


/** /**
* The method of which a parameter type is represented. * The method of which a parameter type is represented.
Expand Down
Expand Up @@ -659,7 +659,7 @@ public TypeList asErasures() {
/** /**
* A type projection of an interface type. * A type projection of an interface type.
*/ */
private static class TypeProjection extends TypeDescription.Generic.LazyProjection.OfAnnotatedElement { private static class TypeProjection extends TypeDescription.Generic.LazyProjection.WithLazyNavigation.OfAnnotatedElement {


/** /**
* The type of which an interface type is represented. * The type of which an interface type is represented.
Expand Down Expand Up @@ -746,7 +746,7 @@ public TypeList asErasures() {
/** /**
* A projection of a specific exception type. * A projection of a specific exception type.
*/ */
private static class TypeProjection extends TypeDescription.Generic.LazyProjection.OfAnnotatedElement { private static class TypeProjection extends TypeDescription.Generic.LazyProjection.WithEagerNavigation.OfAnnotatedElement {


/** /**
* The constructor of which the exception types are represented. * The constructor of which the exception types are represented.
Expand Down Expand Up @@ -833,7 +833,7 @@ public TypeList asErasures() {
/** /**
* A projection of a specific exception type. * A projection of a specific exception type.
*/ */
private static class TypeProjection extends TypeDescription.Generic.LazyProjection.OfAnnotatedElement { private static class TypeProjection extends TypeDescription.Generic.LazyProjection.WithEagerNavigation.OfAnnotatedElement {


/** /**
* The method of which the exception types are represented. * The method of which the exception types are represented.
Expand Down
4 changes: 2 additions & 2 deletions byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java
Expand Up @@ -6582,7 +6582,7 @@ public int getStackSize() {
/** /**
* A representation of a generic type that is described by a {@link GenericTypeToken}. * A representation of a generic type that is described by a {@link GenericTypeToken}.
*/ */
private static class TokenizedGenericType extends Generic.LazyProjection { private static class TokenizedGenericType extends Generic.LazyProjection.WithEagerNavigation {


/** /**
* The type pool to use for locating referenced types. * The type pool to use for locating referenced types.
Expand Down Expand Up @@ -6818,7 +6818,7 @@ public int size() {
/** /**
* A lazy description of a non-well-defined described generic type. * A lazy description of a non-well-defined described generic type.
*/ */
protected static class Malformed extends LazyProjection { protected static class Malformed extends LazyProjection.WithEagerNavigation {


/** /**
* The type pool to use for locating types. * The type pool to use for locating types.
Expand Down

0 comments on commit 3f5efd1

Please sign in to comment.