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;
private void closeOutputStream() {
try {
if (zos != null) zos.close();
+ zos = null;
} catch (IOException ex) {
IMessage message =
new Message("Unable to write outjar "
private void copyResourcesToDestination() throws IOException {
- if (buildConfig.getOutputJar() != null) {
- bcelWeaver.dumpResourcesToOutJar(zos);
- } else {
- bcelWeaver.dumpResourcesToOutPath();
- }
+ // resources that we need to copy are contained in the injars and inpath only
+ for (Iterator i = buildConfig.getInJars().iterator(); i.hasNext(); ) {
+ File inJar = (File)i.next();
+ copyResourcesFromJarFile(inJar);
+ }
+
+ for (Iterator i = buildConfig.getInpath().iterator(); i.hasNext(); ) {
+ File inPathElement = (File)i.next();
+ if (inPathElement.isDirectory()) {
+ copyResourcesFromDirectory(inPathElement);
+ } else {
+ copyResourcesFromJarFile(inPathElement);
+ }
+ }
+
+ if (buildConfig.getSourcePathResources() != null) {
+ for (Iterator i = buildConfig.getSourcePathResources().keySet().iterator(); i.hasNext(); ) {
+ String resource = (String)i.next();
+ copyResourcesFromFile((File)buildConfig.getSourcePathResources().get(resource),
+ resource);
+ }
+ }
}
-
+
+ private void copyResourcesFromJarFile(File jarFile) throws IOException {
+ ZipInputStream inStream = null;
+ try {
+ inStream = new ZipInputStream(new FileInputStream(jarFile));
+ while (true) {
+ ZipEntry entry = inStream.getNextEntry();
+ if (entry == null) break;
+
+ String filename = entry.getName();
+
+ if (!entry.isDirectory() && acceptResource(filename)) {
+ byte[] bytes = FileUtil.readAsByteArray(inStream);
+ writeResource(filename,bytes);
+ }
+
+ inStream.closeEntry();
+ }
+ } finally {
+ if (inStream != null) inStream.close();
+ }
+ }
+
+ private void copyResourcesFromDirectory(File dir) throws IOException {
+ // Get a list of all files (i.e. everything that isnt a directory)
+ File[] files = FileUtil.listFiles(dir,new FileFilter() {
+ public boolean accept(File f) {
+ boolean accept = !(f.isDirectory() || f.getName().endsWith(".class")) ;
+ return accept;
+ }
+ });
+
+ // For each file, add it either as a real .class file or as a resource
+ for (int i = 0; i < files.length; i++) {
+ // ASSERT: files[i].getAbsolutePath().startsWith(inFile.getAbsolutePath()
+ // or we are in trouble...
+ String filename = files[i].getAbsolutePath().substring(
+ dir.getAbsolutePath().length()+1);
+ copyResourcesFromFile(files[i],filename);
+ }
+ }
+
+ private void copyResourcesFromFile(File f,String filename) throws IOException {
+ if (!acceptResource(filename)) return;
+ FileInputStream fis = null;
+ try {
+ fis = new FileInputStream(f);
+ byte[] bytes = FileUtil.readAsByteArray(fis);
+ // String relativePath = files[i].getPath();
+
+ writeResource(filename,bytes);
+ } finally {
+ if (fis != null) fis.close();
+ }
+ }
+
+ private void writeResource(String filename, byte[] content) throws IOException {
+ if (state.resources.contains(filename)) return;
+ if (zos != null) {
+ ZipEntry newEntry = new ZipEntry(filename); //??? get compression scheme right
+
+ zos.putNextEntry(newEntry);
+ zos.write(content);
+ zos.closeEntry();
+ } else {
+ OutputStream fos =
+ FileUtil.makeOutputStream(new File(buildConfig.getOutputDir(),filename));
+ fos.write(content);
+ fos.close();
+ }
+ state.resources.add(filename);
+ }
+
+ private boolean acceptResource(String resourceName) {
+ if (
+ (resourceName.startsWith("CVS/")) ||
+ (resourceName.indexOf("/CVS/") != -1) ||
+ (resourceName.endsWith("/CVS")) ||
+ (resourceName.endsWith(".class"))
+ )
+ {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
/**
* Responsible for managing the ASM model between builds. Contains the policy for
* maintaining the persistance of elements in the model.
state.binarySourceFiles.put(inPathElement.getPath(),unwovenClasses); // good enough for ajc to lump these together
}
- if (buildConfig.getSourcePathResources() != null) {
- for (Iterator i = buildConfig.getSourcePathResources().keySet().iterator(); i.hasNext(); ) {
- // File resource = (File)i.next();
- String resource = (String)i.next();
- bcelWeaver.addResource(resource, (File)buildConfig.getSourcePathResources().get(resource), buildConfig.getOutputDir());
- // bcelWeaver.addResource(resource, buildConfig.getOutputDir());
- }
- }
+// if (buildConfig.getSourcePathResources() != null) {
+// // XXX buildConfig.getSourcePathResources always returns null (CompileCommand.java)
+// for (Iterator i = buildConfig.getSourcePathResources().keySet().iterator(); i.hasNext(); ) {
+// // File resource = (File)i.next();
+// String resource = (String)i.next();
+// bcelWeaver.addResource(resource, (File)buildConfig.getSourcePathResources().get(resource), buildConfig.getOutputDir());
+// // bcelWeaver.addResource(resource, buildConfig.getOutputDir());
+// }
+// }
bcelWeaver.setReweavableMode(buildConfig.isXreweavable(),buildConfig.getXreweavableCompressClasses());
package org.aspectj.ajdt.internal.core.builder;
import java.io.File;
+import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import org.aspectj.ajdt.internal.compiler.InterimCompilationResult;
+import org.aspectj.util.FileUtil;
import org.aspectj.weaver.bcel.UnwovenClassFile;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
Map/*File, List<UnwovenClassFile>*/ binarySourceFiles = new HashMap();
Map/*<String, UnwovenClassFile>*/ classesFromName = new HashMap();
List/*File*/ compiledSourceFiles = new ArrayList();
+ List/*String*/ resources = new ArrayList();
ArrayList/*<String>*/ qualifiedStrings;
ArrayList/*<String>*/ simpleStrings;
thisTime.addAll(addedFiles);
deleteClassFiles();
+ deleteResources();
addAffectedSourceFiles(thisTime,thisTime);
} else {
}
}
}
+
+ private void deleteResources() {
+ List oldResources = new ArrayList();
+ oldResources.addAll(resources);
+
+ // note - this deliberately ignores resources in jars as we don't yet handle jar changes
+ // with incremental compilation
+ for (Iterator i = buildConfig.getInpath().iterator(); i.hasNext(); ) {
+ File inPathElement = (File)i.next();
+ if (inPathElement.isDirectory()) {
+ deleteResourcesFromDirectory(inPathElement,oldResources);
+ }
+ }
+
+ if (buildConfig.getSourcePathResources() != null) {
+ for (Iterator i = buildConfig.getSourcePathResources().keySet().iterator(); i.hasNext(); ) {
+ String resource = (String)i.next();
+ maybeDeleteResource(resource, oldResources);
+ }
+ }
+
+ // oldResources need to be deleted...
+ for (Iterator iter = oldResources.iterator(); iter.hasNext();) {
+ String victim = (String) iter.next();
+ File f = new File(buildConfig.getOutputDir(),victim);
+ if (f.exists()) {
+ f.delete();
+ }
+ resources.remove(victim);
+ }
+ }
+
+ private void maybeDeleteResource(String resName, List oldResources) {
+ if (resources.contains(resName)) {
+ oldResources.remove(resName);
+ File source = new File(buildConfig.getOutputDir(),resName);
+ if ((source != null) && (source.exists()) &&
+ (source.lastModified() >= lastSuccessfulBuildTime)) {
+ resources.remove(resName); // will ensure it is re-copied
+ }
+ }
+ }
+
+ private void deleteResourcesFromDirectory(File dir, List oldResources) {
+ File[] files = FileUtil.listFiles(dir,new FileFilter() {
+ public boolean accept(File f) {
+ boolean accept = !(f.isDirectory() || f.getName().endsWith(".class")) ;
+ return accept;
+ }
+ });
+
+ // For each file, add it either as a real .class file or as a resource
+ for (int i = 0; i < files.length; i++) {
+ // ASSERT: files[i].getAbsolutePath().startsWith(inFile.getAbsolutePath()
+ // or we are in trouble...
+ String filename = files[i].getAbsolutePath().substring(
+ dir.getAbsolutePath().length()+1);
+ maybeDeleteResource(filename, oldResources);
+ }
+ }
private void deleteClassFile(UnwovenClassFile classFile) {
classesFromName.remove(classFile.getClassName());
// private Map sourceJavaClasses = new HashMap(); /* String -> UnwovenClassFile */
private List addedClasses = new ArrayList(); /* List<UnovenClassFile> */
private List deletedTypenames = new ArrayList(); /* List<String> */
- private Map resources = new HashMap(); /* String -> UnwovenClassFile */
+// private Map resources = new HashMap(); /* String -> UnwovenClassFile */
private boolean needToReweaveWorld = false;
private List shadowMungerList = null; // setup by prepareForWeave
// System.err.println("BCELWeaver: processing class from input directory "+classFile);
this.addClassFile(classFile);
addedClassFiles.add(classFile);
- } else {
- if (CopyResourcesFromInpathDirectoriesToOutput) {
- // System.err.println("BCELWeaver: processing resource from input directory "+filename);
- addResource(filename,classFile);
- }
+// } else {
+// if (CopyResourcesFromInpathDirectoriesToOutput) {
+// // System.err.println("BCELWeaver: processing resource from input directory "+filename);
+// addResource(filename,classFile);
+// }
}
fis.close();
}
this.addClassFile(classFile);
addedClassFiles.add(classFile);
}
- else if (!entry.isDirectory()) {
-
- /* bug-44190 Copy meta-data */
- addResource(filename,classFile);
- }
+// else if (!entry.isDirectory()) {
+//
+// /* bug-44190 Copy meta-data */
+// addResource(filename,classFile);
+// }
inStream.closeEntry();
}
return addedClassFiles;
}
- public void addResource(String name, File inPath, File outDir) throws IOException {
-
- /* Eliminate CVS files. Relative paths use "/" */
- if (!name.startsWith("CVS/") && (-1 == name.indexOf("/CVS/")) && !name.endsWith("/CVS")) {
-// System.err.println("? addResource('" + name + "')");
-// BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(inPath));
-// byte[] bytes = new byte[(int)inPath.length()];
-// inStream.read(bytes);
-// inStream.close();
- byte[] bytes = FileUtil.readAsByteArray(inPath);
- UnwovenClassFile resourceFile = new UnwovenClassFile(new File(outDir, name).getAbsolutePath(), bytes);
- addResource(name,resourceFile);
- }
- }
+// public void addResource(String name, File inPath, File outDir) throws IOException {
+//
+// /* Eliminate CVS files. Relative paths use "/" */
+// if (!name.startsWith("CVS/") && (-1 == name.indexOf("/CVS/")) && !name.endsWith("/CVS")) {
+//// System.err.println("? addResource('" + name + "')");
+//// BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(inPath));
+//// byte[] bytes = new byte[(int)inPath.length()];
+//// inStream.read(bytes);
+//// inStream.close();
+// byte[] bytes = FileUtil.readAsByteArray(inPath);
+// UnwovenClassFile resourceFile = new UnwovenClassFile(new File(outDir, name).getAbsolutePath(), bytes);
+// addResource(name,resourceFile);
+// }
+// }
public boolean needToReweaveWorld() {
return needToReweaveWorld;
world.deleteSourceObjectType(TypeX.forName(typename));
}
- public void addResource (String name, UnwovenClassFile resourceFile) {
- /* bug-44190 Change error to warning and copy first resource */
- if (!resources.containsKey(name)) {
- resources.put(name, resourceFile);
- }
- else {
- world.showMessage(IMessage.WARNING, "duplicate resource: '" + name + "'",
- null, null);
- }
- }
+// public void addResource (String name, UnwovenClassFile resourceFile) {
+// /* bug-44190 Change error to warning and copy first resource */
+// if (!resources.containsKey(name)) {
+// resources.put(name, resourceFile);
+// }
+// else {
+// world.showMessage(IMessage.WARNING, "duplicate resource: '" + name + "'",
+// null, null);
+// }
+// }
// ---- weave preparation
// }
// }
- public void dumpResourcesToOutPath() throws IOException {
-// System.err.println("? dumpResourcesToOutPath() resources=" + resources.keySet());
- Iterator i = resources.keySet().iterator();
- while (i.hasNext()) {
- UnwovenClassFile res = (UnwovenClassFile)resources.get(i.next());
- dumpUnchanged(res);
- }
- //resources = new HashMap();
- }
-
+// public void dumpResourcesToOutPath() throws IOException {
+//// System.err.println("? dumpResourcesToOutPath() resources=" + resources.keySet());
+// Iterator i = resources.keySet().iterator();
+// while (i.hasNext()) {
+// UnwovenClassFile res = (UnwovenClassFile)resources.get(i.next());
+// dumpUnchanged(res);
+// }
+// //resources = new HashMap();
+// }
+//
/* BUG #40943 */
- public void dumpResourcesToOutJar() throws IOException {
-// System.err.println("? dumpResourcesToOutJar() resources=" + resources.keySet());
- Iterator i = resources.keySet().iterator();
- while (i.hasNext()) {
- String name = (String)i.next();
- UnwovenClassFile res = (UnwovenClassFile)resources.get(name);
- writeZipEntry(name,res.getBytes());
- }
- //resources = new HashMap();
- }
-
- // halfway house for when the jar is managed outside of the weaver, but the resources
- // to be copied are known in the weaver.
- public void dumpResourcesToOutJar(ZipOutputStream zos) throws IOException {
- this.zipOutputStream = zos;
- dumpResourcesToOutJar();
- }
+// public void dumpResourcesToOutJar() throws IOException {
+//// System.err.println("? dumpResourcesToOutJar() resources=" + resources.keySet());
+// Iterator i = resources.keySet().iterator();
+// while (i.hasNext()) {
+// String name = (String)i.next();
+// UnwovenClassFile res = (UnwovenClassFile)resources.get(name);
+// writeZipEntry(name,res.getBytes());
+// }
+// resources = new HashMap();
+// }
+//
+// // halfway house for when the jar is managed outside of the weaver, but the resources
+// // to be copied are known in the weaver.
+// public void dumpResourcesToOutJar(ZipOutputStream zos) throws IOException {
+// this.zipOutputStream = zos;
+// dumpResourcesToOutJar();
+// }
// ---- weaving
- // Used by some test cases...
+ // Used by some test cases only...
public Collection weave(File file) throws IOException {
OutputStream os = FileUtil.makeOutputStream(file);
this.zipOutputStream = new ZipOutputStream(os);
public IWeaveRequestor getRequestor() {
return new IWeaveRequestor() {
- public void acceptResult(UnwovenClassFile result) {}
+ public void acceptResult(UnwovenClassFile result) {
+ try {
+ writeZipEntry(result.filename, result.bytes);
+ } catch(IOException ex) {}
+ }
public void processingReweavableState() {}
public void addingTypeMungers() {}
public void weavingAspects() {}
};
}
});
- /* BUG 40943*/
- dumpResourcesToOutJar();
+// /* BUG 40943*/
+// dumpResourcesToOutJar();
zipOutputStream.close(); //this flushes and closes the acutal file
return c;
}