From 9fca6d2c418cf886f060fb0eaff1c64fc647a8cf Mon Sep 17 00:00:00 2001 From: acolyer Date: Tue, 16 Mar 2004 11:55:54 +0000 Subject: [PATCH] fix for Bugzilla Bug 43714 weaving from an input jar into that same jar. --- .../internal/core/builder/AjBuildManager.java | 49 ++++++++++-- tests/ajcTests.xml | 23 ++++-- .../org/aspectj/weaver/bcel/BcelWeaver.java | 80 +++++++++++++------ 3 files changed, 115 insertions(+), 37 deletions(-) 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 4b128b17c..94ed3a627 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 @@ -94,7 +94,7 @@ public class AjBuildManager implements IOutputClassFileNameProvider,ICompilerAda AjBuildConfig buildConfig, IMessageHandler baseHandler, boolean batch) throws IOException, AbortException { - + boolean ret = true; batchCompile = batch; try { @@ -134,8 +134,7 @@ public class AjBuildManager implements IOutputClassFileNameProvider,ICompilerAda } if (buildConfig.getOutputJar() != null) { - OutputStream os = FileUtil.makeOutputStream(buildConfig.getOutputJar()); - zos = new ZipOutputStream(os); + if (!openOutputStream(buildConfig.getOutputJar())) return false; } if (batch) { @@ -183,16 +182,52 @@ public class AjBuildManager implements IOutputClassFileNameProvider,ICompilerAda if (buildConfig.isGenerateModelMode()) { AsmManager.getDefault().fireModelUpdated(); } - return !handler.hasErrors(); } finally { if (zos != null) { - zos.close(); + closeOutputStream(); } - handler = null; + ret = !handler.hasErrors(); + handler = null; } + return ret; } - private void copyResourcesToDestination() throws IOException { + + private boolean openOutputStream(File outJar) { + try { + OutputStream os = FileUtil.makeOutputStream(buildConfig.getOutputJar()); + zos = new ZipOutputStream(os); + } catch (IOException ex) { + IMessage message = + new Message("Unable to open outjar " + + outJar.getPath() + + "(" + ex.getMessage() + + ")", + new SourceLocation(outJar,0), + true); + handler.handleMessage(message); + return false; + } + return true; + } + + private void closeOutputStream() { + try { + if (zos != null) zos.close(); + } catch (IOException ex) { + IMessage message = + new Message("Unable to write outjar " + + buildConfig.getOutputJar().getPath() + + "(" + ex.getMessage() + + ")", + new SourceLocation(buildConfig.getOutputJar(),0), + true); + handler.handleMessage(message); + } + } + + + private void copyResourcesToDestination() throws IOException { if (buildConfig.getOutputJar() != null) { bcelWeaver.dumpResourcesToOutJar(zos); } else { diff --git a/tests/ajcTests.xml b/tests/ajcTests.xml index a3050d48b..3a6699227 100644 --- a/tests/ajcTests.xml +++ b/tests/ajcTests.xml @@ -7236,7 +7236,7 @@ - + @@ -7430,8 +7430,8 @@ title="fail in compiling aspect with overriding method introduction with different throws clause "> - - @@ -7466,11 +7466,24 @@ - - + + + + + + + + + + + diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java b/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java index e71707cae..222295e95 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java @@ -18,6 +18,7 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; @@ -38,6 +39,8 @@ import org.apache.bcel.classfile.ClassParser; import org.apache.bcel.classfile.JavaClass; import org.aspectj.bridge.IMessage; import org.aspectj.bridge.IProgressListener; +import org.aspectj.bridge.Message; +import org.aspectj.bridge.SourceLocation; import org.aspectj.util.FileUtil; import org.aspectj.weaver.ConcreteTypeMunger; import org.aspectj.weaver.CrosscuttingMembersSet; @@ -53,6 +56,7 @@ import org.aspectj.weaver.WeaverStateInfo; import org.aspectj.weaver.patterns.DeclareParents; import org.aspectj.weaver.patterns.FastMatchInfo; + public class BcelWeaver implements IWeaver { private BcelWorld world; private CrosscuttingMembersSet xcutSet; @@ -105,7 +109,7 @@ public class BcelWeaver implements IWeaver { - public void addLibraryJarFile(File inFile) throws IOException { + public void addLibraryJarFile(File inFile) throws IOException { ZipInputStream inStream = new ZipInputStream(new FileInputStream(inFile)); //??? buffered List addedAspects = new ArrayList(); @@ -189,39 +193,65 @@ public class BcelWeaver implements IWeaver { /** Adds all class files in the jar */ - public List addJarFile(File inFile, File outDir, boolean canBeDirectory) throws IOException { + public List addJarFile(File inFile, File outDir, boolean canBeDirectory){ // System.err.println("? addJarFile(" + inFile + ", " + outDir + ")"); List addedClassFiles = new ArrayList(); needToReweaveWorld = true; + ZipInputStream inStream = null; - // Is this a directory we are looking at? - if (inFile.isDirectory() && canBeDirectory) { - addedClassFiles.addAll(addDirectoryContents(inFile,outDir)); - } else { - - ZipInputStream inStream = new ZipInputStream(new FileInputStream(inFile)); //??? buffered - - while (true) { - ZipEntry entry = inStream.getNextEntry(); - if (entry == null) break; + try { + // Is this a directory we are looking at? + if (inFile.isDirectory() && canBeDirectory) { + addedClassFiles.addAll(addDirectoryContents(inFile,outDir)); + } else { + + inStream = new ZipInputStream(new FileInputStream(inFile)); //??? buffered - byte[] bytes = FileUtil.readAsByteArray(inStream); - String filename = entry.getName(); - UnwovenClassFile classFile = new UnwovenClassFile(new File(outDir, filename).getAbsolutePath(), bytes); + while (true) { + ZipEntry entry = inStream.getNextEntry(); + if (entry == null) break; + + byte[] bytes = FileUtil.readAsByteArray(inStream); + String filename = entry.getName(); + UnwovenClassFile classFile = new UnwovenClassFile(new File(outDir, filename).getAbsolutePath(), bytes); + + if (filename.endsWith(".class")) { + this.addClassFile(classFile); + addedClassFiles.add(classFile); + } + else if (!entry.isDirectory()) { - if (filename.endsWith(".class")) { - this.addClassFile(classFile); - addedClassFiles.add(classFile); - } - else if (!entry.isDirectory()) { + /* bug-44190 Copy meta-data */ + addResource(filename,classFile); + } - /* bug-44190 Copy meta-data */ - addResource(filename,classFile); + inStream.closeEntry(); + } + inStream.close(); + } + } catch (FileNotFoundException ex) { + IMessage message = new Message( + "Could not find input jar file " + inFile.getPath() + ", ignoring", + new SourceLocation(inFile,0), + false); + world.getMessageHandler().handleMessage(message); + } catch (IOException ex) { + IMessage message = new Message( + "Could not read input jar file " + inFile.getPath() + "(" + ex.getMessage() + ")", + new SourceLocation(inFile,0), + true); + world.getMessageHandler().handleMessage(message); + } finally { + if (inStream != null) { + try {inStream.close();} + catch (IOException ex) { + IMessage message = new Message( + "Could not close input jar file " + inFile.getPath() + "(" + ex.getMessage() + ")", + new SourceLocation(inFile,0), + true); + world.getMessageHandler().handleMessage(message); } - - inStream.closeEntry(); } - inStream.close(); } return addedClassFiles; -- 2.39.5