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 3 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 @@ -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 @@ -705,7 +705,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 @@ -716,7 +716,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 @@ -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.values.get(0).getMethod().getName());
assertEquals("1.2", annotation.values.get(0).getValue());
assertEquals("forRemoval", annotation.values.get(1).getMethod().getName());
assertEquals("true", annotation.values.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.values.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
79 changes: 79 additions & 0 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.fasterxml.jackson.annotation.*;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.With;
import lombok.experimental.FieldDefaults;
Expand All @@ -26,6 +27,7 @@
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.internal.DefaultJavaTypeSignatureBuilder;

import java.lang.invoke.MethodType;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -626,6 +628,83 @@ public static ShallowClass build(String fullyQualifiedName) {
}
}

@Data
class AnnotationValue {
private final Method method;
private final String value;
knutwannheden marked this conversation as resolved.
Show resolved Hide resolved
}

class Annotation extends FullyQualified {
knutwannheden marked this conversation as resolved.
Show resolved Hide resolved

public final List<AnnotationValue> values;
public final FullyQualified type;
knutwannheden marked this conversation as resolved.
Show resolved Hide resolved

public Annotation(FullyQualified type, List<AnnotationValue> values) {
this.type = type;
this.values = values;
}

@Override
public String getFullyQualifiedName() {
return type.getFullyQualifiedName();
}

@Override
public FullyQualified withFullyQualifiedName(String fullyQualifiedName) {
return null;
}

@Override
public List<FullyQualified> getAnnotations() {
return type.getAnnotations();
}

@Override
public boolean hasFlags(Flag... test) {
return type.hasFlags();
}

@Override
public Set<Flag> getFlags() {
return type.getFlags();
}

@Override
public List<FullyQualified> getInterfaces() {
return type.getInterfaces();
}

@Override
public Kind getKind() {
return type.getKind();
}

@Override
public List<Variable> getMembers() {
return type.getMembers();
}

@Override
public List<Method> getMethods() {
return type.getMethods();
}

@Override
public List<JavaType> getTypeParameters() {
return type.getTypeParameters();
}

@Override
public @Nullable FullyQualified getOwningClass() {
return type.getOwningClass();
}

@Override
public @Nullable FullyQualified getSupertype() {
return type.getSupertype();
}
}

@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
class Parameterized extends FullyQualified {
@Getter
Expand Down