use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class StaticImportVisitor method transformMethodCallExpression.
protected Expression transformMethodCallExpression(MethodCallExpression mce) {
Expression args = transform(mce.getArguments());
Expression method = transform(mce.getMethod());
Expression object = transform(mce.getObjectExpression());
boolean isExplicitThisOrSuper = false;
boolean isExplicitSuper = false;
if (object instanceof VariableExpression) {
VariableExpression ve = (VariableExpression) object;
isExplicitThisOrSuper = !mce.isImplicitThis() && (ve.isThisExpression() || ve.isSuperExpression());
isExplicitSuper = ve.isSuperExpression();
}
if (mce.isImplicitThis() || isExplicitThisOrSuper) {
if (mce.isImplicitThis()) {
Expression ret = findStaticMethodImportFromModule(method, args);
if (ret != null) {
setSourcePosition(ret, mce);
return ret;
}
if (method instanceof ConstantExpression && !inLeftExpression) {
// could be a closure field
String methodName = (String) ((ConstantExpression) method).getValue();
ret = findStaticFieldOrPropAccessorImportFromModule(methodName);
if (ret != null) {
ret = new MethodCallExpression(ret, "call", args);
setSourcePosition(ret, mce);
return ret;
}
}
} else if (currentMethod != null && currentMethod.isStatic() && isExplicitSuper) {
MethodCallExpression ret = new MethodCallExpression(new ClassExpression(currentClass.getSuperClass()), method, args);
setSourcePosition(ret, mce);
return ret;
}
if (method instanceof ConstantExpression) {
ConstantExpression ce = (ConstantExpression) method;
Object value = ce.getValue();
if (value instanceof String) {
boolean foundInstanceMethod = false;
String methodName = (String) value;
boolean inInnerClass = isInnerClass(currentClass);
if (currentMethod != null && !currentMethod.isStatic()) {
if (currentClass.hasPossibleMethod(methodName, args)) {
foundInstanceMethod = true;
}
}
boolean lookForPossibleStaticMethod = !methodName.equals("call");
lookForPossibleStaticMethod &= !foundInstanceMethod;
lookForPossibleStaticMethod |= inSpecialConstructorCall;
lookForPossibleStaticMethod &= !inInnerClass;
if (!inClosure && lookForPossibleStaticMethod && (hasPossibleStaticMethod(currentClass, methodName, args, true)) || hasPossibleStaticProperty(currentClass, methodName)) {
StaticMethodCallExpression smce = new StaticMethodCallExpression(currentClass, methodName, args);
setSourcePosition(smce, mce);
return smce;
}
if (!inClosure && inInnerClass && inSpecialConstructorCall && mce.isImplicitThis() && !foundInstanceMethod) {
if (currentClass.getOuterClass().hasPossibleMethod(methodName, args)) {
object = new PropertyExpression(new ClassExpression(currentClass.getOuterClass()), new ConstantExpression("this"));
} else if (hasPossibleStaticMethod(currentClass.getOuterClass(), methodName, args, true) || hasPossibleStaticProperty(currentClass.getOuterClass(), methodName)) {
StaticMethodCallExpression smce = new StaticMethodCallExpression(currentClass.getOuterClass(), methodName, args);
setSourcePosition(smce, mce);
return smce;
}
}
}
}
}
MethodCallExpression result = new MethodCallExpression(object, method, args);
result.setSafe(mce.isSafe());
result.setImplicitThis(mce.isImplicitThis());
result.setSpreadSafe(mce.isSpreadSafe());
result.setMethodTarget(mce.getMethodTarget());
// GROOVY-6757
result.setGenericsTypes(mce.getGenericsTypes());
setSourcePosition(result, mce);
return result;
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class ASTTransformationCollectorCodeVisitor method getMode.
private static AnnotationCollectorMode getMode(AnnotationNode node) {
final Expression member = node.getMember("mode");
if (member != null && member instanceof PropertyExpression) {
PropertyExpression prop = (PropertyExpression) member;
Expression oe = prop.getObjectExpression();
if (oe instanceof ClassExpression) {
ClassExpression ce = (ClassExpression) oe;
if (ce.getType().getName().equals("groovy.transform.AnnotationCollectorMode")) {
return AnnotationCollectorMode.valueOf(prop.getPropertyAsString());
}
}
}
return null;
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class BaseScriptASTTransformation method changeBaseScriptTypeFromPackageOrImport.
private void changeBaseScriptTypeFromPackageOrImport(final SourceUnit source, final AnnotatedNode parent, final AnnotationNode node) {
Expression value = node.getMember("value");
if (!(value instanceof ClassExpression)) {
addError("Annotation " + MY_TYPE_NAME + " member 'value' should be a class literal.", value);
return;
}
List<ClassNode> classes = source.getAST().getClasses();
for (ClassNode classNode : classes) {
if (classNode.isScriptBody()) {
changeBaseScriptType(parent, classNode, value.getType());
}
}
}
use of org.codehaus.groovy.ast.expr.ClassExpression 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);
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class AnnotationVisitor method validateEnumConstant.
private boolean validateEnumConstant(Expression exp) {
if (exp instanceof PropertyExpression) {
PropertyExpression pe = (PropertyExpression) exp;
String name = pe.getPropertyAsString();
if (pe.getObjectExpression() instanceof ClassExpression && name != null) {
ClassExpression ce = (ClassExpression) pe.getObjectExpression();
ClassNode type = ce.getType();
if (type.isEnum()) {
boolean ok = false;
try {
FieldNode enumField = type.getDeclaredField(name);
ok = enumField != null && enumField.getType().equals(type);
} catch (Exception ex) {
// ignore
}
if (!ok) {
addError("No enum const " + type.getName() + "." + name, pe);
return false;
}
}
}
}
return true;
}
Aggregations