< prev index next >

src/com/sun/javatest/lib/JavaCompileCommand.java

Print this page
rev 145 : 7902237: Fixing raw use of parameterized class
Reviewed-by: jjg


 165                         return Status.error("No path specified after -cp or -classpath option");
 166                     classpath = options[++i];
 167                 }
 168                 else if (options[i].equals("-verbose"))
 169                     verbose = true;
 170                 else
 171                     return Status.error("Unrecognized option: " + options[i]);
 172             }
 173         }
 174 
 175         this.log = log;
 176 
 177         try {
 178 
 179             ClassLoader loader;
 180             if (classpath == null)
 181                 loader = null;
 182             else
 183                 loader = new PathClassLoader(classpath);
 184 
 185             Class compilerClass;
 186             if (compilerClassName != null) {
 187                 compilerClass = getClass(loader, compilerClassName);
 188                 if (compilerClass == null)
 189                     return Status.error("Cannot find compiler: " + compilerClassName);
 190             }
 191             else {
 192                 compilerName = "javac";
 193                 compilerClass = getClass(loader, "com.sun.tools.javac.Main");  // JDK1.3+
 194                 if (compilerClass == null)
 195                     compilerClass = getClass(loader, "sun.tools.javac.Main");  // JDK1.1-2
 196                 if (compilerClass == null)
 197                     return Status.error("Cannot find compiler");
 198             }
 199 
 200             loader = null;
 201 
 202             Object[] compileMethodArgs;
 203             Method compileMethod = getMethod(compilerClass, "compile", // JDK1.4+
 204                                              new Class[] { String[].class, PrintWriter.class });
 205             if (compileMethod != null)
 206                 compileMethodArgs = new Object[] { args, ref };
 207             else {
 208                 compileMethod = getMethod(compilerClass, "compile",   // JDK1.1-3
 209                                           new Class[] { String[].class });
 210                 if (compileMethod != null)
 211                     compileMethodArgs = new Object[] { args };
 212                 else
 213                     return Status.error("Cannot find compile method for " + compilerClass.getName());
 214             }
 215 
 216             Object compiler;
 217             if (Modifier.isStatic(compileMethod.getModifiers()))
 218                 compiler =  null;
 219             else {
 220                 Object[] constrArgs;
 221                 Constructor constr = getConstructor(compilerClass, // JDK1.1-2
 222                                                     new Class[] { OutputStream.class, String.class });
 223                 if (constr != null)
 224                     constrArgs = new Object[] { new WriterStream(ref), compilerName };
 225                 else {
 226                     constr = getConstructor(compilerClass, new Class[0]); // JDK1.3
 227                     if (constr != null)
 228                         constrArgs = new Object[0];
 229                     else
 230                         return Status.error("Cannot find suitable constructor for " + compilerClass.getName());
 231                 }
 232                 try {
 233                     compiler = constr.newInstance(constrArgs);
 234                 }
 235                 catch (Throwable t) {
 236                     t.printStackTrace(log);
 237                     return Status.error("Cannot instantiate compiler");
 238                 }
 239             }
 240 
 241             Object result;
 242             try {
 243                 result = compileMethod.invoke(compiler, compileMethodArgs);
 244             }
 245             catch (Throwable t) {
 246                 t.printStackTrace(log);


 248             }
 249 
 250             // result might be a boolean (old javac) or an int (new javac)
 251             if (result instanceof Boolean) {
 252                 boolean ok = ((Boolean)result).booleanValue();
 253                 return (ok ? passed : failed);
 254             }
 255             else if (result instanceof Integer) {
 256                 int rc = ((Integer)result).intValue();
 257                 return (rc == 0 ? passed : failed);
 258             }
 259             else
 260                 return Status.error("Unexpected return value from compiler: " + result);
 261         }
 262         finally {
 263             log.flush();
 264             ref.flush();
 265         }
 266     }
 267 
 268     private Class getClass(ClassLoader loader, String name) {
 269         try {
 270             return (loader == null ? Class.forName(name) : loader.loadClass(name));
 271         }
 272         catch (ClassNotFoundException e) {
 273             return null;
 274         }
 275     }
 276 
 277     private Constructor getConstructor(Class<?> c, Class[] argTypes) {
 278         try {
 279             return c.getConstructor(argTypes);
 280         }
 281         catch (NoSuchMethodException e) {
 282             return null;
 283         }
 284         catch (Throwable t) {
 285             if (verbose)
 286                 t.printStackTrace(log);
 287             return null;
 288         }
 289     }
 290 
 291     private Method getMethod(Class<?> c, String name, Class[] argTypes) {
 292         try {
 293             return c.getMethod(name, argTypes);
 294         }
 295         catch (NoSuchMethodException e) {
 296             return null;
 297         }
 298         catch (Throwable t) {
 299             if (verbose)
 300                 t.printStackTrace(log);
 301             return null;
 302         }
 303     }
 304 
 305     private static String[] shift(String[] args, int n) {
 306         String[] newArgs = new String[args.length - n];
 307         System.arraycopy(args, n, newArgs, 0, newArgs.length);
 308         return newArgs;
 309     }
 310 
 311     public static boolean defaultVerbose = Boolean.getBoolean("javatest.JavaCompileCommand.verbose");


 165                         return Status.error("No path specified after -cp or -classpath option");
 166                     classpath = options[++i];
 167                 }
 168                 else if (options[i].equals("-verbose"))
 169                     verbose = true;
 170                 else
 171                     return Status.error("Unrecognized option: " + options[i]);
 172             }
 173         }
 174 
 175         this.log = log;
 176 
 177         try {
 178 
 179             ClassLoader loader;
 180             if (classpath == null)
 181                 loader = null;
 182             else
 183                 loader = new PathClassLoader(classpath);
 184 
 185             Class<?> compilerClass;
 186             if (compilerClassName != null) {
 187                 compilerClass = getClass(loader, compilerClassName);
 188                 if (compilerClass == null)
 189                     return Status.error("Cannot find compiler: " + compilerClassName);
 190             }
 191             else {
 192                 compilerName = "javac";
 193                 compilerClass = getClass(loader, "com.sun.tools.javac.Main");  // JDK1.3+
 194                 if (compilerClass == null)
 195                     compilerClass = getClass(loader, "sun.tools.javac.Main");  // JDK1.1-2
 196                 if (compilerClass == null)
 197                     return Status.error("Cannot find compiler");
 198             }
 199 
 200             loader = null;
 201 
 202             Object[] compileMethodArgs;
 203             Method compileMethod = getMethod(compilerClass, "compile", // JDK1.4+
 204                                              new Class<?>[] { String[].class, PrintWriter.class });
 205             if (compileMethod != null)
 206                 compileMethodArgs = new Object[] { args, ref };
 207             else {
 208                 compileMethod = getMethod(compilerClass, "compile",   // JDK1.1-3
 209                                           new Class<?>[] { String[].class });
 210                 if (compileMethod != null)
 211                     compileMethodArgs = new Object[] { args };
 212                 else
 213                     return Status.error("Cannot find compile method for " + compilerClass.getName());
 214             }
 215 
 216             Object compiler;
 217             if (Modifier.isStatic(compileMethod.getModifiers()))
 218                 compiler =  null;
 219             else {
 220                 Object[] constrArgs;
 221                 Constructor<?> constr = getConstructor(compilerClass, // JDK1.1-2
 222                                                     new Class<?>[] { OutputStream.class, String.class });
 223                 if (constr != null)
 224                     constrArgs = new Object[] { new WriterStream(ref), compilerName };
 225                 else {
 226                     constr = getConstructor(compilerClass, new Class<?>[0]); // JDK1.3
 227                     if (constr != null)
 228                         constrArgs = new Object[0];
 229                     else
 230                         return Status.error("Cannot find suitable constructor for " + compilerClass.getName());
 231                 }
 232                 try {
 233                     compiler = constr.newInstance(constrArgs);
 234                 }
 235                 catch (Throwable t) {
 236                     t.printStackTrace(log);
 237                     return Status.error("Cannot instantiate compiler");
 238                 }
 239             }
 240 
 241             Object result;
 242             try {
 243                 result = compileMethod.invoke(compiler, compileMethodArgs);
 244             }
 245             catch (Throwable t) {
 246                 t.printStackTrace(log);


 248             }
 249 
 250             // result might be a boolean (old javac) or an int (new javac)
 251             if (result instanceof Boolean) {
 252                 boolean ok = ((Boolean)result).booleanValue();
 253                 return (ok ? passed : failed);
 254             }
 255             else if (result instanceof Integer) {
 256                 int rc = ((Integer)result).intValue();
 257                 return (rc == 0 ? passed : failed);
 258             }
 259             else
 260                 return Status.error("Unexpected return value from compiler: " + result);
 261         }
 262         finally {
 263             log.flush();
 264             ref.flush();
 265         }
 266     }
 267 
 268     private Class<?> getClass(ClassLoader loader, String name) {
 269         try {
 270             return (loader == null ? Class.forName(name) : loader.loadClass(name));
 271         }
 272         catch (ClassNotFoundException e) {
 273             return null;
 274         }
 275     }
 276 
 277     private Constructor<?> getConstructor(Class<?> c, Class<?>[] argTypes) {
 278         try {
 279             return c.getConstructor(argTypes);
 280         }
 281         catch (NoSuchMethodException e) {
 282             return null;
 283         }
 284         catch (Throwable t) {
 285             if (verbose)
 286                 t.printStackTrace(log);
 287             return null;
 288         }
 289     }
 290 
 291     private Method getMethod(Class<?> c, String name, Class<?>[] argTypes) {
 292         try {
 293             return c.getMethod(name, argTypes);
 294         }
 295         catch (NoSuchMethodException e) {
 296             return null;
 297         }
 298         catch (Throwable t) {
 299             if (verbose)
 300                 t.printStackTrace(log);
 301             return null;
 302         }
 303     }
 304 
 305     private static String[] shift(String[] args, int n) {
 306         String[] newArgs = new String[args.length - n];
 307         System.arraycopy(args, n, newArgs, 0, newArgs.length);
 308         return newArgs;
 309     }
 310 
 311     public static boolean defaultVerbose = Boolean.getBoolean("javatest.JavaCompileCommand.verbose");
< prev index next >