use of org.codehaus.groovy.ast.stmt.ReturnStatement in project gradle by gradle.
the class RuleVisitor method visitGeneratedClosure.
// Not part of a normal visitor, see ClosureCreationInterceptingVerifier
public static void visitGeneratedClosure(ClassNode node) {
MethodNode closureCallMethod = AstUtils.getGeneratedClosureImplMethod(node);
Statement closureCode = closureCallMethod.getCode();
InputReferences inputs = closureCode.getNodeMetaData(AST_NODE_METADATA_INPUTS_KEY);
if (inputs != null) {
SourceLocation sourceLocation = closureCode.getNodeMetaData(AST_NODE_METADATA_LOCATION_KEY);
node.addInterface(TRANSFORMED_CLOSURE);
FieldNode inputsField = new FieldNode(INPUTS_FIELD_NAME, Modifier.PRIVATE, POTENTIAL_INPUTS, node, null);
FieldNode ruleFactoryField = new FieldNode(RULE_FACTORY_FIELD_NAME, Modifier.PRIVATE, RULE_FACTORY, node, null);
node.addField(inputsField);
node.addField(ruleFactoryField);
// Generate makeRule() method
List<Statement> statements = new ArrayList<Statement>();
statements.add(new ExpressionStatement(new BinaryExpression(new FieldExpression(inputsField), ASSIGN, new VariableExpression("inputs"))));
statements.add(new ExpressionStatement(new BinaryExpression(new FieldExpression(ruleFactoryField), ASSIGN, new VariableExpression("ruleFactory"))));
node.addMethod(new MethodNode("makeRule", Modifier.PUBLIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(POTENTIAL_INPUTS, "inputs"), new Parameter(RULE_FACTORY, "ruleFactory") }, new ClassNode[0], new BlockStatement(statements, new VariableScope())));
// Generate inputReferences() method
VariableExpression inputsVar = new VariableExpression("inputs", INPUT_REFERENCES);
VariableScope methodVarScope = new VariableScope();
methodVarScope.putDeclaredVariable(inputsVar);
statements = new ArrayList<Statement>();
statements.add(new ExpressionStatement(new DeclarationExpression(inputsVar, ASSIGN, new ConstructorCallExpression(INPUT_REFERENCES, new ArgumentListExpression()))));
for (InputReference inputReference : inputs.getOwnReferences()) {
statements.add(new ExpressionStatement(new MethodCallExpression(inputsVar, "ownReference", new ArgumentListExpression(new ConstantExpression(inputReference.getPath()), new ConstantExpression(inputReference.getLineNumber())))));
}
for (InputReference inputReference : inputs.getNestedReferences()) {
statements.add(new ExpressionStatement(new MethodCallExpression(inputsVar, "nestedReference", new ArgumentListExpression(new ConstantExpression(inputReference.getPath()), new ConstantExpression(inputReference.getLineNumber())))));
}
statements.add(new ReturnStatement(inputsVar));
node.addMethod(new MethodNode("inputReferences", Modifier.PUBLIC, INPUT_REFERENCES, new Parameter[0], new ClassNode[0], new BlockStatement(statements, methodVarScope)));
// Generate sourceLocation() method
statements = new ArrayList<Statement>();
statements.add(new ReturnStatement(new ConstructorCallExpression(SOURCE_LOCATION, new ArgumentListExpression(Arrays.<Expression>asList(new ConstantExpression(SOURCE_URI_TOKEN), new ConstantExpression(SOURCE_DESC_TOKEN), new ConstantExpression(sourceLocation.getExpression()), new ConstantExpression(sourceLocation.getLineNumber()), new ConstantExpression(sourceLocation.getColumnNumber()))))));
node.addMethod(new MethodNode("sourceLocation", Modifier.PUBLIC, SOURCE_LOCATION, new Parameter[0], new ClassNode[0], new BlockStatement(statements, new VariableScope())));
}
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement 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.stmt.ReturnStatement in project hale by halestudio.
the class VertexEntityTransformation method buildGetByMethod.
/**
* Create a static method to retrieve an object by property value.
*
* @param clazz the entity class node
* @param entityName the entity name
* @param typeProperty the name of the property holding the entity name in a
* vertex
* @param propertyName the property name
* @param propertyType the property type
* @return the method
*/
private MethodNode buildGetByMethod(ClassNode clazz, Expression entityName, Expression typeProperty, String propertyName, ClassNode propertyType) {
clazz = ClassHelper.make(clazz.getName());
propertyType = ClassHelper.make(propertyType.getName());
String methodName = "getBy" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
BlockStatement code = new BlockStatement();
// initialize graph
code.addStatement(callInitGraph(clazz, new VariableExpression("graph")));
/*
* def vertex = VertexEntityDelegates.findUniqueByDelegate(graph,
* entityName, typeProperty, propertyName, value)
*/
VariableExpression vertex = new VariableExpression("vertex");
ArgumentListExpression args = new ArgumentListExpression();
args.addExpression(new VariableExpression("graph"));
args.addExpression(entityName);
args.addExpression(typeProperty);
args.addExpression(new ConstantExpression(propertyName));
args.addExpression(new VariableExpression("value"));
code.addStatement(AbstractASTTransformUtil.declStatement(vertex, new StaticMethodCallExpression(VE_DELEGATES_CLASS, VertexEntityDelegates.METHOD_GET_BY, args)));
/*
* return new EntityClass(vertex, graph)
*/
Statement returnEntity = new ReturnStatement(new ConstructorCallExpression(clazz, new ArgumentListExpression(vertex, new VariableExpression("graph"))));
// return null
Statement returnNull = new ReturnStatement(new ConstantExpression(null));
// if (vertex == null) ... else ...
code.addStatement(new IfStatement(AbstractASTTransformUtil.equalsNullExpr(vertex), returnNull, returnEntity));
return new MethodNode(methodName, Modifier.STATIC | Modifier.PUBLIC, clazz, new Parameter[] { new Parameter(GRAPH_CLASS, "graph"), new Parameter(propertyType, "value") }, new ClassNode[] { VE_NON_UNIQUE_EXCEPTION }, code);
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project hale by halestudio.
the class VertexEntityTransformation method buildFindAllMethod.
/**
* Create a static method to retrieve all objects in a graph.
*
* @param clazz the entity class node
* @param entityName the entity name
* @param typeProperty the name of the property holding the entity name in a
* vertex
* @return the method
*/
private MethodNode buildFindAllMethod(ClassNode clazz, Expression entityName, Expression typeProperty) {
clazz = ClassHelper.make(clazz.getName());
ClassNode returnType = ITERABLE_CLASS.getPlainNodeReference();
// add generic type argument
returnType.setGenericsTypes(new GenericsType[] { new GenericsType(clazz) });
BlockStatement code = new BlockStatement();
// initialize graph
code.addStatement(callInitGraph(clazz, new VariableExpression("graph")));
/*
* def vertices = VertexEntityDelegates.findAllDelegate(graph,
* entityName, typeProperty)
*/
VariableExpression vertices = new VariableExpression("vertices");
ArgumentListExpression args = new ArgumentListExpression();
args.addExpression(new VariableExpression("graph"));
args.addExpression(entityName);
args.addExpression(typeProperty);
code.addStatement(AbstractASTTransformUtil.declStatement(vertices, new StaticMethodCallExpression(VE_DELEGATES_CLASS, VertexEntityDelegates.METHOD_FIND_ALL, args)));
/*
* return new IterableDelegate(vertices, EntityClass, graph)
*/
ArgumentListExpression createDelegateArgs = new ArgumentListExpression();
createDelegateArgs.addExpression(vertices);
createDelegateArgs.addExpression(new ClassExpression(clazz));
createDelegateArgs.addExpression(new VariableExpression("graph"));
code.addStatement(new ReturnStatement(new ConstructorCallExpression(VE_ITERABLE_DELEGATE_CLASS, createDelegateArgs)));
return new MethodNode("findAll", Modifier.STATIC | Modifier.PUBLIC, returnType, new Parameter[] { new Parameter(GRAPH_CLASS, "graph") }, new ClassNode[0], code);
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class DefaultGrailsDomainClassInjector method injectToStringMethod.
private void injectToStringMethod(ClassNode classNode) {
final boolean hasToString = /*GrailsASTUtils.*/
implementsZeroArgMethod(classNode, "toString");
if (!hasToString) {
GStringExpression ge = new GStringExpression(classNode.getName() + " : ${id}");
ge.addString(new ConstantExpression(classNode.getName() + " : "));
ge.addValue(new VariableExpression("id"));
Statement s = new ReturnStatement(ge);
MethodNode mn = new MethodNode("toString", Modifier.PUBLIC, new ClassNode(String.class), new Parameter[0], new ClassNode[0], s);
//if(LOG.isDebugEnabled()) {
// LOG.debug("[GrailsDomainInjector] Adding method [toString()] to class [" + classNode.getName() + "]");
//}
classNode.addMethod(mn);
}
}
Aggregations