Skip to content

Commit

Permalink
Prepare for an upcoming change to TypeMetadata
Browse files Browse the repository at this point in the history
openjdk/jdk@b9951dd

PiperOrigin-RevId: 517974362
  • Loading branch information
cushon authored and Error Prone Team committed Mar 20, 2023
1 parent e1db1b9 commit 3816046
Showing 1 changed file with 18 additions and 2 deletions.
Expand Up @@ -23,6 +23,7 @@
import com.sun.tools.javac.code.Type.StructuralTypeMapping;
import com.sun.tools.javac.code.TypeMetadata;
import com.sun.tools.javac.code.Types;
import java.lang.reflect.Method;

/** Utility method to produce {@link Api} objects from javac {@link MethodSymbol}. */
public final class ApiFactory {
Expand Down Expand Up @@ -58,15 +59,30 @@ public Type visitType(Type t, Void unused) {

@Override
public Type visitClassType(Type.ClassType t, Void unused) {
return super.visitClassType(t.cloneWithMetadata(TypeMetadata.EMPTY), unused);
return super.visitClassType((Type.ClassType) cloneWithoutMetadata(t), unused);
}

// Remove annotations from all enclosing containers
@Override
public Type visitArrayType(Type.ArrayType t, Void unused) {
return super.visitArrayType(t.cloneWithMetadata(TypeMetadata.EMPTY), unused);
return super.visitArrayType((Type.ArrayType) cloneWithoutMetadata(t), unused);
}
};

public static Type cloneWithoutMetadata(Type type) {
try {
try {
Method method = Type.class.getMethod("cloneWithMetadata", TypeMetadata.class);
return (Type) method.invoke(type, TypeMetadata.class.getField("EMPTY").get(null));
} catch (NoSuchMethodException e) {
Class<?> annotations = Class.forName("com.sun.tools.javac.code.TypeMetadata$Annotations");
Method method = Type.class.getMethod("dropMetadata", Class.class);
return (Type) method.invoke(type, annotations);
}
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}

private ApiFactory() {}
}

0 comments on commit 3816046

Please sign in to comment.