/** * Get the first public static method of the given class. * * @param className the class name * @return the method name */ public Method getMethod(String className) throws ClassNotFoundException { Class<?> clazz = getClass(className); Method[] methods = clazz.getDeclaredMethods(); for (Method m : methods) { int modifiers = m.getModifiers(); if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) { String name = m.getName(); if (!name.startsWith("_") && !m.getName().equals("main")) { return m; } } } return null; }
private static int exec(String... args) { ByteArrayOutputStream buff = new ByteArrayOutputStream(); try { ProcessBuilder builder = new ProcessBuilder(); // The javac executable allows some of it's flags // to be smuggled in via environment variables. // But if it sees those flags, it will write out a message // to stderr, which messes up our parsing of the output. builder.environment().remove("JAVA_TOOL_OPTIONS"); builder.command(args); Process p = builder.start(); copyInThread(p.getInputStream(), buff); copyInThread(p.getErrorStream(), buff); p.waitFor(); String err = new String(buff.toByteArray(), Constants.UTF8); throwSyntaxError(err); return p.exitValue(); } catch (Exception e) { throw DbException.convert(e); } }
private static void javacProcess(File javaFile) { exec("javac", "-sourcepath", COMPILE_DIR, "-d", COMPILE_DIR, "-encoding", "UTF-8", javaFile.getAbsolutePath()); }
/** * Generate a proxy class. The returned class extends the given class. * * @param c the class to extend * @return the proxy class */ public static Class<?> getClassProxy(Class<?> c) throws ClassNotFoundException { Class<?> p = proxyMap.get(c); if (p != null) { return p; } // TODO how to extend a class with private constructor // TODO call right constructor // TODO use the right package ProxyCodeGenerator cg = new ProxyCodeGenerator(); cg.setPackageName("bytecode"); cg.generateClassProxy(c); StringWriter sw = new StringWriter(); cg.write(new PrintWriter(sw)); String code = sw.toString(); String proxy = "bytecode." + c.getSimpleName() + "Proxy"; compiler.setJavaSystemCompiler(false); compiler.setSource(proxy, code); // System.out.println(code); Class<?> px = compiler.getClass(proxy); proxyMap.put(c, px); return px; }
@Override public Class<?> findClass(String name) throws ClassNotFoundException { Class<?> classInstance = compiled.get(name); if (classInstance == null) { String source = sources.get(name); String packageName = null; int idx = name.lastIndexOf('.'); String className; if (idx >= 0) { packageName = name.substring(0, idx); className = name.substring(idx + 1); } else { className = name; } String s = getCompleteSourceCode(packageName, className, source); if (JAVA_COMPILER != null && useJavaSystemCompiler) { classInstance = javaxToolsJavac(packageName, className, s); } else { byte[] data = javacCompile(packageName, className, s); if (data == null) { classInstance = findSystemClass(name); } else { classInstance = defineClass(name, data, 0, data.length); } } compiled.put(name, classInstance); } return classInstance; } };
out.close(); if (JAVAC_SUN != null) { javacSun(javaFile); } else { javacProcess(javaFile);
if (isGroovySource(source)) { Class<?> clazz = GroovyCompiler.parseClass(source, packageAndClassName); compiled.put(packageAndClassName, clazz);