use of org.codehaus.groovy.ast.expr.ClosureExpression in project groovy-core by groovy.
the class NewifyASTTransformation method transform.
public Expression transform(Expression expr) {
if (expr == null)
return null;
if (expr instanceof MethodCallExpression && candidate == null) {
MethodCallExpression mce = (MethodCallExpression) expr;
Expression args = transform(mce.getArguments());
if (isNewifyCandidate(mce)) {
Expression transformed = transformMethodCall(mce, args);
transformed.setSourcePosition(mce);
return transformed;
}
Expression method = transform(mce.getMethod());
Expression object = transform(mce.getObjectExpression());
MethodCallExpression transformed = callX(object, method, args);
transformed.setSourcePosition(mce);
return transformed;
} else if (expr instanceof ClosureExpression) {
ClosureExpression ce = (ClosureExpression) expr;
ce.getCode().visit(this);
} else if (expr instanceof ConstructorCallExpression) {
ConstructorCallExpression cce = (ConstructorCallExpression) expr;
if (cce.isUsingAnonymousInnerClass()) {
cce.getType().visitContents(this);
}
} else if (expr instanceof DeclarationExpression) {
DeclarationExpression de = (DeclarationExpression) expr;
if (de == candidate || auto) {
candidate = null;
Expression left = de.getLeftExpression();
Expression right = transform(de.getRightExpression());
DeclarationExpression newDecl = new DeclarationExpression(left, de.getOperation(), right);
newDecl.addAnnotations(de.getAnnotations());
return newDecl;
}
return de;
}
return expr.transformExpression(this);
}
use of org.codehaus.groovy.ast.expr.ClosureExpression in project groovy-core by groovy.
the class AutoCloneASTTransformation method createCloneSerialization.
private void createCloneSerialization(ClassNode cNode) {
final BlockStatement body = new BlockStatement();
// def baos = new ByteArrayOutputStream()
final Expression baos = varX("baos");
body.addStatement(declS(baos, ctorX(BAOS_TYPE)));
// baos.withObjectOutputStream{ it.writeObject(this) }
MethodCallExpression writeObject = callX(castX(OOS_TYPE, varX("it")), "writeObject", varX("this"));
writeObject.setImplicitThis(false);
ClosureExpression writeClos = closureX(block(stmt(writeObject)));
writeClos.setVariableScope(new VariableScope());
body.addStatement(stmt(callX(baos, "withObjectOutputStream", args(writeClos))));
// def bais = new ByteArrayInputStream(baos.toByteArray())
final Expression bais = varX("bais");
body.addStatement(declS(bais, ctorX(BAIS_TYPE, args(callX(baos, "toByteArray")))));
// return bais.withObjectInputStream(getClass().classLoader){ (<type>) it.readObject() }
MethodCallExpression readObject = callX(castX(OIS_TYPE, varX("it")), "readObject");
readObject.setImplicitThis(false);
ClosureExpression readClos = closureX(block(stmt(castX(GenericsUtils.nonGeneric(cNode), readObject))));
readClos.setVariableScope(new VariableScope());
Expression classLoader = callX(callThisX("getClass"), "getClassLoader");
body.addStatement(returnS(callX(bais, "withObjectInputStream", args(classLoader, readClos))));
new VariableScopeVisitor(sourceUnit, true).visitClass(cNode);
ClassNode[] exceptions = { make(CloneNotSupportedException.class) };
cNode.addMethod("clone", ACC_PUBLIC, GenericsUtils.nonGeneric(cNode), Parameter.EMPTY_ARRAY, exceptions, body);
}
use of org.codehaus.groovy.ast.expr.ClosureExpression in project groovy-core by groovy.
the class JavaStubGenerator method getAnnotationValue.
private String getAnnotationValue(Object memberValue) {
String val = "null";
if (memberValue instanceof ListExpression) {
StringBuilder sb = new StringBuilder("{");
boolean first = true;
ListExpression le = (ListExpression) memberValue;
for (Expression e : le.getExpressions()) {
if (first)
first = false;
else
sb.append(",");
sb.append(getAnnotationValue(e));
}
sb.append("}");
val = sb.toString();
} else if (memberValue instanceof ConstantExpression) {
ConstantExpression ce = (ConstantExpression) memberValue;
Object constValue = ce.getValue();
if (constValue instanceof AnnotationNode) {
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
printAnnotation(out, (AnnotationNode) constValue);
val = writer.toString();
} else if (constValue instanceof Number || constValue instanceof Boolean)
val = constValue.toString();
else
val = "\"" + escapeSpecialChars(constValue.toString()) + "\"";
} else if (memberValue instanceof PropertyExpression || memberValue instanceof VariableExpression) {
// assume must be static class field or enum value or class that Java can resolve
val = ((Expression) memberValue).getText();
} else if (memberValue instanceof ClosureExpression) {
// annotation closure; replaced with this specific class literal to cover the
// case where annotation type uses Class<? extends Closure> for the closure's type
val = "groovy.lang.Closure.class";
} else if (memberValue instanceof ClassExpression) {
val = ((Expression) memberValue).getText() + ".class";
}
return val;
}
use of org.codehaus.groovy.ast.expr.ClosureExpression in project groovy by apache.
the class MarkupBuilderCodeTransformer method transform.
@Override
public Expression transform(final Expression exp) {
if (exp instanceof BinaryExpression) {
return transformBinaryExpression((BinaryExpression) exp);
}
if (exp instanceof MethodCallExpression) {
return transformMethodCall((MethodCallExpression) exp);
}
if (exp instanceof ClosureExpression) {
ClosureExpression cl = (ClosureExpression) exp;
cl.getCode().visit(this);
return cl;
}
if (exp instanceof VariableExpression) {
VariableExpression var = (VariableExpression) exp;
if (var.getAccessedVariable() instanceof DynamicVariable) {
MethodCallExpression callGetModel = new MethodCallExpression(new VariableExpression("this"), "getModel", ArgumentListExpression.EMPTY_ARGUMENTS);
callGetModel.setImplicitThis(true);
callGetModel.setSourcePosition(exp);
String varName = var.getName();
if ("model".equals(varName) || "unescaped".equals(varName)) {
return callGetModel;
}
MethodCallExpression mce = new MethodCallExpression(callGetModel, "get", new ArgumentListExpression(new ConstantExpression(varName)));
mce.setSourcePosition(exp);
mce.setImplicitThis(false);
MethodCallExpression yield = new MethodCallExpression(new VariableExpression("this"), "tryEscape", new ArgumentListExpression(mce));
yield.setImplicitThis(true);
yield.setSourcePosition(exp);
yield.putNodeMetaData(TARGET_VARIABLE, varName);
return autoEscape ? yield : mce;
}
}
return super.transform(exp);
}
use of org.codehaus.groovy.ast.expr.ClosureExpression in project groovy by apache.
the class AutoNewLineTransformer method visitMethodCallExpression.
@Override
public void visitMethodCallExpression(final MethodCallExpression call) {
boolean old = inBuilderMethod;
inBuilderMethod = false;
if (call.isImplicitThis() && call.getArguments() instanceof TupleExpression) {
List<Expression> expressions = ((TupleExpression) call.getArguments()).getExpressions();
if (!expressions.isEmpty()) {
Expression lastArg = expressions.get(expressions.size() - 1);
if (lastArg instanceof ClosureExpression) {
call.getObjectExpression().visit(this);
call.getMethod().visit(this);
for (Expression expression : expressions) {
inBuilderMethod = (expression == lastArg);
expression.visit(this);
}
}
}
} else {
super.visitMethodCallExpression(call);
}
inBuilderMethod = old;
}
Aggregations