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

Merged
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
334b9e2
preserve annotation values when parsing compiled classes
Dec 3, 2024
8998388
use Collectors.toList() instead for compatiability and consistency
Dec 4, 2024
f9737d5
Remove extra semi-colons
zexblue01 Dec 4, 2024
fcd9bcb
Store actual annotation values
knutwannheden Dec 5, 2024
228585e
Map some more annotation attribute value types
knutwannheden Dec 5, 2024
0749964
Include source retention annotations for Java 1.8 and Java 11
knutwannheden Dec 5, 2024
14a0776
Include source retention annotations for Java 1.8 and Java 11
knutwannheden Dec 5, 2024
a117884
Various refactorings
knutwannheden Dec 10, 2024
19bd9ce
Merge remote-tracking branch 'refs/remotes/origin/main' into eason-pr…
knutwannheden Dec 10, 2024
bdb85c5
Polish
knutwannheden Dec 10, 2024
865540c
Polish
knutwannheden Dec 10, 2024
99ad9cd
Fix failing tests
knutwannheden Dec 10, 2024
d86eeaa
Polish
knutwannheden Dec 10, 2024
1c90a62
Change return type of `JavaType.Annotation.ElementValue#getElement()`
knutwannheden Dec 11, 2024
a490f18
Merge branch 'main' into eason-preserve_annotation_values
timtebeek Jan 6, 2025
af6e18c
Merge branch 'main' into eason-preserve_annotation_values
timtebeek Jan 22, 2025
8b39237
Restore `selectType instanceof Type.ErrorType` check
timtebeek Jan 22, 2025
574223d
Remove unused imports and apply formatter
timtebeek Jan 22, 2025
c399296
Merge branch 'main' into eason-preserve_annotation_values
timtebeek Jan 22, 2025
474aae3
Fix Jackson serialization support
knutwannheden Jan 23, 2025
cf83f5e
Add `JavaTypeVisitor#visitAnnotation()`
knutwannheden Jan 23, 2025
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 @@ -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 @@ -27,14 +28,13 @@

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;
import java.util.stream.Collectors;

import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
import static org.openrewrite.java.tree.JavaType.GenericTypeVariable.Variance.*;

@RequiredArgsConstructor
Expand Down Expand Up @@ -686,22 +686,66 @@ 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 = annotationType(a);
if (annotation == null) continue;
annotations.add(annotation);
}
}
return annotations;
}

private JavaType.@Nullable Annotation annotationType(Attribute.Compound compound) {
JavaType.FullyQualified annotType = TypeUtils.asFullyQualified(type(compound.type));
if (annotType == null) {
return null;
}
List<JavaType.Annotation.ElementValue> elementValues = new ArrayList<>();
for (Pair<Symbol.MethodSymbol, Attribute> attr : compound.values) {
Object value = annotationElementValue(attr.snd.getValue());
JavaType.Method element = requireNonNull(methodDeclarationType(attr.fst, annotType));
JavaType.Annotation.ElementValue elementValue = value instanceof Object[] ?
new JavaType.Annotation.ArrayElementValue(element, ((Object[]) value)) :
new JavaType.Annotation.SingleElementValue(element, value);
elementValues.add(elementValue);
}
return new JavaType.Annotation(annotType, elementValues);
}

private Object annotationElementValue(Object value) {
if (value instanceof Symbol.VarSymbol) {
JavaType.Variable mapped = variableType((Symbol.VarSymbol) value);
return mapped != null ? mapped : JavaType.Unknown.getInstance();
} 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(annotationElementValue(attribute));
}
return list.toArray(new Object[0]);
} else if (value instanceof List<?>) {
List<@Nullable Object> list = new ArrayList<>();
for (Object o : ((List<?>) value)) {
list.add(annotationElementValue(o));
}
return list.toArray(new Object[0]);
} else if (value instanceof Attribute.Class) {
return type(((Attribute.Class) value).classType);
} else if (value instanceof Attribute.Compound) {
JavaType.Annotation mapped = annotationType((Attribute.Compound) value);
return mapped != null ? mapped : JavaType.Unknown.getInstance();
} else if (value instanceof Attribute.Constant) {
return annotationElementValue(((Attribute.Constant) value).value);
} else if (value instanceof Attribute.Enum) {
return annotationElementValue(((Attribute.Enum) value).value);
} else if (value instanceof Attribute.Error) {
return JavaType.Unknown.getInstance();
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.comp.AttrRecover;
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,15 +29,13 @@

import javax.lang.model.type.NullType;
import javax.lang.model.type.TypeMirror;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
import static org.openrewrite.java.tree.JavaType.GenericTypeVariable.Variance.*;

@RequiredArgsConstructor
Expand All @@ -48,7 +47,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 @@ -259,7 +258,7 @@ private JavaType.FullyQualified classType(Type.ClassType classType, String signa
List<JavaType.FullyQualified> interfaces = null;
if (symType.interfaces_field != null) {
interfaces = new ArrayList<>(symType.interfaces_field.length());
for (com.sun.tools.javac.code.Type iParam : symType.interfaces_field) {
for (Type iParam : symType.interfaces_field) {
JavaType.FullyQualified javaType = TypeUtils.asFullyQualified(type(iParam));
if (javaType != null) {
interfaces.add(javaType);
Expand All @@ -273,8 +272,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 @@ -287,7 +286,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 @@ -524,7 +523,7 @@ public JavaType.Primitive primitive(TypeTag tag) {

if (!methodType.argtypes.isEmpty()) {
parameterTypes = new ArrayList<>(methodType.argtypes.size());
for (com.sun.tools.javac.code.Type argtype : methodType.argtypes) {
for (Type argtype : methodType.argtypes) {
if (argtype != null) {
JavaType javaType = type(argtype);
parameterTypes.add(javaType);
Expand Down Expand Up @@ -594,7 +593,7 @@ public JavaType.Primitive primitive(TypeTag tag) {
.collect(Collectors.toList());
} else {
try {
defaultValues = Collections.singletonList(methodSymbol.getDefaultValue().getValue().toString());
defaultValues = singletonList(methodSymbol.getDefaultValue().getValue().toString());
} catch (UnsupportedOperationException e) {
// not all Attribute implementations define `getValue()`
}
Expand All @@ -603,7 +602,7 @@ public JavaType.Primitive primitive(TypeTag tag) {

List<String> declaredFormalTypeNames = null;
for (Symbol.TypeVariableSymbol typeParam : methodSymbol.getTypeParameters()) {
if(typeParam.owner == methodSymbol) {
if (typeParam.owner == methodSymbol) {
if (declaredFormalTypeNames == null) {
declaredFormalTypeNames = new ArrayList<>();
}
Expand Down Expand Up @@ -668,7 +667,7 @@ public JavaType.Primitive primitive(TypeTag tag) {

if (!mt.argtypes.isEmpty()) {
parameterTypes = new ArrayList<>(mt.argtypes.size());
for (com.sun.tools.javac.code.Type argtype : mt.argtypes) {
for (Type argtype : mt.argtypes) {
if (argtype != null) {
JavaType javaType = type(argtype);
parameterTypes.add(javaType);
Expand Down Expand Up @@ -697,22 +696,66 @@ 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 = annotationType(a);
if (annotation == null) continue;
annotations.add(annotation);
}
}
return annotations;
}

private JavaType.@Nullable Annotation annotationType(Attribute.Compound compound) {
JavaType.FullyQualified annotType = TypeUtils.asFullyQualified(type(compound.type));
if (annotType == null) {
return null;
}
List<JavaType.Annotation.ElementValue> elementValues = new ArrayList<>();
for (Pair<Symbol.MethodSymbol, Attribute> attr : compound.values) {
Object value = annotationElementValue(attr.snd.getValue());
JavaType.Method element = requireNonNull(methodDeclarationType(attr.fst, annotType));
JavaType.Annotation.ElementValue elementValue = value instanceof Object[] ?
new JavaType.Annotation.ArrayElementValue(element, ((Object[]) value)) :
new JavaType.Annotation.SingleElementValue(element, value);
elementValues.add(elementValue);
}
return new JavaType.Annotation(annotType, elementValues);
}

private Object annotationElementValue(Object value) {
if (value instanceof Symbol.VarSymbol) {
JavaType.Variable mapped = variableType((Symbol.VarSymbol) value);
return mapped != null ? mapped : JavaType.Unknown.getInstance();
} 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(annotationElementValue(attribute));
}
return list.toArray(new Object[0]);
} else if (value instanceof List<?>) {
List<@Nullable Object> list = new ArrayList<>();
for (Object o : ((List<?>) value)) {
list.add(annotationElementValue(o));
}
return list.toArray(new Object[0]);
} else if (value instanceof Attribute.Class) {
return type(((Attribute.Class) value).classType);
} else if (value instanceof Attribute.Compound) {
JavaType.Annotation mapped = annotationType((Attribute.Compound) value);
return mapped != null ? mapped : JavaType.Unknown.getInstance();
} else if (value instanceof Attribute.Constant) {
return annotationElementValue(((Attribute.Constant) value).value);
} else if (value instanceof Attribute.Enum) {
return annotationElementValue(((Attribute.Enum) value).value);
} else if (value instanceof Attribute.Error) {
return JavaType.Unknown.getInstance();
}
return value;
knutwannheden marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading
Loading