use of org.codehaus.groovy.ast.expr.MethodCallExpression in project groovy by apache.
the class StaticVerifier method visitConstructorOrMethod.
@Override
public void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
MethodNode oldCurrentMethod = currentMethod;
currentMethod = node;
super.visitConstructorOrMethod(node, isConstructor);
if (isConstructor) {
final Set<String> exceptions = new HashSet<String>();
for (final Parameter param : node.getParameters()) {
exceptions.add(param.getName());
if (param.hasInitialExpression()) {
param.getInitialExpression().visit(new CodeVisitorSupport() {
@Override
public void visitVariableExpression(VariableExpression ve) {
if (exceptions.contains(ve.getName()))
return;
Variable av = ve.getAccessedVariable();
if (av instanceof DynamicVariable || !av.isInStaticContext()) {
addVariableError(ve);
}
}
@Override
public void visitMethodCallExpression(MethodCallExpression call) {
Expression objectExpression = call.getObjectExpression();
if (objectExpression instanceof VariableExpression) {
VariableExpression ve = (VariableExpression) objectExpression;
if (ve.isThisExpression()) {
addError("Can't access instance method '" + call.getMethodAsString() + "' for a constructor parameter default value", param);
return;
}
}
super.visitMethodCallExpression(call);
}
@Override
public void visitClosureExpression(ClosureExpression expression) {
//skip contents, because of dynamic scope
}
});
}
}
}
currentMethod = oldCurrentMethod;
}
use of org.codehaus.groovy.ast.expr.MethodCallExpression in project groovy by apache.
the class TraitReceiverTransformer method transformMethodCallOnThisFallBack.
private Expression transformMethodCallOnThisFallBack(final MethodCallExpression call, final Expression method, final Expression arguments) {
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.MethodCallExpression in project groovy by apache.
the class TraitReceiverTransformer method createFieldHelperCall.
private Expression createFieldHelperCall(Expression exp, ClassNode weavedType, String propName) {
String method = Traits.helperGetterName(new FieldNode(propName, 0, ClassHelper.OBJECT_TYPE, weavedType, null));
MethodCallExpression mce = new MethodCallExpression(createFieldHelperReceiver(), method, ArgumentListExpression.EMPTY_ARGUMENTS);
mce.setSourcePosition(exp);
mce.setImplicitThis(false);
return mce;
}
use of org.codehaus.groovy.ast.expr.MethodCallExpression in project groovy by apache.
the class TraitReceiverTransformer method transformBinaryExpression.
private Expression transformBinaryExpression(final BinaryExpression exp, final ClassNode weavedType) {
Expression leftExpression = exp.getLeftExpression();
Expression rightExpression = exp.getRightExpression();
Token operation = exp.getOperation();
if (operation.getText().equals("=")) {
String leftFieldName = null;
// it's an assignment
if (leftExpression instanceof VariableExpression && ((VariableExpression) leftExpression).getAccessedVariable() instanceof FieldNode) {
leftFieldName = ((VariableExpression) leftExpression).getAccessedVariable().getName();
} else if (leftExpression instanceof FieldExpression) {
leftFieldName = ((FieldExpression) leftExpression).getFieldName();
} else if (leftExpression instanceof PropertyExpression && (((PropertyExpression) leftExpression).isImplicitThis() || "this".equals(((PropertyExpression) leftExpression).getObjectExpression().getText()))) {
leftFieldName = ((PropertyExpression) leftExpression).getPropertyAsString();
FieldNode fn = tryGetFieldNode(weavedType, leftFieldName);
if (fieldHelper == null || fn == null && !fieldHelper.hasPossibleMethod(Traits.helperSetterName(new FieldNode(leftFieldName, 0, ClassHelper.OBJECT_TYPE, weavedType, null)), rightExpression)) {
return createAssignmentToField(rightExpression, operation, leftFieldName);
}
}
if (leftFieldName != null) {
FieldNode fn = weavedType.getDeclaredField(leftFieldName);
FieldNode staticField = tryGetFieldNode(weavedType, leftFieldName);
if (fn == null) {
fn = new FieldNode(leftFieldName, 0, ClassHelper.OBJECT_TYPE, weavedType, null);
}
Expression receiver = createFieldHelperReceiver();
boolean isStatic = staticField != null && staticField.isStatic();
if (fn.isStatic()) {
// DO NOT USE isStatic variable here!
receiver = new PropertyExpression(receiver, "class");
}
String method = Traits.helperSetterName(fn);
MethodCallExpression mce = new MethodCallExpression(receiver, method, new ArgumentListExpression(super.transform(rightExpression)));
mce.setSourcePosition(exp);
mce.setImplicitThis(false);
markDynamicCall(mce, staticField, isStatic);
return mce;
}
}
Expression leftTransform = transform(leftExpression);
Expression rightTransform = transform(rightExpression);
Expression ret = exp instanceof DeclarationExpression ? new DeclarationExpression(leftTransform, operation, rightTransform) : new BinaryExpression(leftTransform, operation, rightTransform);
ret.setSourcePosition(exp);
ret.copyNodeMetaData(exp);
return ret;
}
use of org.codehaus.groovy.ast.expr.MethodCallExpression in project groovy by apache.
the class MacroInvocationTrap method visitConstructorCallExpression.
@Override
public void visitConstructorCallExpression(final ConstructorCallExpression call) {
ClassNode type = call.getType();
if (type instanceof InnerClassNode) {
if (((InnerClassNode) type).isAnonymous() && MACROCLASS_TYPE.getNameWithoutPackage().equals(type.getSuperClass().getNameWithoutPackage())) {
//System.out.println("call = " + call.getText());
try {
String source = convertInnerClassToSource(type);
List<Expression> macroArgumentsExpressions = new LinkedList<Expression>();
macroArgumentsExpressions.add(new ConstantExpression(source));
macroArgumentsExpressions.add(buildSubstitutionMap(type));
macroArgumentsExpressions.add(new ClassExpression(ClassHelper.make(ClassNode.class)));
MethodCallExpression macroCall = new MethodCallExpression(new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"), MacroTransformation.MACRO_METHOD, new ArgumentListExpression(macroArgumentsExpressions));
macroCall.setSpreadSafe(false);
macroCall.setSafe(false);
macroCall.setImplicitThis(false);
call.putNodeMetaData(MacroTransformation.class, macroCall);
List<ClassNode> classes = sourceUnit.getAST().getClasses();
for (Iterator<ClassNode> iterator = classes.iterator(); iterator.hasNext(); ) {
final ClassNode aClass = iterator.next();
if (aClass == type || type == aClass.getOuterClass()) {
iterator.remove();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return;
}
}
super.visitConstructorCallExpression(call);
}
Aggregations