Skip to content

Commit

Permalink
Merge branch 'eclipse-jdt:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur authored Nov 2, 2024
2 parents 5bf2739 + 6b20051 commit 979111d
Show file tree
Hide file tree
Showing 184 changed files with 1,316 additions and 782 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public enum SourceVersion {
RELEASE_17,
RELEASE_18,
RELEASE_19,
RELEASE_20;
RELEASE_20,
RELEASE_21,
RELEASE_22,
RELEASE_23;

public static SourceVersion latest() {
return RELEASE_20;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import javax.lang.model.AnnotatedConstruct;
import javax.lang.model.element.*;
import javax.lang.model.util.Elements.DocCommentKind;


public interface Elements {
Expand Down Expand Up @@ -74,6 +75,14 @@ default Set<? extends ModuleElement> getAllModuleElements() {

String getDocComment(Element e);

default DocCommentKind getDocCommentKind(Element e) {
return null;
}

enum DocCommentKind {
END_OF_LINE,
TRADITIONAL
}
boolean isDeprecated(Element e);

default Origin getOrigin(Element e) {
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion org.eclipse.jdt.compiler.apt.tests/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/javax20api.jar"/>
<classpathentry kind="lib" path="lib/javax23api.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 1 addition & 1 deletion org.eclipse.jdt.compiler.apt.tests/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ bin.includes = about.html,\
.
src.includes = about.html
compilerArg=-proc:none
jars.extra.classpath = lib/javax20api.jar
jars.extra.classpath = lib/javax23api.jar
Binary file modified org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors8.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ org.eclipse.jdt.compiler.apt.tests.processors.elements.Java12ElementProcessor
org.eclipse.jdt.compiler.apt.tests.processors.elements.Java13ElementProcessor
org.eclipse.jdt.compiler.apt.tests.processors.elements.RecordElementProcessor
org.eclipse.jdt.compiler.apt.tests.processors.elements.SealedTypeElementProcessor
org.eclipse.jdt.compiler.apt.tests.processors.elements.Java23ElementProcessor
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

package org.eclipse.jdt.compiler.apt.tests.processors.elements;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements.DocCommentKind;
import org.eclipse.jdt.compiler.apt.tests.processors.base.BaseProcessor;

/**
* A processor that explores the Java 23 specific elements and validates the lambda and
* type annotated elements. To enable this processor, add
* -Aorg.eclipse.jdt.compiler.apt.tests.processors.elements.Java23ElementProcessor to the command line.
*/
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class Java23ElementProcessor extends BaseProcessor {
boolean reportSuccessAlready = true;
RoundEnvironment roundEnv = null;
Messager _messager = null;
Filer _filer = null;
boolean isBinaryMode = false;
String mode;
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
_elementUtils = processingEnv.getElementUtils();
_messager = processingEnv.getMessager();
_filer = processingEnv.getFiler();
}
// Always return false from this processor, because it supports "*".
// The return value does not signify success or failure!
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
return false;
}

this.roundEnv = roundEnv;
Map<String, String> options = processingEnv.getOptions();
if (!options.containsKey(this.getClass().getName())) {
// Disable this processor unless we are intentionally performing the test.
return false;
} else {
try {
if (options.containsKey("binary")) {
this.isBinaryMode = true;
this.mode = "binary";
} else {
this.mode = "source";
}
if (!invokeTestMethods(options)) {
testAll();
}
if (this.reportSuccessAlready) {
super.reportSuccess();
}
} catch (AssertionFailedError e) {
super.reportError(getExceptionStackTrace(e));
} catch (Throwable e) {
e.printStackTrace();
}
}
return false;
}

private boolean invokeTestMethods(Map<String, String> options) throws Throwable {
Method testMethod = null;
Set<String> keys = options.keySet();
boolean testsFound = false;
for (String option : keys) {
if (option.startsWith("test")) {
try {
testMethod = this.getClass().getDeclaredMethod(option, new Class[0]);
if (testMethod != null) {
testsFound = true;
testMethod.invoke(this, new Object[0]);
}
} catch (InvocationTargetException e) {
throw e.getCause();
} catch (Exception e) {
super.reportError(getExceptionStackTrace(e));
}
}
}
return testsFound;
}

public void testAll() throws AssertionFailedError, IOException {
testJavadocKind1();
testJavadocKind2();
}

public void testJavadocKind1() throws IOException {
String typeName = "my.mod.Main1";
TypeElement typeElement = _elementUtils.getTypeElement(typeName);
assertNotNull("type element should not be null", typeElement);
DocCommentKind kind = _elementUtils.getDocCommentKind(typeElement);
assertSame("Incorrect doc kind", DocCommentKind.TRADITIONAL, kind);
List<? extends Element> enclosedElements = typeElement.getEnclosedElements();
ExecutableElement method = null;
for (Element element : enclosedElements) {
if ("myMethod".equals(element.getSimpleName().toString())) {
method = (ExecutableElement) element;
}
}
assertNotNull("method element should not be null", method);
kind = _elementUtils.getDocCommentKind(method);
assertSame("Incorrect doc kind", DocCommentKind.END_OF_LINE, kind);
}
public void testJavadocKind2() throws IOException {
String typeName = "my.mod.Main2";
TypeElement typeElement = _elementUtils.getTypeElement(typeName);
assertNotNull("type element should not be null", typeElement);
DocCommentKind kind = _elementUtils.getDocCommentKind(typeElement);
assertSame("Incorrect doc kind", DocCommentKind.END_OF_LINE, kind);
List<? extends Element> enclosedElements = typeElement.getEnclosedElements();
ExecutableElement method = null;
for (Element element : enclosedElements) {
if ("myMethod".equals(element.getSimpleName().toString())) {
method = (ExecutableElement) element;
}
}
assertNotNull("method element should not be null", method);
kind = _elementUtils.getDocCommentKind(method);
assertSame("Incorrect doc kind", DocCommentKind.TRADITIONAL, kind);
}

@Override
public void reportError(String msg) {
throw new AssertionFailedError(msg);
}
private String getExceptionStackTrace(Throwable t) {
StringBuilder buf = new StringBuilder(t.getMessage());
StackTraceElement[] traces = t.getStackTrace();
for (int i = 0; i < traces.length; i++) {
StackTraceElement trace = traces[i];
buf.append("\n\tat " + trace);
if (i == 12)
break; // Don't dump all stacks
}
return buf.toString();
}
public void assertSame(String msg, Object obj1, Object obj2) {
if (obj1 != obj2) {
reportError(msg + ", should be " + obj1.toString() + " but " + obj2.toString());
}
}
public void assertNotNull(String msg, Object obj) {
if (obj == null) {
reportError(msg);
}
}
private static class AssertionFailedError extends Error {
private static final long serialVersionUID = 1L;

public AssertionFailedError(String msg) {
super(msg);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module module.main {
exports my.mod;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package my.mod;

/**
* A traditional Javadoc comment on a class
*/
public class Main1 {
///
/// A markdown type comment on a method
///
public static void myMethod(String argv[]) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package my.mod;

///
/// A markdown type comment on a class
///
public class Main2 {
/**
* A traditional Javadoc comment on a method
*/
public static void myMethod(String argv[]) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

package org.eclipse.jdt.compiler.apt.tests;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.lang.model.SourceVersion;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import junit.framework.TestCase;
import org.eclipse.jdt.internal.compiler.tool.EclipseCompiler;

public class Java23ElementsTests extends TestCase {
private static final String MODULE_PROC = "org.eclipse.jdt.compiler.apt.tests.processors.elements.Java23ElementProcessor";

public void testJavadocKind1() throws IOException {
JavaCompiler compiler = BatchTestUtils.getEclipseCompiler();
internalTestWithBinary(compiler, MODULE_PROC, "23", "testJavadocKind1", null, "modules23", false);
}
public void testJavadocKind2() throws IOException {
JavaCompiler compiler = BatchTestUtils.getEclipseCompiler();
internalTestWithBinary(compiler, MODULE_PROC, "23", "testJavadocKind2", null, "modules23", false);
}
public void testJavadocKind1Javac() throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
internalTestWithBinary(compiler, MODULE_PROC, "23", "testJavadocKind1", null, "modules23", false);
}
public void testJavadocKind2Javac() throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
internalTestWithBinary(compiler, MODULE_PROC, "23", "testJavadocKind2", 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 {
if (!canRunJava23()) {
return;
}
System.clearProperty(processor);
File targetFolder = TestUtils.concatPath(BatchTestUtils.getSrcFolderName(), "mod_locations", resourceArea);
if (testClass == null || testClass.equals("")) {
BatchTestUtils.copyResources("mod_locations/" + resourceArea, targetFolder);
} else {
BatchTestUtils.copyResource("mod_locations/" + resourceArea + "/" + testClass, targetFolder);
}

List<String> options = new ArrayList<>();
options.add("-A" + processor);
options.add("-A" + testMethod);
options.add("-processor");
options.add(processor);
// Javac 1.8 doesn't (yet?) support the -1.8 option
if (compiler instanceof EclipseCompiler) {
options.add("-" + compliance);
} else {
options.add("-source");
options.add(compliance);
}
BatchTestUtils.compileInModuleMode(compiler, options, processor, targetFolder, new DiagnosticListener() {
@Override
public void report(Diagnostic d) {
if (d.getKind() == Diagnostic.Kind.ERROR) {
System.out.println("Compilation error: " + d.getMessage(Locale.getDefault()));
}
}
}, true, processBinariesAgain);
// If it succeeded, the processor will have set this property to "succeeded";
// if not, it will set it to an error value.
assertEquals("succeeded", System.getProperty(processor));
}
public boolean canRunJava23() {
try {
SourceVersion.valueOf("RELEASE_23");
} catch(IllegalArgumentException iae) {
return false;
}
return true;
}
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
@Override
protected void setUp() throws Exception {
super.setUp();
BatchTestUtils.init();
}

/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
}
2 changes: 1 addition & 1 deletion org.eclipse.jdt.core.compiler.batch/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ src.includes = about.html,\
META-INF/services/,\
META-INF/eclipse.inf,\
grammar/
jars.extra.classpath = lib/javax20api.jar,platform:/plugin/org.apache.ant/lib/ant.jar
jars.extra.classpath = lib/javax23api.jar,platform:/plugin/org.apache.ant/lib/ant.jar
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 979111d

Please sign in to comment.