Thu, 31 Aug 2017 15:17:03 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | import java.util.List; |
aoqi@0 | 25 | import java.util.ArrayList; |
aoqi@0 | 26 | import java.io.File; |
aoqi@0 | 27 | //for CompilerHelper |
aoqi@0 | 28 | import java.io.OutputStream; |
aoqi@0 | 29 | import java.io.PrintWriter; |
aoqi@0 | 30 | import java.lang.reflect.Method; |
aoqi@0 | 31 | import java.lang.reflect.InvocationTargetException; |
aoqi@0 | 32 | |
aoqi@0 | 33 | |
aoqi@0 | 34 | |
aoqi@0 | 35 | public class WsCompileExample { |
aoqi@0 | 36 | File destDir; |
aoqi@0 | 37 | File srcDir; |
aoqi@0 | 38 | protected boolean compilerDebug = false; |
aoqi@0 | 39 | protected boolean compilerOptimize = false; |
aoqi@0 | 40 | protected String userClasspath = null; |
aoqi@0 | 41 | |
aoqi@0 | 42 | public static void main(String[] args) { |
aoqi@0 | 43 | new WsCompileExample().do_main(args); |
aoqi@0 | 44 | } |
aoqi@0 | 45 | |
aoqi@0 | 46 | public void do_main(String[] args) { |
aoqi@0 | 47 | if(!args[0].equals("-s")) { |
aoqi@0 | 48 | throw new RuntimeException("specify -s for src"); |
aoqi@0 | 49 | } |
aoqi@0 | 50 | |
aoqi@0 | 51 | //run it once |
aoqi@0 | 52 | srcDir = new File(args[1]); |
aoqi@0 | 53 | if(!args[2].equals("-d")) { |
aoqi@0 | 54 | throw new RuntimeException("specify -d for dest"); |
aoqi@0 | 55 | } |
aoqi@0 | 56 | destDir = new File(args[3]); |
aoqi@0 | 57 | if(!destDir.exists()) |
aoqi@0 | 58 | destDir.mkdirs(); |
aoqi@0 | 59 | System.out.println("----test compile 1-----"); |
aoqi@0 | 60 | compileGeneratedClasses(); |
aoqi@0 | 61 | |
aoqi@0 | 62 | //run it twice |
aoqi@0 | 63 | srcDir = new File(args[1]+"1"); |
aoqi@0 | 64 | destDir = new File(args[3]+"1"); |
aoqi@0 | 65 | if(!destDir.exists()) |
aoqi@0 | 66 | destDir.mkdirs(); |
aoqi@0 | 67 | System.out.println("----test compile 2-----"); |
aoqi@0 | 68 | compileGeneratedClasses(); |
aoqi@0 | 69 | |
aoqi@0 | 70 | } |
aoqi@0 | 71 | protected void compileGeneratedClasses() { |
aoqi@0 | 72 | List sourceFiles = new ArrayList(); |
aoqi@0 | 73 | |
aoqi@0 | 74 | for (File f: srcDir.listFiles()) { |
aoqi@0 | 75 | if (f.getName().endsWith(".java")) { |
aoqi@0 | 76 | sourceFiles.add(f.getAbsolutePath()); |
aoqi@0 | 77 | } |
aoqi@0 | 78 | } |
aoqi@0 | 79 | |
aoqi@0 | 80 | if (sourceFiles.size() > 0) { |
aoqi@0 | 81 | String classDir = destDir.getAbsolutePath(); |
aoqi@0 | 82 | String classpathString = createClasspathString(); |
aoqi@0 | 83 | System.out.println("classpathString: " + classpathString); |
aoqi@0 | 84 | |
aoqi@0 | 85 | String[] args = new String[4 + (compilerDebug == true ? 1 : 0) + |
aoqi@0 | 86 | (compilerOptimize == true ? 1 : 0) + |
aoqi@0 | 87 | sourceFiles.size()]; |
aoqi@0 | 88 | args[0] = "-d"; |
aoqi@0 | 89 | args[1] = classDir; |
aoqi@0 | 90 | args[2] = "-classpath"; |
aoqi@0 | 91 | args[3] = classpathString; |
aoqi@0 | 92 | // args[4]="-DnonBatchMode"; |
aoqi@0 | 93 | int baseIndex = 4; |
aoqi@0 | 94 | if (compilerDebug) { |
aoqi@0 | 95 | args[baseIndex++] = "-g"; |
aoqi@0 | 96 | } |
aoqi@0 | 97 | if (compilerOptimize) { |
aoqi@0 | 98 | args[baseIndex++] = "-O"; |
aoqi@0 | 99 | } |
aoqi@0 | 100 | for (int i = 0; i < sourceFiles.size(); ++i) { |
aoqi@0 | 101 | args[baseIndex + i] = (String)sourceFiles.get(i); |
aoqi@0 | 102 | } |
aoqi@0 | 103 | |
aoqi@0 | 104 | // ByteArrayOutputStream javacOutput = new ByteArrayOutputStream(); |
aoqi@0 | 105 | JavaCompilerHelper compilerHelper = new JavaCompilerHelper(System.out); |
aoqi@0 | 106 | boolean result = compilerHelper.compile(args); |
aoqi@0 | 107 | if (!result) { |
aoqi@0 | 108 | System.out.println("wscompile.compilation Failed"); |
aoqi@0 | 109 | } |
aoqi@0 | 110 | } |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | protected String createClasspathString() { |
aoqi@0 | 114 | if (userClasspath == null) { |
aoqi@0 | 115 | userClasspath = ""; |
aoqi@0 | 116 | } |
aoqi@0 | 117 | String jcp = userClasspath + File.pathSeparator + System.getProperty("java.class.path"); |
aoqi@0 | 118 | return jcp; |
aoqi@0 | 119 | } |
aoqi@0 | 120 | } |
aoqi@0 | 121 | /////////////////////////////////////////////////////////////////// |
aoqi@0 | 122 | class JavaCompilerHelper { |
aoqi@0 | 123 | public JavaCompilerHelper(OutputStream out) { |
aoqi@0 | 124 | this.out = out; |
aoqi@0 | 125 | } |
aoqi@0 | 126 | |
aoqi@0 | 127 | public boolean compile(String[] args) { |
aoqi@0 | 128 | return internalCompile(args); |
aoqi@0 | 129 | } |
aoqi@0 | 130 | |
aoqi@0 | 131 | protected boolean internalCompile(String[] args) { |
aoqi@0 | 132 | |
aoqi@0 | 133 | System.out.println("Args: "); |
aoqi@0 | 134 | for(String arg : args){ |
aoqi@0 | 135 | System.out.print(arg+" "); |
aoqi@0 | 136 | } |
aoqi@0 | 137 | System.out.println(); |
aoqi@0 | 138 | ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
aoqi@0 | 139 | Class comSunToolsJavacMainClass = null; |
aoqi@0 | 140 | try { |
aoqi@0 | 141 | /* try to use the new compiler */ |
aoqi@0 | 142 | comSunToolsJavacMainClass = |
aoqi@0 | 143 | cl.loadClass("com.sun.tools.javac.Main"); |
aoqi@0 | 144 | try { |
aoqi@0 | 145 | Method compileMethod = |
aoqi@0 | 146 | comSunToolsJavacMainClass.getMethod( |
aoqi@0 | 147 | "compile", |
aoqi@0 | 148 | compile141MethodSignature); |
aoqi@0 | 149 | try { |
aoqi@0 | 150 | Object result = |
aoqi@0 | 151 | compileMethod.invoke( |
aoqi@0 | 152 | null, |
aoqi@0 | 153 | new Object[] { args, new PrintWriter(out)}); |
aoqi@0 | 154 | if (!(result instanceof Integer)) { |
aoqi@0 | 155 | return false; |
aoqi@0 | 156 | } |
aoqi@0 | 157 | return ((Integer) result).intValue() == 0; |
aoqi@0 | 158 | } catch (IllegalAccessException e3) { |
aoqi@0 | 159 | return false; |
aoqi@0 | 160 | } catch (IllegalArgumentException e3) { |
aoqi@0 | 161 | return false; |
aoqi@0 | 162 | } catch (InvocationTargetException e3) { |
aoqi@0 | 163 | return false; |
aoqi@0 | 164 | } |
aoqi@0 | 165 | } catch (NoSuchMethodException e2) { |
aoqi@0 | 166 | System.out.println("ERROR: Compile failed with error:" + e2.toString() ); |
aoqi@0 | 167 | } |
aoqi@0 | 168 | } catch (ClassNotFoundException e) { |
aoqi@0 | 169 | e.printStackTrace(); |
aoqi@0 | 170 | return false; |
aoqi@0 | 171 | } catch (SecurityException e) { |
aoqi@0 | 172 | return false; |
aoqi@0 | 173 | } |
aoqi@0 | 174 | return true; |
aoqi@0 | 175 | } |
aoqi@0 | 176 | |
aoqi@0 | 177 | protected String getGenericErrorMessage() {return "javacompiler.error"; } |
aoqi@0 | 178 | protected void run() { } |
aoqi@0 | 179 | protected boolean parseArguments(String[] args) {return false;} |
aoqi@0 | 180 | protected OutputStream out; |
aoqi@0 | 181 | |
aoqi@0 | 182 | protected static final Class[] compile141MethodSignature; |
aoqi@0 | 183 | static |
aoqi@0 | 184 | { |
aoqi@0 | 185 | compile141MethodSignature = new Class[2]; |
aoqi@0 | 186 | compile141MethodSignature[0] = (new String[0]).getClass(); |
aoqi@0 | 187 | compile141MethodSignature[1] = PrintWriter.class; |
aoqi@0 | 188 | } |
aoqi@0 | 189 | } |