| 1 | use v5; | ||
| 2 | |||
| 3 | package Perlito5::Java::JavaCompiler; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | sub emit_java_imports { | ||
| 7 | return <<'EOT' | ||
| 8 | import java.io.ByteArrayOutputStream; | ||
| 9 | import java.io.FileNotFoundException; | ||
| 10 | import java.io.IOException; | ||
| 11 | import java.io.OutputStream; | ||
| 12 | import java.lang.reflect.Constructor; | ||
| 13 | import java.lang.reflect.Method; | ||
| 14 | import java.net.*; | ||
| 15 | import java.util.ArrayList; | ||
| 16 | import java.util.HashMap; | ||
| 17 | import java.util.Map; | ||
| 18 | import javax.tools.FileObject; | ||
| 19 | import javax.tools.ForwardingJavaFileManager; | ||
| 20 | import javax.tools.JavaCompiler; | ||
| 21 | import javax.tools.JavaFileManager; | ||
| 22 | import javax.tools.JavaFileObject; | ||
| 23 | import javax.tools.SimpleJavaFileObject; | ||
| 24 | import javax.tools.ToolProvider; | ||
| 25 | import java.util.List; | ||
| 26 | import java.util.ArrayList; | ||
| 27 | import java.util.Arrays; | ||
| 28 | import org.perlito.Perlito5.*; | ||
| 29 | |||
| 30 | EOT | ||
| 31 | } | ||
| 32 | |||
| 33 | sub emit_java { | ||
| 34 | return <<'EOT' | ||
| 35 | |||
| 36 | /****************************************************************************/ | ||
| 37 | // Credits for the PlJavaCompiler idea: | ||
| 38 | // | ||
| 39 | // http://udn.yyuap.com/doc/jdk6-api-zh/javax/tools/JavaCompiler.html | ||
| 40 | // * idea to reuse the same file manager to allow caching of jar files | ||
| 41 | // https://github.com/turpid-monkey/InMemoryJavaCompiler | ||
| 42 | // https://github.com/trung/InMemoryJavaCompiler | ||
| 43 | // * provided a working example | ||
| 44 | // * Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt | ||
| 45 | // http://stackoverflow.com/questions/1563909/how-to-set-classpath-when-i-use-javax-tools-javacompiler-compile-the-source | ||
| 46 | // * set classpath | ||
| 47 | /****************************************************************************/ | ||
| 48 | |||
| 49 | class PlJavaCompiler { | ||
| 50 | private PlJavaCompiler() {} // defined so class can't be instantiated. | ||
| 51 | |||
| 52 | static ArrayList<SourceCode> compilationUnits; | ||
| 53 | static ExtendedStandardJavaFileManager fileManager; | ||
| 54 | static public DynamicClassLoader classLoader; | ||
| 55 | static JavaCompiler javac; | ||
| 56 | static Boolean initDone; | ||
| 57 | static List<String> optionList; | ||
| 58 | |||
| 59 | public static void init() throws Exception | ||
| 60 | { | ||
| 61 | // System.out.println("initializing Perlito5.LibPerl"); | ||
| 62 | // try { | ||
| 63 | // LibPerl.main( new String[]{} ); | ||
| 64 | // } | ||
| 65 | // catch(Exception e) { | ||
| 66 | // System.out.println("Errors in main()"); | ||
| 67 | // } | ||
| 68 | |||
| 69 | javac = ToolProvider.getSystemJavaCompiler(); | ||
| 70 | classLoader = new DynamicClassLoader( new PlArray().getClass().getClassLoader() ); | ||
| 71 | // classLoader = new DynamicClassLoader(ClassLoader.getSystemClassLoader()); | ||
| 72 | compilationUnits = new ArrayList<SourceCode>(); | ||
| 73 | |||
| 74 | optionList = new ArrayList<String>(); | ||
| 75 | // set compiler's classpath to be same as the runtime's | ||
| 76 | StringBuilder cp = new StringBuilder(); | ||
| 77 | int cpCount = 0; | ||
| 78 | try { | ||
| 79 | for (URL url : ((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs()) { | ||
| 80 | // System.out.println("url: " + url.getFile()); | ||
| 81 | if (cpCount++ != 0) { | ||
| 82 | cp.append(":"); | ||
| 83 | } | ||
| 84 | cp.append(url.getFile()); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | catch (Exception e) { | ||
| 88 | // java.base/jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to | ||
| 89 | // java.base/java.net.URLClassLoader | ||
| 90 | } | ||
| 91 | String systemCp = System.getProperty("java.class.path"); | ||
| 92 | if (! systemCp.equals("")) { | ||
| 93 | if (cpCount++ != 0) { | ||
| 94 | cp.append(":"); | ||
| 95 | } | ||
| 96 | cp.append(systemCp); | ||
| 97 | } | ||
| 98 | optionList.addAll(Arrays.asList("-classpath", cp.toString())); | ||
| 99 | optionList.addAll(Arrays.asList("-source", "7")); | ||
| 100 | // optionList.addAll(Arrays.asList("-Xlint:deprecation")); | ||
| 101 | } | ||
| 102 | |||
| 103 | public static PlObject eval_java_string(PlArray List__) | ||
| 104 | { | ||
| 105 | |||
| 106 | String className = List__.shift().toString(); | ||
| 107 | String source = List__.shift().toString(); | ||
| 108 | String constants = List__.shift().toString(); | ||
| 109 | |||
| 110 | if (source.equals("")) { | ||
| 111 | return PlCx.UNDEF; | ||
| 112 | } | ||
| 113 | |||
| 114 | try { | ||
| 115 | if (initDone == null) { | ||
| 116 | PlJavaCompiler.init(); | ||
| 117 | // System.out.println("eval_string: init"); | ||
| 118 | initDone = true; | ||
| 119 | } | ||
| 120 | |||
| 121 | // TODO - test local(); initialize local() stack if needed | ||
| 122 | |||
| 123 | StringBuilder source5 = new StringBuilder(); | ||
| 124 | source5.append(constants); | ||
| 125 | source5.append(" @SuppressWarnings(\"unchecked\")"); | ||
| 126 | source5.append(" public static PlObject runEval(int want, PlArray List__) {\n"); | ||
| 127 | source5.append(" int return_context = want;\n"); | ||
| 128 | source5.append(" try {\n"); | ||
| 129 | source5.append(" " + source + "\n"); | ||
| 130 | source5.append(" }\n"); | ||
| 131 | source5.append(" catch(PlReturnException e) {\n"); | ||
| 132 | source5.append(" return e.ret;\n"); | ||
| 133 | source5.append(" }\n"); | ||
| 134 | source5.append(" catch(PlNextException e) {\n"); | ||
| 135 | source5.append(" throw(e);\n"); | ||
| 136 | source5.append(" }\n"); | ||
| 137 | source5.append(" catch(PlLastException e) {\n"); | ||
| 138 | source5.append(" throw(e);\n"); | ||
| 139 | source5.append(" }\n"); | ||
| 140 | source5.append(" catch(PlRedoException e) {\n"); | ||
| 141 | source5.append(" throw(e);\n"); | ||
| 142 | source5.append(" }\n"); | ||
| 143 | source5.append(" catch(java.lang.NullPointerException e) {\n"); | ||
| 144 | source5.append(" e.printStackTrace();\n"); | ||
| 145 | source5.append(" String message = \"null pointer: java.lang.NullPointerException\";\n"); | ||
| 146 | source5.append(" PlV.Scalar_EVAL_ERROR.set(new PlString(\"\" + message));\n"); | ||
| 147 | source5.append(" return PerlOp.context(want);\n"); | ||
| 148 | source5.append(" }\n"); | ||
| 149 | source5.append(" catch(Exception e) {\n"); | ||
| 150 | |||
| 151 | if ( PlV.sget("Perlito5::Java::DEBUG").get().to_boolean() ) { | ||
| 152 | source5.append(" e.printStackTrace();\n"); | ||
| 153 | } | ||
| 154 | |||
| 155 | source5.append(" String message = e.getMessage();\n"); | ||
| 156 | source5.append(" PlV.Scalar_EVAL_ERROR.set(new PlString(\"\" + message));\n"); | ||
| 157 | source5.append(" return PerlOp.context(want);\n"); | ||
| 158 | source5.append(" }\n"); | ||
| 159 | source5.append(" }\n"); | ||
| 160 | source5.append("}\n"); | ||
| 161 | String cls5 = source5.toString(); | ||
| 162 | |||
| 163 | if ( PlV.sget("Perlito5::Java::DEBUG").get().to_boolean() ) { | ||
| 164 | System.out.println("\neval_ast:\n" + cls5 + "\n"); | ||
| 165 | } | ||
| 166 | |||
| 167 | // TODO - retrieve errors in Java->bytecode | ||
| 168 | Class<?> class5 = compileClassInMemory( | ||
| 169 | className, | ||
| 170 | cls5 | ||
| 171 | ); | ||
| 172 | Method method5 = class5.getMethod("runEval", new Class[]{int.class, PlArray.class}); | ||
| 173 | PlObject out = (org.perlito.Perlito5.PlObject)method5.invoke(null, PlCx.VOID, List__); | ||
| 174 | // System.out.println("eval_string result: " + out.toString()); | ||
| 175 | return out; | ||
| 176 | } | ||
| 177 | catch(PlReturnException e) { | ||
| 178 | return e.ret; | ||
| 179 | } | ||
| 180 | catch(PlNextException e) { | ||
| 181 | throw(e); | ||
| 182 | } | ||
| 183 | catch(PlLastException e) { | ||
| 184 | throw(e); | ||
| 185 | } | ||
| 186 | catch(PlRedoException e) { | ||
| 187 | throw(e); | ||
| 188 | } | ||
| 189 | catch(Exception e) { | ||
| 190 | // e.printStackTrace(); | ||
| 191 | String message = e.getMessage(); | ||
| 192 | // System.out.println("Exception in eval_string: " + message); | ||
| 193 | PlV.Scalar_EVAL_ERROR.set(new PlString("" + message)); | ||
| 194 | } | ||
| 195 | return PlCx.UNDEF; | ||
| 196 | } | ||
| 197 | |||
| 198 | public static PlObject eval_perl_string( | ||
| 199 | String source, | ||
| 200 | String namespace, | ||
| 201 | String wantarray, | ||
| 202 | PlInt scalar_hints, // $^H | ||
| 203 | PlHashRef hash_hints, // \%^H | ||
| 204 | PlObject scope, // "my" declarations | ||
| 205 | String[] scalar_name, // new String[]{"x_100"}; capture name | ||
| 206 | PlLvalue[] scalar_val, // new PlLvalue[]{x_100}; capture value | ||
| 207 | String[] array_name, // new String[]{"xx_101"}; | ||
| 208 | PlArray[] array_val, // new PlArray[]{xx_101}; | ||
| 209 | String[] hash_name, // new String[]{}; | ||
| 210 | PlHash[] hash_val, // new PlHash[]{} | ||
| 211 | int want, | ||
| 212 | PlArray List__ | ||
| 213 | ) | ||
| 214 | { | ||
| 215 | // System.out.println("eval_string: enter"); | ||
| 216 | // (new Throwable()).printStackTrace(); | ||
| 217 | |||
| 218 | String className; | ||
| 219 | String outJava; | ||
| 220 | String constants; | ||
| 221 | PlObject tmp_scalar_hints = PlV.sget("main::" + (char)8).get(); // save $^H | ||
| 222 | PlHash tmp_hash_hints = new PlHash(PlV.hash_get("main::" + (char)8)); // save %^H | ||
| 223 | try { | ||
| 224 | |||
| 225 | PlV.sset("main::" + (char)8, scalar_hints); // $^H | ||
| 226 | PlV.hash_set("main::" + (char)8, hash_hints.hash_deref_strict()); // %^H | ||
| 227 | // Perlito5::Java::JavaCompiler::perl5_to_java($source, $namespace, $want, $scope_java) | ||
| 228 | PlObject code[] = org.perlito.Perlito5.LibPerl.apply( | ||
| 229 | "Perlito5::Java::Runtime::perl5_to_java", | ||
| 230 | new PlString(source), | ||
| 231 | new PlString(namespace), | ||
| 232 | new PlString(wantarray), | ||
| 233 | scope | ||
| 234 | ); | ||
| 235 | className = code[0].toString(); | ||
| 236 | outJava = code[1].toString(); | ||
| 237 | constants = code[2].toString(); | ||
| 238 | // System.out.println("eval_string: from Perlito5::Java::JavaCompiler::perl5_to_java \n[[[ " + outJava + " ]]"); | ||
| 239 | // System.out.println("eval_string: constants \n[[[ " + constants + " ]]"); | ||
| 240 | } | ||
| 241 | catch(Exception e) { | ||
| 242 | // e.printStackTrace(); | ||
| 243 | String message = e.getMessage(); | ||
| 244 | // System.out.println("Exception in eval_string: " + message); | ||
| 245 | PlV.Scalar_EVAL_ERROR.set(new PlString("" + message)); | ||
| 246 | PlV.sset("main::" + (char)8, tmp_scalar_hints); // restore $^H | ||
| 247 | PlV.hash_set("main::" + (char)8, tmp_hash_hints); // restore %^H | ||
| 248 | return PlCx.UNDEF; | ||
| 249 | } | ||
| 250 | PlV.sset("main::" + (char)8, tmp_scalar_hints); // restore $^H | ||
| 251 | PlV.hash_set("main::" + (char)8, tmp_hash_hints); // restore %^H | ||
| 252 | |||
| 253 | // return eval_java_string(outJava.toString()); | ||
| 254 | |||
| 255 | if (source.equals("")) { | ||
| 256 | PlV.Scalar_EVAL_ERROR.set(PlCx.EMPTY); | ||
| 257 | return PlCx.UNDEF; | ||
| 258 | } | ||
| 259 | |||
| 260 | try { | ||
| 261 | if (initDone == null) { | ||
| 262 | PlJavaCompiler.init(); | ||
| 263 | // System.out.println("eval_string: init"); | ||
| 264 | initDone = true; | ||
| 265 | } | ||
| 266 | |||
| 267 | // TODO - test local(); initialize local() stack if needed | ||
| 268 | |||
| 269 | StringBuilder source5 = new StringBuilder(); | ||
| 270 | source5.append(constants); | ||
| 271 | source5.append(" @SuppressWarnings(\"unchecked\")"); | ||
| 272 | source5.append(" public static PlObject runEval(int want, Object scalar_val, Object array_val, Object hash_val, PlArray List__) {\n"); | ||
| 273 | source5.append(" int return_context = want;\n"); | ||
| 274 | for (int i = 0; i < scalar_name.length; i++) { | ||
| 275 | source5.append(" PlLvalue " + scalar_name[i] + " = ((PlLvalue[])(scalar_val))[" + i + "];\n"); | ||
| 276 | } | ||
| 277 | for (int i = 0; i < array_name.length; i++) { | ||
| 278 | source5.append(" PlArray " + array_name[i] + " = ((PlArray[])(array_val))[" + i + "];\n"); | ||
| 279 | } | ||
| 280 | for (int i = 0; i < hash_name.length; i++) { | ||
| 281 | source5.append(" PlHash " + hash_name[i] + " = ((PlHash[])(hash_val))[" + i + "];\n"); | ||
| 282 | } | ||
| 283 | source5.append(" try {\n"); | ||
| 284 | source5.append(" PlObject ret = " + outJava + ";\n"); | ||
| 285 | source5.append(" PlV.Scalar_EVAL_ERROR.set(PlCx.EMPTY);\n"); | ||
| 286 | source5.append(" return ret;\n"); | ||
| 287 | source5.append(" }\n"); | ||
| 288 | source5.append(" catch(PlReturnException e) {\n"); | ||
| 289 | source5.append(" PlV.Scalar_EVAL_ERROR.set(PlCx.EMPTY);\n"); | ||
| 290 | source5.append(" return e.ret;\n"); | ||
| 291 | source5.append(" }\n"); | ||
| 292 | source5.append(" catch(PlNextException e) {\n"); | ||
| 293 | source5.append(" PlV.Scalar_EVAL_ERROR.set(PlCx.EMPTY);\n"); | ||
| 294 | source5.append(" throw(e);\n"); | ||
| 295 | source5.append(" }\n"); | ||
| 296 | source5.append(" catch(PlLastException e) {\n"); | ||
| 297 | source5.append(" PlV.Scalar_EVAL_ERROR.set(PlCx.EMPTY);\n"); | ||
| 298 | source5.append(" throw(e);\n"); | ||
| 299 | source5.append(" }\n"); | ||
| 300 | source5.append(" catch(PlRedoException e) {\n"); | ||
| 301 | source5.append(" PlV.Scalar_EVAL_ERROR.set(PlCx.EMPTY);\n"); | ||
| 302 | source5.append(" throw(e);\n"); | ||
| 303 | source5.append(" }\n"); | ||
| 304 | source5.append(" catch(java.lang.NullPointerException e) {\n"); | ||
| 305 | source5.append(" e.printStackTrace();\n"); | ||
| 306 | source5.append(" String message = \"null pointer: java.lang.NullPointerException\";\n"); | ||
| 307 | source5.append(" PlV.Scalar_EVAL_ERROR.set(new PlString(\"\" + message));\n"); | ||
| 308 | source5.append(" return PerlOp.context(want);\n"); | ||
| 309 | source5.append(" }\n"); | ||
| 310 | source5.append(" catch(Exception e) {\n"); | ||
| 311 | |||
| 312 | if ( PlV.sget("Perlito5::Java::DEBUG").get().to_boolean() ) { | ||
| 313 | source5.append(" e.printStackTrace();\n"); | ||
| 314 | } | ||
| 315 | |||
| 316 | source5.append(" String message = e.getMessage();\n"); | ||
| 317 | source5.append(" PlV.Scalar_EVAL_ERROR.set(new PlString(\"\" + message));\n"); | ||
| 318 | source5.append(" return PerlOp.context(want);\n"); | ||
| 319 | source5.append(" }\n"); | ||
| 320 | source5.append(" }\n"); | ||
| 321 | source5.append("}\n"); | ||
| 322 | String cls5 = source5.toString(); | ||
| 323 | |||
| 324 | if ( PlV.sget("Perlito5::Java::DEBUG").get().to_boolean() ) { | ||
| 325 | System.out.println("\neval_perl_string:\n" + cls5 + "\n"); | ||
| 326 | } | ||
| 327 | |||
| 328 | // TODO - retrieve errors in Java->bytecode | ||
| 329 | Class<?> class5 = compileClassInMemory( | ||
| 330 | className, | ||
| 331 | cls5 | ||
| 332 | ); | ||
| 333 | Method method5 = class5.getMethod("runEval", new Class[]{int.class, Object.class, Object.class, Object.class, PlArray.class}); | ||
| 334 | PlObject out = (org.perlito.Perlito5.PlObject)method5.invoke(null, want, scalar_val, array_val, hash_val, List__); | ||
| 335 | // System.out.println("eval_string result: " + out.toString()); | ||
| 336 | return out; | ||
| 337 | } | ||
| 338 | catch(PlReturnException e) { | ||
| 339 | return e.ret; | ||
| 340 | } | ||
| 341 | catch(PlNextException e) { | ||
| 342 | throw(e); | ||
| 343 | } | ||
| 344 | catch(PlLastException e) { | ||
| 345 | throw(e); | ||
| 346 | } | ||
| 347 | catch(PlRedoException e) { | ||
| 348 | throw(e); | ||
| 349 | } | ||
| 350 | catch(Exception e) { | ||
| 351 | // e.printStackTrace(); | ||
| 352 | String message = e.getMessage(); | ||
| 353 | // System.out.println("Exception in eval_string: " + message); | ||
| 354 | PlV.Scalar_EVAL_ERROR.set(new PlString("" + message)); | ||
| 355 | } | ||
| 356 | return PlCx.UNDEF; | ||
| 357 | } | ||
| 358 | |||
| 359 | static Class<?> compileClassInMemory(String className, String classSourceCode) throws Exception | ||
| 360 | { | ||
| 361 | SourceCode sourceCodeObj = new SourceCode(className, classSourceCode); | ||
| 362 | // System.out.println("PlJavaCompiler.compileClassInMemory: name=" + className); | ||
| 363 | classLoader.customCompiledCode.put(className, new CompiledCode(className)); | ||
| 364 | if (fileManager == null) { | ||
| 365 | // initializing the file manager | ||
| 366 | compilationUnits.add(sourceCodeObj); | ||
| 367 | fileManager = new ExtendedStandardJavaFileManager( | ||
| 368 | javac.getStandardFileManager(null, null, null), classLoader); | ||
| 369 | } | ||
| 370 | else { | ||
| 371 | // reusing the file manager; replace the source code | ||
| 372 | compilationUnits.set(0, sourceCodeObj); | ||
| 373 | } | ||
| 374 | |||
| 375 | // run the compiler | ||
| 376 | JavaCompiler.CompilationTask task = javac.getTask(null, fileManager, | ||
| 377 | null, optionList, null, compilationUnits); | ||
| 378 | boolean result = task.call(); | ||
| 379 | if (!result) | ||
| 380 | throw new RuntimeException("Unknown error during compilation."); | ||
| 381 | return classLoader.loadClass(className); | ||
| 382 | } | ||
| 383 | |||
| 384 | } | ||
| 385 | |||
| 386 | class ExtendedStandardJavaFileManager extends ForwardingJavaFileManager<JavaFileManager> { | ||
| 387 | private DynamicClassLoader cl; | ||
| 388 | |||
| 389 | protected ExtendedStandardJavaFileManager(JavaFileManager fileManager, DynamicClassLoader cl) { | ||
| 390 | super(fileManager); | ||
| 391 | this.cl = cl; | ||
| 392 | } | ||
| 393 | |||
| 394 | @Override | ||
| 395 | public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException { | ||
| 396 | // System.out.println("ExtendedStandardJavaFileManager.getJavaFileForOutput: name=" + className); | ||
| 397 | CompiledCode cc = cl.customCompiledCode.get(className); | ||
| 398 | if (cc != null) { | ||
| 399 | return cc; | ||
| 400 | } | ||
| 401 | // source file not found for this output class: this is ok, because we can have a class like 'PlEval$1' | ||
| 402 | // System.out.println("ExtendedStandardJavaFileManager.getJavaFileForOutput: create name=" + className); | ||
| 403 | try { | ||
| 404 | cc = new CompiledCode(className); | ||
| 405 | } | ||
| 406 | catch(Exception e) { | ||
| 407 | throw new FileNotFoundException("Error creating output file for class " + className ); | ||
| 408 | } | ||
| 409 | cl.customCompiledCode.put(className, cc); | ||
| 410 | return cc; | ||
| 411 | } | ||
| 412 | |||
| 413 | @Override | ||
| 414 | public ClassLoader getClassLoader(JavaFileManager.Location location) { | ||
| 415 | return cl; | ||
| 416 | } | ||
| 417 | } | ||
| 418 | |||
| 419 | class CompiledCode extends SimpleJavaFileObject { | ||
| 420 | private ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
| 421 | private String className; | ||
| 422 | |||
| 423 | public CompiledCode(String className) throws Exception { | ||
| 424 | super(new URI(className), Kind.CLASS); | ||
| 425 | this.className = className; | ||
| 426 | } | ||
| 427 | |||
| 428 | public String getClassName() { | ||
| 429 | // System.out.println("CompiledCode.getClassName: name=" + className); | ||
| 430 | return className; | ||
| 431 | } | ||
| 432 | |||
| 433 | @Override | ||
| 434 | public OutputStream openOutputStream() throws IOException { | ||
| 435 | // System.out.println("CompiledCode.openOutputStream()"); | ||
| 436 | return baos; | ||
| 437 | } | ||
| 438 | |||
| 439 | public byte[] getByteCode() { | ||
| 440 | return baos.toByteArray(); | ||
| 441 | } | ||
| 442 | } | ||
| 443 | |||
| 444 | class DynamicClassLoader extends ClassLoader { | ||
| 445 | public Map<String, CompiledCode> customCompiledCode = new HashMap<String, CompiledCode>(); | ||
| 446 | |||
| 447 | public DynamicClassLoader(ClassLoader parent) { | ||
| 448 | super(parent); | ||
| 449 | } | ||
| 450 | |||
| 451 | public void addCode(CompiledCode cc) { | ||
| 452 | // System.out.println("DynamicClassLoader.addCode: name=" + cc.getName()); | ||
| 453 | customCompiledCode.put(cc.getName(), cc); | ||
| 454 | } | ||
| 455 | |||
| 456 | @Override | ||
| 457 | protected Class<?> findClass(String name) throws ClassNotFoundException { | ||
| 458 | // System.out.println("DynamicClassLoader.findClass: name=" + name); | ||
| 459 | CompiledCode cc = customCompiledCode.get(name); | ||
| 460 | if (cc == null) { | ||
| 461 | return super.findClass(name); | ||
| 462 | } | ||
| 463 | byte[] byteCode = cc.getByteCode(); | ||
| 464 | return defineClass(name, byteCode, 0, byteCode.length); | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | class SourceCode extends SimpleJavaFileObject { | ||
| 469 | private String contents = null; | ||
| 470 | private String className; | ||
| 471 | |||
| 472 | public SourceCode(String className, String contents) throws Exception { | ||
| 473 | super(URI.create("string:///" + className.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE); | ||
| 474 | this.contents = contents; | ||
| 475 | this.className = className; | ||
| 476 | } | ||
| 477 | |||
| 478 | public String getClassName() { | ||
| 479 | return className; | ||
| 480 | } | ||
| 481 | |||
| 482 | public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { | ||
| 483 | return contents; | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | EOT | ||
| 488 | |||
| 489 | } # end of emit_java() | ||
| 490 | |||
| 491 | 1; | ||
| 492 | |||
| 493 |