From 34dbb0c41b3e324e3c7fc5a150c314fbecfd5e6e Mon Sep 17 00:00:00 2001 From: acolyer Date: Wed, 17 Mar 2004 12:25:49 +0000 Subject: [PATCH] fix for Bugzilla Bug 47910 ajc -outjar jarfile does not contain MANIFEST.MF --- ajde/testdata/JarManifestTest/noweave.lst | 3 + ajde/testdata/JarManifestTest/src/Logging.aj | 13 +++ ajde/testdata/JarManifestTest/src/Main.java | 25 ++++++ ajde/testdata/JarManifestTest/weave.lst | 2 + ajde/testsrc/org/aspectj/ajde/AjdeTests.java | 1 + .../org/aspectj/ajde/InpathTestcase.java | 15 ++-- .../org/aspectj/ajde/JarManifestTest.java | 86 +++++++++++++++++++ .../aspectj/ajde/ResourceCopyTestCase.java | 3 +- .../internal/core/builder/AjBuildManager.java | 27 ++++-- .../org/aspectj/weaver/bcel/BcelWeaver.java | 41 +++++++-- 10 files changed, 195 insertions(+), 21 deletions(-) create mode 100644 ajde/testdata/JarManifestTest/noweave.lst create mode 100644 ajde/testdata/JarManifestTest/src/Logging.aj create mode 100644 ajde/testdata/JarManifestTest/src/Main.java create mode 100644 ajde/testdata/JarManifestTest/weave.lst create mode 100644 ajde/testsrc/org/aspectj/ajde/JarManifestTest.java diff --git a/ajde/testdata/JarManifestTest/noweave.lst b/ajde/testdata/JarManifestTest/noweave.lst new file mode 100644 index 000000000..814b7c8ee --- /dev/null +++ b/ajde/testdata/JarManifestTest/noweave.lst @@ -0,0 +1,3 @@ +src/Main.java +src/Logging.aj +-noweave \ No newline at end of file diff --git a/ajde/testdata/JarManifestTest/src/Logging.aj b/ajde/testdata/JarManifestTest/src/Logging.aj new file mode 100644 index 000000000..3ef6ef0e8 --- /dev/null +++ b/ajde/testdata/JarManifestTest/src/Logging.aj @@ -0,0 +1,13 @@ +public aspect Logging { + + pointcut methods () : + execution(* *..*(..)) && !within(Logging); + + before () : methods () { + System.err.println("> " + thisJoinPoint.getSignature().toLongString()); + } + + after () : methods () { + System.err.println("< " + thisJoinPoint.getSignature().toLongString()); + } +} diff --git a/ajde/testdata/JarManifestTest/src/Main.java b/ajde/testdata/JarManifestTest/src/Main.java new file mode 100644 index 000000000..94369dd8e --- /dev/null +++ b/ajde/testdata/JarManifestTest/src/Main.java @@ -0,0 +1,25 @@ +import java.io.IOException; + +/* + * Created on 30-Jul-03 + * + * To change this generated comment go to + * Window>Preferences>Java>Code Generation>Code and Comments + */ + +/** + * @author websterm + * + * To change this generated comment go to + * Window>Preferences>Java>Code Generation>Code and Comments + */ +public class Main { + + public void println () { + System.out.println("Main."); + } + + public static void main(String[] args) throws IOException { + new Main().println(); + } +} diff --git a/ajde/testdata/JarManifestTest/weave.lst b/ajde/testdata/JarManifestTest/weave.lst new file mode 100644 index 000000000..0ad7a8a9a --- /dev/null +++ b/ajde/testdata/JarManifestTest/weave.lst @@ -0,0 +1,2 @@ +src/Main.java +src/Logging.aj diff --git a/ajde/testsrc/org/aspectj/ajde/AjdeTests.java b/ajde/testsrc/org/aspectj/ajde/AjdeTests.java index 06ead0b2c..ecd8471ce 100644 --- a/ajde/testsrc/org/aspectj/ajde/AjdeTests.java +++ b/ajde/testsrc/org/aspectj/ajde/AjdeTests.java @@ -35,6 +35,7 @@ public class AjdeTests extends TestCase { suite.addTestSuite(ResourceCopyTestCase.class); suite.addTestSuite(ModelPerformanceTest.class); suite.addTestSuite(SavedModelConsistencyTest. class); + suite.addTestSuite(JarManifestTest.class); //$JUnit-END$ return suite; diff --git a/ajde/testsrc/org/aspectj/ajde/InpathTestcase.java b/ajde/testsrc/org/aspectj/ajde/InpathTestcase.java index ce7b3e5f3..e65b2441d 100644 --- a/ajde/testsrc/org/aspectj/ajde/InpathTestcase.java +++ b/ajde/testsrc/org/aspectj/ajde/InpathTestcase.java @@ -15,6 +15,7 @@ package org.aspectj.ajde; import java.io.*; import java.util.*; +import java.util.jar.JarInputStream; import java.util.zip.*; import org.aspectj.util.FileUtil; @@ -244,13 +245,13 @@ public class InpathTestcase extends AjdeTestCase { public int fetchFromJar(File outjarFile, String filename) { int ret = -1; try { - ZipInputStream outjar; + JarInputStream outjar; outjar = - new ZipInputStream(new java.io.FileInputStream(outjarFile)); + new JarInputStream(new java.io.FileInputStream(outjarFile)); ZipEntry entry; - while (null != (entry = outjar.getNextEntry())) { + while (null != (entry = (ZipEntry)outjar.getNextEntry())) { String zipentryname = entry.getName(); if (zipentryname.equals(filename)) { byte[] filedata = FileUtil.readAsByteArray(outjar); @@ -285,8 +286,8 @@ public class InpathTestcase extends AjdeTestCase { // Go through the output jar file, for each element, remove it from // the expectedOutputJarContents - when we finish, the expectedOutputJarContents // set should be empty! - ZipInputStream outjar = - new ZipInputStream(new java.io.FileInputStream(outjarFile)); + JarInputStream outjar = + new JarInputStream(new java.io.FileInputStream(outjarFile)); ZipEntry entry; while (null != (entry = outjar.getNextEntry())) { String fileName = entry.getName(); @@ -319,8 +320,8 @@ public class InpathTestcase extends AjdeTestCase { try { - ZipInputStream outjar = - new ZipInputStream(new java.io.FileInputStream(outjarFile)); + JarInputStream outjar = + new JarInputStream(new java.io.FileInputStream(outjarFile)); ZipEntry entry; while (null != (entry = outjar.getNextEntry())) { String fileName = entry.getName(); diff --git a/ajde/testsrc/org/aspectj/ajde/JarManifestTest.java b/ajde/testsrc/org/aspectj/ajde/JarManifestTest.java new file mode 100644 index 000000000..6583dae49 --- /dev/null +++ b/ajde/testsrc/org/aspectj/ajde/JarManifestTest.java @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/* + * Created on 16-Mar-2004 + * + * To change the template for this generated file go to + * Window>Preferences>Java>Code Generation>Code and Comments + */ +package org.aspectj.ajde; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.jar.JarInputStream; +import java.util.jar.Manifest; + +import org.aspectj.util.FileUtil; + +/** + * @author websterm + * + * To change the template for this generated type comment go to + * Window>Preferences>Java>Code Generation>Code and Comments + */ +public class JarManifestTest extends AjdeTestCase { + + public static final String PROJECT_DIR = "JarManifestTest"; + public static final String srcDir = PROJECT_DIR + "/src"; + public static final String binDir = "bin"; + + public static final String outjarName = "/bin/output.jar"; + + /** + * Constructor for JarResourceCopyTestCase. + * @param arg0 + */ + public JarManifestTest (String arg0) { + super(arg0); + } + + /* + * Ensure the output directpry in clean + */ + protected void setUp() throws Exception { + super.setUp(PROJECT_DIR); + FileUtil.deleteContents(openFile(binDir)); + } + + public void testWeave () { + File outjar = openFile(outjarName); + ideManager.getProjectProperties().setOutJar(outjar.getAbsolutePath()); + assertTrue("Build failed",doSynchronousBuild("weave.lst")); + assertTrue("Build warnings",ideManager.getCompilationSourceLineTasks().isEmpty()); + checkManifest(outjar); + } + + public void testNoweave () { + File outjar = openFile(outjarName); + ideManager.getProjectProperties().setOutJar(outjar.getAbsolutePath()); + assertTrue("Build failed",doSynchronousBuild("noweave.lst")); + assertTrue("Build warnings",ideManager.getCompilationSourceLineTasks().isEmpty()); + checkManifest(outjar); + } + + private void checkManifest (File outjarFile) { + Manifest manifest = null; + + try { + JarInputStream outjar = new JarInputStream(new FileInputStream(outjarFile)); + manifest = outjar.getManifest(); + outjar.close(); + assertNotNull("'" + outjarFile.getCanonicalPath() + "' should contain a manifest",manifest); + } + catch (IOException ex) { + fail(ex.toString()); + } + } +} diff --git a/ajde/testsrc/org/aspectj/ajde/ResourceCopyTestCase.java b/ajde/testsrc/org/aspectj/ajde/ResourceCopyTestCase.java index bd02c18db..cb7ca236d 100644 --- a/ajde/testsrc/org/aspectj/ajde/ResourceCopyTestCase.java +++ b/ajde/testsrc/org/aspectj/ajde/ResourceCopyTestCase.java @@ -14,6 +14,7 @@ package org.aspectj.ajde; import java.io.*; import java.util.*; +import java.util.jar.JarInputStream; import java.util.zip.*; import org.aspectj.util.FileUtil; @@ -164,7 +165,7 @@ public class ResourceCopyTestCase extends AjdeTestCase { try { - ZipInputStream outjar = new ZipInputStream(new java.io.FileInputStream(outjarFile)); + ZipInputStream outjar = new JarInputStream(new java.io.FileInputStream(outjarFile)); ZipEntry entry; while (null != (entry = outjar.getNextEntry())) { String fileName = entry.getName(); diff --git a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/AjBuildManager.java b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/AjBuildManager.java index 6a6e0aa8f..f49ea9e3c 100644 --- a/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/AjBuildManager.java +++ b/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/AjBuildManager.java @@ -17,8 +17,6 @@ import java.io.*; import java.util.*; import java.util.jar.*; import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; -import java.util.zip.ZipOutputStream; import org.aspectj.ajdt.internal.compiler.AjCompilerAdapter; import org.aspectj.ajdt.internal.compiler.IIntermediateResultsRequestor; @@ -45,6 +43,7 @@ import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; public class AjBuildManager implements IOutputClassFileNameProvider,ICompilerAdapterFactory { private static final String CANT_WRITE_RESULT = "unable to write compilation result"; + private static final String MANIFEST_NAME = "META-INF/MANIFEST.MF"; static final boolean COPY_INPATH_DIR_RESOURCES = false; static final boolean FAIL_IF_RUNTIME_NOT_FOUND = false; private IProgressListener progressListener = null; @@ -52,7 +51,7 @@ public class AjBuildManager implements IOutputClassFileNameProvider,ICompilerAda private int compiledCount; private int sourceFileCount; - private ZipOutputStream zos; + private JarOutputStream zos; private boolean batchCompile = true; private INameEnvironment environment; @@ -198,7 +197,7 @@ public class AjBuildManager implements IOutputClassFileNameProvider,ICompilerAda private boolean openOutputStream(File outJar) { try { OutputStream os = FileUtil.makeOutputStream(buildConfig.getOutputJar()); - zos = new ZipOutputStream(os); + zos = new JarOutputStream(os,bcelWeaver.getManifest(true)); } catch (IOException ex) { IMessage message = new Message("Unable to open outjar " @@ -253,12 +252,14 @@ public class AjBuildManager implements IOutputClassFileNameProvider,ICompilerAda copyResourcesFromFile(from,resource,from); } } + + writeManifest(); } private void copyResourcesFromJarFile(File jarFile) throws IOException { - ZipInputStream inStream = null; + JarInputStream inStream = null; try { - inStream = new ZipInputStream(new FileInputStream(jarFile)); + inStream = new JarInputStream(new FileInputStream(jarFile)); while (true) { ZipEntry entry = inStream.getNextEntry(); if (entry == null) break; @@ -334,6 +335,20 @@ public class AjBuildManager implements IOutputClassFileNameProvider,ICompilerAda } state.resources.add(filename); } + + /* + * If we are writing to an output directory copy the manifest but only + * if we already have one + */ + private void writeManifest () throws IOException { + Manifest manifest = bcelWeaver.getManifest(false); + if (manifest != null && zos == null) { + OutputStream fos = + FileUtil.makeOutputStream(new File(buildConfig.getOutputDir(),MANIFEST_NAME)); + manifest.write(fos); + fos.close(); + } + } private boolean acceptResource(String resourceName) { if ( diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java b/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java index 7cc91555b..d900c2035 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java @@ -25,12 +25,14 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Set; +import java.util.jar.Attributes; +import java.util.jar.Attributes.Name; +import java.util.jar.JarInputStream; +import java.util.jar.Manifest; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; @@ -80,7 +82,8 @@ public class BcelWeaver implements IWeaver { // private Map sourceJavaClasses = new HashMap(); /* String -> UnwovenClassFile */ private List addedClasses = new ArrayList(); /* List */ private List deletedTypenames = new ArrayList(); /* List */ -// private Map resources = new HashMap(); /* String -> UnwovenClassFile */ +// private Map resources = new HashMap(); /* String -> UnwovenClassFile */ + private Manifest manifest = null; private boolean needToReweaveWorld = false; private List shadowMungerList = null; // setup by prepareForWeave @@ -142,8 +145,8 @@ public class BcelWeaver implements IWeaver { } - // The ANT copy task should be used to copy resources across. - private final static boolean CopyResourcesFromInpathDirectoriesToOutput=false; +// // The ANT copy task should be used to copy resources across. +// private final static boolean CopyResourcesFromInpathDirectoriesToOutput=false; private Set alreadyConfirmedReweavableState; /** @@ -197,7 +200,7 @@ public class BcelWeaver implements IWeaver { // System.err.println("? addJarFile(" + inFile + ", " + outDir + ")"); List addedClassFiles = new ArrayList(); needToReweaveWorld = true; - ZipInputStream inStream = null; + JarInputStream inStream = null; try { // Is this a directory we are looking at? @@ -205,7 +208,8 @@ public class BcelWeaver implements IWeaver { addedClassFiles.addAll(addDirectoryContents(inFile,outDir)); } else { - inStream = new ZipInputStream(new FileInputStream(inFile)); //??? buffered + inStream = new JarInputStream(new FileInputStream(inFile)); //??? buffered + addManifest(inStream.getManifest()); while (true) { ZipEntry entry = inStream.getNextEntry(); @@ -387,6 +391,29 @@ public class BcelWeaver implements IWeaver { // dumpResourcesToOutJar(); // } + public void addManifest (Manifest newManifest) { + if (manifest == null) { + manifest = newManifest; + } + } + + private static final String WEAVER_MANIFEST_VERSION = "1.0"; + private static final Attributes.Name CREATED_BY = new Name("Created-By"); + private static final String WEAVER_CREATED_BY = "AspectJ Compiler"; + + public Manifest getManifest (boolean shouldCreate) { + + if (manifest == null && shouldCreate) { + manifest = new Manifest(); + + Attributes attributes = manifest.getMainAttributes(); + attributes.put(Name.MANIFEST_VERSION,WEAVER_MANIFEST_VERSION); + attributes.put(CREATED_BY,WEAVER_CREATED_BY); + } + + return manifest; + } + // ---- weaving // Used by some test cases only... -- 2.39.5