use of org.codehaus.groovy.ast.expr.FieldExpression in project groovy by apache.
the class IfElseTest method testLoop.
public void testLoop() throws Exception {
ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
classNode.addProperty(new PropertyNode("result", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
BooleanExpression expression = new BooleanExpression(new BinaryExpression(new FieldExpression(new FieldNode("bar", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("==", 0, 0), new ConstantExpression("abc")));
Statement trueStatement = new ExpressionStatement(new BinaryExpression(new FieldExpression(new FieldNode("result", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("=", 0, 0), new ConstantExpression("worked")));
Statement falseStatement = createPrintlnStatement(new ConstantExpression("false"));
IfStatement statement = new IfStatement(expression, trueStatement, falseStatement);
classNode.addMethod(new MethodNode("ifDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statement));
Class fooClass = loadClass(classNode);
assertTrue("Loaded a new class", fooClass != null);
Object bean = fooClass.getDeclaredConstructor().newInstance();
assertTrue("Managed to create bean", bean != null);
assertSetProperty(bean, "bar", "abc");
System.out.println("################ Now about to invoke method");
Object[] array = {};
InvokerHelper.invokeMethod(bean, "ifDemo", array);
System.out.println("################ Done");
assertGetProperty(bean, "result", "worked");
}
use of org.codehaus.groovy.ast.expr.FieldExpression in project groovy by apache.
the class TraitReceiverTransformer method transformBinaryExpression.
private Expression transformBinaryExpression(final BinaryExpression exp, final ClassNode weavedType) {
Expression leftExpression = exp.getLeftExpression();
Expression rightExpression = exp.getRightExpression();
Token operation = exp.getOperation();
if (operation.getText().equals("=")) {
String leftFieldName = null;
// it's an assignment
if (leftExpression instanceof VariableExpression && ((VariableExpression) leftExpression).getAccessedVariable() instanceof FieldNode) {
leftFieldName = ((VariableExpression) leftExpression).getAccessedVariable().getName();
} else if (leftExpression instanceof FieldExpression) {
leftFieldName = ((FieldExpression) leftExpression).getFieldName();
} else if (leftExpression instanceof PropertyExpression && (((PropertyExpression) leftExpression).isImplicitThis() || "this".equals(((PropertyExpression) leftExpression).getObjectExpression().getText()))) {
leftFieldName = ((PropertyExpression) leftExpression).getPropertyAsString();
FieldNode fn = tryGetFieldNode(weavedType, leftFieldName);
if (fieldHelper == null || fn == null && !fieldHelper.hasPossibleMethod(Traits.helperSetterName(new FieldNode(leftFieldName, 0, ClassHelper.OBJECT_TYPE, weavedType, null)), rightExpression)) {
return binX(propX(varX(weaved), leftFieldName), operation, transform(rightExpression));
}
}
if (leftFieldName != null) {
FieldNode fn = weavedType.getDeclaredField(leftFieldName);
FieldNode staticField = tryGetFieldNode(weavedType, leftFieldName);
if (fn == null) {
fn = new FieldNode(leftFieldName, 0, ClassHelper.OBJECT_TYPE, weavedType, null);
}
Expression receiver = createFieldHelperReceiver();
boolean isStatic = staticField != null && staticField.isStatic();
if (fn.isStatic()) {
// DO NOT USE isStatic variable here!
receiver = propX(receiver, "class");
}
String method = Traits.helperSetterName(fn);
MethodCallExpression mce = callX(receiver, method, args(super.transform(rightExpression)));
mce.setImplicitThis(false);
mce.setSourcePosition(exp);
markDynamicCall(mce, staticField, isStatic);
return mce;
}
}
Expression leftTransform = transform(leftExpression);
Expression rightTransform = transform(rightExpression);
Expression ret = exp instanceof DeclarationExpression ? new DeclarationExpression(leftTransform, operation, rightTransform) : binX(leftTransform, operation, rightTransform);
ret.setSourcePosition(exp);
ret.copyNodeMetaData(exp);
return ret;
}
use of org.codehaus.groovy.ast.expr.FieldExpression 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.expr.FieldExpression 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.expr.FieldExpression in project grails-core by grails.
the class TestMixinTransformation method addJunitRuleFields.
protected void addJunitRuleFields(ClassNode classNode) {
if (classNode.getField(JUNIT_ADAPTER_FIELD_NAME) != null) {
return;
}
ClassNode junitAdapterType = ClassHelper.make(TestRuntimeJunitAdapter.class);
FieldNode junitAdapterFieldNode = classNode.addField(JUNIT_ADAPTER_FIELD_NAME, Modifier.STATIC, junitAdapterType, new ConstructorCallExpression(junitAdapterType, MethodCallExpression.NO_ARGUMENTS));
boolean spockTest = isSpockTest(classNode);
FieldNode staticRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "StaticClassRule", Modifier.PRIVATE | Modifier.STATIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newClassRule", new ClassExpression(classNode)));
AnnotationNode classRuleAnnotation = new AnnotationNode(ClassHelper.make(ClassRule.class));
if (spockTest) {
// @ClassRule must be added to @Shared field in spock
FieldNode spockSharedRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "SharedClassRule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new FieldExpression(staticRuleFieldNode));
spockSharedRuleFieldNode.addAnnotation(classRuleAnnotation);
spockSharedRuleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Shared.class)));
addSpockFieldMetadata(spockSharedRuleFieldNode, 0);
} else {
staticRuleFieldNode.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
staticRuleFieldNode.addAnnotation(classRuleAnnotation);
}
FieldNode ruleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "Rule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newRule", new VariableExpression("this")));
ruleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Rule.class)));
if (spockTest) {
addSpockFieldMetadata(ruleFieldNode, 0);
}
}
Aggregations