|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| JavacCompiler.java | 17.3% | 25% | 65% | 26.9% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.model.compiler; | |
| 38 | ||
| 39 | import java.util.List; | |
| 40 | import java.util.Arrays; | |
| 41 | import java.util.Set; | |
| 42 | import java.util.HashSet; | |
| 43 | import java.util.Collections; | |
| 44 | import java.io.File; | |
| 45 | import edu.rice.cs.drjava.DrJava; | |
| 46 | import edu.rice.cs.drjava.config.OptionConstants; | |
| 47 | import edu.rice.cs.drjava.model.DJError; | |
| 48 | import edu.rice.cs.drjava.model.DrJavaFileUtils; | |
| 49 | import edu.rice.cs.util.ArgumentTokenizer; | |
| 50 | import edu.rice.cs.plt.reflect.JavaVersion; | |
| 51 | ||
| 52 | import javax.swing.filechooser.FileFilter; | |
| 53 | import edu.rice.cs.drjava.ui.SmartSourceFilter; | |
| 54 | ||
| 55 | import java.lang.reflect.Constructor; | |
| 56 | ||
| 57 | /** An abstract parent for all javac-based compiler interfaces. Manages the auxiliary naming methods. | |
| 58 | * To support loading via reflection, all subclasses are assumed to have a public constructor with | |
| 59 | * a matching signature. | |
| 60 | * @version $Id: JavacCompiler.java 5436 2011-08-02 06:58:19Z mgricken $ | |
| 61 | */ | |
| 62 | public abstract class JavacCompiler implements CompilerInterface { | |
| 63 | ||
| 64 | protected final JavaVersion.FullVersion _version; | |
| 65 | protected final String _location; | |
| 66 | protected List<? extends File> _defaultBootClassPath; | |
| 67 | ||
| 68 | /** The set of class names that are run as ACM Java Task Force library programs. */ | |
| 69 | protected static final Set<String> ACM_PROGRAM_CLASSES = new HashSet<String>(); | |
| 70 | static { | |
| 71 | 55 | Collections.addAll(ACM_PROGRAM_CLASSES, new String[] { |
| 72 | "acm.program.Program", | |
| 73 | "acm.graphics.GTurtle" | |
| 74 | }); | |
| 75 | } | |
| 76 | ||
| 77 | 2198 | protected JavacCompiler(JavaVersion.FullVersion version, String location, List<? extends File> defaultBootClassPath) { |
| 78 | 2198 | _version = version; |
| 79 | 2198 | _location = location; |
| 80 | 2198 | _defaultBootClassPath = defaultBootClassPath; |
| 81 | } | |
| 82 | ||
| 83 | public abstract boolean isAvailable(); | |
| 84 | ||
| 85 | public abstract List<? extends DJError> compile(List<? extends File> files, List<? extends File> classPath, | |
| 86 | List<? extends File> sourcePath, File destination, | |
| 87 | List<? extends File> bootClassPath, String sourceVersion, | |
| 88 | boolean showWarnings); | |
| 89 | ||
| 90 | 0 | public JavaVersion version() { return _version.majorVersion(); } |
| 91 | ||
| 92 | 709 | public String getName() { return "JDK " + _version.versionString(); } |
| 93 | ||
| 94 | 42 | public String getDescription() { return getName() + " from " + _location; } |
| 95 | ||
| 96 | 160 | public String toString() { return getName(); } |
| 97 | ||
| 98 | /** A compiler can instruct DrJava to include additional elements for the boot | |
| 99 | * class path of the Interactions JVM. This isn't necessary for the Java compilers, though. */ | |
| 100 | 177 | public List<File> additionalBootClassPathForInteractions() { return Arrays.<File>asList(); } |
| 101 | ||
| 102 | /** Transform the command line to be interpreted into something the Interactions JVM can use. | |
| 103 | * This replaces "java MyClass a b c" with Java code to call MyClass.main(new String[]{"a","b","c"}). | |
| 104 | * "import MyClass" is not handled here. | |
| 105 | * transformCommands should support at least "run", "java" and "applet". | |
| 106 | * @param interactionsString unprocessed command line | |
| 107 | * @return command line with commands transformed */ | |
| 108 | 35 | public String transformCommands(String interactionsString) { |
| 109 | 35 | if (interactionsString.startsWith("java ")) { |
| 110 | 1 | interactionsString = transformJavaCommand(interactionsString); |
| 111 | } | |
| 112 | 34 | else if (interactionsString.startsWith("applet ")) { |
| 113 | 0 | interactionsString = transformAppletCommand(interactionsString); |
| 114 | } | |
| 115 | 34 | else if (interactionsString.startsWith("run ")) { |
| 116 | 0 | interactionsString = transformRunCommand(interactionsString); |
| 117 | } | |
| 118 | 35 | return interactionsString; |
| 119 | } | |
| 120 | ||
| 121 | 17 | public static String transformJavaCommand(String s) { |
| 122 | // check the return type and public access before executing, per bug #1585210 | |
| 123 | 17 | String command = |
| 124 | "try '{'\n" + | |
| 125 | " java.lang.reflect.Method m = {0}.class.getMethod(\"main\", java.lang.String[].class);\n" + | |
| 126 | " if (!m.getReturnType().equals(void.class)) throw new java.lang.NoSuchMethodException();\n" + | |
| 127 | "'}'\n" + | |
| 128 | "catch (java.lang.NoSuchMethodException e) '{'\n" + | |
| 129 | " throw new java.lang.NoSuchMethodError(\"main\");\n" + | |
| 130 | "'}'\n" + | |
| 131 | "{0}.main(new String[]'{'{1}'}');"; | |
| 132 | 17 | return _transformCommand(s, command); |
| 133 | } | |
| 134 | ||
| 135 | 16 | public static String transformAppletCommand(String s) { |
| 136 | 16 | return _transformCommand(s,"edu.rice.cs.plt.swing.SwingUtil.showApplet(new {0}({1}), 400, 300);"); |
| 137 | } | |
| 138 | ||
| 139 | /** This method performs the "smart run". Unfortunately, we don't get the right static error messages. | |
| 140 | * @param s full command line, i.e. "run MyClass 1 2 3" | |
| 141 | * @param c class to be run, i.e. MyClass.class | |
| 142 | */ | |
| 143 | 0 | @SuppressWarnings("unchecked") |
| 144 | public static void runCommand(String s, Class<?> c) throws Throwable { | |
| 145 | 0 | if (s.endsWith(";")) s = _deleteSemiColon(s); |
| 146 | 0 | List<String> tokens = ArgumentTokenizer.tokenize(s, true); |
| 147 | 0 | final String classNameWithQuotes = tokens.get(1); // this is "MyClass" |
| 148 | 0 | final String className = |
| 149 | classNameWithQuotes.substring(1, classNameWithQuotes.length() - 1); // removes quotes, becomes MyClass | |
| 150 | 0 | String[] args = new String[tokens.size() - 2]; |
| 151 | 0 | for (int i = 2; i < tokens.size(); i++) { |
| 152 | 0 | String t = tokens.get(i); |
| 153 | 0 | args[i - 2] = t.substring(1, t.length() - 1); |
| 154 | } | |
| 155 | ||
| 156 | 0 | boolean isProgram = false; |
| 157 | 0 | boolean isApplet = false; |
| 158 | 0 | Class<?> oldC = c; |
| 159 | 0 | while(c != null) { |
| 160 | 0 | if (ACM_PROGRAM_CLASSES.contains(c.getName())) { isProgram = true; break; } |
| 161 | 0 | c = c.getSuperclass(); |
| 162 | } | |
| 163 | 0 | c = oldC; |
| 164 | 0 | if (!isProgram) { |
| 165 | 0 | try { |
| 166 | // if this doesn't throw, c is a subclass of Applet | |
| 167 | 0 | c.asSubclass(java.applet.Applet.class); |
| 168 | 0 | isApplet = true; |
| 169 | } catch(ClassCastException cce) { } | |
| 170 | } | |
| 171 | ||
| 172 | 0 | java.lang.reflect.Method m = null; |
| 173 | 0 | if (isApplet) { |
| 174 | 0 | try { |
| 175 | 0 | m = c.getMethod("main", java.lang.String[].class); |
| 176 | 0 | if (!m.getReturnType().equals(void.class)) { m = null; } |
| 177 | } | |
| 178 | 0 | catch (java.lang.NoSuchMethodException e) { m = null; } |
| 179 | 0 | if (m==null) { |
| 180 | 0 | java.applet.Applet instance = null; |
| 181 | 0 | if (args.length==0) { |
| 182 | 0 | try { |
| 183 | // try default (nullary) constructor first | |
| 184 | 0 | Constructor<?> ctor = c.getConstructor(); |
| 185 | 0 | instance = java.applet.Applet.class.cast(ctor.newInstance()); |
| 186 | } | |
| 187 | 0 | catch(NoSuchMethodException nsme) { instance = null; } |
| 188 | 0 | catch(InstantiationException ie) { instance = null; } |
| 189 | 0 | catch(IllegalAccessException iae) { instance = null; } |
| 190 | catch(java.lang.reflect.InvocationTargetException ite) { | |
| 191 | 0 | if (ite.getCause()!=null) { |
| 192 | 0 | throw ite.getCause(); |
| 193 | } | |
| 194 | else { | |
| 195 | 0 | System.err.println("Error: Please turn off 'Smart Run' or use 'java' command instead of 'run'."); |
| 196 | } | |
| 197 | } | |
| 198 | 0 | if (instance==null) { |
| 199 | 0 | try { |
| 200 | // try String[] constructor next | |
| 201 | 0 | Constructor<?> ctor = c.getConstructor(String[].class); |
| 202 | 0 | instance = java.applet.Applet.class.cast(ctor.newInstance(new Object[] { new String[0] })); |
| 203 | } | |
| 204 | 0 | catch(NoSuchMethodException nsme) { instance = null; } |
| 205 | 0 | catch(InstantiationException ie) { instance = null; } |
| 206 | 0 | catch(IllegalAccessException iae) { instance = null; } |
| 207 | catch(java.lang.reflect.InvocationTargetException ite) { | |
| 208 | 0 | if (ite.getCause()!=null) { |
| 209 | 0 | throw ite.getCause(); |
| 210 | } | |
| 211 | else { | |
| 212 | 0 | System.err.println("Error: Please turn off 'Smart Run' or use 'java' command instead of 'run'."); |
| 213 | 0 | return; |
| 214 | } | |
| 215 | } | |
| 216 | } | |
| 217 | 0 | if (instance==null) { |
| 218 | 0 | System.err.println("Static Error: This applet does not have a default constructor or a constructor "+ |
| 219 | "accepting String[]."); | |
| 220 | 0 | return; |
| 221 | } | |
| 222 | } | |
| 223 | else { | |
| 224 | 0 | try { |
| 225 | // try String[] constructor | |
| 226 | 0 | Constructor<?> ctor = c.getConstructor(String[].class); |
| 227 | 0 | instance = java.applet.Applet.class.cast(ctor.newInstance(new Object[] { args })); |
| 228 | } | |
| 229 | 0 | catch(NoSuchMethodException nsme) { instance = null; } |
| 230 | 0 | catch(InstantiationException ie) { instance = null; } |
| 231 | 0 | catch(IllegalAccessException iae) { instance = null; } |
| 232 | catch(java.lang.reflect.InvocationTargetException ite) { | |
| 233 | 0 | if (ite.getCause()!=null) { |
| 234 | 0 | throw ite.getCause(); |
| 235 | } | |
| 236 | else { | |
| 237 | 0 | System.err.println("Error: Please turn off 'Smart Run' or use 'java' command instead of 'run'."); |
| 238 | 0 | return; |
| 239 | } | |
| 240 | } | |
| 241 | 0 | if (instance==null) { |
| 242 | 0 | System.err.println("Static Error: This applet does not have a constructor accepting String[]."); |
| 243 | 0 | return; |
| 244 | } | |
| 245 | } | |
| 246 | 0 | edu.rice.cs.plt.swing.SwingUtil.showApplet(instance, 400, 300); |
| 247 | } | |
| 248 | } | |
| 249 | else { | |
| 250 | 0 | try { |
| 251 | 0 | m = c.getMethod("main", java.lang.String[].class); |
| 252 | 0 | if (!m.getReturnType().equals(void.class)) { |
| 253 | 0 | System.err.println("Static Error: This class does not have a static void main method accepting String[]."); |
| 254 | 0 | m = null; |
| 255 | } | |
| 256 | } | |
| 257 | catch (java.lang.NoSuchMethodException e) { | |
| 258 | 0 | System.err.println("Static Error: This class does not have a static void main method accepting String[]."); |
| 259 | 0 | m = null; |
| 260 | } | |
| 261 | } | |
| 262 | 0 | if (m != null) { |
| 263 | 0 | if (isProgram) { |
| 264 | 0 | String[] newArgs = new String[args.length+1]; |
| 265 | 0 | newArgs[0] = "code="+c.getName(); |
| 266 | 0 | System.arraycopy(args, 0, newArgs, 1, args.length); |
| 267 | 0 | args = newArgs; |
| 268 | } | |
| 269 | 0 | try { |
| 270 | 0 | m.setAccessible(true); |
| 271 | 0 | m.invoke(null, new Object[] { args }); |
| 272 | } | |
| 273 | catch(SecurityException se) { | |
| 274 | 0 | System.err.println("Error: Please turn off 'Smart Run' or use 'java' command instead of 'run'."); |
| 275 | } | |
| 276 | catch(IllegalAccessException iae) { | |
| 277 | 0 | System.err.println("Error: Please turn off 'Smart Run' or use 'java' command instead of 'run'."); |
| 278 | } | |
| 279 | catch(java.lang.reflect.InvocationTargetException ite) { | |
| 280 | 0 | if (ite.getCause()!=null) { |
| 281 | 0 | throw ite.getCause(); |
| 282 | } | |
| 283 | else { | |
| 284 | 0 | System.err.println("Error: Please turn off 'Smart Run' or use 'java' command instead of 'run'."); |
| 285 | } | |
| 286 | } | |
| 287 | } | |
| 288 | } | |
| 289 | ||
| 290 | // This is a command that automatically detects if | |
| 291 | // a) the class is an ACM Java Task Force program (subclass of acm.program.Program) | |
| 292 | // b) an applet | |
| 293 | // c) a class with a static main method | |
| 294 | // | |
| 295 | // If a), then DrJava inserts "code=MyClass" as argument 0. | |
| 296 | // If b), then DrJava performs the same as "applet MyClass" (see above). | |
| 297 | // If c), then DrJava executes MyClass.main (traditional java behavior). | |
| 298 | 0 | public static String transformRunCommand(String s) { |
| 299 | 0 | if (s.endsWith(";")) s = _deleteSemiColon(s); |
| 300 | 0 | List<String> args = ArgumentTokenizer.tokenize(s, true); |
| 301 | 0 | final String classNameWithQuotes = args.get(1); // this is "MyClass" |
| 302 | 0 | final String className = |
| 303 | classNameWithQuotes.substring(1, classNameWithQuotes.length() - 1); // removes quotes, becomes MyClass | |
| 304 | ||
| 305 | // we pass MyClass.class just to get a "Static Error: Undefined class 'MyClass'" | |
| 306 | 0 | String ret = JavacCompiler.class.getName()+".runCommand(\""+s.toString()+"\", "+className+".class)"; |
| 307 | // System.out.println(ret); | |
| 308 | 0 | return ret; |
| 309 | } | |
| 310 | ||
| 311 | /** Assumes a trimmed String. Returns a string of the call that the interpreter can use. | |
| 312 | * The arguments get formatted as comma-separated list of strings enclosed in quotes. | |
| 313 | * Example: _transformCommand("java MyClass arg1 arg2 arg3", "{0}.main(new String[]'{'{1}'}');") | |
| 314 | * returns "MyClass.main(new String[]{\"arg1\",\"arg2\",\"arg3\"});" | |
| 315 | * NOTE: the command to run is constructed using {@link java.text.MessageFormat}. That means that certain characters, | |
| 316 | * single quotes and curly braces, for example, are special. To write single quotes, you need to double them. | |
| 317 | * To write curly braces, you need to enclose them in single quotes. Example: | |
| 318 | * MessageFormat.format("Abc {0} ''foo'' '{'something'}'", "def") returns "Abc def 'foo' {something}". | |
| 319 | * @param s the command line, either "java MyApp arg1 arg2 arg3" or "applet MyApplet arg1 arg2 arg3" | |
| 320 | * @param command the command to execute, with {0} marking the place for the class name and {1} the place for the arguments | |
| 321 | */ | |
| 322 | 33 | protected static String _transformCommand(String s, String command) { |
| 323 | 0 | if (s.endsWith(";")) s = _deleteSemiColon(s); |
| 324 | 33 | List<String> args = ArgumentTokenizer.tokenize(s, true); |
| 325 | 33 | final String classNameWithQuotes = args.get(1); // this is "MyClass" |
| 326 | 33 | final String className = classNameWithQuotes.substring(1, classNameWithQuotes.length() - 1); // removes quotes, becomes MyClass |
| 327 | 33 | final StringBuilder argsString = new StringBuilder(); |
| 328 | 33 | boolean seenArg = false; |
| 329 | 33 | for (int i = 2; i < args.size(); i++) { |
| 330 | 6 | if (seenArg) argsString.append(","); |
| 331 | 32 | else seenArg = true; |
| 332 | 38 | argsString.append(args.get(i)); |
| 333 | } | |
| 334 | 33 | return java.text.MessageFormat.format(command, className, argsString.toString()); |
| 335 | } | |
| 336 | ||
| 337 | /** Deletes the last character of a string. Assumes semicolon at the end, but does not check. Helper | |
| 338 | * for _transformCommand(String,String). | |
| 339 | * @param s the String containing the semicolon | |
| 340 | * @return a substring of s with one less character | |
| 341 | */ | |
| 342 | 0 | protected static String _deleteSemiColon(String s) { return s.substring(0, s.length() - 1); } |
| 343 | ||
| 344 | /** .java --> true | |
| 345 | * .dj --> true | |
| 346 | * .dj0 --> true | |
| 347 | * .dj1 --> true | |
| 348 | * .dj2 --> true | |
| 349 | * otherwise false | |
| 350 | * @return true if the specified file is a source file for this compiler. */ | |
| 351 | 103 | public boolean isSourceFileForThisCompiler(File f) { |
| 352 | // by default, use DrJavaFileUtils.isSourceFile | |
| 353 | 103 | return DrJavaFileUtils.isSourceFile(f); |
| 354 | } | |
| 355 | ||
| 356 | /** Return the set of source file extensions that this compiler supports. | |
| 357 | * @return the set of source file extensions that this compiler supports. */ | |
| 358 | 0 | public Set<String> getSourceFileExtensions() { return DrJavaFileUtils.getSourceFileExtensions(); } |
| 359 | ||
| 360 | /** Return the suggested file extension that will be appended to a file without extension. | |
| 361 | * @return the suggested file extension */ | |
| 362 | 0 | public String getSuggestedFileExtension() { |
| 363 | 0 | return DrJavaFileUtils.getSuggestedFileExtension(); |
| 364 | } | |
| 365 | ||
| 366 | /** Return a file filter that can be used to open files this compiler supports. | |
| 367 | * @return file filter for appropriate source files for this compiler */ | |
| 368 | 76 | public FileFilter getFileFilter() { return new SmartSourceFilter(); } |
| 369 | ||
| 370 | /** Return the extension of the files that should be opened with the "Open Folder..." command. | |
| 371 | * @return file extension for the "Open Folder..." command for this compiler. */ | |
| 372 | 0 | public String getOpenAllFilesInFolderExtension() { |
| 373 | 0 | return OptionConstants.LANGUAGE_LEVEL_EXTENSIONS[DrJava.getConfig().getSetting(OptionConstants.LANGUAGE_LEVEL)]; |
| 374 | } | |
| 375 | ||
| 376 | /** Return true if this compiler can be used in conjunction with the language level facility. | |
| 377 | * @return true if language levels can be used. */ | |
| 378 | 38 | public boolean supportsLanguageLevels() { return true; } |
| 379 | ||
| 380 | /** Return the set of keywords that should be highlighted in the specified file. | |
| 381 | * @param f file for which to return the keywords | |
| 382 | * @return the set of keywords that should be highlighted in the specified file. */ | |
| 383 | 320 | public Set<String> getKeywordsForFile(File f) { return new HashSet<String>(JAVA_KEYWORDS); } |
| 384 | ||
| 385 | /** Set of Java/GJ keywords for special coloring. */ | |
| 386 | public static final HashSet<String> JAVA_KEYWORDS = new HashSet<String>(); | |
| 387 | static { | |
| 388 | 55 | final String[] words = { |
| 389 | "import", "native", "package", "goto", "const", "if", "else", "switch", "while", "for", "do", "true", "false", | |
| 390 | "null", "this", "super", "new", "instanceof", "return", "static", "synchronized", "transient", "volatile", | |
| 391 | "final", "strictfp", "throw", "try", "catch", "finally", "throws", "extends", "implements", "interface", "class", | |
| 392 | "break", "continue", "public", "protected", "private", "abstract", "case", "default", "assert", "enum" | |
| 393 | }; | |
| 394 | 2420 | for(String s: words) { JAVA_KEYWORDS.add(s); } |
| 395 | } | |
| 396 | ||
| 397 | // // This is a command that automatically detects if | |
| 398 | // // a) the class is an ACM Java Task Force program (subclass of acm.program.Program or acm.graphics.GTurtle) | |
| 399 | // // b) an applet | |
| 400 | // // c) a class with a static main method | |
| 401 | // // | |
| 402 | // // If a), then DrJava inserts "code=MyClass" as argument 0. | |
| 403 | // // If b), then DrJava performs the same as "applet MyClass" (see above). | |
| 404 | // // If c), then DrJava executes MyClass.main (traditional java behavior). | |
| 405 | // public static String transformRunCommand(String s) { | |
| 406 | // if (s.endsWith(";")) s = _deleteSemiColon(s); | |
| 407 | // List<String> tokens = ArgumentTokenizer.tokenize(s, true); | |
| 408 | // final String classNameWithQuotes = tokens.get(1); // this is "MyClass" | |
| 409 | // final String className = | |
| 410 | // classNameWithQuotes.substring(1, classNameWithQuotes.length() - 1); // removes quotes, becomes MyClass | |
| 411 | // String[] args = new String[tokens.size() - 2]; | |
| 412 | // final StringBuilder argsString = new StringBuilder(); | |
| 413 | // boolean seenArg = false; | |
| 414 | // for (int i = 2; i < tokens.size(); i++) { | |
| 415 | // String t = tokens.get(i); // with quotes | |
| 416 | // args[i - 2] = t.substring(1, t.length() - 1); | |
| 417 | // | |
| 418 | // if (seenArg) argsString.append(","); | |
| 419 | // else seenArg = true; | |
| 420 | // argsString.append(t); | |
| 421 | // } | |
| 422 | // | |
| 423 | // StringBuilder command = new StringBuilder(); | |
| 424 | // | |
| 425 | // command.append("{\n"+ | |
| 426 | // "boolean isProgram = false;\n" + | |
| 427 | // "boolean isApplet = false;\n" + | |
| 428 | // "Class c = ").append(className).append(".class;\n" + | |
| 429 | // "while(c != null) {\n" + | |
| 430 | // " if (\"acm.program.Program\".equals(c.getName()) ||\n" + | |
| 431 | // " \"acm.graphics.GTurtle\".equals(c.getName())) { isProgram = true; break; }\n" + | |
| 432 | // " c = c.getSuperclass();\n" + | |
| 433 | // "}\n" + | |
| 434 | // "c = ").append(className).append(".class;\n" + | |
| 435 | // "if (!isProgram) {\n" + | |
| 436 | // " try {\n" + | |
| 437 | // " // if this doesn't throw, c is a subclass of Applet\n" + | |
| 438 | // " c.asSubclass(java.applet.Applet.class);\n" + | |
| 439 | // " isApplet = true;\n" + | |
| 440 | // " } catch(ClassCastException cce) { }\n" + | |
| 441 | // "}\n" + | |
| 442 | // "java.lang.reflect.Method m = null;\n" + | |
| 443 | // "String[] args = new String[] {").append(argsString.toString()).append("};\n" + | |
| 444 | // "if (isApplet) {\n" + | |
| 445 | // " try {\n" + | |
| 446 | // " m = c.getMethod(\"main\", java.lang.String[].class);\n" + | |
| 447 | // " if (!m.getReturnType().equals(void.class)) { m = null; }\n" + | |
| 448 | // " }\n" + | |
| 449 | // " catch (java.lang.NoSuchMethodException e) { m = null; }\n" + | |
| 450 | // " if (m==null) {\n" + | |
| 451 | // " java.applet.Applet instance = null;\n" + | |
| 452 | // " boolean fail = false;\n"); | |
| 453 | // if (args.length==0) { | |
| 454 | // command.append( | |
| 455 | // " try {\n" + | |
| 456 | // " // try default (nullary) constructor first\n" + | |
| 457 | // " java.lang.reflect.Constructor ctor = c.getConstructor();\n" + | |
| 458 | // " instance = java.applet.Applet.class.cast(ctor.newInstance());\n" + | |
| 459 | // " }\n" + | |
| 460 | // " catch(NoSuchMethodException nsme) { instance = null; }\n" + | |
| 461 | // " catch(InstantiationException ie) { instance = null; }\n" + | |
| 462 | // " catch(IllegalAccessException iae) { instance = null; }\n" + | |
| 463 | // " catch(java.lang.reflect.InvocationTargetException ite) {\n" + | |
| 464 | // " if (ite.getCause()!=null) {\n" + | |
| 465 | // " throw ite.getCause();\n" + | |
| 466 | // " }\n" + | |
| 467 | // " else {\n" + | |
| 468 | // " System.err.println(\"Error: Please turn off 'Smart Run' or use 'java' command instead of 'run'.\");\n" + | |
| 469 | // " }\n" + | |
| 470 | // " }\n" + | |
| 471 | // " if (instance==null) {\n" + | |
| 472 | // " try {\n" + | |
| 473 | // " // try String[] constructor next\n" + | |
| 474 | // " java.lang.reflect.Constructor ctor = c.getConstructor(String[].class);\n" + | |
| 475 | // " instance = java.applet.Applet.class.cast(ctor.newInstance(new Object[] { new String[0] }));\n" + | |
| 476 | // " }\n" + | |
| 477 | // " catch(NoSuchMethodException nsme) { instance = null; }\n" + | |
| 478 | // " catch(InstantiationException ie) { instance = null; }\n" + | |
| 479 | // " catch(IllegalAccessException iae) { instance = null; }\n" + | |
| 480 | // " catch(java.lang.reflect.InvocationTargetException ite) {\n" + | |
| 481 | // " if (ite.getCause()!=null) {\n" + | |
| 482 | // " throw ite.getCause();\n" + | |
| 483 | // " }\n" + | |
| 484 | // " else {\n" + | |
| 485 | // " System.err.println(\"Error: Please turn off 'Smart Run' or use 'java' command instead of 'run'.\");\n" + | |
| 486 | // " fail = true;\n" + | |
| 487 | // " }\n" + | |
| 488 | // " }\n" + | |
| 489 | // " }\n" + | |
| 490 | // " if (!fail && (instance==null)) {\n" + | |
| 491 | // " System.err.println(\"Error: This applet does not have a default constructor or a constructor \"+\n" + | |
| 492 | // " \"accepting String[].\");\n" + | |
| 493 | // " fail = true;\n" + | |
| 494 | // " }\n"); | |
| 495 | // } | |
| 496 | // else { | |
| 497 | // command.append( | |
| 498 | // " try {\n" + | |
| 499 | // " // try String[] constructor\n" + | |
| 500 | // " java.lang.reflect.Constructor ctor = c.getConstructor(String[].class);\n" + | |
| 501 | // " instance = java.applet.Applet.class.cast(ctor.newInstance(new Object[] { args }));\n" + | |
| 502 | // " }\n" + | |
| 503 | // " catch(NoSuchMethodException nsme) { instance = null; }\n" + | |
| 504 | // " catch(InstantiationException ie) { instance = null; }\n" + | |
| 505 | // " catch(IllegalAccessException iae) { instance = null; }\n" + | |
| 506 | // " catch(java.lang.reflect.InvocationTargetException ite) {\n" + | |
| 507 | // " if (ite.getCause()!=null) {\n" + | |
| 508 | // " throw ite.getCause();\n" + | |
| 509 | // " }\n" + | |
| 510 | // " else {\n" + | |
| 511 | // " System.err.println(\"Error: Please turn off 'Smart Run' or use 'java' command instead of 'run'.\");\n" + | |
| 512 | // " fail = true;\n" + | |
| 513 | // " }\n" + | |
| 514 | // " }\n" + | |
| 515 | // " if (!fail && (instance==null)) {\n" + | |
| 516 | // " System.err.println(\"Error: This applet does not have a constructor accepting String[].\");\n" + | |
| 517 | // " fail = true;\n" + | |
| 518 | // " }\n"); | |
| 519 | // } | |
| 520 | // command.append( | |
| 521 | // " if (!fail) { edu.rice.cs.plt.swing.SwingUtil.showApplet(instance, 400, 300); }\n" + | |
| 522 | // " } // if (m==null)\n" + | |
| 523 | // "} // if (isApplet)\n" + | |
| 524 | // "else {\n" + | |
| 525 | // " try {\n" + | |
| 526 | // " m = c.getMethod(\"main\", java.lang.String[].class);\n" + | |
| 527 | // " if (!m.getReturnType().equals(void.class)) {\n" + | |
| 528 | // " System.err.println(\"Error: This class does not have a static void main method accepting String[].\");\n" + | |
| 529 | // " m = null;\n" + | |
| 530 | // " }\n" + | |
| 531 | // " }\n" + | |
| 532 | // " catch (java.lang.NoSuchMethodException e) {\n" + | |
| 533 | // " System.err.println(\"Error: This class does not have a static void main method accepting String[].\");\n" + | |
| 534 | // " m = null;\n" + | |
| 535 | // " }\n" + | |
| 536 | // "} // else\n" + | |
| 537 | // "if (m != null) {\n" + | |
| 538 | // " if (isProgram) {\n" + | |
| 539 | // " String[] newArgs = new String[args.length+1];\n" + | |
| 540 | // " newArgs[0] = \"code=\"+c.getName();\n" + | |
| 541 | // " System.arraycopy(args, 0, newArgs, 1, args.length);\n" + | |
| 542 | // " args = newArgs;\n" + | |
| 543 | // " }\n" + | |
| 544 | // " try {\n" + | |
| 545 | // " m.setAccessible(true);\n" + | |
| 546 | // " m.invoke(null, new Object[] { args });\n" + | |
| 547 | // " }\n" + | |
| 548 | // " catch(SecurityException se) {\n" + | |
| 549 | // " System.err.println(\"Error: Please turn off 'Smart Run' or use 'java' command instead of 'run'.\");\n" + | |
| 550 | // " }\n" + | |
| 551 | // " catch(IllegalAccessException iae) {\n" + | |
| 552 | // " System.err.println(\"Error: Please turn off 'Smart Run' or use 'java' command instead of 'run'.\");\n" + | |
| 553 | // " }\n" + | |
| 554 | // " catch(java.lang.reflect.InvocationTargetException ite) {\n" + | |
| 555 | // " if (ite.getCause()!=null) {\n" + | |
| 556 | // " throw ite.getCause();\n" + | |
| 557 | // " }\n" + | |
| 558 | // " else {\n" + | |
| 559 | // " System.err.println(\"Error: Please turn off 'Smart Run' or use 'java' command instead of 'run'.\");\n" + | |
| 560 | // " }\n" + | |
| 561 | // " }\n" + | |
| 562 | // "}\n" + | |
| 563 | // "}"); | |
| 564 | // | |
| 565 | // // System.out.println(command); | |
| 566 | // | |
| 567 | // return command.toString(); | |
| 568 | // } | |
| 569 | } |
|
||||||||||