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

[23] Add implementation for Types#stripAnnotations() #3439 #3444

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions JCL/javax23api/src/javax/lang/model/util/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ DeclaredType getDeclaredType(DeclaredType containing,
TypeElement typeElem, TypeMirror... typeArgs);

TypeMirror asMemberOf(DeclaredType containing, Element element);
default <T extends TypeMirror> T stripAnnotations(T t) {
throw new UnsupportedOperationException();
}
}
Binary file modified org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar
Binary file not shown.
Binary file modified org.eclipse.jdt.compiler.apt.tests/lib/javax23api.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements.DocCommentKind;
import org.eclipse.jdt.compiler.apt.tests.processors.base.BaseProcessor;

Expand Down Expand Up @@ -206,6 +211,53 @@ public void testMarkdownContent3() throws IOException {
+ "@param p parameter", docComment);
}

public void testStripAnnotations() {
String typeName = "my.mod.Main3";
String annotationName = "my.annot.MyAnnotation";
TypeElement typeElement = _elementUtils.getTypeElement(typeName);
List<? extends TypeMirror> interfaces = typeElement.getInterfaces();
assertEquals("Wrong no of super interfaces", 1, interfaces.size());
TypeMirror superclass = interfaces.get(0);
List<? extends AnnotationMirror> annotations = superclass.getAnnotationMirrors();
assertEquals("Incorrect no of annotations", 1, annotations.size());
TypeMirror withoutAnnotations = _typeUtils.stripAnnotations(superclass);
assertTrue("Type should not have any annotations", withoutAnnotations.getAnnotationMirrors().isEmpty());
assertTrue("Should be of the same type", _typeUtils.isSameType(superclass, withoutAnnotations));
List<ExecutableElement> methods = ElementFilter.methodsIn(typeElement.getEnclosedElements());
for (ExecutableElement method : methods) {
if (method.getSimpleName().toString().equals("run")) {
continue;
}
TypeMirror typeMirror = method.getReturnType();

TypeMirror expAnnotation = _elementUtils.getTypeElement(annotationName).asType();
annotations = typeMirror.getAnnotationMirrors();
assertEquals("Incorrect no of annotations", 1, annotations.size());
DeclaredType annotationType = typeMirror.getAnnotationMirrors().get(0).getAnnotationType();
assertTrue("Incorrecton annotation type", _typeUtils.isSameType(expAnnotation, annotationType));

// after stripAnnotations() TypeMirror should not have any annotations.
withoutAnnotations = _typeUtils.stripAnnotations(typeMirror);
assertTrue("Type should not have any annotations", withoutAnnotations.getAnnotationMirrors().isEmpty());

TypeMirror typeMirror1 = null;
if (typeMirror.getKind() != TypeKind.WILDCARD) {
assertTrue("Should be of the same type", _typeUtils.isSameType(typeMirror, withoutAnnotations));
} else {
typeMirror1 = _typeUtils.getWildcardType(typeMirror, null);
annotations = typeMirror.getAnnotationMirrors();
assertTrue("Type should not have any annotations", typeMirror1.getAnnotationMirrors().isEmpty());
}

typeMirror1 = _typeUtils.erasure(typeMirror);
annotations = typeMirror.getAnnotationMirrors();
assertEquals("Incorrect no of annotations", 1, annotations.size());

typeMirror1 = _typeUtils.getArrayType(typeMirror);
annotations = typeMirror.getAnnotationMirrors();
assertEquals("Incorrect no of annotations", 1, annotations.size());
}
}
@Override
public void reportError(String msg) {
throw new AssertionFailedError(msg);
Expand Down Expand Up @@ -253,6 +305,11 @@ public void assertNotNull(String msg, Object obj) {
reportError(msg);
}
}
public void assertTrue(String msg, boolean result) {
if (!result) {
reportError(msg);
}
}
private static class AssertionFailedError extends Error {
private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package my.annot;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(ElementType.TYPE_USE)
public @interface MyAnnotation {
String value() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package my.mod;

import my.annot.*;

public class Main3 implements @MyAnnotation Runnable {

public void run() {
}

public static @MyAnnotation("1") int method1() {
return 0;
}

public static @MyAnnotation("2") Integer method2() {
return null;
}

public static @MyAnnotation("3") String method3() {
return null;
}

public static String @MyAnnotation("4") [] method4() {
return null;
}

public static java.util.@MyAnnotation("5") Set<@MyAnnotation("6") String> method5() {
return null;
}

public static <@MyAnnotation("7") T extends @MyAnnotation("8") String> @MyAnnotation("9") T method6() {
return null;
}

public static java.util.@MyAnnotation("10") Set<@MyAnnotation("11") ? extends @MyAnnotation("12") Number> method7() {
return null;
}

public static <@MyAnnotation("13") S extends Number & Runnable> @MyAnnotation("14") S method8() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public void testMarkdownContent3Javac() throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
internalTestWithBinary(compiler, MODULE_PROC, "23", "testMarkdownContent3", null, "modules23", false);
}
public void testStripAnnotations() throws IOException {
JavaCompiler compiler = BatchTestUtils.getEclipseCompiler();
internalTestWithBinary(compiler, MODULE_PROC, "23", "testStripAnnotations", null, "modules23", false);
}
public void testStripAnnotationsJavac() throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
internalTestWithBinary(compiler, MODULE_PROC, "23", "testStripAnnotations", null, "modules23", false);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void internalTestWithBinary(JavaCompiler compiler, String processor, String compliance, String testMethod, String testClass, String resourceArea,
boolean processBinariesAgain) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.jdt.compiler.tool.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.jdt.compiler.tool.tests
Bundle-Version: 1.4.600.qualifier
Bundle-Version: 1.4.700.qualifier
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-RequiredExecutionEnvironment: JavaSE-17
Expand Down
Binary file modified org.eclipse.jdt.compiler.tool.tests/lib/javax23api.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions org.eclipse.jdt.compiler.tool.tests/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2012, 2017 Eclipse Foundation and others.
Copyright (c) 2012, 2024 Eclipse Foundation and others.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Distribution License v1.0
which accompanies this distribution, and is available at
Expand All @@ -19,7 +19,7 @@
<relativePath>../tests-pom/</relativePath>
</parent>
<artifactId>org.eclipse.jdt.compiler.tool.tests</artifactId>
<version>1.4.600-SNAPSHOT</version>
<version>1.4.700-SNAPSHOT</version>
<packaging>eclipse-test-plugin</packaging>
<properties>
<testSuite>${project.artifactId}</testSuite>
Expand Down
Binary file modified org.eclipse.jdt.core.compiler.batch/lib/javax23api.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -570,4 +570,15 @@ public PrimitiveType unboxedType(TypeMirror t) {
return (PrimitiveType) this._env.getFactory().newTypeMirror(unboxed);
}

@Override
@SuppressWarnings("unchecked")
public <T extends TypeMirror> T stripAnnotations(T t) {
if (t instanceof TypeMirrorImpl typeImpl) {
Binding b = typeImpl.binding();
if (b instanceof TypeBinding typeBinding) {
return (T) this._env.getFactory().newTypeMirror(typeBinding.unannotated());
}
}
return t;
}
}
Loading