use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.
the class Java5 method setMethodDefaultValue.
private static void setMethodDefaultValue(MethodNode mn, Method m) {
Object defaultValue = m.getDefaultValue();
ConstantExpression cExp = ConstantExpression.NULL;
if (defaultValue != null)
cExp = new ConstantExpression(defaultValue);
mn.setCode(new ReturnStatement(cExp));
mn.setAnnotationDefault(true);
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.
the class Java5 method annotationValueToExpression.
private Expression annotationValueToExpression(Object value) {
if (value == null || value instanceof String || value instanceof Number || value instanceof Character || value instanceof Boolean)
return new ConstantExpression(value);
if (value instanceof Class)
return new ClassExpression(ClassHelper.makeWithoutCaching((Class) value));
if (value.getClass().isArray()) {
ListExpression elementExprs = new ListExpression();
int len = Array.getLength(value);
for (int i = 0; i != len; ++i) elementExprs.addExpression(annotationValueToExpression(Array.get(value, i)));
return elementExprs;
}
return null;
}
use of org.codehaus.groovy.ast.expr.ConstantExpression 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.ConstantExpression in project groovy by apache.
the class AnnotationConstantsVisitor method transformConstantExpression.
private static Expression transformConstantExpression(Expression val, ClassNode returnType) {
ClassNode returnWrapperType = ClassHelper.getWrapper(returnType);
if (val instanceof ConstantExpression) {
Expression result = revertType(val, returnWrapperType);
if (result != null) {
return result;
}
return val;
}
if (val instanceof CastExpression) {
CastExpression castExp = (CastExpression) val;
Expression castee = castExp.getExpression();
if (castee instanceof ConstantExpression) {
if (ClassHelper.getWrapper(castee.getType()).isDerivedFrom(returnWrapperType)) {
return castee;
}
Expression result = revertType(castee, returnWrapperType);
if (result != null) {
return result;
}
return castee;
}
}
return val;
}
use of org.codehaus.groovy.ast.expr.ConstantExpression in project groovy by apache.
the class OperandStack method pushDynamicName.
public void pushDynamicName(Expression name) {
if (name instanceof ConstantExpression) {
ConstantExpression ce = (ConstantExpression) name;
Object value = ce.getValue();
if (value instanceof String) {
pushConstant(ce);
return;
}
}
new CastExpression(ClassHelper.STRING_TYPE, name).visit(controller.getAcg());
}
Aggregations