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