1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/Paths/6638501/WsCompileExample.java Fri Mar 14 16:09:30 2008 -0700 1.3 @@ -0,0 +1,189 @@ 1.4 +/* 1.5 + * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 +import java.util.List; 1.28 +import java.util.ArrayList; 1.29 +import java.io.File; 1.30 +//for CompilerHelper 1.31 +import java.io.OutputStream; 1.32 +import java.io.PrintWriter; 1.33 +import java.lang.reflect.Method; 1.34 +import java.lang.reflect.InvocationTargetException; 1.35 + 1.36 + 1.37 + 1.38 +public class WsCompileExample { 1.39 + File destDir; 1.40 + File srcDir; 1.41 + protected boolean compilerDebug = false; 1.42 + protected boolean compilerOptimize = false; 1.43 + protected String userClasspath = null; 1.44 + 1.45 + public static void main(String[] args) { 1.46 + new WsCompileExample().do_main(args); 1.47 + } 1.48 + 1.49 + public void do_main(String[] args) { 1.50 + if(!args[0].equals("-s")) { 1.51 + throw new RuntimeException("specify -s for src"); 1.52 + } 1.53 + 1.54 + //run it once 1.55 + srcDir = new File(args[1]); 1.56 + if(!args[2].equals("-d")) { 1.57 + throw new RuntimeException("specify -d for dest"); 1.58 + } 1.59 + destDir = new File(args[3]); 1.60 + if(!destDir.exists()) 1.61 + destDir.mkdirs(); 1.62 + System.out.println("----test compile 1-----"); 1.63 + compileGeneratedClasses(); 1.64 + 1.65 + //run it twice 1.66 + srcDir = new File(args[1]+"1"); 1.67 + destDir = new File(args[3]+"1"); 1.68 + if(!destDir.exists()) 1.69 + destDir.mkdirs(); 1.70 + System.out.println("----test compile 2-----"); 1.71 + compileGeneratedClasses(); 1.72 + 1.73 + } 1.74 + protected void compileGeneratedClasses() { 1.75 + List sourceFiles = new ArrayList(); 1.76 + 1.77 + for (File f: srcDir.listFiles()) { 1.78 + if (f.getName().endsWith(".java")) { 1.79 + sourceFiles.add(f.getAbsolutePath()); 1.80 + } 1.81 + } 1.82 + 1.83 + if (sourceFiles.size() > 0) { 1.84 + String classDir = destDir.getAbsolutePath(); 1.85 + String classpathString = createClasspathString(); 1.86 + System.out.println("classpathString: " + classpathString); 1.87 + 1.88 + String[] args = new String[4 + (compilerDebug == true ? 1 : 0) + 1.89 + (compilerOptimize == true ? 1 : 0) + 1.90 + sourceFiles.size()]; 1.91 + args[0] = "-d"; 1.92 + args[1] = classDir; 1.93 + args[2] = "-classpath"; 1.94 + args[3] = classpathString; 1.95 +// args[4]="-DnonBatchMode"; 1.96 + int baseIndex = 4; 1.97 + if (compilerDebug) { 1.98 + args[baseIndex++] = "-g"; 1.99 + } 1.100 + if (compilerOptimize) { 1.101 + args[baseIndex++] = "-O"; 1.102 + } 1.103 + for (int i = 0; i < sourceFiles.size(); ++i) { 1.104 + args[baseIndex + i] = (String)sourceFiles.get(i); 1.105 + } 1.106 + 1.107 + // ByteArrayOutputStream javacOutput = new ByteArrayOutputStream(); 1.108 + JavaCompilerHelper compilerHelper = new JavaCompilerHelper(System.out); 1.109 + boolean result = compilerHelper.compile(args); 1.110 + if (!result) { 1.111 + System.out.println("wscompile.compilation Failed"); 1.112 + } 1.113 + } 1.114 + } 1.115 + 1.116 + protected String createClasspathString() { 1.117 + if (userClasspath == null) { 1.118 + userClasspath = ""; 1.119 + } 1.120 + String jcp = userClasspath + File.pathSeparator + System.getProperty("java.class.path"); 1.121 + return jcp; 1.122 + } 1.123 +} 1.124 +/////////////////////////////////////////////////////////////////// 1.125 +class JavaCompilerHelper { 1.126 + public JavaCompilerHelper(OutputStream out) { 1.127 + this.out = out; 1.128 + } 1.129 + 1.130 + public boolean compile(String[] args) { 1.131 + return internalCompile(args); 1.132 + } 1.133 + 1.134 + protected boolean internalCompile(String[] args) { 1.135 + 1.136 + System.out.println("Args: "); 1.137 + for(String arg : args){ 1.138 + System.out.print(arg+" "); 1.139 + } 1.140 + System.out.println(); 1.141 + ClassLoader cl = Thread.currentThread().getContextClassLoader(); 1.142 + Class comSunToolsJavacMainClass = null; 1.143 + try { 1.144 + /* try to use the new compiler */ 1.145 + comSunToolsJavacMainClass = 1.146 + cl.loadClass("com.sun.tools.javac.Main"); 1.147 + try { 1.148 + Method compileMethod = 1.149 + comSunToolsJavacMainClass.getMethod( 1.150 + "compile", 1.151 + compile141MethodSignature); 1.152 + try { 1.153 + Object result = 1.154 + compileMethod.invoke( 1.155 + null, 1.156 + new Object[] { args, new PrintWriter(out)}); 1.157 + if (!(result instanceof Integer)) { 1.158 + return false; 1.159 + } 1.160 + return ((Integer) result).intValue() == 0; 1.161 + } catch (IllegalAccessException e3) { 1.162 + return false; 1.163 + } catch (IllegalArgumentException e3) { 1.164 + return false; 1.165 + } catch (InvocationTargetException e3) { 1.166 + return false; 1.167 + } 1.168 + } catch (NoSuchMethodException e2) { 1.169 + System.out.println("ERROR: Compile failed with error:" + e2.toString() ); 1.170 + } 1.171 + } catch (ClassNotFoundException e) { 1.172 + e.printStackTrace(); 1.173 + return false; 1.174 + } catch (SecurityException e) { 1.175 + return false; 1.176 + } 1.177 + return true; 1.178 + } 1.179 + 1.180 + protected String getGenericErrorMessage() {return "javacompiler.error"; } 1.181 + protected void run() { } 1.182 + protected boolean parseArguments(String[] args) {return false;} 1.183 + protected OutputStream out; 1.184 + 1.185 + protected static final Class[] compile141MethodSignature; 1.186 + static 1.187 + { 1.188 + compile141MethodSignature = new Class[2]; 1.189 + compile141MethodSignature[0] = (new String[0]).getClass(); 1.190 + compile141MethodSignature[1] = PrintWriter.class; 1.191 + } 1.192 +}