]> source.dussan.org Git - aspectj.git/commitdiff
Fixes and tests for bugs 39842 and 40807
authorwisberg <wisberg>
Sat, 26 Jul 2003 04:13:54 +0000 (04:13 +0000)
committerwisberg <wisberg>
Sat, 26 Jul 2003 04:13:54 +0000 (04:13 +0000)
taskdefs/src/org/aspectj/tools/ant/taskdefs/AjcTask.java
taskdefs/src/org/aspectj/tools/ant/taskdefs/ICommandEditor.java [new file with mode: 0644]
taskdefs/testsrc/org/aspectj/tools/ant/taskdefs/AjcTaskTest.java

index a9ced3695b91301f932a2fa7ce4bf4c1034c2d58..d1b6d4d5fa86858a71f6d1a0556770c9cc2dd4eb 100644 (file)
@@ -116,9 +116,6 @@ public class AjcTask extends MatchingTask {
             return "null javac";
         } else if (null == destDir) {
             destDir = javac.getDestdir();
-            if (null == destDir) {
-                destDir = new File(".");
-            }
         }
         // no null checks b/c AjcTask handles null input gracefully
         ajc.setProject(javac.getProject());
@@ -139,7 +136,9 @@ public class AjcTask extends MatchingTask {
         ajc.setTarget(javac.getTarget());        
         ajc.setSource(javac.getSource());        
         ajc.setEncoding(javac.getEncoding());        
-        ajc.setDestdir(destDir);        
+        if (DEFAULT_DESTDIR != destDir) {
+            ajc.setDestdir(destDir);
+        }        
         ajc.setBootclasspath(javac.getBootclasspath());
         ajc.setExtdirs(javac.getExtdirs());
         ajc.setClasspath(javac.getClasspath());        
@@ -224,7 +223,11 @@ public class AjcTask extends MatchingTask {
      */
     private static final int MAX_COMMANDLINE = 4096;
     
-    private static final File DEFAULT_DESTDIR = new File(".");
+    private static final File DEFAULT_DESTDIR = new File(".") {
+        public String toString() {
+            return "AjcTask.DEFAULT_DESTDIR";
+        }
+    };
     
     /** do not throw BuildException on fail/abort message with usage */
     private static final String USAGE_SUBSTRING = "AspectJ-specific options";
@@ -243,7 +246,12 @@ public class AjcTask extends MatchingTask {
         * @see org.aspectj.weaver.Lint 
         */
     private static final List VALID_XLINT;
-        
+
+    public static final String COMMAND_EDITOR_NAME
+        = AjcTask.class.getName() + ".COMMAND_EDITOR";
+
+    private static final ICommandEditor COMMAND_EDITOR;
+            
     static {
         String[] xs = new String[] 
             {   "serializableAspects", "incrementalFile"
@@ -263,7 +271,20 @@ public class AjcTask extends MatchingTask {
         
         xs = new String[] { "error", "warning", "ignore"};
         VALID_XLINT = Collections.unmodifiableList(Arrays.asList(xs));
-        
+    
+        ICommandEditor editor = null;
+        try {
+            String editorClassName = System.getProperty(COMMAND_EDITOR_NAME);
+            if (null != editorClassName) {
+                ClassLoader cl = AjcTask.class.getClassLoader();
+                Class editorClass = cl.loadClass(editorClassName);
+                editor = (ICommandEditor) editorClass.newInstance();
+            }
+        } catch (Throwable t) {
+            System.err.println("Warning: unable to load command editor");
+            t.printStackTrace(System.err);
+        }
+        COMMAND_EDITOR = editor;
     }
        // ---------------------------- state and Ant interface thereto
     private boolean verbose;
@@ -291,6 +312,7 @@ public class AjcTask extends MatchingTask {
     private String[] adapterArguments;
 
     private IMessageHolder messageHolder;
+    private ICommandEditor commandEditor;
 
     // -------- resource-copying
     /** true if copying injar non-.class files to the output jar */
@@ -674,7 +696,32 @@ public class AjcTask extends MatchingTask {
             throw new BuildException(m, t);
         }
     }
-
+    
+    /** direct API for testing */
+    public void setCommandEditor(ICommandEditor editor) {
+        this.commandEditor = editor;
+    }
+    
+    /**
+     * Setup command-line filter.
+     * To do this staticly, define the environment variable
+     * <code>org.aspectj.tools.ant.taskdefs.AjcTask.COMMAND_EDITOR</code>
+     * with the <code>className</code> parameter.
+     * @param className the String fully-qualified-name of a class
+     *          reachable from this object's class loader,
+     *          implementing ICommandEditor, and 
+     *          having a public no-argument constructor.
+     * @throws BuildException if unable to create instance of className
+     */
+    public void setCommandEditorClass(String className) { // skip Ant interface?
+        try {
+            Class mclass = Class.forName(className);
+            setCommandEditor((ICommandEditor) mclass.newInstance());
+        } catch (Throwable t) {
+            String m = "unable to instantiate command editor: " + className;
+            throw new BuildException(m, t);
+        }
+    }
     //---------------------- Path lists
 
     /**
@@ -928,18 +975,38 @@ public class AjcTask extends MatchingTask {
 
         result.addAll(cmd.extractArguments());        
         addListArgs(result);
-        return (String[]) result.toArray(new String[0]);
+
+        String[] command = (String[]) result.toArray(new String[0]);
+        if (null != commandEditor) {
+            command = commandEditor.editCommand(command);
+        } else if (null != COMMAND_EDITOR) {
+            command = COMMAND_EDITOR.editCommand(command);
+        }
+        return command;
     }   
 
     /**
      * @throw BuildException if options conflict
      */
     protected void verifyOptions() {
+        StringBuffer sb = new StringBuffer();
         if (fork && isInIncrementalMode() && !isInIncrementalFileMode()) {
-            String m = "can fork incremental only using tag file";
-            throw new BuildException(m);
+            sb.append("can fork incremental only using tag file.\n");
+        }
+        
+        if ((null == outjar) && (DEFAULT_DESTDIR == destDir)) { 
+            final String REQ = " requires dest dir or output jar.\n";\r            if (copyInjars) {
+                sb.append("copyInjars");
+                sb.append(REQ);
+            }
+            if (null != sourceRootCopyFilter) {
+                sb.append("sourceRootCopyFilter");
+                sb.append(REQ);
+            }
+        }
+        if (0 < sb.length()) {
+            throw new BuildException(sb.toString());
         }
-
     }
 
     /**
@@ -1279,7 +1346,8 @@ public class AjcTask extends MatchingTask {
     private void completeDestdir() {
         if (!copyInjars && (null == sourceRootCopyFilter)) {
             return;
-        } else if (!destDir.canWrite()) {
+        } else if ((destDir == DEFAULT_DESTDIR)
+                    || !destDir.canWrite()) {
             String s = "unable to copy resources to destDir: " + destDir;
             throw new BuildException(s);
         }
diff --git a/taskdefs/src/org/aspectj/tools/ant/taskdefs/ICommandEditor.java b/taskdefs/src/org/aspectj/tools/ant/taskdefs/ICommandEditor.java
new file mode 100644 (file)
index 0000000..98b47cd
--- /dev/null
@@ -0,0 +1,27 @@
+/* *******************************************************************
+ * Copyright (c) 2003 Contributors.
+ * 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: 
+ *     Wes Isberg     initial implementation 
+ * ******************************************************************/
+
+package org.aspectj.tools.ant.taskdefs;
+
+/**
+ * Preprocess command-lines
+ * @author Wes Isberg
+ */
+public interface ICommandEditor {
+    /**
+     * Edit command being used.
+     * @param command the String[] to edit
+     * @return String[] input command if unchanged, 
+     *         or new non-null array of non-null components otherwise
+     */
+    String[] editCommand(String[] command);
+}
index 15ab567e1835cc97b8cfcbdff1bfd3e34d522c20..f7936d1e90624fc9fcfb0a23993293d2b5e3965b 100644 (file)
@@ -133,6 +133,59 @@ public class AjcTaskTest extends TestCase {
         MESSAGES.setLength(0);
     }
     
+    public void testNullDestDir() {
+        AjcTask task = getTask(NOFILE, null);
+        String[] cmd = task.makeCommand();
+        
+        for (int i = 0; i < cmd.length; i++) {
+            assertTrue(!"-d".equals(cmd[i]));
+        }
+    }
+    
+    public void testOutputRequirement() {
+        AjcTask task = getTask("default.lst");
+        checkRun(task, null);
+
+        task = getTask("default.lst", null);
+        task.setCopyInjars(true);
+        checkRun(task, "copyInjars");
+
+        task = getTask("default.lst", null);
+        task.setSourceRootCopyFilter("*.java");
+        checkRun(task, "sourceRoot");
+    }
+    
+    private void checkRun(AjcTask task, String exceptionString) {
+        try {
+            task.execute();
+            assertTrue(null == exceptionString);
+        } catch (BuildException e) {
+            if(-1 == e.getMessage().indexOf(exceptionString)) {
+                assertEquals(exceptionString, e.getMessage());
+            }
+        }
+        
+    }
+
+    public void testCommandEditor() {
+        String className = VerboseCommandEditor.class.getName();
+        System.setProperty(AjcTask.COMMAND_EDITOR_NAME, className);
+        assertEquals(className, 
+            System.getProperty(AjcTask.COMMAND_EDITOR_NAME));
+        AjcTask task = getTask(NOFILE);
+        task.setCommandEditor(new VerboseCommandEditor());
+        String[] cmd = task.makeCommand();
+        assertEquals(VerboseCommandEditor.VERBOSE, cmd[0]);
+
+        task = getTask(NOFILE);
+        task.setCommandEditorClass(VerboseCommandEditor.class.getName());
+        cmd = task.makeCommand();
+        assertEquals(VerboseCommandEditor.VERBOSE, cmd[0]);
+    }
+//    public void testStaticCommandEditor() {
+//        // XXX need to test COMMAND_EDITOR, but can't require property when run
+//    }
+
     public void testLimitTo() {
         int numArgs = 100;
         String arg = "123456789";
@@ -191,10 +244,16 @@ public class AjcTaskTest extends TestCase {
     }
     
     protected AjcTask getTask(String input) {
+        return getTask(input, getTempDir());
+    }
+    
+    protected AjcTask getTask(String input, File destDir) {
         AjcTask task = new AjcTask();
         Project p = new Project();
         task.setProject(p);
-        task.setDestdir(getTempDir());
+        if (null != destDir) {
+            task.setDestdir(destDir);
+        }
         if (NOFILE.equals(input)) {
                // add nothing
         } else if (input.endsWith(".lst")) {
@@ -218,7 +277,7 @@ public class AjcTaskTest extends TestCase {
                }        
         }
         task.setClasspath(new Path(p, "../lib/test/aspectjrt.jar"));
-        task.setVerbose(true); // XXX    
+        //task.setVerbose(true);   
         return task;
     }
     
@@ -498,3 +557,44 @@ public class AjcTaskTest extends TestCase {
         }
     }
 }
+
+class VerboseCommandEditor implements ICommandEditor {
+    public static final String VERBOSE = "-verbose";
+    public String[] editCommand(String[] command) {
+        for (int i = 0; i < command.length; i++) {
+            if (VERBOSE.equals(command[i])) {
+                return command;
+            }
+        }
+        
+        String[] result = new String[1+command.length];
+        result[0] = VERBOSE;
+        System.arraycopy(result, 1, command, 0, command.length);
+        return result;
+    }
+}
+
+class AppendingCommandEditor implements ICommandEditor {
+    private static String[] NONE = new String[0];
+    public static ICommandEditor VERBOSE = 
+        new AppendingCommandEditor(new String[] {"-verbose"}, NONE);
+    public static ICommandEditor INVALID = 
+        new AppendingCommandEditor(NONE, new String[] {"-invalidOption"});
+
+    final String[] prefix;
+    final String[] suffix;
+
+    public AppendingCommandEditor(String[] prefix, String[] suffix) {
+        this.prefix = prefix;
+        this.suffix = suffix;
+    }
+
+    public String[] editCommand(String[] command) {
+        int len = command.length + prefix.length + suffix.length;
+        String[] result = new String[len];
+        System.arraycopy(result, 0, prefix, 0, prefix.length);
+        System.arraycopy(result, prefix.length, command, 0, command.length);
+        System.arraycopy(result, prefix.length + command.length, suffix, 0, suffix.length);
+        return result;
+    }
+}