Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Dec 10, 2024
1 parent 99ad9cd commit d86eeaa
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@

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 @@ -37,46 +30,4 @@ 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() {
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.getType().getFullyQualifiedName());
assertEquals("since", annotation.getValues().get(0).getElement().getName());
assertEquals("1.2", annotation.getValues().get(0).getValue());
assertEquals("forRemoval", annotation.getValues().get(1).getElement().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.getType().getFullyQualifiedName());
assertTrue(annotation.getValues().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ void recursiveElementValue() {
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@A
private @interface A {
A[] value() default @A;
}
Expand Down Expand Up @@ -453,8 +454,8 @@ class TypeAnnotationTest {
}

@Test
@MinimumJava21
void testPreserveAnnotationsFromClasspath() {
@MinimumJava21 // Because of `@Deprecated#forRemoval`
void annotationElementValues() {
JavaParser p = JavaParser.fromJavaVersion().build();
/*
* Using these annotations in core library for testing this feature:
Expand Down

0 comments on commit d86eeaa

Please sign in to comment.