use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.
the class MethodTest method testMethods.
public void testMethods() throws Exception {
ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
Statement statementA = new ReturnStatement(new ConstantExpression("calledA"));
Statement statementB = new ReturnStatement(new ConstantExpression("calledB"));
Statement emptyStatement = new BlockStatement();
classNode.addMethod(new MethodNode("a", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementA));
classNode.addMethod(new MethodNode("b", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementB));
classNode.addMethod(new MethodNode("noReturnMethodA", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
classNode.addMethod(new MethodNode("noReturnMethodB", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
classNode.addMethod(new MethodNode("c", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
Class fooClass = loadClass(classNode);
assertTrue("Loaded a new class", fooClass != null);
Object bean = fooClass.newInstance();
assertTrue("Created instance of class: " + bean, bean != null);
assertCallMethod(bean, "a", "calledA");
assertCallMethod(bean, "b", "calledB");
assertCallMethod(bean, "noReturnMethodA", null);
assertCallMethod(bean, "noReturnMethodB", null);
assertCallMethod(bean, "c", null);
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.
the class ForTest method testManyParam.
public void testManyParam() throws Exception {
ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
Parameter[] parameters = { new Parameter(ClassHelper.OBJECT_TYPE, "coll1"), new Parameter(ClassHelper.OBJECT_TYPE, "coll2"), new Parameter(ClassHelper.OBJECT_TYPE, "coll3") };
BlockStatement statement = new BlockStatement();
statement.addStatement(createPrintlnStatement(new VariableExpression("coll1")));
statement.addStatement(createPrintlnStatement(new VariableExpression("coll2")));
statement.addStatement(createPrintlnStatement(new VariableExpression("coll3")));
classNode.addMethod(new MethodNode("manyParamDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement));
Class fooClass = loadClass(classNode);
assertTrue("Loaded a new class", fooClass != null);
Object bean = fooClass.newInstance();
assertTrue("Managed to create bean", bean != null);
System.out.println("################ Now about to invoke a method with many parameters");
Object[] array = { new Integer(1000 * 1000), "foo-", "bar~" };
try {
InvokerHelper.invokeMethod(bean, "manyParamDemo", array);
} catch (InvokerInvocationException e) {
System.out.println("Caught: " + e.getCause());
e.getCause().printStackTrace();
fail("Should not have thrown an exception");
}
System.out.println("################ Done");
}
use of org.codehaus.groovy.ast.stmt.BlockStatement 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.BlockStatement in project hale by halestudio.
the class VertexEntityTransformation method createSetter.
private Statement createSetter(String name, FieldNode vertexField) {
BlockStatement block = new BlockStatement();
VariableExpression value = new VariableExpression("value");
/*
* v.setProperty(name, value)
*/
ArgumentListExpression args = new ArgumentListExpression();
args.addExpression(new ConstantExpression(name));
args.addExpression(value);
Statement ifNotNull = new ExpressionStatement(new MethodCallExpression(new FieldExpression(vertexField), "setProperty", args));
/*
* v.removeProperty(name)
*/
Statement ifNull = new ExpressionStatement(new MethodCallExpression(new FieldExpression(vertexField), "removeProperty", new ArgumentListExpression(new ConstantExpression(name))));
block.addStatement(new IfStatement(AbstractASTTransformUtil.equalsNullExpr(value), ifNull, ifNotNull));
return block;
}
use of org.codehaus.groovy.ast.stmt.BlockStatement 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);
}
Aggregations