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);
}
}
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);
}
}
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));
}
}
}
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);
}
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;
}
Aggregations