Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add annotation element values to type attribution model #4746

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,10 @@ private void completeClassSymbol(Symbol.ClassSymbol classSymbol) {
if (retention != null && retention.value() == RetentionPolicy.SOURCE) {
continue;
}
annotations.add(annotType);
List<JavaType.AnnotationValue> annotationValues = a.values.stream().map(attr -> new JavaType.AnnotationValue(
methodDeclarationType(attr.fst, annotType), attr.snd.getValue().toString())).collect(Collectors.toList());
JavaType.Annotation annotation = new JavaType.Annotation(annotType, annotationValues);
annotations.add(annotation);
}
}
return annotations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Pair;
import lombok.RequiredArgsConstructor;
import org.jspecify.annotations.Nullable;
import org.openrewrite.java.JavaTypeMapping;
Expand All @@ -28,8 +29,6 @@

import javax.lang.model.type.NullType;
import javax.lang.model.type.TypeMirror;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -47,7 +46,7 @@ class ReloadableJava17TypeMapping implements JavaTypeMapping<Tree> {

public JavaType type(com.sun.tools.javac.code.@Nullable Type type) {
if (type == null || type instanceof Type.ErrorType || type instanceof Type.PackageType || type instanceof Type.UnknownType ||
type instanceof NullType) {
type instanceof NullType) {
return JavaType.Class.Unknown.getInstance();
}

Expand Down Expand Up @@ -272,8 +271,8 @@ private JavaType.FullyQualified classType(Type.ClassType classType, String signa
if (sym.members_field != null) {
for (Symbol elem : sym.members_field.getSymbols()) {
if (elem instanceof Symbol.VarSymbol &&
(elem.flags_field & (Flags.SYNTHETIC | Flags.BRIDGE | Flags.HYPOTHETICAL |
Flags.GENERATEDCONSTR | Flags.ANONCONSTR)) == 0) {
(elem.flags_field & (Flags.SYNTHETIC | Flags.BRIDGE | Flags.HYPOTHETICAL |
Flags.GENERATEDCONSTR | Flags.ANONCONSTR)) == 0) {
if (fqn.equals("java.lang.String") && elem.name.toString().equals("serialPersistentFields")) {
// there is a "serialPersistentFields" member within the String class which is used in normal Java
// serialization to customize how the String field is serialized. This field is tripping up Jackson
Expand All @@ -286,7 +285,7 @@ private JavaType.FullyQualified classType(Type.ClassType classType, String signa
}
fields.add(variableType(elem, clazz));
} else if (elem instanceof Symbol.MethodSymbol &&
(elem.flags_field & (Flags.SYNTHETIC | Flags.BRIDGE | Flags.HYPOTHETICAL | Flags.ANONCONSTR)) == 0) {
(elem.flags_field & (Flags.SYNTHETIC | Flags.BRIDGE | Flags.HYPOTHETICAL | Flags.ANONCONSTR)) == 0) {
if (methods == null) {
methods = new ArrayList<>();
}
Expand Down Expand Up @@ -590,7 +589,7 @@ public JavaType.Primitive primitive(TypeTag tag) {
} else {
try {
defaultValues = Collections.singletonList(methodSymbol.getDefaultValue().getValue().toString());
} catch(UnsupportedOperationException e) {
} catch (UnsupportedOperationException e) {
// not all Attribute implementations define `getValue()`
}
}
Expand Down Expand Up @@ -692,22 +691,65 @@ private void completeClassSymbol(Symbol.ClassSymbol classSymbol) {
}
}

private @Nullable List<JavaType.FullyQualified> listAnnotations(Symbol symb) {
private @Nullable List<JavaType.FullyQualified> listAnnotations(Symbol sym) {
List<JavaType.FullyQualified> annotations = null;
if (!symb.getDeclarationAttributes().isEmpty()) {
annotations = new ArrayList<>(symb.getDeclarationAttributes().size());
for (Attribute.Compound a : symb.getDeclarationAttributes()) {
JavaType.FullyQualified annotType = TypeUtils.asFullyQualified(type(a.type));
if (annotType == null) {
continue;
}
Retention retention = a.getAnnotationType().asElement().getAnnotation(Retention.class);
if (retention != null && retention.value() == RetentionPolicy.SOURCE) {
continue;
}
annotations.add(annotType);
if (!sym.getDeclarationAttributes().isEmpty()) {
annotations = new ArrayList<>(sym.getDeclarationAttributes().size());
for (Attribute.Compound a : sym.getDeclarationAttributes()) {
JavaType.Annotation annotation = annotationValue(a);
if (annotation == null) continue;
annotations.add(annotation);
}
}
return annotations;
}

private JavaType.@Nullable Annotation annotationValue(Attribute.Compound compound) {
JavaType.FullyQualified annotType = TypeUtils.asFullyQualified(type(compound.type));
if (annotType == null) {
return null;
}
List<JavaType.AnnotationValue> annotationValues = new ArrayList<>();
for (Pair<Symbol.MethodSymbol, Attribute> attr : compound.values) {
JavaType.AnnotationValue annotationValue = new JavaType.AnnotationValue(
methodDeclarationType(attr.fst, annotType), annotationAttributeValue(attr.snd.getValue()));
annotationValues.add(annotationValue);
}
return new JavaType.Annotation(annotType, annotationValues);
}

private @Nullable Object annotationAttributeValue(Object value) {
if (value instanceof Symbol.VarSymbol) {
return variableType((Symbol.VarSymbol) value);
} else if (value instanceof Type.ClassType) {
return type((Type.ClassType) value);
} else if (value instanceof Attribute.Array) {
List<@Nullable Object> list = new ArrayList<>();
for (Attribute attribute : ((Attribute.Array) value).values) {
list.add(annotationAttributeValue(attribute));
}
return list;
} else if (value instanceof Attribute.Class) {
return type(((Attribute.Class) value).classType);
} else if (value instanceof Attribute.Compound) {
return annotationValue((Attribute.Compound) value);
} else if (value instanceof Attribute.Constant) {
return annotationAttributeValue(((Attribute.Constant) value).value);
} else if (value instanceof Attribute.Enum) {
return annotationAttributeValue(((Attribute.Enum) value).value);
} else if (value instanceof List<?>) {
List<@Nullable Object> list = new ArrayList<>();
for (Object o : ((List<?>) value)) {
list.add(annotationAttributeValue(o));
}
return list;
} else if (value instanceof String) {
return value;
} else if (value instanceof Boolean) {
return value;
} else if (value instanceof Integer) {
return value;
}
return value;
knutwannheden marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Pair;
import lombok.RequiredArgsConstructor;
import org.jspecify.annotations.Nullable;
import org.openrewrite.java.JavaTypeMapping;
Expand All @@ -28,8 +29,6 @@

import javax.lang.model.type.NullType;
import javax.lang.model.type.TypeMirror;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -47,7 +46,7 @@ class ReloadableJava21TypeMapping implements JavaTypeMapping<Tree> {

public JavaType type(com.sun.tools.javac.code.@Nullable Type type) {
if (type == null || type instanceof Type.ErrorType || type instanceof Type.PackageType || type instanceof Type.UnknownType ||
type instanceof NullType) {
type instanceof NullType) {
return JavaType.Class.Unknown.getInstance();
}

Expand Down Expand Up @@ -423,7 +422,7 @@ public JavaType.Primitive primitive(TypeTag tag) {
}

private JavaType.@Nullable Variable variableType(@Nullable Symbol symbol,
JavaType.@Nullable FullyQualified owner) {
JavaType.@Nullable FullyQualified owner) {
if (!(symbol instanceof Symbol.VarSymbol)) {
return null;
}
Expand Down Expand Up @@ -593,15 +592,15 @@ public JavaType.Primitive primitive(TypeTag tag) {
}
}
List<String> defaultValues = null;
if(methodSymbol.getDefaultValue() != null) {
if(methodSymbol.getDefaultValue() instanceof Attribute.Array) {
if (methodSymbol.getDefaultValue() != null) {
if (methodSymbol.getDefaultValue() instanceof Attribute.Array) {
defaultValues = ((Attribute.Array) methodSymbol.getDefaultValue()).getValue().stream()
.map(attr -> attr.getValue().toString())
.collect(Collectors.toList());
} else {
try {
defaultValues = Collections.singletonList(methodSymbol.getDefaultValue().getValue().toString());
} catch(UnsupportedOperationException e) {
} catch (UnsupportedOperationException e) {
// not all Attribute implementations define `getValue()`
}
}
Expand Down Expand Up @@ -703,22 +702,65 @@ private void completeClassSymbol(Symbol.ClassSymbol classSymbol) {
}
}

private @Nullable List<JavaType.FullyQualified> listAnnotations(Symbol symb) {
private @Nullable List<JavaType.FullyQualified> listAnnotations(Symbol sym) {
List<JavaType.FullyQualified> annotations = null;
if (!symb.getDeclarationAttributes().isEmpty()) {
annotations = new ArrayList<>(symb.getDeclarationAttributes().size());
for (Attribute.Compound a : symb.getDeclarationAttributes()) {
JavaType.FullyQualified annotType = TypeUtils.asFullyQualified(type(a.type));
if (annotType == null) {
continue;
}
Retention retention = a.getAnnotationType().asElement().getAnnotation(Retention.class);
if (retention != null && retention.value() == RetentionPolicy.SOURCE) {
continue;
}
annotations.add(annotType);
if (!sym.getDeclarationAttributes().isEmpty()) {
annotations = new ArrayList<>(sym.getDeclarationAttributes().size());
for (Attribute.Compound a : sym.getDeclarationAttributes()) {
JavaType.Annotation annotation = annotationValue(a);
if (annotation == null) continue;
annotations.add(annotation);
}
}
return annotations;
}

private JavaType.@Nullable Annotation annotationValue(Attribute.Compound compound) {
JavaType.FullyQualified annotType = TypeUtils.asFullyQualified(type(compound.type));
if (annotType == null) {
return null;
}
List<JavaType.AnnotationValue> annotationValues = new ArrayList<>();
for (Pair<Symbol.MethodSymbol, Attribute> attr : compound.values) {
JavaType.AnnotationValue annotationValue = new JavaType.AnnotationValue(
methodDeclarationType(attr.fst, annotType), annotationAttributeValue(attr.snd.getValue()));
annotationValues.add(annotationValue);
}
return new JavaType.Annotation(annotType, annotationValues);
}

private @Nullable Object annotationAttributeValue(Object value) {
if (value instanceof Symbol.VarSymbol) {
return variableType((Symbol.VarSymbol) value);
} else if (value instanceof Type.ClassType) {
return type((Type.ClassType) value);
} else if (value instanceof Attribute.Array) {
List<@Nullable Object> list = new ArrayList<>();
for (Attribute attribute : ((Attribute.Array) value).values) {
list.add(annotationAttributeValue(attribute));
}
return list;
} else if (value instanceof Attribute.Class) {
return type(((Attribute.Class) value).classType);
} else if (value instanceof Attribute.Compound) {
return annotationValue((Attribute.Compound) value);
} else if (value instanceof Attribute.Constant) {
return annotationAttributeValue(((Attribute.Constant) value).value);
} else if (value instanceof Attribute.Enum) {
return annotationAttributeValue(((Attribute.Enum) value).value);
} else if (value instanceof List<?>) {
List<@Nullable Object> list = new ArrayList<>();
for (Object o : ((List<?>) value)) {
list.add(annotationAttributeValue(o));
}
return list;
} else if (value instanceof String) {
return value;
} else if (value instanceof Boolean) {
return value;
} else if (value instanceof Integer) {
return value;
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.SourceFile;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.test.RewriteTest;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class Java21ParserTest implements RewriteTest {

Expand All @@ -30,4 +37,46 @@ void shouldLoadResourceFromClasspath() throws IOException {
Files.deleteIfExists(Paths.get(System.getProperty("user.home"), ".rewrite", "classpath", "jackson-annotations-2.17.1.jar"));
rewriteRun(spec -> spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "jackson-annotations")));
}

@Test
void testPreserveAnnotationsFromClasspath() throws IOException {
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
JavaParser p = JavaParser.fromJavaVersion().build();
/**
* Using these annotations in core library for testing this feature:
*
* @Deprecated(since="1.2", forRemoval=true)
* public final void stop()
*
* @CallerSensitive
* public ClassLoader getContextClassLoader() {
*/
List<SourceFile> sourceFiles = p.parse(
"""
class Test {
public void test() {
Thread.currentThread().stop();
Thread.currentThread().getContextClassLoader();
}
}
"""
).toList();
J.CompilationUnit cu = (J.CompilationUnit) sourceFiles.get(0);

J.MethodDeclaration md = (J.MethodDeclaration) cu.getClasses().get(0).getBody().getStatements().get(0);
J.MethodInvocation mi = (J.MethodInvocation) md.getBody().getStatements().get(0);
JavaType.Annotation annotation = (JavaType.Annotation) mi.getMethodType().getAnnotations().get(0);

// Thread.currentThread().stop();
assertEquals("java.lang.Deprecated" ,annotation.type.getFullyQualifiedName());
assertEquals("since", annotation.getValues().get(0).getMethod().getName());
assertEquals("1.2", annotation.getValues().get(0).getValue());
assertEquals("forRemoval", annotation.getValues().get(1).getMethod().getName());
assertEquals(Boolean.TRUE, annotation.getValues().get(1).getValue());

// Thread.currentThread().getContextClassLoader();
mi = (J.MethodInvocation) md.getBody().getStatements().get(1);
annotation = (JavaType.Annotation) mi.getMethodType().getAnnotations().get(0);
assertEquals("jdk.internal.reflect.CallerSensitive" ,annotation.type.getFullyQualifiedName());
assertTrue(annotation.getValues().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,10 @@ private void completeClassSymbol(Symbol.ClassSymbol classSymbol) {
if (retention != null && retention.value() == RetentionPolicy.SOURCE) {
continue;
}
annotations.add(annotType);
List<JavaType.AnnotationValue> annotationValues = a.values.stream().map(attr -> new JavaType.AnnotationValue(
methodDeclarationType(attr.fst, annotType), attr.snd.getValue().toString())).collect(Collectors.toList());
JavaType.Annotation annotation = new JavaType.Annotation(annotType, annotationValues);
annotations.add(annotation);
}
}
return annotations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,17 @@ default void enumTypeB() {
@Test
default void ignoreSourceRetentionAnnotations() {
knutwannheden marked this conversation as resolved.
Show resolved Hide resolved
JavaType.Parameterized goat = goatType();
assertThat(goat.getAnnotations().size()).isEqualTo(1);
assertThat(goat.getAnnotations().get(0).getClassName()).isEqualTo("AnnotationWithRuntimeRetention");
assertThat(goat.getAnnotations().size()).isEqualTo(2);
assertThat(goat.getAnnotations()).satisfiesExactlyInAnyOrder(
a -> assertThat(a.getClassName()).isEqualTo("AnnotationWithRuntimeRetention"),
a -> assertThat(a.getClassName()).isEqualTo("AnnotationWithSourceRetention")
);

JavaType.Method clazzMethod = methodType("clazz");
assertThat(clazzMethod.getAnnotations().size()).isEqualTo(1);
assertThat(clazzMethod.getAnnotations().get(0).getClassName()).isEqualTo("AnnotationWithRuntimeRetention");
assertThat(clazzMethod.getAnnotations()).satisfiesExactlyInAnyOrder(
a -> assertThat(a.getClassName()).isEqualTo("AnnotationWithRuntimeRetention"),
a -> assertThat(a.getClassName()).isEqualTo("AnnotationWithSourceRetention")
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/1367")
Expand Down
Loading
Loading