Skip to content

Commit

Permalink
adding logic for using addMetadata and dropMetadata through methodHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
akulk022 committed Nov 25, 2023
1 parent 89474f7 commit ef844c8
Showing 1 changed file with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public Type visitParameterizedType(ParameterizedTypeTree tree, Void p) {
: com.sun.tools.javac.util.List.nil();
TypeMetadata typeMetadata = tmBuilder.create(nullableAnnotationCompound);
Type currentTypeArgType = curTypeArg.accept(this, null);
Type newTypeArgType = currentTypeArgType.cloneWithMetadata(typeMetadata);
Type newTypeArgType = tmBuilder.cloneTypeWithMetaData(currentTypeArgType, typeMetadata);
newTypeArgs.add(newTypeArgType);
}
Type.ClassType finalType =
Expand All @@ -97,6 +97,8 @@ protected Type defaultAction(Tree node, Void unused) {

private interface TypeMetadataBuilder {
TypeMetadata create(com.sun.tools.javac.util.List<Attribute.TypeCompound> attrs);

Type cloneTypeWithMetaData(Type typeToBeCloned, TypeMetadata metaData);
}

private static class JDK17AndEarlierTypeMetadataBuilder implements TypeMetadataBuilder {
Expand All @@ -105,11 +107,18 @@ private static class JDK17AndEarlierTypeMetadataBuilder implements TypeMetadataB
public TypeMetadata create(com.sun.tools.javac.util.List<Attribute.TypeCompound> attrs) {
return new TypeMetadata(new TypeMetadata.Annotations(attrs));
}

@Override
public Type cloneTypeWithMetaData(Type typeToBeCloned, TypeMetadata metaData) {
return typeToBeCloned.cloneWithMetadata(metaData);
}
}

private static class JDK21TypeMetadataBuilder implements TypeMetadataBuilder {

private static final MethodHandle typeMetadataHandle = createHandle();
private static final MethodHandle cloneAddMetadataHandle = createAddMethodHandle();
private static final MethodHandle cloneDropMetadataHandle = createDropMethodHandle();

Check warning on line 121 in nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java#L119-L121

Added lines #L119 - L121 were not covered by tests

private static MethodHandle createHandle() {
MethodHandles.Lookup lookup = MethodHandles.lookup();
Expand All @@ -123,6 +132,30 @@ private static MethodHandle createHandle() {
}
}

private static MethodHandle createAddMethodHandle() {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType mt = MethodType.methodType(Type.class, TypeMetadata.class);

Check warning on line 137 in nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java#L136-L137

Added lines #L136 - L137 were not covered by tests
try {
return lookup.findVirtual(com.sun.tools.javac.code.Type.class, "addMetadata", mt);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);

Check warning on line 143 in nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java#L139-L143

Added lines #L139 - L143 were not covered by tests
}
}

private static MethodHandle createDropMethodHandle() {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType mt = MethodType.methodType(Type.class, Class.class);

Check warning on line 149 in nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java#L148-L149

Added lines #L148 - L149 were not covered by tests
try {
return lookup.findVirtual(com.sun.tools.javac.code.Type.class, "dropMetadata", mt);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);

Check warning on line 155 in nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java#L151-L155

Added lines #L151 - L155 were not covered by tests
}
}

@Override
public TypeMetadata create(com.sun.tools.javac.util.List<Attribute.TypeCompound> attrs) {
ListBuffer<Attribute.TypeCompound> b = new ListBuffer<>();
Expand All @@ -133,6 +166,23 @@ public TypeMetadata create(com.sun.tools.javac.util.List<Attribute.TypeCompound>
throw new RuntimeException(e);

Check warning on line 166 in nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java#L164-L166

Added lines #L164 - L166 were not covered by tests
}
}

@Override
public Type cloneTypeWithMetaData(Type typeToBeCloned, TypeMetadata metaData) {
try {
Type clonedType = null;

Check warning on line 173 in nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java#L173

Added line #L173 was not covered by tests
if (metaData.getClass().getComponentType() != null) {
clonedType = (Type) cloneAddMetadataHandle.invoke(typeToBeCloned, metaData);

Check warning on line 175 in nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java#L175

Added line #L175 was not covered by tests
} else {
Type clonedTypeDrop =
(Type) cloneDropMetadataHandle.invoke(typeToBeCloned, metaData.getClass());
clonedType = (Type) cloneAddMetadataHandle.invoke(clonedTypeDrop, metaData);

Check warning on line 179 in nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java#L177-L179

Added lines #L177 - L179 were not covered by tests
}
return clonedType;
} catch (Throwable e) {
throw new RuntimeException(e);

Check warning on line 183 in nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/PreservedAnnotationTreeVisitor.java#L181-L183

Added lines #L181 - L183 were not covered by tests
}
}
}

private static final TypeMetadataBuilder tmBuilder =
Expand Down

0 comments on commit ef844c8

Please sign in to comment.