use of org.codehaus.groovy.ast.expr.ClassExpression in project gradle by gradle.
the class GradleResolveVisitor method transformInlineConstants.
// resolve constant-looking expressions statically (do here as gets transformed away later)
private Expression transformInlineConstants(Expression exp) {
if (exp instanceof PropertyExpression) {
PropertyExpression pe = (PropertyExpression) exp;
if (pe.getObjectExpression() instanceof ClassExpression) {
ClassExpression ce = (ClassExpression) pe.getObjectExpression();
ClassNode type = ce.getType();
if (type.isEnum()) {
return exp;
}
FieldNode fn = type.getField(pe.getPropertyAsString());
if (fn != null && !fn.isEnum() && fn.isStatic() && fn.isFinal()) {
if (fn.getInitialValueExpression() instanceof ConstantExpression) {
return fn.getInitialValueExpression();
}
}
}
} else if (exp instanceof ListExpression) {
ListExpression le = (ListExpression) exp;
ListExpression result = new ListExpression();
for (Expression e : le.getExpressions()) {
result.addExpression(transformInlineConstants(e));
}
return result;
} else if (exp instanceof AnnotationConstantExpression) {
ConstantExpression ce = (ConstantExpression) exp;
if (ce.getValue() instanceof AnnotationNode) {
// replicate a little bit of AnnotationVisitor here
// because we can't wait until later to do this
AnnotationNode an = (AnnotationNode) ce.getValue();
for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) {
member.setValue(transformInlineConstants(member.getValue()));
}
}
}
return exp;
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project gradle by gradle.
the class GradleResolveVisitor method checkThisAndSuperAsPropertyAccess.
private void checkThisAndSuperAsPropertyAccess(PropertyExpression expression) {
if (expression.isImplicitThis()) {
return;
}
String prop = expression.getPropertyAsString();
if (prop == null) {
return;
}
if (!prop.equals("this") && !prop.equals("super")) {
return;
}
ClassNode type = expression.getObjectExpression().getType();
if (expression.getObjectExpression() instanceof ClassExpression) {
if (!(currentClass instanceof InnerClassNode) && !Traits.isTrait(type)) {
addError("The usage of 'Class.this' and 'Class.super' is only allowed in nested/inner classes.", expression);
return;
}
if (currentScope != null && !currentScope.isInStaticContext() && Traits.isTrait(type) && "super".equals(prop) && directlyImplementsTrait(type)) {
return;
}
ClassNode iterType = currentClass;
while (iterType != null) {
if (iterType.equals(type)) {
break;
}
iterType = iterType.getOuterClass();
}
if (iterType == null) {
addError("The class '" + type.getName() + "' needs to be an outer class of '" + currentClass.getName() + "' when using '.this' or '.super'.", expression);
}
if ((currentClass.getModifiers() & Opcodes.ACC_STATIC) == 0) {
return;
}
if (currentScope != null && !currentScope.isInStaticContext()) {
return;
}
addError("The usage of 'Class.this' and 'Class.super' within static nested class '" + currentClass.getName() + "' is not allowed in a static context.", expression);
}
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project gradle by gradle.
the class GradleResolveVisitor method transformPropertyExpression.
protected Expression transformPropertyExpression(PropertyExpression pe) {
boolean itlp = isTopLevelProperty;
boolean ipe = inPropertyExpression;
Expression objectExpression = pe.getObjectExpression();
inPropertyExpression = true;
isTopLevelProperty = objectExpression.getClass() != PropertyExpression.class;
objectExpression = transform(objectExpression);
// we handle the property part as if it were not part of the property
inPropertyExpression = false;
Expression property = transform(pe.getProperty());
isTopLevelProperty = itlp;
inPropertyExpression = ipe;
boolean spreadSafe = pe.isSpreadSafe();
PropertyExpression old = pe;
pe = new PropertyExpression(objectExpression, property, pe.isSafe());
pe.setSpreadSafe(spreadSafe);
pe.setSourcePosition(old);
String className = lookupClassName(pe);
if (className != null) {
ClassNode type = ClassHelper.make(className);
if (resolve(type)) {
Expression ret = new ClassExpression(type);
ret.setSourcePosition(pe);
return ret;
}
}
if (objectExpression instanceof ClassExpression && pe.getPropertyAsString() != null) {
// possibly an inner class
ClassExpression ce = (ClassExpression) objectExpression;
ClassNode type = new ConstructedNestedClass(ce.getType(), pe.getPropertyAsString());
if (resolve(type, false, false, false)) {
Expression ret = new ClassExpression(type);
ret.setSourcePosition(ce);
return ret;
}
}
Expression ret = pe;
checkThisAndSuperAsPropertyAccess(pe);
if (isTopLevelProperty) {
ret = correctClassClassChain(pe);
}
return ret;
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy-core by groovy.
the class BinaryExpressionHelper method evaluateInstanceof.
private void evaluateInstanceof(BinaryExpression expression) {
OperandStack operandStack = controller.getOperandStack();
expression.getLeftExpression().visit(controller.getAcg());
operandStack.box();
Expression rightExp = expression.getRightExpression();
ClassNode classType;
if (rightExp instanceof ClassExpression) {
ClassExpression classExp = (ClassExpression) rightExp;
classType = classExp.getType();
} else {
throw new RuntimeException("Right hand side of the instanceof keyword must be a class name, not: " + rightExp);
}
String classInternalName = BytecodeHelper.getClassInternalName(classType);
controller.getMethodVisitor().visitTypeInsn(INSTANCEOF, classInternalName);
operandStack.replace(ClassHelper.boolean_TYPE);
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy-core by groovy.
the class InvokeDynamicWriter method coerce.
@Override
public void coerce(ClassNode from, ClassNode target) {
ClassNode wrapper = ClassHelper.getWrapper(target);
makeIndyCall(invokeMethod, EmptyExpression.INSTANCE, false, false, "asType", new ClassExpression(wrapper));
if (ClassHelper.boolean_TYPE.equals(target) || ClassHelper.Boolean_TYPE.equals(target)) {
writeIndyCast(ClassHelper.OBJECT_TYPE, target);
} else {
BytecodeHelper.doCast(controller.getMethodVisitor(), wrapper);
controller.getOperandStack().replace(wrapper);
controller.getOperandStack().doGroovyCast(target);
}
}
Aggregations