use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.
the class Java5 method configureAnnotation.
public void configureAnnotation(AnnotationNode node) {
ClassNode type = node.getClassNode();
List<AnnotationNode> annotations = type.getAnnotations();
for (AnnotationNode an : annotations) {
configureAnnotationFromDefinition(an, node);
}
configureAnnotationFromDefinition(node, node);
}
use of org.codehaus.groovy.ast.AnnotationNode in project ratpack by ratpack.
the class ScriptEngine method createClassLoader.
private GroovyClassLoader createClassLoader(final Path scriptPath) {
final CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
if (!scriptBaseClass.equals(Script.class)) {
compilerConfiguration.setScriptBaseClass(scriptBaseClass.getName());
}
compilerConfiguration.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {
@Override
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
if (staticCompile) {
classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
}
classNode.addAnnotation(new AnnotationNode(new ClassNode(InheritConstructors.class)));
if (scriptPath != null) {
AnnotationNode scriptPathAnnotation = new AnnotationNode(new ClassNode(ScriptPath.class));
scriptPathAnnotation.addMember("value", new ConstantExpression(scriptPath.toUri().toString()));
classNode.addAnnotation(scriptPathAnnotation);
}
}
});
return new GroovyClassLoader(parentLoader, compilerConfiguration) {
@Override
protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
return new CompilationUnit(config, source, this) {
{
verifier = new Verifier() {
@Override
public void visitClass(ClassNode node) {
if (node.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type)) {
AnnotationNode lineNumberAnnotation = new AnnotationNode(LINE_NUMBER_CLASS_NODE);
lineNumberAnnotation.addMember("value", new ConstantExpression(node.getLineNumber(), true));
node.addAnnotation(lineNumberAnnotation);
}
super.visitClass(node);
}
};
}
};
}
};
}
use of org.codehaus.groovy.ast.AnnotationNode in project hale by halestudio.
the class VertexEntityTransformation method processClass.
/**
* Process a class and make it a vertex entity class.
*
* @param clazz the class node
*/
private void processClass(ClassNode clazz) {
// check if class already was processed (has field v)
if (clazz.getField("v") != null) {
return;
}
// find all classes annotated with @ODocumentEntity
List<AnnotationNode> entityAnnotations = clazz.getAnnotations(VERTEX_ENTITY_CLASS);
if (entityAnnotations != null && !entityAnnotations.isEmpty()) {
Expression entityName = entityAnnotations.get(0).getMember("value");
Expression typeProperty = entityAnnotations.get(0).getMember("typeProperty");
if (typeProperty == null) {
// default value if none given
typeProperty = new ConstantExpression("_type");
}
Expression superEntityName = null;
FieldNode vertexField = null;
FieldNode graphField = null;
ClassNode superClass = clazz.getSuperClass();
if (superClass != null) {
List<AnnotationNode> superAnnotations = superClass.getAnnotations(VERTEX_ENTITY_CLASS);
if (superAnnotations != null && !superAnnotations.isEmpty()) {
// super class is also a vertex entity
superEntityName = superAnnotations.get(0).getMember("value");
// super class must be processed first
processClass(superClass);
// use fields from super class
vertexField = clazz.getField("v");
graphField = clazz.getField("g");
} else {
superClass = null;
}
}
// add the vertex field
if (vertexField == null) {
vertexField = clazz.addField("v", Modifier.PROTECTED, VERTEX_CLASS, null);
}
// add the graph field
if (graphField == null) {
graphField = clazz.addField("g", Modifier.PROTECTED, GRAPH_CLASS, null);
}
// add constructor
clazz.addConstructor(buildVertexGraphConstructor(vertexField, graphField, superClass, typeProperty, entityName));
Map<String, Expression> initialExpressions = new HashMap<>();
// get all non-static properties
List<PropertyNode> properties = AbstractASTTransformUtil.getInstanceProperties(clazz);
List<PropertyNode> newProperties = new ArrayList<>();
for (PropertyNode property : properties) {
// collect initial expressions for create function
if (property.getField().getInitialExpression() != null) {
initialExpressions.put(property.getName(), property.getInitialExpression());
}
// add static findByX method
clazz.addMethod(buildFindByMethod(clazz, entityName, typeProperty, property.getName(), property.getType()));
// add static findUniqueByX method
clazz.addMethod(buildGetByMethod(clazz, entityName, typeProperty, property.getName(), property.getType()));
// update property
property.setGetterBlock(createGetter(property.getName(), vertexField, property.getType(), property.getField().getInitialExpression()));
property.setSetterBlock(createSetter(property.getName(), vertexField));
newProperties.add(property);
}
// readd updated properties
for (PropertyNode property : newProperties) {
readdProperty(clazz, property);
}
// add the vertex getter
//
clazz.addMethod(//
"getV", //
Modifier.PUBLIC, //
VERTEX_CLASS, new Parameter[0], new ClassNode[0], new ReturnStatement(new FieldExpression(vertexField)));
// add the graph getter
//
clazz.addMethod(//
"getG", //
Modifier.PUBLIC, //
GRAPH_CLASS, new Parameter[0], new ClassNode[0], new ReturnStatement(new FieldExpression(graphField)));
// add the id getter
clazz.addMethod("getId", Modifier.PUBLIC, ClassHelper.OBJECT_TYPE, new Parameter[0], new ClassNode[0], new ReturnStatement(new MethodCallExpression(new FieldExpression(vertexField), "getId", new ArgumentListExpression())));
// add delete method
clazz.addMethod(buildDeleteMethod(vertexField, graphField));
// add static create method
clazz.addMethod(buildCreateMethod(clazz, entityName, initialExpressions));
// add static findAll method
clazz.addMethod(buildFindAllMethod(clazz, entityName, typeProperty));
// add static getById method
clazz.addMethod(buildGetByIdMethod(clazz));
// add static initGraph method
clazz.addMethod(buildInitGraphMethod(entityName, superEntityName));
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project ratpack by ratpack.
the class GroovySnippetExecuter method execute.
@Override
public void execute(TestCodeSnippet snippet) throws Exception {
CompilerConfiguration config = new CompilerConfiguration();
config.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {
@Override
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
if (compileStatic) {
classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
}
}
});
ClassLoader classLoader = new URLClassLoader(new URL[] {}, getClass().getClassLoader());
GroovyShell groovyShell = new GroovyShell(classLoader, new Binding(), config);
List<String> importsAndSnippet = extractImports(snippet.getSnippet());
String imports = importsAndSnippet.get(0);
String snippetMinusImports = fixture.transform(importsAndSnippet.get(1));
String fullSnippet = imports + fixture.pre() + snippetMinusImports + fixture.post();
Script script;
try {
script = groovyShell.parse(fullSnippet, snippet.getClassName());
} catch (MultipleCompilationErrorsException e) {
Message error = e.getErrorCollector().getError(0);
if (error instanceof SyntaxErrorMessage) {
// noinspection ThrowableResultOfMethodCallIgnored
System.out.println(snippet.getSnippet());
throw new CompileException(e, ((SyntaxErrorMessage) error).getCause().getLine());
} else {
throw e;
}
}
ClassLoader previousContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(groovyShell.getClassLoader());
fixture.around(script::run);
} finally {
Thread.currentThread().setContextClassLoader(previousContextClassLoader);
}
}
use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.
the class MixinTransformation method visit.
public void visit(ASTNode[] astNodes, SourceUnit source) {
if (!(astNodes[0] instanceof AnnotationNode) || !(astNodes[1] instanceof AnnotatedNode)) {
throw new RuntimeException("Internal error: wrong types: $node.class / $parent.class");
}
AnnotatedNode parent = (AnnotatedNode) astNodes[1];
AnnotationNode node = (AnnotationNode) astNodes[0];
if (!MY_TYPE.equals(node.getClassNode()) || !(parent instanceof ClassNode)) {
return;
}
ClassNode classNode = (ClassNode) parent;
String cName = classNode.getName();
if (classNode.isInterface()) {
throw new RuntimeException("Error processing interface '" + cName + "'. " + MY_TYPE_NAME + " not allowed for interfaces.");
}
ListExpression values = getListOfClasses(node);
weaveMixinsIntoClass(classNode, values);
}
Aggregations