use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.
the class TraitComposer method createDelegatingForwarder.
private static Statement createDelegatingForwarder(final MethodNode forwarderMethod, final ClassNode next) {
// generates --> next$Trait$Helper.method(this, arg1, arg2)
TraitHelpersTuple helpers = Traits.findHelpers(next);
ArgumentListExpression args = new ArgumentListExpression();
args.addExpression(new VariableExpression("this"));
Parameter[] forwarderMethodParameters = forwarderMethod.getParameters();
for (final Parameter forwarderMethodParameter : forwarderMethodParameters) {
args.addExpression(new VariableExpression(forwarderMethodParameter));
}
StaticMethodCallExpression delegateCall = new StaticMethodCallExpression(helpers.getHelper(), forwarderMethod.getName(), args);
Statement result;
if (ClassHelper.VOID_TYPE.equals(forwarderMethod.getReturnType())) {
BlockStatement stmt = new BlockStatement();
stmt.addStatement(new ExpressionStatement(delegateCall));
stmt.addStatement(new ReturnStatement(new ConstantExpression(null)));
result = stmt;
} else {
result = new ReturnStatement(delegateCall);
}
return result;
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.
the class TraitReceiverTransformer method transformMethodCallOnThis.
private Expression transformMethodCallOnThis(final MethodCallExpression call) {
Expression method = call.getMethod();
Expression arguments = call.getArguments();
if (method instanceof ConstantExpression) {
String methodName = method.getText();
List<MethodNode> methods = traitClass.getMethods(methodName);
for (MethodNode methodNode : methods) {
if (methodName.equals(methodNode.getName()) && methodNode.isPrivate()) {
return transformPrivateMethodCall(call, arguments, methodName);
}
}
}
MethodCallExpression transformed = new MethodCallExpression(weaved, method, transform(arguments));
transformed.setSourcePosition(call);
transformed.setSafe(call.isSafe());
transformed.setSpreadSafe(call.isSpreadSafe());
transformed.setImplicitThis(false);
return transformed;
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy-core by groovy.
the class Java5 method annotationValueToExpression.
private Expression annotationValueToExpression(Object value) {
if (value == null || value instanceof String || value instanceof Number || value instanceof Character || value instanceof Boolean)
return new ConstantExpression(value);
if (value instanceof Class)
return new ClassExpression(ClassHelper.makeWithoutCaching((Class) value));
if (value.getClass().isArray()) {
ListExpression elementExprs = new ListExpression();
int len = Array.getLength(value);
for (int i = 0; i != len; ++i) elementExprs.addExpression(annotationValueToExpression(Array.get(value, i)));
return elementExprs;
}
return null;
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project gradle by gradle.
the class MagicFieldTransform method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
ClassNode clazz = (ClassNode) nodes[1];
clazz.addField("magicField", Modifier.PUBLIC, ClassHelper.STRING_TYPE, new ConstantExpression("magicValue"));
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project grails-core by grails.
the class AbstractGrailsArtefactTransformer method populateAutowiredApiLookupMethod.
protected MethodNode populateAutowiredApiLookupMethod(ClassNode classNode, ClassNode implementationNode, String apiProperty, String methodName, BlockStatement methodBody) {
addApiLookupFieldAndSetter(classNode, implementationNode, apiProperty, null);
VariableExpression apiVar = new VariableExpression(apiProperty, implementationNode);
BlockStatement ifBlock = new BlockStatement();
ArgumentListExpression arguments = new ArgumentListExpression();
arguments.addExpression(new ConstantExpression("Method on class [" + classNode + "] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly."));
ifBlock.addStatement(new ThrowStatement(new ConstructorCallExpression(new ClassNode(IllegalStateException.class), arguments)));
BlockStatement elseBlock = new BlockStatement();
elseBlock.addStatement(new ReturnStatement(apiVar));
methodBody.addStatement(new IfStatement(new BooleanExpression(new BinaryExpression(apiVar, GrailsASTUtils.EQUALS_OPERATOR, GrailsASTUtils.NULL_EXPRESSION)), ifBlock, elseBlock));
MethodNode methodNode = new MethodNode(methodName, PUBLIC_STATIC_MODIFIER, implementationNode, ZERO_PARAMETERS, null, methodBody);
return methodNode;
}
Aggregations