Browse Source

Merge pull request #73 from kriegaex/ajc-release-option

Enable AJC compilation with `--release`
tags/V1_9_8_M1
Andy Clement 2 years ago
parent
commit
82f13664d0
No account linked to committer's email address

+ 4
- 2
bcel-builder/src/main/java/org/aspectj/apache/bcel/Constants.java View File

@@ -96,8 +96,10 @@ public interface Constants {
short MINOR_15 = 0;
short MAJOR_16 = 60;
short MINOR_16 = 0;
// short MAJOR_17 = 61;
// short MINOR_17 = 0;
short MAJOR_17 = 61;
short MINOR_17 = 0;
// short MAJOR_18 = 62;
// short MINOR_18 = 0;

int PREVIEW_MINOR_VERSION = 65535;


+ 4
- 0
org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/ajc/BuildArgParser.java View File

@@ -415,6 +415,10 @@ public class BuildArgParser extends Main {
return ret;
}

public FileSystem.Classpath[] getCheckedClasspaths() {
return checkedClasspaths;
}

private void addExtDirs(String extdirs, List classpathCollector) {
StringTokenizer tokenizer = new StringTokenizer(extdirs, File.pathSeparator);
while (tokenizer.hasMoreTokens()) {

+ 17
- 8
org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/core/builder/AjBuildConfig.java View File

@@ -19,17 +19,20 @@ package org.aspectj.ajdt.internal.core.builder;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

import org.aspectj.ajdt.ajc.BuildArgParser;
import org.aspectj.ajdt.internal.compiler.CompilationResultDestinationManager;
import org.aspectj.org.eclipse.jdt.internal.compiler.batch.ClasspathJep247;
import org.aspectj.org.eclipse.jdt.internal.compiler.batch.ClasspathLocation;
import org.aspectj.org.eclipse.jdt.internal.compiler.batch.FileSystem;
import org.aspectj.org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath;
@@ -930,7 +933,18 @@ public class AjBuildConfig implements CompilerConfigurationChangeFlags {

// ArrayList<Classpath> allPaths = handleBootclasspath(bootclasspaths, customEncoding);
ArrayList<FileSystem.Classpath> allPaths = new ArrayList<>();
allPaths.addAll(processStringPath(bootclasspath, encoding));
if (
Arrays.stream(buildArgParser.getCheckedClasspaths())
.anyMatch(cp -> cp instanceof ClasspathJep247)
) {
allPaths.addAll(
Arrays.stream(buildArgParser.getCheckedClasspaths())
.filter(cp -> cp instanceof ClasspathJep247)
.collect(Collectors.toList())
);
}
else
allPaths.addAll(processStringPath(bootclasspath, encoding));
allPaths.addAll(processFilePath(inJars, encoding));
allPaths.addAll(processFilePath(inPath, encoding));
allPaths.addAll(processFilePath(aspectpath, encoding));
@@ -943,12 +957,7 @@ public class AjBuildConfig implements CompilerConfigurationChangeFlags {
// The classpath is done after modules to give precedence to modules that share the
// same paths as classpath elements (the upcoming normalize will remove later dups)
allPaths.addAll(processStringPath(classpath, encoding));
for (Iterator<FileSystem.Classpath> iter = allPaths.iterator();iter.hasNext();) {
Classpath next = iter.next();
if (next == null) {
iter.remove();
}
}
allPaths.removeIf(Objects::isNull);
allPaths = FileSystem.ClasspathNormalizer.normalize(allPaths);
this.checkedClasspaths = new FileSystem.Classpath[allPaths.size()];
allPaths.toArray(this.checkedClasspaths);

+ 39
- 0
testing/src/test/java/org/aspectj/testing/XMLBasedAjcTestCaseForJava17Only.java View File

@@ -0,0 +1,39 @@
/* *******************************************************************
* Copyright (c) 2021 Contributors
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
* ******************************************************************/
package org.aspectj.testing;

import org.aspectj.util.LangUtil;

/**
* Makes sure tests are running on the right level of JDK.
*
* @author Alexander Kriegisch
*/
public abstract class XMLBasedAjcTestCaseForJava17Only extends XMLBasedAjcTestCase {

@Override
public void setUp() throws Exception {
// Activate this block after upgrading to JDT Core Java 18
/*
throw new IllegalStateException(
"These tests need a Java 17 level AspectJ compiler " +
"(e.g. because they use version-specific preview features). " +
"This compiler does not support preview features of a previous version anymore."
);
*/
if (!LangUtil.is17VMOrGreater() || LangUtil.is18VMOrGreater()) {
throw new IllegalStateException(
"These tests should be run on Java 17 only " +
"(e.g. because they use version-specific preview features)"
);
}
super.setUp();
}

}

+ 27
- 0
testing/src/test/java/org/aspectj/testing/XMLBasedAjcTestCaseForJava17OrLater.java View File

@@ -0,0 +1,27 @@
/* *******************************************************************
* Copyright (c) 2021 Contributors
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
* ******************************************************************/
package org.aspectj.testing;

import org.aspectj.util.LangUtil;

/**
* Makes sure tests are running on the right level of JDK.
*
* @author Alexander Kriegisch
*/
public abstract class XMLBasedAjcTestCaseForJava17OrLater extends XMLBasedAjcTestCase {

@Override
public void setUp() throws Exception {
if (!LangUtil.is17VMOrGreater())
throw new IllegalStateException("These tests should be run on Java 17 or later");
super.setUp();
}

}

+ 20
- 0
tests/features198/compiler_release/Buffers.java View File

@@ -0,0 +1,20 @@
import java.nio.Buffer;
import java.nio.ByteBuffer;

public class Buffers {
/**
* Running this method will fail during runtime on JDK 8, if compiled on JDK 9+ with {@code -source 8 -target 8},
* because the API has changed: In JDK 8 there was only {@code Buffer.flip()}, but since JDK 9 it is overloaded by
* {@code ByteBuffer.flip()}.
* <p>
* Therefore, it is imperative to compile against the old API, using the correct boot classpath. On JDK 9+, the
* canonical way to do this is to use {@code --release 8}, because the JDK contains a compatibility layer exactly for
* this purpose.
* <p>
* If incorrectly compiled against JDK 9+ API, this will fail with:
* <pre>{@code java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer; }</pre>
*/
public static Buffer flip(ByteBuffer buffer) {
return buffer.flip();
}
}

+ 2
- 0
tests/src/test/java/org/aspectj/systemtest/AllTests19.java View File

@@ -17,6 +17,7 @@ import org.aspectj.systemtest.ajc196.AllTestsAspectJ196;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.aspectj.systemtest.ajc197.AllTestsAspectJ197;
import org.aspectj.systemtest.ajc198.AllTestsAspectJ198;

/**
* @author Andy Clement
@@ -34,6 +35,7 @@ public class AllTests19 {
suite.addTest(AllTestsAspectJ195.suite());
suite.addTest(AllTestsAspectJ196.suite());
suite.addTest(AllTestsAspectJ197.suite());
suite.addTest(AllTestsAspectJ198.suite());
suite.addTest(AllTests18.suite());
// $JUnit-END$
return suite;

+ 64
- 0
tests/src/test/java/org/aspectj/systemtest/ajc198/Ajc198TestsJava.java View File

@@ -0,0 +1,64 @@
/*******************************************************************************
* Copyright (c) 2021 Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
*******************************************************************************/
package org.aspectj.systemtest.ajc198;

import junit.framework.Test;
import org.aspectj.apache.bcel.Constants;
import org.aspectj.testing.XMLBasedAjcTestCase;
import org.aspectj.testing.XMLBasedAjcTestCaseForJava17OrLater;

/**
* @author Alexander Kriegisch
*/
public class Ajc198TestsJava extends XMLBasedAjcTestCaseForJava17OrLater {

public void testHiddenClass() {
runTest("hidden class");
checkVersion("HiddenClassDemo", Constants.MAJOR_17, Constants.MINOR_17);
}

public void testTextBlock1() {
runTest("textblock 1");
checkVersion("Code", Constants.MAJOR_17, Constants.MINOR_17);
}

public void testTextBlock2() {
runTest("textblock 2");
checkVersion("Code2", Constants.MAJOR_17, Constants.MINOR_17);
}

public void testRecords() {
runTest("simple record");
checkVersion("Person", Constants.MAJOR_17, Constants.MINOR_17);
}

public void testRecords2() {
runTest("using a record");
checkVersion("UsingPersonRecord", Constants.MAJOR_17, Constants.MINOR_17);
}

public void testAdvisingRecords() {
runTest("advising records");
checkVersion("TraceRecordComponents", Constants.MAJOR_17, Constants.MINOR_17);
}

public void testInstanceofPatterns() {
runTest("instanceof patterns");
checkVersion("Jep305", Constants.MAJOR_17, Constants.MINOR_17);
}

public static Test suite() {
return XMLBasedAjcTestCase.loadSuite(Ajc198TestsJava.class);
}

@Override
protected java.net.URL getSpecFile() {
return getClassResource("ajc198.xml");
}

}

+ 33
- 0
tests/src/test/java/org/aspectj/systemtest/ajc198/AllTestsAspectJ198.java View File

@@ -0,0 +1,33 @@
/*******************************************************************************
* Copyright (c) 2021 Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
*******************************************************************************/
package org.aspectj.systemtest.ajc198;

import junit.framework.Test;
import junit.framework.TestSuite;
import org.aspectj.util.LangUtil;

/**
* @author Alexander Kriegisch
*/
public class AllTestsAspectJ198 {

public static Test suite() {
TestSuite suite = new TestSuite("AspectJ 1.9.8 tests");
if (LangUtil.is9VMOrGreater()) {
suite.addTest(org.aspectj.systemtest.ajc198.CompileWithReleaseTests.suite());
}
if (LangUtil.is17VMOrGreater()) {
suite.addTest(SanityTestsJava17.suite());
suite.addTest(Ajc198TestsJava.suite());
}
if (LangUtil.is17VMOrGreater() && !LangUtil.is18VMOrGreater()) {
suite.addTest(Java17PreviewFeaturesTests.suite());
}
return suite;
}
}

+ 73
- 0
tests/src/test/java/org/aspectj/systemtest/ajc198/CompileWithReleaseTests.java View File

@@ -0,0 +1,73 @@
/*******************************************************************************
* Copyright (c) 2021 Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
*******************************************************************************/
package org.aspectj.systemtest.ajc198;

import junit.framework.Test;
import org.aspectj.apache.bcel.Constants;
import org.aspectj.apache.bcel.classfile.JavaClass;
import org.aspectj.apache.bcel.classfile.Method;
import org.aspectj.testing.XMLBasedAjcTestCase;
import org.aspectj.testing.XMLBasedAjcTestCaseForJava9OrLater;

import java.util.Objects;

/**
* @author Alexander Kriegisch
*/
public class CompileWithReleaseTests extends XMLBasedAjcTestCaseForJava9OrLater {

/**
* In order to avoid a complicated test involving two different JDKs (9+ for compilation, 8 for runtime), we inspect
* the byte code of test class {@code Buffers} with BCEL, simply grepping on the disassembled byte code. If compiled
* correctly with {@code --release 8}, the byte code should contain the equivalent of a {@code Buffer.flip()} call,
* not a {@code ByteBuffer.flip()} one.
*/
public void testCompileToOlderJDKRelease() {
runTest("compile to older JDK release");

// Check compiled byte code version
String className = "Buffers";
checkVersion(className, Constants.MAJOR_1_8, Constants.MINOR_1_8);

// Disassemble method and check if Java 8 API is used as expected
JavaClass javaClass;
try {
javaClass = getClassFrom(ajc.getSandboxDirectory(), className);
}
catch (ClassNotFoundException e) {
throw new IllegalStateException("Cannot find class " + className, e);
}
Method method = Objects.requireNonNull(getMethodFromClass(javaClass, "flip"));
String disassembledMethod = method.getCode().toString();

final String JAVA8_API_CALL = "invokevirtual\tjava.nio.ByteBuffer.flip ()Ljava/nio/Buffer;";
final String JAVA9_API_CALL = "invokevirtual\tjava.nio.ByteBuffer.flip ()Ljava/nio/ByteBuffer;";
if (disassembledMethod.contains(JAVA9_API_CALL))
fail(
"Class '" + className + "' was compiled against Java 9+ API. " +
"There seems to be a problem with the '--release' compiler option.\n" +
"Disassembled method:\n" + disassembledMethod
);
else if (!disassembledMethod.contains(JAVA8_API_CALL))
fail(
"Cannot determine if class '" + className + "' was compiled against Java 8 or 9+ API. " +
"This should never happen.\n" +
"Disassembled method:\n" + disassembledMethod
);
}

public static Test suite() {
return XMLBasedAjcTestCase.loadSuite(CompileWithReleaseTests.class);
}

@Override
protected java.net.URL getSpecFile() {
return getClassResource("ajc198.xml");
}

}

+ 47
- 0
tests/src/test/java/org/aspectj/systemtest/ajc198/Java17PreviewFeaturesTests.java View File

@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2021 Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
*******************************************************************************/
package org.aspectj.systemtest.ajc198;

import junit.framework.Test;
import org.aspectj.apache.bcel.Constants;
import org.aspectj.testing.XMLBasedAjcTestCase;
import org.aspectj.testing.XMLBasedAjcTestCaseForJava17Only;

/**
* @author Alexander Kriegisch
*/
public class Java17PreviewFeaturesTests extends XMLBasedAjcTestCaseForJava17Only {

/*
public void testSealedClassWithLegalSubclasses() {
runTest("sealed class with legal subclasses");
checkVersion("Employee", Constants.MAJOR_16, Constants.PREVIEW_MINOR_VERSION);
checkVersion("Manager", Constants.MAJOR_16, Constants.PREVIEW_MINOR_VERSION);
}

public void testSealedClassWithIllegalSubclass() {
runTest("sealed class with illegal subclass");
checkVersion("Person", Constants.MAJOR_16, Constants.PREVIEW_MINOR_VERSION);
}

public void testWeaveSealedClass() {
runTest("weave sealed class");
checkVersion("PersonAspect", Constants.MAJOR_16, Constants.PREVIEW_MINOR_VERSION);
}
*/

public static Test suite() {
return XMLBasedAjcTestCase.loadSuite(Java17PreviewFeaturesTests.class);
}

@Override
protected java.net.URL getSpecFile() {
return getClassResource("ajc198.xml");
}

}

+ 87
- 0
tests/src/test/java/org/aspectj/systemtest/ajc198/SanityTestsJava17.java View File

@@ -0,0 +1,87 @@
/*******************************************************************************
* Copyright (c) 2021 Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
*******************************************************************************/
package org.aspectj.systemtest.ajc198;

import junit.framework.Test;
import org.aspectj.testing.XMLBasedAjcTestCase;
import org.aspectj.testing.XMLBasedAjcTestCaseForJava17OrLater;

/*
* Some very trivial tests that help verify things are OK.
* These are a copy of the earlier Sanity Tests created for 1.6 but these supply the -10 option
* to check code generation and modification with that version specified.
*
* @author Alexander Kriegisch
*/
public class SanityTestsJava17 extends XMLBasedAjcTestCaseForJava17OrLater {

public static final int bytecode_version_for_JDK_level = 61;

// Incredibly trivial test programs that check the compiler works at all (these are easy-ish to debug)
public void testSimpleJava_A() {
runTest("simple - a");
}

public void testSimpleJava_B() {
runTest("simple - b");
}

public void testSimpleCode_C() {
runTest("simple - c");
}

public void testSimpleCode_D() {
runTest("simple - d");
}

public void testSimpleCode_E() {
runTest("simple - e");
}

public void testSimpleCode_F() {
runTest("simple - f");
}

public void testSimpleCode_G() {
runTest("simple - g");
}

public void testSimpleCode_H() {
runTest("simple - h", true);
}

public void testSimpleCode_I() {
runTest("simple - i");
}

public void testVersionCorrect1() {
runTest("simple - j");
checkVersion("A", bytecode_version_for_JDK_level, 0);
}

public void testVersionCorrect2() {
runTest("simple - k");
checkVersion("A", bytecode_version_for_JDK_level, 0);
}

public void testVersionCorrect4() {
runTest("simple - m");
// Must be 49.0 when -1.5 is specified
checkVersion("A", 49, 0);
}

public static Test suite() {
return XMLBasedAjcTestCase.loadSuite(SanityTestsJava17.class);
}

@Override
protected java.net.URL getSpecFile() {
return getClassResource("sanity-tests-17.xml");
}

}

+ 40
- 0
tests/src/test/resources/org/aspectj/systemtest/ajc198/ajc198.xml View File

@@ -0,0 +1,40 @@
<!DOCTYPE suite SYSTEM "../tests/ajcTestSuite.dtd"[]>


<suite>

<!-- Java ?? final, Java 16, 15 preview -->
<!--
<ajc-test dir="features197/java15" vm="17" title="sealed class with legal subclasses">
<compile files="Person.java Employee.java Manager.java" options="&#45;&#45;enable-preview -16" />
</ajc-test>
-->

<!-- Java ?? final, Java 16, 15 preview -->
<!--
<ajc-test dir="features197/java15" vm="17" title="sealed class with illegal subclass">
<compile files="Person.java Employee.java Manager.java PersonaNonGrata.java" options="&#45;&#45;enable-preview -16">
<message kind="error" file="PersonaNonGrata.java" text="should be a permitted subtype of Person"/>
</compile>
</ajc-test>
-->

<!-- Java ?? final, Java 16, 15 preview -->
<!--
<ajc-test dir="features197/java15" vm="17" title="weave sealed class">
<compile files="Person.java Employee.java Manager.java TopManager.java PersonAspect.aj" options="&#45;&#45;enable-preview -16" />
<run class="TopManager" vmargs="&#45;&#45;enable-preview">
<stdout>
<line text="Hello Sir John" />
<line text="CEO" />
</stdout>
</run>
</ajc-test>
-->

<!-- Javac/ECJ 9+ compiler option, but belated implementation in AJC 1.9.7 -->
<ajc-test dir="features198/compiler_release" vm="9" title="compile to older JDK release">
<compile files="Buffers.java" options="--release 8"/>
</ajc-test>

</suite>

+ 70
- 0
tests/src/test/resources/org/aspectj/systemtest/ajc198/sanity-tests-17.xml View File

@@ -0,0 +1,70 @@
<!DOCTYPE suite SYSTEM "../tests/ajcTestSuite.dtd"[]>

<suite>

<!-- empty class -->
<ajc-test dir="bugs160/simplejava" title="simple - a">
<compile files="SimpleA.java" options="-17"/>
</ajc-test>

<!-- class with one method -->
<ajc-test dir="bugs160/simplejava" title="simple - b">
<compile files="SimpleB.java" options="-17"/>
<run class="SimpleB"/>
</ajc-test>

<!-- empty aspect -->
<ajc-test dir="bugs160/simplejava" title="simple - c">
<compile files="SimpleC.java" options="-17"/>
</ajc-test>

<!-- simple before -->
<ajc-test dir="bugs160/simplejava" title="simple - d">
<compile files="SimpleD.java" options="-17"/>
</ajc-test>

<!-- simple itd field -->
<ajc-test dir="bugs160/simplejava" title="simple - e">
<compile files="SimpleE.java" options="-17"/>
</ajc-test>

<!-- aspect with main calling a static method -->
<ajc-test dir="bugs160/simplejava" title="simple - f">
<compile files="SimpleF.java" options="-17"/>
</ajc-test>

<!-- pertarget -->
<ajc-test dir="bugs160/simplejava" title="simple - g">
<compile files="SimpleG.java" options="-17"/>
</ajc-test>

<!-- generic ctor itds -->
<ajc-test dir="bugs160/simplejava" title="simple - h">
<compile files="SimpleH.java" options="-17"/>
</ajc-test>

<!-- overriding generic itd methods -->
<ajc-test dir="bugs160/simplejava" title="simple - i">
<compile files="SimpleI.java" options="-17"/>
</ajc-test>

<!-- check class file version is 60.0 (Java 16) -->
<ajc-test dir="bugs160/simplejava" title="simple - j">
<compile files="SimpleJ.java" options="-17"/>
</ajc-test>

<!-- check class file version is 60.0 (Java 16) -->
<ajc-test dir="bugs160/simplejava" title="simple - k">
<compile files="SimpleJ.java" options="-source 16"/>
</ajc-test>

<!-- check class file version is 49.0 -->
<ajc-test dir="bugs160/simplejava" title="simple - m">
<compile files="SimpleJ.java" options="-1.5"/>
</ajc-test>

<ajc-test dir="bugs160/simplejava" title="simple - n">
<compile files="SimpleN.java" options="-17"/>
</ajc-test>

</suite>

+ 4
- 0
util/src/main/java/org/aspectj/util/LangUtil.java View File

@@ -190,6 +190,10 @@ public class LangUtil {
return 17 <= vmVersion;
}

public static boolean is18VMOrGreater() {
return 18 <= vmVersion;
}

/**
* Shorthand for "if null, throw IllegalArgumentException"
*

Loading…
Cancel
Save