use of org.codehaus.groovy.ast.expr.PropertyExpression 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;
}
use of org.codehaus.groovy.ast.expr.PropertyExpression in project groovy by apache.
the class GeneralUtils method copyAnnotatedNodeAnnotations.
/**
* Copies all <tt>candidateAnnotations</tt> with retention policy {@link java.lang.annotation.RetentionPolicy#RUNTIME}
* and {@link java.lang.annotation.RetentionPolicy#CLASS}.
* <p>
* Annotations with {@link org.codehaus.groovy.runtime.GeneratedClosure} members are not supported at present.
*/
public static void copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNode, final List<AnnotationNode> copied, List<AnnotationNode> notCopied) {
List<AnnotationNode> annotationList = annotatedNode.getAnnotations();
for (AnnotationNode annotation : annotationList) {
List<AnnotationNode> annotations = annotation.getClassNode().getAnnotations(AbstractASTTransformation.RETENTION_CLASSNODE);
if (annotations.isEmpty())
continue;
if (hasClosureMember(annotation)) {
notCopied.add(annotation);
continue;
}
AnnotationNode retentionPolicyAnnotation = annotations.get(0);
Expression valueExpression = retentionPolicyAnnotation.getMember("value");
if (!(valueExpression instanceof PropertyExpression))
continue;
PropertyExpression propertyExpression = (PropertyExpression) valueExpression;
boolean processAnnotation = propertyExpression.getProperty() instanceof ConstantExpression && ("RUNTIME".equals(((ConstantExpression) (propertyExpression.getProperty())).getValue()) || "CLASS".equals(((ConstantExpression) (propertyExpression.getProperty())).getValue()));
if (processAnnotation) {
AnnotationNode newAnnotation = new AnnotationNode(annotation.getClassNode());
for (Map.Entry<String, Expression> member : annotation.getMembers().entrySet()) {
newAnnotation.addMember(member.getKey(), member.getValue());
}
newAnnotation.setSourcePosition(annotatedNode);
copied.add(newAnnotation);
}
}
}
use of org.codehaus.groovy.ast.expr.PropertyExpression in project groovy by apache.
the class VariableExpressionTransformer method tryTransformDelegateToProperty.
private static Expression tryTransformDelegateToProperty(VariableExpression expr) {
// we need to transform variable expressions that go to a delegate
// to a property expression, as ACG would loose the information
// in processClassVariable before it reaches any makeCall, that could
// handle it
Object val = expr.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
if (val == null)
return null;
VariableExpression implicitThis = new VariableExpression("this");
PropertyExpression pexp = new PropertyExpression(implicitThis, expr.getName());
pexp.copyNodeMetaData(expr);
pexp.setImplicitThis(true);
ClassNode owner = expr.getNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER);
if (owner != null) {
implicitThis.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, owner);
implicitThis.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, val);
}
return pexp;
}
use of org.codehaus.groovy.ast.expr.PropertyExpression 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.PropertyExpression in project groovy by apache.
the class BinaryExpressionHelper method execMethodAndStoreForSubscriptOperator.
private void execMethodAndStoreForSubscriptOperator(int op, String method, Expression expression, VariableSlotLoader usesSubscript, Expression orig) {
final OperandStack operandStack = controller.getOperandStack();
writePostOrPrefixMethod(op, method, expression, orig);
// we need special code for arrays to store the result (like for a[1]++)
if (usesSubscript != null) {
CompileStack compileStack = controller.getCompileStack();
BinaryExpression be = (BinaryExpression) expression;
ClassNode methodResultType = operandStack.getTopOperand();
final int resultIdx = compileStack.defineTemporaryVariable("postfix_" + method, methodResultType, true);
BytecodeExpression methodResultLoader = new VariableSlotLoader(methodResultType, resultIdx, operandStack);
// execute the assignment, this will leave the right side
// (here the method call result) on the stack
assignToArray(be, be.getLeftExpression(), usesSubscript, methodResultLoader);
compileStack.removeVar(resultIdx);
} else // here we handle a.b++ and a++
if (expression instanceof VariableExpression || expression instanceof FieldExpression || expression instanceof PropertyExpression) {
operandStack.dup();
controller.getCompileStack().pushLHS(true);
expression.visit(controller.getAcg());
controller.getCompileStack().popLHS();
}
// other cases don't need storing, so nothing to be done for them
}
Aggregations