Search in sources :

Example 41 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project groovy by apache.

the class ImmutableASTTransformation method visit.

@Override
public void visit(final ASTNode[] nodes, final SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(anno.getClassNode()))
        return;
    if (parent instanceof ClassNode) {
        final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
        final PropertyHandler handler = PropertyHandler.createPropertyHandler(this, classLoader, (ClassNode) parent);
        if (handler == null || !handler.validateAttributes(this, anno))
            return;
        doMakeImmutable((ClassNode) parent, anno, handler);
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyHandler(groovy.transform.options.PropertyHandler) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 42 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project groovy by apache.

the class RecordTypeASTTransformation method visit.

@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(anno.getClassNode()))
        return;
    if (parent instanceof ClassNode) {
        final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
        final PropertyHandler handler = PropertyHandler.createPropertyHandler(this, classLoader, (ClassNode) parent);
        if (handler == null)
            return;
        if (!handler.validateAttributes(this, anno))
            return;
        doProcessRecordType((ClassNode) parent, handler);
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyHandler(groovy.transform.options.PropertyHandler) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 43 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project groovy by apache.

the class MapConstructorASTTransformation method visit.

@Override
public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(anno.getClassNode()))
        return;
    if (parent instanceof ClassNode) {
        ClassNode cNode = (ClassNode) parent;
        if (!checkNotInterface(cNode, MY_TYPE_NAME))
            return;
        boolean includeFields = memberHasValue(anno, "includeFields", true);
        boolean includeProperties = !memberHasValue(anno, "includeProperties", false);
        boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true);
        boolean includeSuperFields = memberHasValue(anno, "includeSuperFields", true);
        boolean includeStatic = memberHasValue(anno, "includeStatic", true);
        boolean allProperties = memberHasValue(anno, "allProperties", true);
        boolean noArg = memberHasValue(anno, "noArg", true);
        boolean specialNamedArgHandling = !memberHasValue(anno, "specialNamedArgHandling", false);
        List<String> excludes = getMemberStringList(anno, "excludes");
        List<String> includes = getMemberStringList(anno, "includes");
        boolean allNames = memberHasValue(anno, "allNames", true);
        if (!checkIncludeExcludeUndefinedAware(anno, excludes, includes, MY_TYPE_NAME))
            return;
        if (!checkPropertyList(cNode, includes, "includes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties))
            return;
        if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties))
            return;
        final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
        final PropertyHandler handler = PropertyHandler.createPropertyHandler(this, classLoader, cNode);
        if (handler == null)
            return;
        if (!handler.validateAttributes(this, anno))
            return;
        Expression pre = anno.getMember("pre");
        if (pre != null && !(pre instanceof ClosureExpression)) {
            addError("Expected closure value for annotation parameter 'pre'. Found " + pre, cNode);
            return;
        }
        Expression post = anno.getMember("post");
        if (post != null && !(post instanceof ClosureExpression)) {
            addError("Expected closure value for annotation parameter 'post'. Found " + post, cNode);
            return;
        }
        createConstructors(this, anno, handler, cNode, includeFields, includeProperties, includeSuperProperties, includeSuperFields, noArg, allNames, allProperties, specialNamedArgHandling, includeStatic, excludes, includes, (ClosureExpression) pre, (ClosureExpression) post, source);
        if (pre != null) {
            anno.setMember("pre", new ClosureExpression(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE));
        }
        if (post != null) {
            anno.setMember("post", new ClosureExpression(Parameter.EMPTY_ARRAY, EmptyStatement.INSTANCE));
        }
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyHandler(groovy.transform.options.PropertyHandler) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 44 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project groovy by apache.

the class ClassInfoLeakStressTest method testLeak.

@Test
public void testLeak() {
    assertFalse(Boolean.getBoolean("groovy.use.classvalue"));
    for (int i = 0; i < NUM_OBJECTS; i++) {
        GroovyClassLoader gcl = new GroovyClassLoader();
        Class scriptClass = gcl.parseClass("int myvar = " + i);
        ClassInfo ci = ClassInfo.getClassInfo(scriptClass);
        Reference<ClassLoader> classLoaderRef = new WeakReference<ClassLoader>(gcl, classLoaderQueue);
        Reference<Class<?>> classRef = new WeakReference<Class<?>>(scriptClass, classQueue);
        Reference<ClassInfo> classInfoRef = new WeakReference<ClassInfo>(ci, classInfoQueue);
        refList.add(classLoaderRef);
        refList.add(classRef);
        refList.add(classInfoRef);
        gcl = null;
        scriptClass = null;
        ci = null;
        GCUtils.gc();
    }
    // Add new class to help evict the last collected entry
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class scriptClass = gcl.parseClass("int myvar = 7777");
    ClassInfo ci = ClassInfo.getClassInfo(scriptClass);
    GCUtils.gc();
    // All objects should have been collected
    assertEquals("GroovyClassLoaders not collected by GC", NUM_OBJECTS, queueSize(classLoaderQueue));
    assertEquals("Script Classes not collected by GC", NUM_OBJECTS, queueSize(classQueue));
    int ciSize = queueSize(classInfoQueue);
    assertEquals("ClassInfo objects [" + ciSize + "] collected by GC, expected [" + NUM_OBJECTS + "]", NUM_OBJECTS, ciSize);
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) WeakReference(java.lang.ref.WeakReference) GroovyClassLoader(groovy.lang.GroovyClassLoader) Test(org.junit.Test)

Example 45 with GroovyClassLoader

use of groovy.lang.GroovyClassLoader in project groovy by apache.

the class ScriptCompilationExecuter method execute.

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;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) URLClassLoader(java.net.URLClassLoader) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) URLClassLoader(java.net.URLClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) File(java.io.File)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)136 File (java.io.File)28 GroovyObject (groovy.lang.GroovyObject)19 Test (org.junit.jupiter.api.Test)18 IOException (java.io.IOException)15 HashMap (java.util.HashMap)15 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)14 Map (java.util.Map)12 Binding (groovy.lang.Binding)10 URL (java.net.URL)10 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)10 BuildException (org.apache.tools.ant.BuildException)9 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)9 ClassNode (org.codehaus.groovy.ast.ClassNode)9 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)9 DefaultGrailsApplication (grails.core.DefaultGrailsApplication)8 GrailsApplication (grails.core.GrailsApplication)8 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)8 ArtefactHandler (grails.core.ArtefactHandler)7 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)7