use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.
the class TupleConstructorASTTransformation method processArgsBlock.
private static BlockStatement processArgsBlock(ClassNode cNode, VariableExpression namedArgs) {
BlockStatement block = new BlockStatement();
for (PropertyNode pNode : cNode.getProperties()) {
if (pNode.isStatic())
continue;
// if namedArgs.containsKey(propertyName) setProperty(propertyName, namedArgs.get(propertyName));
Statement ifStatement = ifS(callX(namedArgs, "containsKey", constX(pNode.getName())), assignS(varX(pNode), propX(namedArgs, pNode.getName())));
block.addStatement(ifStatement);
}
block.addStatement(stmt(callX(CHECK_METHOD_TYPE, "checkPropNames", args(varX("this"), namedArgs))));
return block;
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.
the class ImmutableASTTransformation method createConstructorOrdered.
private static void createConstructorOrdered(ClassNode cNode, List<PropertyNode> list) {
final MapExpression argMap = new MapExpression();
final Parameter[] orderedParams = new Parameter[list.size()];
int index = 0;
for (PropertyNode pNode : list) {
Parameter param = new Parameter(pNode.getField().getType(), pNode.getField().getName());
orderedParams[index++] = param;
argMap.addMapEntryExpression(constX(pNode.getName()), varX(pNode.getName()));
}
final BlockStatement orderedBody = new BlockStatement();
orderedBody.addStatement(stmt(ctorX(ClassNode.THIS, args(castX(HASHMAP_TYPE, argMap)))));
doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, orderedParams, ClassNode.EMPTY_ARRAY, orderedBody));
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.
the class ImmutableASTTransformation method createConstructorMap.
private void createConstructorMap(ClassNode cNode, List<PropertyNode> list, List<String> knownImmutableClasses, List<String> knownImmutables) {
final BlockStatement body = new BlockStatement();
body.addStatement(ifS(equalsNullX(varX("args")), assignS(varX("args"), new MapExpression())));
for (PropertyNode pNode : list) {
body.addStatement(createConstructorStatement(cNode, pNode, knownImmutableClasses, knownImmutables));
}
// check for missing properties
body.addStatement(stmt(callX(SELF_TYPE, "checkPropNames", args("this", "args"))));
createConstructorMapCommon(cNode, body);
if (!list.isEmpty()) {
createNoArgConstructor(cNode);
}
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.
the class ImmutableASTTransformation method createConstructorMapSpecial.
private static void createConstructorMapSpecial(ClassNode cNode, List<PropertyNode> list) {
final BlockStatement body = new BlockStatement();
body.addStatement(createConstructorStatementMapSpecial(list.get(0).getField()));
createConstructorMapCommon(cNode, body);
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy by apache.
the class LogASTTransformation method visit.
public void visit(ASTNode[] nodes, final SourceUnit source) {
init(nodes, source);
AnnotatedNode targetClass = (AnnotatedNode) nodes[1];
AnnotationNode logAnnotation = (AnnotationNode) nodes[0];
final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
final LoggingStrategy loggingStrategy = createLoggingStrategy(logAnnotation, classLoader);
if (loggingStrategy == null)
return;
final String logFieldName = lookupLogFieldName(logAnnotation);
final String categoryName = lookupCategoryName(logAnnotation);
if (!(targetClass instanceof ClassNode))
throw new GroovyBugError("Class annotation " + logAnnotation.getClassNode().getName() + " annotated no Class, this must not happen.");
final ClassNode classNode = (ClassNode) targetClass;
ClassCodeExpressionTransformer transformer = new ClassCodeExpressionTransformer() {
private FieldNode logNode;
@Override
protected SourceUnit getSourceUnit() {
return source;
}
public Expression transform(Expression exp) {
if (exp == null)
return null;
if (exp instanceof MethodCallExpression) {
return transformMethodCallExpression(exp);
}
if (exp instanceof ClosureExpression) {
return transformClosureExpression((ClosureExpression) exp);
}
return super.transform(exp);
}
@Override
public void visitClass(ClassNode node) {
FieldNode logField = node.getField(logFieldName);
if (logField != null && logField.getOwner().equals(node)) {
addError("Class annotated with Log annotation cannot have log field declared", logField);
} else if (logField != null && !Modifier.isPrivate(logField.getModifiers())) {
addError("Class annotated with Log annotation cannot have log field declared because the field exists in the parent class: " + logField.getOwner().getName(), logField);
} else {
logNode = loggingStrategy.addLoggerFieldToClass(node, logFieldName, categoryName);
}
super.visitClass(node);
}
private Expression transformClosureExpression(ClosureExpression exp) {
if (exp.getCode() instanceof BlockStatement) {
BlockStatement code = (BlockStatement) exp.getCode();
super.visitBlockStatement(code);
}
return exp;
}
private Expression transformMethodCallExpression(Expression exp) {
Expression modifiedCall = addGuard((MethodCallExpression) exp);
return modifiedCall == null ? super.transform(exp) : modifiedCall;
}
private Expression addGuard(MethodCallExpression mce) {
// only add guard to methods of the form: logVar.logMethod(params)
if (!(mce.getObjectExpression() instanceof VariableExpression)) {
return null;
}
VariableExpression variableExpression = (VariableExpression) mce.getObjectExpression();
if (!variableExpression.getName().equals(logFieldName) || !(variableExpression.getAccessedVariable() instanceof DynamicVariable)) {
return null;
}
String methodName = mce.getMethodAsString();
if (methodName == null)
return null;
if (!loggingStrategy.isLoggingMethod(methodName))
return null;
// since there is no saving
if (usesSimpleMethodArgumentsOnly(mce))
return null;
variableExpression.setAccessedVariable(logNode);
return loggingStrategy.wrapLoggingMethodCall(variableExpression, methodName, mce);
}
private boolean usesSimpleMethodArgumentsOnly(MethodCallExpression mce) {
Expression arguments = mce.getArguments();
if (arguments instanceof TupleExpression) {
TupleExpression tuple = (TupleExpression) arguments;
for (Expression exp : tuple.getExpressions()) {
if (!isSimpleExpression(exp))
return false;
}
return true;
}
return !isSimpleExpression(arguments);
}
private boolean isSimpleExpression(Expression exp) {
if (exp instanceof ConstantExpression)
return true;
if (exp instanceof VariableExpression)
return true;
return false;
}
};
transformer.visitClass(classNode);
// GROOVY-6373: references to 'log' field are normally already FieldNodes by now, so revisit scoping
new VariableScopeVisitor(sourceUnit, true).visitClass(classNode);
}
Aggregations