Skip to content

Commit

Permalink
Let tests include org.eclipse.jdt.annotation_v1 rather than declaring a
Browse files Browse the repository at this point in the history
dependency

+ access annotation_v1 from a local copy (jar)
+ can no longer use compliance to switch annotation jars since
  version 1.7- are no longer supported
+ revive capability to tests with declaration annotations via new
  class NullDeclarationAnnotationTest
  - pull up helpers from ResourceLeakAnnotatedTests which allow running
    inherited tests.

Fixes #3029
  • Loading branch information
stephan-herrmann committed Oct 11, 2024
1 parent 86a6041 commit 50229da
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 171 deletions.
1 change: 0 additions & 1 deletion org.eclipse.jdt.core.tests.builder/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Require-Bundle: org.junit;bundle-version="3.8.1",
org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)",
org.eclipse.test;bundle-version="[3.6.0,4.0.0)",
org.eclipse.test.performance;bundle-version="[3.1.0,4.0.0)",
org.eclipse.jdt.annotation;bundle-version="[1.1.0,2.0.0)";resolution:=optional,
org.eclipse.jdt.annotation;bundle-version="[2.0.0,3.0.0)";resolution:=optional,
org.eclipse.jdt.apt.core
Bundle-RequiredExecutionEnvironment: JavaSE-17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.builder;

import java.io.File;
import java.io.IOException;
import junit.framework.Test;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.tests.compiler.regression.AbstractNullAnnotationTest;
import org.eclipse.jdt.core.tests.util.Util;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.osgi.framework.Bundle;

/**
* Tests to verify that annotation changes cause recompilation of dependent types.
Expand Down Expand Up @@ -144,17 +142,19 @@ private void addAnnotationType() {

void setupProjectForNullAnnotations() throws JavaModelException {
// add the org.eclipse.jdt.annotation library (bin/ folder or jar) to the project:
Bundle[] bundles = Platform.getBundles("org.eclipse.jdt.annotation","[1.1.0,2.0.0)");
File bundleFile = FileLocator.getBundleFileLocation(bundles[0]).get();
String annotationsLib = bundleFile.isDirectory() ? bundleFile.getPath()+"/bin" : bundleFile.getPath();
IJavaProject javaProject = env.getJavaProject(this.projectPath);
IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
int len = rawClasspath.length;
System.arraycopy(rawClasspath, 0, rawClasspath = new IClasspathEntry[len+1], 0, len);
rawClasspath[len] = JavaCore.newLibraryEntry(new Path(annotationsLib), null, null);
javaProject.setRawClasspath(rawClasspath, null);

javaProject.setOption(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, JavaCore.ENABLED);
try {
String annotationsLib = AbstractNullAnnotationTest.getAnnotationV1LibPath();
IJavaProject javaProject = env.getJavaProject(this.projectPath);
IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
int len = rawClasspath.length;
System.arraycopy(rawClasspath, 0, rawClasspath = new IClasspathEntry[len+1], 0, len);
rawClasspath[len] = JavaCore.newLibraryEntry(new Path(annotationsLib), null, null);
javaProject.setRawClasspath(rawClasspath, null);

javaProject.setOption(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, JavaCore.ENABLED);
} catch (IOException ioe) {
throw new JavaModelException(ioe, -13);
}
}

/**
Expand Down
1 change: 0 additions & 1 deletion org.eclipse.jdt.core.tests.compiler/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Require-Bundle: org.junit;bundle-version="3.8.1",
org.eclipse.test;bundle-version="[3.6.0,4.0.0)",
org.eclipse.test.performance;bundle-version="[3.10.0,4.0.0)",
org.eclipse.core.resources;bundle-version="[3.21.0,4.0.0)",
org.eclipse.jdt.annotation;bundle-version="[1.1.0,2.0.0)";resolution:=optional,
org.eclipse.jdt.annotation;bundle-version="[2.0.0,3.0.0)";resolution:=optional
Import-Package: jakarta.annotation;version="[2.1.0,3.0.0)",
org.eclipse.jdt.internal.compiler.apt.dispatch
Expand Down
3 changes: 2 additions & 1 deletion org.eclipse.jdt.core.tests.compiler/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ bin.includes = test.xml,\
.,\
META-INF/,\
plugin.properties,\
workspace/
workspace/,\
lib/org.eclipse.jdt.annotation_1.2.100.v20241001-0914.jar
source.. = src/
output.. = bin/
src.includes = about.html
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.osgi.framework.Bundle;

Expand Down Expand Up @@ -89,16 +90,24 @@ protected void setUpAnnotationLib() throws IOException {
int len = defaultLibs.length;
this.LIBS = new String[len+1];
System.arraycopy(defaultLibs, 0, this.LIBS, 0, len);
String version = this.complianceLevel >= ClassFileConstants.JDK1_8 ? "[2.0.0,3.0.0)" : "[1.1.0,2.0.0)";
Bundle[] bundles = org.eclipse.jdt.core.tests.compiler.Activator.getPackageAdmin().getBundles("org.eclipse.jdt.annotation", version);
File bundleFile = FileLocator.getBundleFileLocation(bundles[0]).get();
if (bundleFile.isDirectory())
this.LIBS[len] = bundleFile.getPath()+"/bin";
else
this.LIBS[len] = bundleFile.getPath();
this.LIBS[len] = getAnnotationLibPath();
}
}

protected String getAnnotationLibPath() throws IOException {
Bundle[] bundles = org.eclipse.jdt.core.tests.compiler.Activator.getPackageAdmin().getBundles("org.eclipse.jdt.annotation", "[2.0.0,3.0.0)");

Check warning on line 98 in org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/AbstractNullAnnotationTest.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler and API Tools

Deprecation

NORMAL: The type Activator is deprecated

Check warning on line 98 in org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/AbstractNullAnnotationTest.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler and API Tools

Deprecation

NORMAL: The method getPackageAdmin() from the type Activator is deprecated

Check warning on line 98 in org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/AbstractNullAnnotationTest.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler and API Tools

Deprecation

NORMAL: The method getBundles(String, String) from the type PackageAdmin is deprecated
File bundleFile = FileLocator.getBundleFileLocation(bundles[0]).get();
if (bundleFile.isDirectory())
return bundleFile.getPath()+"/bin";
else
return bundleFile.getPath();
}

public static String getAnnotationV1LibPath() throws IOException {
URL libEntry = Platform.getBundle("org.eclipse.jdt.core.tests.compiler").getEntry("/lib/org.eclipse.jdt.annotation_1.2.100.v20241001-0914.jar");
return FileLocator.toFileURL(libEntry).getPath();
}

// Conditionally augment problem detection settings
static boolean setNullRelatedOptions = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
Expand Down Expand Up @@ -1341,6 +1343,59 @@ public void run() {
public AbstractRegressionTest(String name) {
super(name);
}

/* argument 'inheritedDepth' is not exposed in original API, therefore these helpers are copied below with this arg added */
protected static void buildMinimalComplianceTestSuite(int minimalCompliance, int inheritedDepth, TestSuite suite, Class<?> evaluationTestClass) {
int complianceLevels = getPossibleComplianceLevels();
for (int[] map : complianceTestLevelMapping) {
if ((complianceLevels & map[0]) != 0) {
long complianceLevelForJavaVersion = ClassFileConstants.getComplianceLevelForJavaVersion(map[1]);
checkCompliance(evaluationTestClass, minimalCompliance, suite, complianceLevels, inheritedDepth, map[0], map[1], getVersionString(complianceLevelForJavaVersion));
}
}
}
protected static void checkCompliance(Class<?> evaluationTestClass, int minimalCompliance, TestSuite suite, int complianceLevels, int inheritedDepth,
int abstractCompilerTestCompliance, int classFileConstantsVersion, String release) {
int lev = complianceLevels & abstractCompilerTestCompliance;
if (lev != 0) {
if (lev < minimalCompliance) {
System.err.println("Cannot run "+evaluationTestClass.getName()+" at compliance " + release + "!");
} else {
suite.addTest(buildUniqueComplianceTestSuite(evaluationTestClass, ClassFileConstants.getComplianceLevelForJavaVersion(classFileConstantsVersion), inheritedDepth));
}
}
}
public static Test buildUniqueComplianceTestSuite(Class<?> evaluationTestClass, long uniqueCompliance, int inheritedDepth) {
long highestLevel = highestComplianceLevels();
if (highestLevel < uniqueCompliance) {
String complianceString;
if (highestLevel == ClassFileConstants.JDK10)
complianceString = "10";
else if (highestLevel == ClassFileConstants.JDK9)
complianceString = "9";
else if (highestLevel <= CompilerOptions.getFirstSupportedJdkLevel())
complianceString = CompilerOptions.getFirstSupportedJavaVersion();
else {
highestLevel = ClassFileConstants.getLatestJDKLevel();
if (highestLevel > 0) {
complianceString = CompilerOptions.versionFromJdkLevel(highestLevel);
} else {
complianceString = "unknown";
}

}

System.err.println("Cannot run "+evaluationTestClass.getName()+" at compliance "+complianceString+"!");
return new TestSuite();
}
TestSuite complianceSuite = new RegressionTestSetup(uniqueCompliance);
List<Test> tests = buildTestsList(evaluationTestClass, inheritedDepth);
for (int index=0, size=tests.size(); index<size; index++) {
complianceSuite.addTest(tests.get(index));
}
return complianceSuite;
}

protected boolean checkPreviewAllowed() {
return this.complianceLevel == ClassFileConstants.getLatestJDKLevel();
}
Expand Down Expand Up @@ -1736,13 +1791,12 @@ protected String[] getDefaultClassPaths() {
return DefaultJavaRuntimeEnvironment.getDefaultClassPaths();
}
/** Get class library paths built from default class paths plus the JDT null annotations. */
protected String[] getLibsWithNullAnnotations(long sourceLevel) {
protected String[] getLibsWithNullAnnotations() throws IOException {
String[] defaultLibs = getDefaultClassPaths();
int len = defaultLibs.length;
String[] libs = new String[len+1];
System.arraycopy(defaultLibs, 0, libs, 0, len);
String version = sourceLevel < ClassFileConstants.JDK1_8 ? "[1.1.0,2.0.0)" : "[2.0.0,3.0.0)";
Bundle[] bundles = Platform.getBundles("org.eclipse.jdt.annotation", version);
Bundle[] bundles = Platform.getBundles("org.eclipse.jdt.annotation", "[2.0.0,3.0.0)");
File bundleFile = FileLocator.getBundleFileLocation(bundles[0]).get();
if (bundleFile.isDirectory())
libs[len] = bundleFile.getPath()+"/bin";
Expand Down Expand Up @@ -2196,7 +2250,6 @@ protected static void javacUsePathOption(String option) {
}
}
}

/*
* Run Sun compilation using javac.
* Launch compilation in a thread and verify that it does not take more than 5s
Expand Down
Loading

0 comments on commit 50229da

Please sign in to comment.