use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class EqualsAndHashCodeASTTransformation method calculateHashStatements.
private static Statement calculateHashStatements(ClassNode cNode, Expression hash, boolean includeFields, boolean callSuper, List<String> excludes, List<String> includes) {
final List<PropertyNode> pList = getInstanceProperties(cNode);
final List<FieldNode> fList = new ArrayList<FieldNode>();
if (includeFields) {
fList.addAll(getInstanceNonPropertyFields(cNode));
}
final BlockStatement body = new BlockStatement();
// def _result = HashCodeHelper.initHash()
final Expression result = varX("_result");
body.addStatement(declS(result, callX(HASHUTIL_TYPE, "initHash")));
for (PropertyNode pNode : pList) {
if (shouldSkip(pNode.getName(), excludes, includes))
continue;
// _result = HashCodeHelper.updateHash(_result, getProperty()) // plus self-reference checking
Expression getter = getterX(cNode, pNode);
final Expression current = callX(HASHUTIL_TYPE, "updateHash", args(result, getter));
body.addStatement(ifS(notX(sameX(getter, varX("this"))), assignS(result, current)));
}
for (FieldNode fNode : fList) {
if (shouldSkip(fNode.getName(), excludes, includes))
continue;
// _result = HashCodeHelper.updateHash(_result, field) // plus self-reference checking
final Expression fieldExpr = varX(fNode);
final Expression current = callX(HASHUTIL_TYPE, "updateHash", args(result, fieldExpr));
body.addStatement(ifS(notX(sameX(fieldExpr, varX("this"))), assignS(result, current)));
}
if (callSuper) {
// _result = HashCodeHelper.updateHash(_result, super.hashCode())
final Expression current = callX(HASHUTIL_TYPE, "updateHash", args(result, callSuperX("hashCode")));
body.addStatement(assignS(result, current));
}
// $hash$code = _result
if (hash != null) {
body.addStatement(assignS(hash, result));
} else {
body.addStatement(returnS(result));
}
return body;
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class EqualsAndHashCodeASTTransformation method differentSelfRecursivePropertyX.
private static BinaryExpression differentSelfRecursivePropertyX(PropertyNode pNode, Expression other) {
String getterName = getGetterName(pNode);
Expression selfGetter = callThisX(getterName);
Expression otherGetter = callX(other, getterName);
return orX(andX(sameX(selfGetter, varX("this")), notX(sameX(otherGetter, other))), andX(notX(sameX(selfGetter, varX("this"))), sameX(otherGetter, other)));
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class LazyASTTransformation method visitField.
static void visitField(AnnotationNode node, FieldNode fieldNode) {
final Expression soft = node.getMember("soft");
final Expression init = getInitExpr(fieldNode);
fieldNode.rename("$" + fieldNode.getName());
fieldNode.setModifiers(ACC_PRIVATE | (fieldNode.getModifiers() & (~(ACC_PUBLIC | ACC_PROTECTED))));
if (soft instanceof ConstantExpression && ((ConstantExpression) soft).getValue().equals(true)) {
createSoft(fieldNode, init);
} else {
create(fieldNode, init);
// @Lazy not meaningful with primitive so convert to wrapper if needed
if (ClassHelper.isPrimitiveType(fieldNode.getType())) {
fieldNode.setType(ClassHelper.getWrapper(fieldNode.getType()));
}
}
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class LazyASTTransformation method createSoftSetter.
private static void createSoftSetter(FieldNode fieldNode, ClassNode type) {
final BlockStatement body = new BlockStatement();
final Expression fieldExpr = varX(fieldNode);
final String name = "set" + MetaClassHelper.capitalize(fieldNode.getName().substring(1));
final Parameter parameter = param(type, "value");
final Expression paramExpr = varX(parameter);
body.addStatement(ifElseS(notNullX(paramExpr), assignS(fieldExpr, ctorX(SOFT_REF, paramExpr)), assignS(fieldExpr, NULL_EXPR)));
int visibility = ACC_PUBLIC;
if (fieldNode.isStatic())
visibility |= ACC_STATIC;
fieldNode.getDeclaringClass().addMethod(name, visibility, ClassHelper.VOID_TYPE, params(parameter), ClassNode.EMPTY_ARRAY, body);
}
use of org.codehaus.groovy.ast.expr.Expression in project groovy-core by groovy.
the class MemoizedASTTransformation method buildMemoizeClosureCallExpression.
private MethodCallExpression buildMemoizeClosureCallExpression(MethodNode privateMethod, int protectedCacheSize, int maxCacheSize) {
Parameter[] srcParams = privateMethod.getParameters();
Parameter[] newParams = cloneParams(srcParams);
List<Expression> argList = new ArrayList<Expression>(newParams.length);
for (int i = 0; i < srcParams.length; i++) {
argList.add(varX(newParams[i]));
}
ClosureExpression expression = new ClosureExpression(newParams, stmt(callThisX(privateMethod.getName(), args(argList))));
MethodCallExpression mce;
if (protectedCacheSize == 0 && maxCacheSize == 0) {
mce = callX(expression, MEMOIZE_METHOD_NAME);
} else if (protectedCacheSize == 0) {
mce = callX(expression, MEMOIZE_AT_MOST_METHOD_NAME, args(constX(maxCacheSize)));
} else if (maxCacheSize == 0) {
mce = callX(expression, MEMOIZE_AT_LEAST_METHOD_NAME, args(constX(protectedCacheSize)));
} else {
mce = callX(expression, MEMOIZE_BETWEEN_METHOD_NAME, args(constX(protectedCacheSize), constX(maxCacheSize)));
}
mce.setImplicitThis(false);
return mce;
}
Aggregations