From 7b81556cebccfacfc94ca00f5af88987c87cdefb Mon Sep 17 00:00:00 2001 From: Andy Clement Date: Wed, 6 Mar 2019 07:40:20 -0800 Subject: [PATCH] Fix AspectJ 1.8.x fails with modular and multi-release JARs --- .../testdata/IgnoreJava9JarElements/README | 2 + .../IgnoreJava9JarElements/build.gradle | 54 +++++++++++++ .../build/libs/IgnoreJava9JarElements.jar | Bin 0 -> 2296 bytes .../main/java/org/aspectj/JdkSpecific.java | 15 ++++ .../main/java11/org/aspectj/JdkSpecific.java | 15 ++++ .../src/main/module-info/module-info.java | 11 +++ .../org/aspectj/ajde/core/AjdeCoreTests.java | 2 + .../core/tests/IgnoreJava9JarElements.java | 76 ++++++++++++++++++ .../org/aspectj/weaver/bcel/BcelWeaver.java | 15 +++- 9 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 ajde.core/testdata/IgnoreJava9JarElements/README create mode 100644 ajde.core/testdata/IgnoreJava9JarElements/build.gradle create mode 100644 ajde.core/testdata/IgnoreJava9JarElements/build/libs/IgnoreJava9JarElements.jar create mode 100644 ajde.core/testdata/IgnoreJava9JarElements/src/main/java/org/aspectj/JdkSpecific.java create mode 100644 ajde.core/testdata/IgnoreJava9JarElements/src/main/java11/org/aspectj/JdkSpecific.java create mode 100644 ajde.core/testdata/IgnoreJava9JarElements/src/main/module-info/module-info.java create mode 100644 ajde.core/testsrc/org/aspectj/ajde/core/tests/IgnoreJava9JarElements.java diff --git a/ajde.core/testdata/IgnoreJava9JarElements/README b/ajde.core/testdata/IgnoreJava9JarElements/README new file mode 100644 index 000000000..2993093bd --- /dev/null +++ b/ajde.core/testdata/IgnoreJava9JarElements/README @@ -0,0 +1,2 @@ +Generates a modular, multi-release JAR. To regenerate, run `gradle jar`. The output +is `build/libs/IgnoreJava9JarElements.jar`. diff --git a/ajde.core/testdata/IgnoreJava9JarElements/build.gradle b/ajde.core/testdata/IgnoreJava9JarElements/build.gradle new file mode 100644 index 000000000..70ee66060 --- /dev/null +++ b/ajde.core/testdata/IgnoreJava9JarElements/build.gradle @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2019 Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ + +apply plugin: 'java-library' + +if (!JavaVersion.current().isJava11Compatible()) { + throw new GradleException("Run with with JDK 11 or newer.") +} + +sourceSets { + java11 { + java { + srcDirs = ['src/main/java11'] + } + } + moduleInfo { + java { + srcDirs = ['src/main/module-info'] + } + } +} + +compileJava { + sourceCompatibility = 8 + targetCompatibility = 8 +} + +compileJava11Java { + sourceCompatibility = 11 + targetCompatibility = 11 +} + +compileModuleInfoJava { + sourceCompatibility = 11 + targetCompatibility = 11 +} + +jar { + into('META-INF/versions/11') { + from sourceSets.java11.output + } + into('') { + from sourceSets.moduleInfo.output + } + manifest.attributes( + 'Multi-Release': 'true', + 'Main-Class': 'org.aspectj.JdkSpecific' + ) +} diff --git a/ajde.core/testdata/IgnoreJava9JarElements/build/libs/IgnoreJava9JarElements.jar b/ajde.core/testdata/IgnoreJava9JarElements/build/libs/IgnoreJava9JarElements.jar new file mode 100644 index 0000000000000000000000000000000000000000..634759720b7d4a1c6f38da33315636ecb29f07a7 GIT binary patch literal 2296 zcmWIWW@h1HVBp|jc-xxl#{dLOAOZ+Df!NnI#8KDN&rP41Apk|;5%#B#f`AHwfmj4t zp|7K#r<-eVh@P+8XWuiYeY|z`F7kToYMncCeshq)72^j_Kk1&;Jb8uJ>#V1*j^EZG z4NWayRxY))XD_XutB8q5Yd#TQ`dU>?e2Lohl$VuDQ`ng!XJ*bkxhG~%44NB&*1{dj z0%YeGrDM1gSq%?VO=59DYH~>yhT0QR?%oBAKu-fhKo(7{S4ws;P*-MJX0l##PGWI! zY2azULk=RghgWY|q`=W}{t;K~h2Bmz33D|)kHu;NX6xFFrX9RAY1@P!oOOMACWQ~# ze*_-8?bM`pBlFppdBxA~UAB(DFF&8buW*TCv#w}r$fgxpH{5&=_BF+}ooOvetvER$ zN9MakQ20FugD1W(yJo-Kq5ntkz4Z|h-}8_B`hOdBiKl4EoN8E@R<$qpLxR$pf|M=n z{j$F%@n6acJT=36`oc>)%-w5_@wV^Yc9FC8<*l_l6u;+c1x6_vXUOKiihdO3c9v^D zm-*r+G9oFWZ=P=GXKr(we&)rOHs^)gJ+GhtI`aeb%bmNv3v8ZtxOu_K2L|l2rERb5 z_pQ8n>1=Mj%P;v=pG8`de2hOYQp`!)>SbeW|EPvr(r~dslXTB1y(sj~& z$uw3IrwffbOZB@~Xe3OYkY{&J=P#b97Xn5VJe`)M78Pga=M`f_E^8iF=fU4@UgrncDX{ zlI?r?As)43XFqK8jxSOXYx-oKt16dt3ftP( z=WK&xPH%{O7yi&~#+=5cElrDg`U(#Pyq_pJ_g;d|7wJo1(}LqU&QD*rc=zHCv)p&T zd0CfBzF1*2<-eid-D_p<*n}_r54fvtFAyv>%UXTmjxTz#kLUHUr^J8Iys4kFKypf? z#7&baNx7GO0$eAp)2>NMcxZoVT~LaE(V-_lt#5d%WN%rnRMagtAyQ2sPuMP*{pQt& zVIe#L@q(w1-=7d6eu2Z0{nC_udW)3WW1V*W(%Afg8MS;o)p~zR6ENOZ0I?tt=jNxB z=A`Oo=B4FB$`9+W`Pc2(*w~8M^2*rw+}PGV_;YE|laTFa=7hMhHQxC%<I b-;)(fP7Cm6WdkYU2ErG>ko^Tz#J~Um{dLsn literal 0 HcmV?d00001 diff --git a/ajde.core/testdata/IgnoreJava9JarElements/src/main/java/org/aspectj/JdkSpecific.java b/ajde.core/testdata/IgnoreJava9JarElements/src/main/java/org/aspectj/JdkSpecific.java new file mode 100644 index 000000000..eb4a5fe6c --- /dev/null +++ b/ajde.core/testdata/IgnoreJava9JarElements/src/main/java/org/aspectj/JdkSpecific.java @@ -0,0 +1,15 @@ +/******************************************************************************* + * Copyright (c) 2019 Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ + +package org.aspectj; + +public class JdkSpecific { + public static void main(String... args) { + System.out.println("This is the Java 8 version of the class."); + } +} diff --git a/ajde.core/testdata/IgnoreJava9JarElements/src/main/java11/org/aspectj/JdkSpecific.java b/ajde.core/testdata/IgnoreJava9JarElements/src/main/java11/org/aspectj/JdkSpecific.java new file mode 100644 index 000000000..c560eea91 --- /dev/null +++ b/ajde.core/testdata/IgnoreJava9JarElements/src/main/java11/org/aspectj/JdkSpecific.java @@ -0,0 +1,15 @@ +/******************************************************************************* + * Copyright (c) 2019 Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ + +package org.aspectj; + +public class JdkSpecific { + public static void main(String... args) { + System.out.println("This is the Java 11 version of the class."); + } +} diff --git a/ajde.core/testdata/IgnoreJava9JarElements/src/main/module-info/module-info.java b/ajde.core/testdata/IgnoreJava9JarElements/src/main/module-info/module-info.java new file mode 100644 index 000000000..8bee12d57 --- /dev/null +++ b/ajde.core/testdata/IgnoreJava9JarElements/src/main/module-info/module-info.java @@ -0,0 +1,11 @@ +/******************************************************************************* + * Copyright (c) 2019 Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ + +module org.aspectj.java9plusjar { + +} diff --git a/ajde.core/testsrc/org/aspectj/ajde/core/AjdeCoreTests.java b/ajde.core/testsrc/org/aspectj/ajde/core/AjdeCoreTests.java index 7e45b4757..f95c7211b 100644 --- a/ajde.core/testsrc/org/aspectj/ajde/core/AjdeCoreTests.java +++ b/ajde.core/testsrc/org/aspectj/ajde/core/AjdeCoreTests.java @@ -18,6 +18,7 @@ import org.aspectj.ajde.core.tests.AjConfigTests; import org.aspectj.ajde.core.tests.BuildCancellingTests; import org.aspectj.ajde.core.tests.CompilerMessagesTests; import org.aspectj.ajde.core.tests.DuplicateManifestTests; +import org.aspectj.ajde.core.tests.IgnoreJava9JarElements; import org.aspectj.ajde.core.tests.InpathTests; import org.aspectj.ajde.core.tests.JarManifestTests; import org.aspectj.ajde.core.tests.OutxmlTests; @@ -48,6 +49,7 @@ public class AjdeCoreTests extends TestCase { suite.addTestSuite(JarManifestTests.class); suite.addTestSuite(OutxmlTests.class); suite.addTestSuite(AjConfigTests.class); + suite.addTestSuite(IgnoreJava9JarElements.class); // $JUnit-END$ return suite; diff --git a/ajde.core/testsrc/org/aspectj/ajde/core/tests/IgnoreJava9JarElements.java b/ajde.core/testsrc/org/aspectj/ajde/core/tests/IgnoreJava9JarElements.java new file mode 100644 index 000000000..0de4b811c --- /dev/null +++ b/ajde.core/testsrc/org/aspectj/ajde/core/tests/IgnoreJava9JarElements.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2019 Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.aspectj.ajde.core.tests; + +import java.io.File; +import java.io.IOException; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.jar.Manifest; + +import org.aspectj.ajde.core.AjdeCoreTestCase; +import org.aspectj.ajde.core.TestCompilerConfiguration; +import org.aspectj.ajde.core.TestMessageHandler; + +public class IgnoreJava9JarElements extends AjdeCoreTestCase { + + public static final String injarName = "build/libs/IgnoreJava9JarElements.jar"; + public static final String outjarName = "outjar.jar"; + + private TestMessageHandler handler; + private TestCompilerConfiguration compilerConfig; + + protected void setUp() throws Exception { + super.setUp(); + initialiseProject("IgnoreJava9JarElements"); + handler = (TestMessageHandler) getCompiler().getMessageHandler(); + compilerConfig = (TestCompilerConfiguration) getCompiler() + .getCompilerConfiguration(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + handler = null; + compilerConfig = null; + } + + public void testWeave() { + Set injars = new HashSet(); + injars.add(openFile(injarName)); + compilerConfig.setInpath(injars); + Set aspectpath = new HashSet(); + compilerConfig.setAspectPath(aspectpath); + File outjar = openFile(outjarName); + compilerConfig.setOutjar(outjar.getAbsolutePath()); + doBuild(true); + assertTrue("Expected no compiler errors or warnings but found " + + handler.getMessages(), handler.getMessages().isEmpty()); + compareManifests(openFile(injarName), openFile(outjarName)); + } + + private void compareManifests(File inFile, File outFile) { + + try { + JarFile inJar = new JarFile(inFile); + Manifest inManifest = inJar.getManifest(); + inJar.close(); + JarFile outJar = new JarFile(outFile); + Manifest outManifest = outJar.getManifest(); + outJar.close(); + assertTrue("The manifests in '" + inFile.getCanonicalPath() + + "' and '" + outFile.getCanonicalPath() + + "' sould be the same", inManifest.equals(outManifest)); + } catch (IOException ex) { + fail(ex.toString()); + } + } + +} diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java b/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java index 1a66c72b3..93812bbd0 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java @@ -1,5 +1,5 @@ /* ******************************************************************* - * Copyright (c) 2002-2010 Contributors + * Copyright (c) 2002-2019 Contributors * All rights reserved. * This program and the accompanying materials are made available * under the terms of the Eclipse Public License v1.0 @@ -100,6 +100,7 @@ import org.aspectj.weaver.tools.TraceFactory; * @author PARC * @author Andy Clement * @author Alexandre Vasseur + * @author Eric Edens */ public class BcelWeaver { @@ -391,15 +392,23 @@ public class BcelWeaver { while (entries.hasMoreElements()) { JarEntry entry = (JarEntry) entries.nextElement(); + String filename = entry.getName(); + String filenameLowercase = filename.toLowerCase(); + + // Ignore class files that Java 8 won't understand (multi-release and module-info) + if (filenameLowercase.startsWith("meta-inf") + || filenameLowercase.endsWith("module-info.class")) { + continue; + } + InputStream inStream = inJar.getInputStream(entry); byte[] bytes = FileUtil.readAsByteArray(inStream); - String filename = entry.getName(); // System.out.println("? addJarFile() filename='" + filename // + "'"); UnwovenClassFile classFile = new UnwovenClassFile(new File(outDir, filename).getAbsolutePath(), bytes); - if (filename.endsWith(".class")) { + if (filenameLowercase.endsWith(".class")) { ReferenceType type = this.addClassFile(classFile, false); StringBuffer sb = new StringBuffer(); sb.append(inFile.getAbsolutePath()); -- 2.39.5