use of org.codehaus.groovy.ast.expr.FieldExpression in project hale by halestudio.
the class VertexEntityTransformation method createGetter.
private Statement createGetter(String name, FieldNode vertexField, ClassNode propertyType, Expression initialExpression) {
BlockStatement block = new BlockStatement();
// def tmp
VariableExpression tmpValue = new VariableExpression("tmp");
/*
* > tmp = v.getProperty(name)
*/
ArgumentListExpression args = new ArgumentListExpression();
args.addExpression(new ConstantExpression(name));
block.addStatement(AbstractASTTransformUtil.declStatement(tmpValue, new MethodCallExpression(new FieldExpression(vertexField), "getProperty", args)));
if (ClassHelper.isPrimitiveType(propertyType) && initialExpression != null) {
// if the class is a primitive, we must do a null check here
// if (tmp == null) return <initial-value>
block.addStatement(new IfStatement(AbstractASTTransformUtil.equalsNullExpr(tmpValue), new ReturnStatement(initialExpression), new EmptyStatement()));
}
block.addStatement(new ReturnStatement(tmpValue));
return block;
}
use of org.codehaus.groovy.ast.expr.FieldExpression in project hale by halestudio.
the class VertexEntityTransformation method buildVertexGraphConstructor.
/**
* Create a constructor taking a Vertex and a Graph as an argument,
* assigning them to the vertex and graph fields.
*
* @param vertexField the vertex field
* @param graphField the graph field
* @param superClass the vertex entity super class or <code>null</code>
* @param typeProperty the expression specifying the name of the type
* property
* @param entityName the expression specifying the entity name
* @return a constructor taking a Vertex as an argument
*/
private ConstructorNode buildVertexGraphConstructor(FieldNode vertexField, FieldNode graphField, ClassNode superClass, Expression typeProperty, Expression entityName) {
BlockStatement block = new BlockStatement();
// parameter vertex
VariableExpression vertex = new VariableExpression("vertex");
// parameter graph
VariableExpression graph = new VariableExpression("graph");
if (superClass != null) {
// super(vertex, graph)
block.addStatement(new ExpressionStatement(new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(vertex, graph))));
} else {
// > this.v = vertex
block.addStatement(AbstractASTTransformUtil.assignStatement(new FieldExpression(vertexField), new VariableExpression("vertex")));
// > this.g = graph
block.addStatement(AbstractASTTransformUtil.assignStatement(new FieldExpression(graphField), new VariableExpression("graph")));
}
// vertex.setProperty(typeProperty, entityName)
Statement notOrientStatement = new ExpressionStatement(new MethodCallExpression(vertex, "setProperty", new ArgumentListExpression(new Expression[] { typeProperty, entityName })));
// > if (!(graph instanceof OrientGraph))
// > this.v.setProperty(typeProperty, entityName)
block.addStatement(new IfStatement(new NotExpression(AbstractASTTransformUtil.isInstanceOf(graph, ORIENT_GRAPH_CLASS)), notOrientStatement, new EmptyStatement()));
return new ConstructorNode(Modifier.PUBLIC, new Parameter[] { new Parameter(VERTEX_CLASS, "vertex"), new Parameter(GRAPH_CLASS, "graph") }, new ClassNode[0], block);
}
use of org.codehaus.groovy.ast.expr.FieldExpression in project hale by halestudio.
the class VertexEntityTransformation method buildDeleteMethod.
/**
* Create a method the removes the entity from the graph.
*
* @param vertexField the vertex field
* @param graphField the graph field
* @return the delete method
*/
private MethodNode buildDeleteMethod(FieldNode vertexField, FieldNode graphField) {
BlockStatement code = new BlockStatement();
// > g.removeVertex(v)
code.addStatement(new ExpressionStatement(new MethodCallExpression(new FieldExpression(graphField), "removeVertex", new ArgumentListExpression(new FieldExpression(vertexField)))));
// reset graph field
// > g = null
code.addStatement(AbstractASTTransformUtil.assignStatement(new FieldExpression(graphField), new ConstantExpression(null)));
return new MethodNode("delete", Modifier.PUBLIC, ClassHelper.VOID_TYPE, new Parameter[0], new ClassNode[0], code);
}
use of org.codehaus.groovy.ast.expr.FieldExpression in project groovy-core by groovy.
the class Verifier method addFieldInitialization.
protected void addFieldInitialization(List list, List staticList, FieldNode fieldNode, boolean isEnumClassNode, List initStmtsAfterEnumValuesInit, Set explicitStaticPropsInEnum) {
Expression expression = fieldNode.getInitialExpression();
if (expression != null) {
final FieldExpression fe = new FieldExpression(fieldNode);
if (fieldNode.getType().equals(ClassHelper.REFERENCE_TYPE) && ((fieldNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0)) {
fe.setUseReferenceDirectly(true);
}
ExpressionStatement statement = new ExpressionStatement(new BinaryExpression(fe, Token.newSymbol(Types.EQUAL, fieldNode.getLineNumber(), fieldNode.getColumnNumber()), expression));
if (fieldNode.isStatic()) {
// GROOVY-3311: pre-defined constants added by groovy compiler for numbers/characters should be
// initialized first so that code dependent on it does not see their values as empty
Expression initialValueExpression = fieldNode.getInitialValueExpression();
if (initialValueExpression instanceof ConstantExpression) {
ConstantExpression cexp = (ConstantExpression) initialValueExpression;
cexp = transformToPrimitiveConstantIfPossible(cexp);
if (fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(fieldNode.getType())) {
// GROOVY-5150: primitive type constants will be initialized directly
return;
}
staticList.add(0, statement);
} else {
staticList.add(statement);
}
// to avoid double initialization in case of several constructors
fieldNode.setInitialValueExpression(null);
/*
* If it is a statement for an explicitly declared static field inside an enum, store its
* reference. For enums, they need to be handled differently as such init statements should
* come after the enum values have been initialized inside <clinit> block. GROOVY-3161.
*/
if (isEnumClassNode && explicitStaticPropsInEnum.contains(fieldNode.getName())) {
initStmtsAfterEnumValuesInit.add(statement);
}
} else {
list.add(statement);
}
}
}
use of org.codehaus.groovy.ast.expr.FieldExpression in project groovy-core by groovy.
the class TraitASTTransformation method processProperty.
/**
* Mostly copied from the {@link Verifier} class but does *not* generate bytecode
*
* @param cNode
* @param node
*/
private static void processProperty(final ClassNode cNode, PropertyNode node) {
String name = node.getName();
FieldNode field = node.getField();
int propNodeModifiers = node.getModifiers();
String getterName = "get" + Verifier.capitalize(name);
String setterName = "set" + Verifier.capitalize(name);
// GROOVY-3726: clear volatile, transient modifiers so that they don't get applied to methods
if ((propNodeModifiers & Modifier.VOLATILE) != 0) {
propNodeModifiers = propNodeModifiers - Modifier.VOLATILE;
}
if ((propNodeModifiers & Modifier.TRANSIENT) != 0) {
propNodeModifiers = propNodeModifiers - Modifier.TRANSIENT;
}
Statement getterBlock = node.getGetterBlock();
if (getterBlock == null) {
MethodNode getter = cNode.getGetterMethod(getterName);
if (getter == null && ClassHelper.boolean_TYPE == node.getType()) {
String secondGetterName = "is" + Verifier.capitalize(name);
getter = cNode.getGetterMethod(secondGetterName);
}
if (!node.isPrivate() && methodNeedsReplacement(cNode, getter)) {
getterBlock = new ExpressionStatement(new FieldExpression(field));
}
}
Statement setterBlock = node.getSetterBlock();
if (setterBlock == null) {
// 2nd arg false below: though not usual, allow setter with non-void return type
MethodNode setter = cNode.getSetterMethod(setterName, false);
if (!node.isPrivate() && (propNodeModifiers & ACC_FINAL) == 0 && methodNeedsReplacement(cNode, setter)) {
setterBlock = new ExpressionStatement(new BinaryExpression(new FieldExpression(field), Token.newSymbol(Types.EQUAL, 0, 0), new VariableExpression("value")));
}
}
if (getterBlock != null) {
MethodNode getter = new MethodNode(getterName, propNodeModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
getter.setSynthetic(true);
cNode.addMethod(getter);
if (ClassHelper.boolean_TYPE == node.getType() || ClassHelper.Boolean_TYPE == node.getType()) {
String secondGetterName = "is" + Verifier.capitalize(name);
MethodNode secondGetter = new MethodNode(secondGetterName, propNodeModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
secondGetter.setSynthetic(true);
cNode.addMethod(secondGetter);
}
}
if (setterBlock != null) {
Parameter[] setterParameterTypes = { new Parameter(node.getType(), "value") };
VariableExpression var = (VariableExpression) ((BinaryExpression) ((ExpressionStatement) setterBlock).getExpression()).getRightExpression();
var.setAccessedVariable(setterParameterTypes[0]);
MethodNode setter = new MethodNode(setterName, propNodeModifiers, ClassHelper.VOID_TYPE, setterParameterTypes, ClassNode.EMPTY_ARRAY, setterBlock);
setter.setSynthetic(true);
cNode.addMethod(setter);
}
}
Aggregations