use of org.codehaus.groovy.ast.expr.StaticMethodCallExpression in project groovy-core by groovy.
the class StaticMethodCallExpressionTransformer method transformStaticMethodCallExpression.
Expression transformStaticMethodCallExpression(final StaticMethodCallExpression orig) {
MethodNode target = (MethodNode) orig.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
if (target != null) {
MethodCallExpression call = new MethodCallExpression(new ClassExpression(orig.getOwnerType()), orig.getMethod(), orig.getArguments());
call.setMethodTarget(target);
call.setSourcePosition(orig);
call.copyNodeMetaData(orig);
return transformer.transform(call);
}
return transformer.superTransform(orig);
}
use of org.codehaus.groovy.ast.expr.StaticMethodCallExpression in project groovy by apache.
the class StaticImportVisitor method transformBinaryExpression.
protected Expression transformBinaryExpression(BinaryExpression be) {
int type = be.getOperation().getType();
boolean oldInLeftExpression;
Expression right = transform(be.getRightExpression());
be.setRightExpression(right);
Expression left;
if (type == Types.EQUAL && be.getLeftExpression() instanceof VariableExpression) {
oldInLeftExpression = inLeftExpression;
inLeftExpression = true;
left = transform(be.getLeftExpression());
inLeftExpression = oldInLeftExpression;
if (left instanceof StaticMethodCallExpression) {
StaticMethodCallExpression smce = (StaticMethodCallExpression) left;
StaticMethodCallExpression result = new StaticMethodCallExpression(smce.getOwnerType(), smce.getMethod(), right);
result.copyNodeMetaData(smce);
setSourcePosition(result, be);
return result;
}
} else {
left = transform(be.getLeftExpression());
}
be.setLeftExpression(left);
return be;
}
use of org.codehaus.groovy.ast.expr.StaticMethodCallExpression in project groovy by apache.
the class StaticImportVisitor method transformMethodCallExpression.
protected Expression transformMethodCallExpression(MethodCallExpression mce) {
Expression object = transform(mce.getObjectExpression());
Expression method = transform(mce.getMethod());
Expression args = transform(mce.getArguments());
// GROOVY-10396: skip the instance method checks when the context is static with-respect-to current class
boolean staticWrtCurrent = inSpecialConstructorCall || currentMethod != null && currentMethod.isStatic();
if (mce.isImplicitThis()) {
String name = mce.getMethodAsString();
boolean thisOrSuperMethod = staticWrtCurrent ? hasPossibleStaticMethod(currentClass, name, args, true) : currentClass.tryFindPossibleMethod(name, args) != null;
if (!thisOrSuperMethod && currentClass.getOuterClasses().stream().noneMatch(oc -> oc.tryFindPossibleMethod(name, args) != null)) {
Expression result = findStaticMethodImportFromModule(method, args);
if (result != null) {
setSourcePosition(result, mce);
return result;
}
}
} else if (staticWrtCurrent && isSuperExpression(object)) {
Expression result = new MethodCallExpression(new ClassExpression(currentClass.getSuperClass()), method, args);
result.setSourcePosition(mce);
return result;
}
if (method instanceof ConstantExpression && ((ConstantExpression) method).getValue() instanceof String && (mce.isImplicitThis() || isThisOrSuper(object))) {
String methodName = (String) ((ConstantExpression) method).getValue();
boolean foundInstanceMethod = !staticWrtCurrent && currentClass.hasPossibleMethod(methodName, args);
Predicate<ClassNode> hasPossibleStaticMember = cn -> {
if (hasPossibleStaticMethod(cn, methodName, args, true)) {
return true;
}
// GROOVY-9587: don't check for property for non-empty call args
if (args instanceof TupleExpression && ((TupleExpression) args).getExpressions().isEmpty() && hasPossibleStaticProperty(cn, methodName)) {
return true;
}
return false;
};
if (mce.isImplicitThis()) {
if (isInnerClass(currentClass)) {
if (inSpecialConstructorCall && !foundInstanceMethod) {
// check for reference to outer class method in this(...) or super(...)
if (currentClass.getOuterClass().hasPossibleMethod(methodName, args)) {
object = new PropertyExpression(new ClassExpression(currentClass.getOuterClass()), new ConstantExpression("this"));
} else if (hasPossibleStaticMember.test(currentClass.getOuterClass())) {
Expression result = new StaticMethodCallExpression(currentClass.getOuterClass(), methodName, args);
result.setSourcePosition(mce);
return result;
}
}
} else if (inSpecialConstructorCall || (!inClosure && !foundInstanceMethod && !methodName.equals("call"))) {
// check for reference to static method in this(...) or super(...) or when call not resolved
if (hasPossibleStaticMember.test(currentClass)) {
Expression result = new StaticMethodCallExpression(currentClass, methodName, args);
result.setSourcePosition(mce);
return result;
}
}
}
}
MethodCallExpression result = new MethodCallExpression(object, method, args);
result.setGenericsTypes(mce.getGenericsTypes());
result.setMethodTarget(mce.getMethodTarget());
result.setImplicitThis(mce.isImplicitThis());
result.setSpreadSafe(mce.isSpreadSafe());
result.setSafe(mce.isSafe());
result.setSourcePosition(mce);
return result;
}
use of org.codehaus.groovy.ast.expr.StaticMethodCallExpression in project groovy by apache.
the class ConfigurationSetup method init.
/**
* Adds an instance field which allows to control whether GContract assertions
* are enabled or not. Before assertions are evaluated this field will be checked.
*
* @param type the current {@link ClassNode}
* @see Configurator
*/
public void init(final ClassNode type) {
Validate.notNull(type);
StaticMethodCallExpression checkAssertionsEnabledMethodCall = callX(ClassHelper.makeWithoutCaching(Configurator.class), "checkAssertionsEnabled", args(constX(type.getName())));
final FieldNode fieldNode = type.addField(BaseVisitor.GCONTRACTS_ENABLED_VAR, Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_FINAL, ClassHelper.boolean_TYPE, checkAssertionsEnabledMethodCall);
fieldNode.setSynthetic(true);
}
use of org.codehaus.groovy.ast.expr.StaticMethodCallExpression in project groovy by apache.
the class InvocationWriter method writeInvokeStaticMethod.
public void writeInvokeStaticMethod(final StaticMethodCallExpression call) {
Expression receiver = new ClassExpression(call.getOwnerType());
Expression messageName = new ConstantExpression(call.getMethod());
makeCall(call, receiver, messageName, call.getArguments(), InvocationWriter.invokeStaticMethod, false, false, false);
}
Aggregations