Skip to content

Commit

Permalink
For certain metadata we only ever need outlines, regardless of whethe…
Browse files Browse the repository at this point in the history
…r we're matching or transforming
  • Loading branch information
mcculls committed Nov 27, 2022
1 parent e36ff09 commit db9c5cb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@

/** Type description that lazily caches expensive results. */
final class CachingType extends WithName {

// non-null sentinels for fields that can legitimately be null
private static final Generic UNSET_SUPER_CLASS = Generic.VOID;
private static final TypeDescription UNSET_DECLARING_TYPE = TypeDescription.VOID;

private final TypeDescription delegate;

private Generic superClass;
private Generic superClass = UNSET_SUPER_CLASS;
private TypeList.Generic interfaces;
private TypeDescription declaringType = UNSET_DECLARING_TYPE;
private AnnotationList annotations;
private FieldList<FieldDescription.InDefinedShape> fields;
private MethodList<MethodDescription.InDefinedShape> methods;
Expand All @@ -30,7 +36,7 @@ protected TypeDescription delegate() {

@Override
public Generic getSuperClass() {
if (superClass == null) {
if (superClass == UNSET_SUPER_CLASS) {
superClass = delegate.getSuperClass();
}
return superClass;
Expand All @@ -44,6 +50,14 @@ public TypeList.Generic getInterfaces() {
return interfaces;
}

@Override
public TypeDescription getDeclaringType() {
if (declaringType == UNSET_DECLARING_TYPE) {
declaringType = delegate.getDeclaringType();
}
return declaringType;
}

@Override
public AnnotationList getDeclaredAnnotations() {
if (annotations == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public TypeDescription parse(Class<?> loadedType) {
null != superClass ? superClass.getName() : null,
extractTypeNames(loadedType.getInterfaces()));

Class<?> declaringClass = loadedType.getDeclaringClass();
if (null != declaringClass) {
typeOutline.declaredBy(declaringClass.getName());
}

for (Annotation a : loadedType.getDeclaredAnnotations()) {
typeOutline.declare(annotationOutline(Type.getDescriptor(a.annotationType())));
}
Expand Down Expand Up @@ -102,6 +107,13 @@ public void visit(
typeOutline = new TypeOutline(version, access, name, superName, interfaces);
}

@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {
if (null != outerName && typeOutline.getInternalName().equals(name)) {
typeOutline.declaredBy(outerName);
}
}

@Override
public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
typeOutline.declare(annotationOutline(descriptor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Map;
import java.util.function.Function;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.jar.asm.Type;
import net.bytebuddy.pool.TypePool;
Expand Down Expand Up @@ -301,14 +302,47 @@ final class LazyType extends WithName {
super(name);
}

@Override
public int getModifiers() {
return outline().getModifiers();
}

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

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

@Override
public TypeDescription getDeclaringType() {
return outline().getDeclaringType();
}

private TypeDescription outline() {
TypeDescription outline;
if (createOutlines) {
outline = doResolve(true);
} else {
// temporarily switch to outlines as that's all we need
createOutlines = true;
outline = doResolve(true);
createOutlines = false;
}
return outline;
}

@Override
protected TypeDescription delegate() {
return doResolve(true);
}

TypeDescription doResolve(boolean throwIfMissing) {
// re-resolve type when switching to full descriptions
if (null == delegate || createOutlines != isOutline) {
if (null == delegate || (isOutline && !createOutlines)) {
delegate = resolveType(name);
isOutline = createOutlines;
if (throwIfMissing && null == delegate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final class TypeOutline extends WithName {
private final int modifiers;
private final String superName;
private final String[] interfaces;
private String declaringName;

private List<AnnotationDescription> declaredAnnotations;

Expand Down Expand Up @@ -69,6 +70,14 @@ public TypeList.Generic getInterfaces() {
return new TypeList.Generic.Explicit(outlines);
}

@Override
public TypeDescription getDeclaringType() {
if (null != declaringName) {
return findType(declaringName.replace('/', '.'));
}
return null;
}

@Override
public int getModifiers() {
return modifiers;
Expand Down Expand Up @@ -96,6 +105,10 @@ public MethodList<MethodDescription.InDefinedShape> getDeclaredMethods() {
return declaredMethods.isEmpty() ? NO_METHODS : new MethodList.Explicit<>(declaredMethods);
}

void declaredBy(String declaringName) {
this.declaringName = declaringName;
}

void declare(AnnotationDescription annotation) {
if (null != annotation) {
if (null == declaredAnnotations) {
Expand Down

0 comments on commit db9c5cb

Please sign in to comment.