Search in sources :

Example 56 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project grails-core by grails.

the class LoggingTransformer method performInjectionOnAnnotatedClass.

@Override
public void performInjectionOnAnnotatedClass(SourceUnit source, ClassNode classNode) {
    if (classNode.getNodeMetaData(Slf4j.class) != null)
        return;
    String packageName = Slf4j.class.getPackage().getName();
    // if already annotated skip
    for (AnnotationNode annotationNode : classNode.getAnnotations()) {
        if (annotationNode.getClassNode().getPackageName().equals(packageName)) {
            return;
        }
    }
    FieldNode logField = classNode.getField("log");
    if (logField != null) {
        if (!Modifier.isPrivate(logField.getModifiers())) {
            return;
        }
    }
    if (classNode.getSuperClass().getName().equals("grails.boot.config.GrailsAutoConfiguration")) {
        return;
    }
    AnnotationNode annotationNode = new AnnotationNode(ClassHelper.make(Slf4j.class));
    LogASTTransformation logASTTransformation = new LogASTTransformation();
    logASTTransformation.setCompilationUnit(new CompilationUnit(new GroovyClassLoader(getClass().getClassLoader())));
    logASTTransformation.visit(new ASTNode[] { annotationNode, classNode }, source);
    classNode.putNodeMetaData(Slf4j.class, annotationNode);
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) GroovyClassLoader(groovy.lang.GroovyClassLoader) LogASTTransformation(org.codehaus.groovy.transform.LogASTTransformation) Slf4j(groovy.util.logging.Slf4j)

Example 57 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project qi4j-sdk by Qi4j.

the class GroovyMixin method invokeAsObject.

private Object invokeAsObject(Method method, Object[] args, URL groovySource) throws Throwable {
    try {
        Class declaringClass = method.getDeclaringClass();
        GroovyObject groovyObject = groovyObjects.get(declaringClass);
        if (groovyObject == null) {
            InputStream is = null;
            final Class groovyClass;
            try {
                is = groovySource.openStream();
                StringBuilder sourceBuilder = new StringBuilder();
                Inputs.text(groovySource).transferTo(Outputs.text(sourceBuilder));
                GroovyClassLoader groovyClassLoader = new GroovyClassLoader(declaringClass.getClassLoader());
                groovyClass = groovyClassLoader.parseClass(sourceBuilder.toString());
            } finally {
                if (is != null) {
                    is.close();
                }
            }
            groovyObject = (GroovyObject) groovyClass.newInstance();
            if (hasProperty(groovyObject, "This")) {
                groovyObject.setProperty("This", me);
            }
            groovyObjects.put(declaringClass, groovyObject);
        }
        return groovyObject.invokeMethod(method.getName(), args);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) InputStream(java.io.InputStream) MissingPropertyException(groovy.lang.MissingPropertyException) GroovyObject(groovy.lang.GroovyObject)

Example 58 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project fess by codelibs.

the class DocBoostMatcherTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    ScriptEngineFactory scriptEngineFactory = new ScriptEngineFactory();
    ComponentUtil.register(scriptEngineFactory, "scriptEngineFactory");
    new AbstractScriptEngine() {

        @Override
        public Object evaluate(String template, Map<String, Object> paramMap) {
            final Map<String, Object> bindingMap = new HashMap<>(paramMap);
            bindingMap.put("container", SingletonLaContainerFactory.getContainer());
            final GroovyShell groovyShell = new GroovyShell(new Binding(bindingMap));
            try {
                return groovyShell.evaluate(template);
            } catch (final JobProcessingException e) {
                throw e;
            } catch (final Exception e) {
                return null;
            } finally {
                final GroovyClassLoader loader = groovyShell.getClassLoader();
                loader.clearCache();
            }
        }

        @Override
        protected String getName() {
            return Constants.DEFAULT_SCRIPT;
        }
    }.register();
}
Also used : Binding(groovy.lang.Binding) JobProcessingException(org.codelibs.fess.exception.JobProcessingException) GroovyShell(groovy.lang.GroovyShell) JobProcessingException(org.codelibs.fess.exception.JobProcessingException) GroovyClassLoader(groovy.lang.GroovyClassLoader) ScriptEngineFactory(org.codelibs.fess.script.ScriptEngineFactory) Map(java.util.Map) HashMap(java.util.HashMap) AbstractScriptEngine(org.codelibs.fess.script.AbstractScriptEngine)

Example 59 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project groovy-core by groovy.

the class JavacJavaCompiler method makeParameters.

private String[] makeParameters(List<String> files, GroovyClassLoader parentClassLoader) {
    Map options = config.getJointCompilationOptions();
    LinkedList<String> paras = new LinkedList<String>();
    File target = config.getTargetDirectory();
    if (target == null)
        target = new File(".");
    // defaults
    paras.add("-d");
    paras.add(target.getAbsolutePath());
    paras.add("-sourcepath");
    paras.add(((File) options.get("stubDir")).getAbsolutePath());
    // add flags
    String[] flags = (String[]) options.get("flags");
    if (flags != null) {
        for (String flag : flags) {
            paras.add('-' + flag);
        }
    }
    boolean hadClasspath = false;
    // add namedValues
    String[] namedValues = (String[]) options.get("namedValues");
    if (namedValues != null) {
        for (int i = 0; i < namedValues.length; i += 2) {
            String name = namedValues[i];
            if (name.equals("classpath"))
                hadClasspath = true;
            paras.add('-' + name);
            paras.add(namedValues[i + 1]);
        }
    }
    // append classpath if not already defined
    if (!hadClasspath) {
        // add all classpaths that compilation unit sees
        StringBuilder resultPath = new StringBuilder(DefaultGroovyMethods.join((Iterable) config.getClasspath(), File.pathSeparator));
        ClassLoader cl = parentClassLoader;
        while (cl != null) {
            if (cl instanceof URLClassLoader) {
                for (URL u : ((URLClassLoader) cl).getURLs()) {
                    try {
                        resultPath.append(File.pathSeparator);
                        resultPath.append(new File(u.toURI()).getPath());
                    } catch (URISyntaxException e) {
                    // ignore it
                    }
                }
            }
            cl = cl.getParent();
        }
        paras.add("-classpath");
        paras.add(resultPath.toString());
    }
    // files to compile
    paras.addAll(files);
    return paras.toArray(new String[paras.size()]);
}
Also used : URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) File(java.io.File)

Example 60 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project groovy-core by groovy.

the class ClassNodeResolver method tryAsLoaderClassOrScript.

/**
 * 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);
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)126 File (java.io.File)26 GroovyObject (groovy.lang.GroovyObject)17 Test (org.junit.jupiter.api.Test)17 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)13 IOException (java.io.IOException)11 Binding (groovy.lang.Binding)10 URL (java.net.URL)10 HashMap (java.util.HashMap)10 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)10 Map (java.util.Map)9 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)8 GrailsApplication (grails.core.GrailsApplication)8 BuildException (org.apache.tools.ant.BuildException)8 ArtefactHandler (grails.core.ArtefactHandler)7 GroovyShell (groovy.lang.GroovyShell)7 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)7 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)7 Script (groovy.lang.Script)6 Decorator (com.opensymphony.module.sitemesh.Decorator)5