Refine search
/** * compiles the Groovy source code * * @param sFilterCode * @return * @throws org.codehaus.groovy.control.CompilationFailedException * */ public Class compileGroovy(String sFilterCode) throws org.codehaus.groovy.control.CompilationFailedException { GroovyClassLoader loader = new GroovyClassLoader(); return loader.parseClass(sFilterCode); }
/** * Implemented here to check package access prior to returning an * already loaded class. * * @throws CompilationFailedException if the compilation failed * @throws ClassNotFoundException if the class was not found * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean) */ protected Class loadClass(final String name, boolean resolve) throws ClassNotFoundException { return loadClass(name, true, true, resolve); }
protected GroovyClassLoader createClassLoader() { GroovyClassLoader gcl = AccessController.doPrivileged( new PrivilegedAction<GroovyClassLoader>() { @Override public GroovyClassLoader run() { return new GroovyClassLoader(ClassLoader.getSystemClassLoader(), config); } }); Path path = getClasspath(); if (path != null) { final String[] filePaths = path.list(); for (int i = 0; i < filePaths.length; i++) { String filePath = filePaths[i]; gcl.addClasspath(filePath); } } return gcl; }
/** * creates a GroovyClassLoader. * * @param parent the parent class loader * @param config the compiler configuration * @param useConfigurationClasspath determines if the configurations classpath should be added */ public GroovyClassLoader(ClassLoader parent, CompilerConfiguration config, boolean useConfigurationClasspath) { super(EMPTY_URL_ARRAY, parent); if (config == null) config = CompilerConfiguration.DEFAULT; this.config = config; if (useConfigurationClasspath) { for (String path : config.getClasspath()) { this.addClasspath(path); } } initSourceEncoding(config); }
/** * When a method is not found in the current script, checks that it's possible to call a method closure from the binding. * * @throws IOException * @throws CompilationFailedException * @throws IllegalAccessException * @throws InstantiationException */ public void testInvokeMethodFallsThroughToMethodClosureInBinding() throws IOException, CompilationFailedException, IllegalAccessException, InstantiationException { String text = "if (method() == 3) { println 'succeeded' }"; GroovyCodeSource codeSource = new GroovyCodeSource(text, "groovy.script", "groovy.script"); GroovyClassLoader loader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader()); Class clazz = loader.parseClass(codeSource); Script script = ((Script) clazz.newInstance()); Binding binding = new Binding(); binding.setVariable("method", new MethodClosure(new Dummy(), "method")); script.setBinding(binding); script.run(); }
private Closure createTemplateClosure(List<StringSection> sections, final ClassLoader parentLoader, StringBuilder target) throws ClassNotFoundException { final GroovyClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<GroovyClassLoader>() { public GroovyClassLoader run() { return new GroovyClassLoader(parentLoader); } }); final Class groovyClass; try { groovyClass = loader.parseClass(new GroovyCodeSource(target.toString(), TEMPLATE_SCRIPT_PREFIX + counter++ + ".groovy", "x")); } catch (MultipleCompilationErrorsException e) { throw mangleMultipleCompilationErrorsException(e, sections); } catch (Exception e) { throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage()); } Closure result; try { final GroovyObject object = (GroovyObject) groovyClass.newInstance(); Closure chicken = (Closure) object.invokeMethod("getTemplate", null); //bind the two first parameters of the generated closure to this class and the sections list result = chicken.curry(this, sections); } catch (InstantiationException e) { throw new ClassNotFoundException(e.getMessage()); } catch (IllegalAccessException e) { throw new ClassNotFoundException(e.getMessage()); } return result; }
@Override public void setup() { CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass("org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport.TypeCheckingDSL"); ImportCustomizer ic = new ImportCustomizer(); ic.addStarImports("org.codehaus.groovy.ast.expr"); ic.addStaticStars("org.codehaus.groovy.ast.ClassHelper"); ic.addStaticStars("org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport"); config.addCompilationCustomizers(ic); final GroovyClassLoader transformLoader = compilationUnit!=null?compilationUnit.getTransformLoader():typeCheckingVisitor.getSourceUnit().getClassLoader(); Class<?> clazz = transformLoader.loadClass(scriptPath, false, true); if (TypeCheckingDSL.class.isAssignableFrom(clazz)) { script = (TypeCheckingDSL) clazz.newInstance(); GroovyShell shell = new GroovyShell(transformLoader, new Binding(), config); script = (TypeCheckingDSL) shell.parse( new InputStreamReader(is, typeCheckingVisitor.getSourceUnit().getConfiguration().getSourceEncoding()) );
@Override protected void setUp() throws Exception { CompilerConfiguration config = new CompilerConfiguration(); config.setScriptBaseClass(GroovyScriptCommand.class.getName()); // loader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config); shell = new GroovyShell(loader); }
CompilerConfiguration conf = new CompilerConfiguration(); conf.setDebug(true); shell = new GroovyShell(conf); if (addClasspath != null && addClasspath.length() > 0) { for (File fcp : Files.listPathsFiles(addClasspath)) { throw new ProcessException("Path not found `" + fcp + "` for `" + ADD_CLASSPATH.getDisplayName() + "`"); shell.getClassLoader().addClasspath(fcp.toString()); shell.getClassLoader().addClasspath(groovyClasspath);
public long execute() throws Exception { ClassLoader cl = new URLClassLoader(classpath, ClassLoader.getSystemClassLoader().getParent()); GroovyClassLoader gcl = new GroovyClassLoader(cl); CompilationUnit cu = new CompilationUnit(new CompilerConfiguration(), null, gcl, new GroovyClassLoader(this.getClass().getClassLoader())); for (File source : sources) { cu.addSource(source); } long sd = System.nanoTime(); cu.compile(CompilePhase.CLASS_GENERATION.getPhaseNumber()); long dur = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sd); return dur; } }
@Override public void afterPropertiesSet() throws Exception { if (this.beanFactory != null && this.beanFactory instanceof ConfigurableListableBeanFactory) { ((ConfigurableListableBeanFactory) this.beanFactory).ignoreDependencyType(MetaClass.class); } CompilerConfiguration compilerConfig = this.compilerConfiguration; if (compilerConfig == null && this.compileStatic) { compilerConfig = new CompilerConfiguration(); compilerConfig.addCompilationCustomizers(new ASTTransformationCustomizer(CompileStatic.class)); } this.groovyClassLoader = new GroovyClassLoader(this.beanClassLoader, compilerConfig); }
GroovyClassLoader gcl = new GroovyClassLoader(TestUtil.class.getClassLoader()); File[] dependencies = ConfigUtil.getTestDependencies(); for(File dependency: dependencies) { gcl.addClasspath(dependency.getAbsolutePath()); Class testClass = gcl.parseClass(script, "__GenRESTTestCase__");
private void evalFiles(String[] filepaths, CommandLine cli) throws CompilationFailedException, IOException { Binding binding = setupBinding(new Binding(), cli.hasOption(MUTATIONS_ENABLED_FLAG_SHORT)); GroovyShell shell = new GroovyShell(binding); if(cli.hasOption(CLASSPATH_FLAG_SHORT)) { shell.getClassLoader().addClasspath(cli.getOptionValue(CLASSPATH_FLAG_SHORT)); } for(String filepath : filepaths) { File file = new File(filepath); shell.evaluate(file); } }
public Class parseClass(final GroovyCodeSource codeSource, Map<String, String> hints) throws CompilationFailedException { modelTypes.set(hints); try { return super.parseClass(codeSource); } finally { modelTypes.set(null); } } }
/** * Construct a GroovyScriptEngine that will use the supplied ScriptLocation. * * @param script location of the .groovy script file * @throws EngineException if there is an exception loading, parsing, or constructing the test from the * file. */ public GroovyScriptEngine(ScriptLocation script) throws EngineException { // Get groovy to compile the script and access the callable closure final ClassLoader parent = getClass().getClassLoader(); CompilerConfiguration configuration = new CompilerConfiguration(); configuration.setSourceEncoding("UTF-8"); final GroovyClassLoader loader = new GroovyClassLoader(parent, configuration, true); try { m_groovyClass = loader.parseClass(script.getFile()); m_grinderRunner = new GrinderContextExecutor(m_groovyClass); m_grinderRunner.runBeforeProcess(); assert m_grinderRunner.testCount() > 0; } catch (IOException io) { throw new EngineException("Unable to parse groovy script at: " + script.getFile().getAbsolutePath(), io); } catch (Throwable e) { throw new EngineException("Error while initialize test runner", e); } }
public static GroovyClassLoader newEvaluator(Imports imports, Classpath classpath, String outDir, ImportCustomizer icz, ClassLoader parent) { try { Class.forName("org.codehaus.groovy.control.customizers.ImportCustomizer"); } catch (ClassNotFoundException e1) { String gjp = System.getenv(GROOVY_JAR_PATH); String errorMsg = null; if (gjp != null && !gjp.isEmpty()) { errorMsg = "Groovy libary not found, GROOVY_JAR_PATH = " + gjp; } else { errorMsg = "Default groovy libary not found. No GROOVY_JAR_PATH variable set."; } throw new GroovyNotFoundException(errorMsg); } icz = addImportsCustomizer(icz, imports); CompilerConfiguration config = new CompilerConfiguration().addCompilationCustomizers(icz); String acloader_cp = String.join(File.pathSeparatorChar + "", classpath.getPathsAsStrings()); config.setClasspath(acloader_cp); return new GroovyClassLoader(parent, config); }
/** * Build a {@link GroovyClassLoader} for the given {@code ClassLoader}. * @param classLoader the ClassLoader to build a GroovyClassLoader for * @since 4.3.3 */ protected GroovyClassLoader buildGroovyClassLoader(@Nullable ClassLoader classLoader) { return (this.compilerConfiguration != null ? new GroovyClassLoader(classLoader, this.compilerConfiguration) : new GroovyClassLoader(classLoader)); }
public Object run() { CompilerConfiguration configuration = new CompilerConfiguration(); configuration.setClasspath(mgr.getClassPath()); return new GroovyClassLoader(finalParent, configuration); } });
new GroovyShell().parse( new File( "test.groovy" ) ).invokeMethod( "hello_world", null ) ; Class scriptClass = new GroovyClassLoader().parseClass( new File( "test.groovy" ) ) ; Object scriptInstance = scriptClass.newInstance() ; scriptClass.getDeclaredMethod( "hello_world", new Class[] {} ).invoke( scriptInstance, new Object[] {} ) ;
public void testInitOrder () throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { System.out.println("GET FIELD"); final Field f = new GroovyClassLoader().loadClass("org.codehaus.groovy.runtime.X", false, false, false).getField("field"); System.out.println(failed); assertTrue(!failed); f.getInt(null); System.out.println(failed); assertTrue(failed); } }