jjg@14: /* ohair@554: * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved. jjg@14: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@14: * jjg@14: * This code is free software; you can redistribute it and/or modify it jjg@14: * under the terms of the GNU General Public License version 2 only, as jjg@14: * published by the Free Software Foundation. jjg@14: * jjg@14: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@14: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@14: * version 2 for more details (a copy is included in the LICENSE file that jjg@14: * accompanied this code). jjg@14: * jjg@14: * You should have received a copy of the GNU General Public License version jjg@14: * 2 along with this work; if not, write to the Free Software Foundation, jjg@14: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@14: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@14: */ jjg@14: jjg@14: import java.util.List; jjg@14: import java.util.ArrayList; jjg@14: import java.io.File; jjg@14: //for CompilerHelper jjg@14: import java.io.OutputStream; jjg@14: import java.io.PrintWriter; jjg@14: import java.lang.reflect.Method; jjg@14: import java.lang.reflect.InvocationTargetException; jjg@14: jjg@14: jjg@14: jjg@14: public class WsCompileExample { jjg@14: File destDir; jjg@14: File srcDir; jjg@14: protected boolean compilerDebug = false; jjg@14: protected boolean compilerOptimize = false; jjg@14: protected String userClasspath = null; jjg@14: jjg@14: public static void main(String[] args) { jjg@14: new WsCompileExample().do_main(args); jjg@14: } jjg@14: jjg@14: public void do_main(String[] args) { jjg@14: if(!args[0].equals("-s")) { jjg@14: throw new RuntimeException("specify -s for src"); jjg@14: } jjg@14: jjg@14: //run it once jjg@14: srcDir = new File(args[1]); jjg@14: if(!args[2].equals("-d")) { jjg@14: throw new RuntimeException("specify -d for dest"); jjg@14: } jjg@14: destDir = new File(args[3]); jjg@14: if(!destDir.exists()) jjg@14: destDir.mkdirs(); jjg@14: System.out.println("----test compile 1-----"); jjg@14: compileGeneratedClasses(); jjg@14: jjg@14: //run it twice jjg@14: srcDir = new File(args[1]+"1"); jjg@14: destDir = new File(args[3]+"1"); jjg@14: if(!destDir.exists()) jjg@14: destDir.mkdirs(); jjg@14: System.out.println("----test compile 2-----"); jjg@14: compileGeneratedClasses(); jjg@14: jjg@14: } jjg@14: protected void compileGeneratedClasses() { jjg@14: List sourceFiles = new ArrayList(); jjg@14: jjg@14: for (File f: srcDir.listFiles()) { jjg@14: if (f.getName().endsWith(".java")) { jjg@14: sourceFiles.add(f.getAbsolutePath()); jjg@14: } jjg@14: } jjg@14: jjg@14: if (sourceFiles.size() > 0) { jjg@14: String classDir = destDir.getAbsolutePath(); jjg@14: String classpathString = createClasspathString(); jjg@14: System.out.println("classpathString: " + classpathString); jjg@14: jjg@14: String[] args = new String[4 + (compilerDebug == true ? 1 : 0) + jjg@14: (compilerOptimize == true ? 1 : 0) + jjg@14: sourceFiles.size()]; jjg@14: args[0] = "-d"; jjg@14: args[1] = classDir; jjg@14: args[2] = "-classpath"; jjg@14: args[3] = classpathString; jjg@14: // args[4]="-DnonBatchMode"; jjg@14: int baseIndex = 4; jjg@14: if (compilerDebug) { jjg@14: args[baseIndex++] = "-g"; jjg@14: } jjg@14: if (compilerOptimize) { jjg@14: args[baseIndex++] = "-O"; jjg@14: } jjg@14: for (int i = 0; i < sourceFiles.size(); ++i) { jjg@14: args[baseIndex + i] = (String)sourceFiles.get(i); jjg@14: } jjg@14: jjg@14: // ByteArrayOutputStream javacOutput = new ByteArrayOutputStream(); jjg@14: JavaCompilerHelper compilerHelper = new JavaCompilerHelper(System.out); jjg@14: boolean result = compilerHelper.compile(args); jjg@14: if (!result) { jjg@14: System.out.println("wscompile.compilation Failed"); jjg@14: } jjg@14: } jjg@14: } jjg@14: jjg@14: protected String createClasspathString() { jjg@14: if (userClasspath == null) { jjg@14: userClasspath = ""; jjg@14: } jjg@14: String jcp = userClasspath + File.pathSeparator + System.getProperty("java.class.path"); jjg@14: return jcp; jjg@14: } jjg@14: } jjg@14: /////////////////////////////////////////////////////////////////// jjg@14: class JavaCompilerHelper { jjg@14: public JavaCompilerHelper(OutputStream out) { jjg@14: this.out = out; jjg@14: } jjg@14: jjg@14: public boolean compile(String[] args) { jjg@14: return internalCompile(args); jjg@14: } jjg@14: jjg@14: protected boolean internalCompile(String[] args) { jjg@14: jjg@14: System.out.println("Args: "); jjg@14: for(String arg : args){ jjg@14: System.out.print(arg+" "); jjg@14: } jjg@14: System.out.println(); jjg@14: ClassLoader cl = Thread.currentThread().getContextClassLoader(); jjg@14: Class comSunToolsJavacMainClass = null; jjg@14: try { jjg@14: /* try to use the new compiler */ jjg@14: comSunToolsJavacMainClass = jjg@14: cl.loadClass("com.sun.tools.javac.Main"); jjg@14: try { jjg@14: Method compileMethod = jjg@14: comSunToolsJavacMainClass.getMethod( jjg@14: "compile", jjg@14: compile141MethodSignature); jjg@14: try { jjg@14: Object result = jjg@14: compileMethod.invoke( jjg@14: null, jjg@14: new Object[] { args, new PrintWriter(out)}); jjg@14: if (!(result instanceof Integer)) { jjg@14: return false; jjg@14: } jjg@14: return ((Integer) result).intValue() == 0; jjg@14: } catch (IllegalAccessException e3) { jjg@14: return false; jjg@14: } catch (IllegalArgumentException e3) { jjg@14: return false; jjg@14: } catch (InvocationTargetException e3) { jjg@14: return false; jjg@14: } jjg@14: } catch (NoSuchMethodException e2) { jjg@14: System.out.println("ERROR: Compile failed with error:" + e2.toString() ); jjg@14: } jjg@14: } catch (ClassNotFoundException e) { jjg@14: e.printStackTrace(); jjg@14: return false; jjg@14: } catch (SecurityException e) { jjg@14: return false; jjg@14: } jjg@14: return true; jjg@14: } jjg@14: jjg@14: protected String getGenericErrorMessage() {return "javacompiler.error"; } jjg@14: protected void run() { } jjg@14: protected boolean parseArguments(String[] args) {return false;} jjg@14: protected OutputStream out; jjg@14: jjg@14: protected static final Class[] compile141MethodSignature; jjg@14: static jjg@14: { jjg@14: compile141MethodSignature = new Class[2]; jjg@14: compile141MethodSignature[0] = (new String[0]).getClass(); jjg@14: compile141MethodSignature[1] = PrintWriter.class; jjg@14: } jjg@14: }