/** * Returns the class loader for loading AST transformations. * @return - the transform class loader */ public GroovyClassLoader getTransformLoader() { return astTransformationsContext.getTransformLoader() == null ? getClassLoader() : astTransformationsContext.getTransformLoader(); }
public Class resolveJvmClass(String name) { try { return unit.getClassLoader().loadClass(name, false, true); } catch (ClassNotFoundException e) { throw new GroovyBugError("JVM class can't be loaded for " + name, e); } }
return cu.getClassLoader().getParent().loadClass(main); } catch (ClassNotFoundException e3) {} GroovyClassLoader loader = cu.getClassLoader(); loader.addClasspath(toolsJar.getAbsolutePath()); return loader.loadClass(main);
/** * This method is used to realize the lookup of a class using the compilation * unit class loader. Should no class be found we fall back to a script lookup. * If a class is found we check if there is also a script and maybe use that * one in case it is newer.<p/> * * Two class search strategies are possible: by ASM decompilation or by usual Java classloading. * The latter is slower but is unavoidable for scripts executed in dynamic environments where * the referenced classes might only be available in the classloader, not on disk. */ private LookupResult tryAsLoaderClassOrScript(String name, CompilationUnit compilationUnit) { GroovyClassLoader loader = compilationUnit.getClassLoader(); Map<String, Boolean> options = compilationUnit.configuration.getOptimizationOptions(); boolean useAsm = !Boolean.FALSE.equals(options.get("asmResolving")); boolean useClassLoader = !Boolean.FALSE.equals(options.get("classLoaderResolving")); LookupResult result = useAsm ? findDecompiled(name, compilationUnit, loader) : null; if (result != null) { return result; } if (!useClassLoader) { return tryAsScript(name, compilationUnit, null); } return findByClassLoading(name, compilationUnit, loader); }
if (file.isFile()) { URL url = file.getAbsoluteFile().getParentFile().toURI().toURL(); compiler.unit.getClassLoader().addURL(url); compiler.unit.getClassLoader().setResourceLoader(new GroovyResourceLoader() { public URL loadGroovySource(String filename) throws MalformedURLException { return null;
/** * Compiles a string of code. */ public void compile( String name, String code ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( new SourceUnit(name, code, configuration, unit.getClassLoader(), unit.getErrorCollector()) ); unit.compile(); }
/** * try to find a script using the compilation unit class loader. */ private static LookupResult tryAsScript(String name, CompilationUnit compilationUnit, ClassNode oldClass) { LookupResult lr = null; if (oldClass!=null) { lr = new LookupResult(null, oldClass); } if (name.startsWith("java.")) return lr; //TODO: don't ignore inner static classes completely if (name.indexOf('$') != -1) return lr; // try to find a script from classpath*/ GroovyClassLoader gcl = compilationUnit.getClassLoader(); URL url = null; try { url = gcl.getResourceLoader().loadGroovySource(name); } catch (MalformedURLException e) { // fall through and let the URL be null } if (url != null && ( oldClass==null || isSourceNewer(url, oldClass))) { SourceUnit su = compilationUnit.addSource(url); return new LookupResult(su,null); } return lr; }
/** * A helper method that can be used to evaluate expressions as found in annotation * parameters. For example, it will evaluate a constant, be it referenced directly as * an integer or as a reference to a field. * <p> * If this method throws an exception, then the expression cannot be evaluated on its own. * * @param expr the expression to be evaluated * @param config the compiler configuration * @return the result of the expression */ public static Object evaluateExpression(Expression expr, CompilerConfiguration config) { String className = "Expression$" + UUID.randomUUID().toString().replace('-', '$'); ClassNode node = new ClassNode(className, Opcodes.ACC_PUBLIC, OBJECT_TYPE); ReturnStatement code = new ReturnStatement(expr); addGeneratedMethod(node, "eval", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code); CompilerConfiguration copyConf = new CompilerConfiguration(config); CompilationUnit cu = new CompilationUnit(copyConf); cu.addClassNode(node); cu.compile(Phases.CLASS_GENERATION); @SuppressWarnings("unchecked") List<GroovyClass> classes = (List<GroovyClass>) cu.getClasses(); Class aClass = cu.getClassLoader().defineClass(className, classes.get(0).getBytes()); try { return aClass.getMethod("eval").invoke(null); } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new GroovyBugError(e); } }
public void compile(List<String> files, CompilationUnit cu) { String[] javacParameters = makeParameters(files, cu.getClassLoader()); StringBuilderWriter javacOutput = null; int javacReturnValue = 0;
/** * Returns the class loader for loading AST transformations. * @return - the transform class loader */ public GroovyClassLoader getTransformLoader() { return transformLoader == null ? getClassLoader() : transformLoader; }
/** * Returns the class loader for loading AST transformations. * @return - the transform class loader */ public GroovyClassLoader getTransformLoader() { return transformLoader == null ? getClassLoader() : transformLoader; }
/** * Returns the class loader for loading AST transformations. * @return - the transform class loader */ public GroovyClassLoader getTransformLoader() { return transformLoader == null ? getClassLoader() : transformLoader; }
return cu.getClassLoader().getParent().loadClass(main); } catch (ClassNotFoundException e3) {} GroovyClassLoader loader = cu.getClassLoader(); loader.addClasspath(toolsJar.getAbsolutePath()); return loader.loadClass(main);
return cu.getClassLoader().getParent().loadClass(main); } catch (ClassNotFoundException e3) {} GroovyClassLoader loader = cu.getClassLoader(); loader.addClasspath(toolsJar.getAbsolutePath()); return loader.loadClass(main);
return cu.getClassLoader().getParent().loadClass(main); } catch (ClassNotFoundException e3) {} GroovyClassLoader loader = cu.getClassLoader(); loader.addClasspath(toolsJar.getAbsolutePath()); return loader.loadClass(main);
/** * Compiles a string of code. */ public void compile( String name, String code ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( new SourceUnit(name, code, configuration, unit.getClassLoader(), unit.getErrorCollector()) ); unit.compile(); }
/** * Compiles a string of code. */ public void compile( String name, String code ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( new SourceUnit(name, code, configuration, unit.getClassLoader(), unit.getErrorCollector()) ); unit.compile(); }
/** * Compiles a string of code. */ public void compile( String name, String code ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( new SourceUnit(name, code, configuration, unit.getClassLoader(), unit.getErrorCollector()) ); unit.compile(); }
/** * Compiles a string of code. */ public void compile( String name, String code ) throws CompilationFailedException { CompilationUnit unit = new CompilationUnit( configuration ); unit.addSource( new SourceUnit(name, code, configuration, unit.getClassLoader(), unit.getErrorCollector()) ); unit.compile(); }
/** * try to find a script using the compilation unit class loader. */ private LookupResult tryAsScript(String name, CompilationUnit compilationUnit, Class oldClass) { LookupResult lr = null; if (oldClass!=null) { ClassNode cn = ClassHelper.make(oldClass); lr = new LookupResult(null,cn); } if (name.startsWith("java.")) return lr; //TODO: don't ignore inner static classes completely if (name.indexOf('$') != -1) return lr; // try to find a script from classpath*/ GroovyClassLoader gcl = compilationUnit.getClassLoader(); URL url = null; try { url = gcl.getResourceLoader().loadGroovySource(name); } catch (MalformedURLException e) { // fall through and let the URL be null } if (url != null && ( oldClass==null || isSourceNewer(url, oldClass))) { SourceUnit su = compilationUnit.addSource(url); return new LookupResult(su,null); } return lr; }