package edu.neu.ccs.demeter.tools.daj; import java.io.*; import java.util.*; class CommandLine { static void printUsage() { String usage = "Usage: \n" // + "java -jar daj.jar" + "java edu.neu.ccs.demeter.tools.daj.DAJ" + " [options] [java_files] [traversal_files]\n" + " java_files The java files that need to be compiled.\n" + " traversal_files The traversal files that need to be compiled.\n" + "\nOptions\n" + " -gpa Generate Printing Advice.\n" + " -dtrv The path to the generated traversal files\n" + " -ajc Execute an aspect compiler via the shell instead of\n" + " invoking the AspectJ compiler via the classpath.\n" + " -ajtdebug Output debug info during traversal generation\n" + " -ajcopt [options] The options for the AspectJ compiler.\n" + " -tg Stop after generating the stubs.\n" + " -tgc Stop after compiling the stubs.\n" + " -tc Stop after generating the traversals.\n" ; System.out.println(usage); } CommandLine(String args[]) throws CommandLineException { for (int i = 0; i= args.length) fail("-dtrv option must be followed by a directory name."); setTraversalDestinationOption(new File(args[i])); } else if (args[i].equals("-dtmp")) { if (++i >= args.length) fail("-dtmp option must be followed by a directory name."); setTempFileDirOption(new File(args[i])); } else if (args[i].equals("-gencp")) { if (++i >= args.length) fail("-gencp option must be followed by a class path."); setGenerationClassPathOption(args[i]); } else if (args[i].equals("-ajc")) { if (++i >= args.length) fail("-ajc option must be followed by the AspectJ compiler name."); setAJC(args[i]); } else if (args[i].equals("-gpa")) { setGeneratePrintingAdviceOption(); } else if (args[i].equals("-tgc")) { setTraversalGenerationCompilationOption(); } else if (args[i].equals("-tg")) { setTraversalGenerationOption(); } else if (args[i].equals("-tc")) { setTraversalCompilationOption(); } else if (args[i].equals("-ajtdebug")) { setAspectJTraversalDebugOption(); } else if (args[i].equals("-ajcopt")) { while (++i < args.length) addAJCOption(args[i]); } else { fail("Command line argument unrecognized: " + args[i]); } } checkForErrors(); } List javaFiles = new ArrayList(); List trvFiles = new ArrayList(); File dtrv; File dtmp; String gencp; String ajc; boolean gpa; boolean tgc; boolean tg; boolean tc; boolean ajtdebug; List ajcOptions = new ArrayList(); boolean isTraversalGenerationCompilationOption() { return tgc; } void setTraversalGenerationCompilationOption() { tgc = true; } boolean isTraversalGenerationOption() { return tg; } void setTraversalGenerationOption() { tg = true; } boolean isAspectJTraversalDebugOption() { return ajtdebug; } void setAspectJTraversalDebugOption() { ajtdebug = true; } boolean isTraversalCompilationOption() { return tc; } void setTraversalCompilationOption() { tc = true; } boolean getGeneratePrintingAdviceOption() { return gpa; } void setGeneratePrintingAdviceOption() { gpa = true; } String getGenerationClassPathOption() { return gencp; } void setGenerationClassPathOption(String x) { gencp = x; } File getTraversalDestinationOption() { return dtrv; } void setTraversalDestinationOption(File x) { dtrv = x; } File getTempFileDirOption() { return dtmp; } void setTempFileDirOption(File x) { dtmp = x; } List getAJCOptions() { return Collections.unmodifiableList(ajcOptions); } void addAJCOption(String x) { ajcOptions.add(x); } String getAJC() { return ajc; } void setAJC(String x) { ajc = x; } List getJavaFiles() { return Collections.unmodifiableList(javaFiles); } void addJavaFile(File x) { javaFiles.add(x); } List getTraversalFiles() { return Collections.unmodifiableList(trvFiles); } void addTraversalFile(File x) { trvFiles.add(x); } static void fail(String str) throws CommandLineException { throw new CommandLineException(str); } static boolean dirExists(String str) { return dirExists(new File(str)); } static boolean dirExists(File f) { return f.exists() && f.isDirectory() && f.canWrite(); } static boolean fileExists(String str) { return fileExists(new File(str)); } static boolean fileExists(File f) { return f.exists() && f.isFile() && f.canRead(); } void checkForErrors() throws CommandLineException { // make sure that the user has specified at least one java file List flist = getJavaFiles(); if (flist.isEmpty()) fail("You must specify at least one .java file to compile."); // make sure that all directories exist File trvdir = getTraversalDestinationOption(); if (trvdir != null && !dirExists(trvdir)) fail("The traversal generation directory specified by -dtrv\n" + trvdir + " does not exist."); File tmpdir = getTempFileDirOption(); if (tmpdir != null && !dirExists(tmpdir)) fail("The tmp directory specified by the -dtmp\n" + tmpdir + " does not exist or not writable."); if (!filesInCmdLineExist()) { fail("Files do not exist or are not readable."); } } boolean filesInCmdLineExist() { boolean returnVal = true; for (Iterator it = getTraversalFiles().iterator(); it.hasNext();) { File f = (File) it.next(); if (!fileExists(f)) { System.out.println("Traversal file: " + f + " does not exist or is not readable."); returnVal = false; } } for (Iterator it = getJavaFiles().iterator(); it.hasNext();) { File f = (File) it.next(); if (!fileExists(f)) { System.out.println("Java file: " + f + " does not exist or is not readable."); returnVal = false; } } return returnVal; } } class CommandLineException extends Exception { CommandLineException(String str) { super(str); } }