1 24 25 package org.aspectj.compiler.base; 26 27 import java.io.File ; 28 29 import java.util.*; 30 import java.io.OutputStream ; 31 import java.lang.reflect.*; 32 33 import org.aspectj.compiler.base.*; 34 import org.aspectj.compiler.base.ast.World; 35 36 import org.aspectj.compiler.base.JavaCompiler; 37 import org.aspectj.compiler.base.CompilerObject; 38 39 public class JavaCWrapper extends CompilerObject { 40 41 public JavaCWrapper(JavaCompiler compiler) { 42 super(compiler); 43 } 44 45 private String addClassPath(File pathdir) { 46 String classpath = getOptions().classpath; if (classpath != null) return pathdir.getPath()+File.pathSeparator+classpath; 48 classpath = System.getProperty("java.class.path"); 49 if (classpath != null) return pathdir.getPath()+File.pathSeparator+classpath; 50 return pathdir.getPath(); 51 } 52 53 public boolean compile(File inputdir, File outputdir, File [] files) { 54 List args = new ArrayList(files.length+6); 55 56 args.add("-d"); 57 args.add(inputdir.getPath()); 58 args.add("-classpath"); 59 args.add(addClassPath(outputdir)); 60 if (getOptions().deprecation) { 61 args.add("-deprecation"); 62 } 63 if (getOptions().O) { 64 args.add("-O"); 65 } 66 if (getOptions().g) { 67 args.add("-g"); 68 } 69 if (getOptions().source.equals("1.4")) { 70 args.add("-source"); 71 args.add("1.4"); 72 } 73 if (getOptions().target != null) { 74 args.add("-target"); 75 args.add(getOptions().target); 76 } 77 78 if (getOptions().bootclasspath != null) { 79 args.add("-bootclasspath"); 80 args.add(getOptions().bootclasspath); 81 } 82 83 if (getOptions().extdirs != null) { 84 args.add("-extdirs"); 85 args.add(getOptions().extdirs); 86 } 87 88 for(int i=0; i<files.length; i++) { 90 args.add(files[i].getPath()); 91 } 92 93 101 102 103 String [] argsArray = (String [])args.toArray(new String [0]); 104 106 Class compilerClass; 107 108 try { 109 compilerClass = Class.forName("com.sun.tools.javac.Main"); 110 return doModernCompile(compilerClass, argsArray); 111 } catch (ClassNotFoundException ce1) { 112 try { 113 compilerClass = Class.forName("sun.tools.javac.Main"); 114 return doClassicCompile(compilerClass, argsArray); 115 } catch (ClassNotFoundException ce2) { 116 showNoJavacError(); 117 return false; 118 } 119 } 120 } 121 122 123 void showNoJavacError() { 124 System.err.println("The -usejavac flag requires that com.sun.tools.javac.Main"); 125 System.err.println("is on your classpath. Generally, this will involve editing"); 126 System.err.println("your launch script (ajc.bat or ajc) to include tools.jar"); 127 System.err.println("on the classpath that is passed to the jvm which runs ajc."); 128 System.err.println("See README-TOOLS.html for suggestions on how to fix this."); 129 throw new ExitRequestException(-1); 130 } 131 132 133 134 boolean doModernCompile(Class compiler, String [] args) { 135 getCompiler().showMessage(" modern compile"); 136 try { 138 Method m = compiler.getMethod("compile", new Class [] {String [].class}); 139 Object inst = compiler.newInstance(); 140 Integer i = (Integer )m.invoke(inst, new Object [] {args}); 141 return i.intValue() == 0; 142 } catch (Exception e) { 143 System.out.println(e); 144 return false; 145 } 146 } 147 148 boolean doClassicCompile(Class compiler, String [] args) { 149 getCompiler().showMessage(" classic compile"); 150 try { 154 Constructor c = compiler.getConstructor(new Class [] {OutputStream .class, String .class}); 155 Object inst = c.newInstance(new Object [] {System.err, "ajc"}); 156 Method m = compiler.getMethod("compile", new Class [] {String [].class}); 157 Boolean b = (Boolean )m.invoke(inst, new Object [] {args}); 158 return b.booleanValue(); 159 } catch (Exception e) { 160 System.out.println(e); 161 return false; 162 } 163 } 164 } 165 | Popular Tags |