test/tools/javac/Paths/6638501/WsCompileExample.java

Fri, 14 Mar 2008 16:09:30 -0700

author
jjg
date
Fri, 14 Mar 2008 16:09:30 -0700
changeset 14
58039502942e
child 554
9d9f26857129
permissions
-rw-r--r--

6638501: Regression with Javac in JDK6 U4 b03?
Summary: replace some String paths with File paths in Paths.java
Reviewed-by: ksrini

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

mercurial