use of org.codehaus.groovy.ast.expr.ClassExpression in project grails-core by grails.
the class DelegateAsyncTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
throw new GroovyBugError("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
}
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode annotationNode = (AnnotationNode) nodes[0];
if (parent instanceof ClassNode) {
Expression value = annotationNode.getMember("value");
if (value instanceof ClassExpression) {
ClassNode targetApi = value.getType().getPlainNodeReference();
ClassNode classNode = (ClassNode) parent;
final String fieldName = '$' + Introspector.decapitalize(targetApi.getNameWithoutPackage());
FieldNode fieldNode = classNode.getField(fieldName);
if (fieldNode == null) {
fieldNode = new FieldNode(fieldName, Modifier.PRIVATE, targetApi, classNode, new ConstructorCallExpression(targetApi, NO_ARGS));
classNode.addField(fieldNode);
}
applyDelegateAsyncTransform(classNode, targetApi, fieldName);
}
} else if (parent instanceof FieldNode) {
FieldNode fieldNode = (FieldNode) parent;
ClassNode targetApi = fieldNode.getType().getPlainNodeReference();
ClassNode classNode = fieldNode.getOwner();
applyDelegateAsyncTransform(classNode, targetApi, fieldNode.getName());
}
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy-core by groovy.
the class JavaStubGenerator method getAnnotationValue.
private String getAnnotationValue(Object memberValue) {
String val = "null";
if (memberValue instanceof ListExpression) {
StringBuilder sb = new StringBuilder("{");
boolean first = true;
ListExpression le = (ListExpression) memberValue;
for (Expression e : le.getExpressions()) {
if (first)
first = false;
else
sb.append(",");
sb.append(getAnnotationValue(e));
}
sb.append("}");
val = sb.toString();
} else if (memberValue instanceof ConstantExpression) {
ConstantExpression ce = (ConstantExpression) memberValue;
Object constValue = ce.getValue();
if (constValue instanceof AnnotationNode) {
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
printAnnotation(out, (AnnotationNode) constValue);
val = writer.toString();
} else if (constValue instanceof Number || constValue instanceof Boolean)
val = constValue.toString();
else
val = "\"" + escapeSpecialChars(constValue.toString()) + "\"";
} else if (memberValue instanceof PropertyExpression || memberValue instanceof VariableExpression) {
// assume must be static class field or enum value or class that Java can resolve
val = ((Expression) memberValue).getText();
} else if (memberValue instanceof ClosureExpression) {
// annotation closure; replaced with this specific class literal to cover the
// case where annotation type uses Class<? extends Closure> for the closure's type
val = "groovy.lang.Closure.class";
} else if (memberValue instanceof ClassExpression) {
val = ((Expression) memberValue).getText() + ".class";
}
return val;
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class MacroInvocationTrap method handleTargetMethodCallExpression.
@Override
protected boolean handleTargetMethodCallExpression(MethodCallExpression macroCall) {
final ClosureExpression closureExpression = getClosureArgument(macroCall);
if (closureExpression == null) {
return true;
}
if (closureExpression.getParameters() != null && closureExpression.getParameters().length > 0) {
addError("Macro closure arguments are not allowed", closureExpression);
return true;
}
final MapExpression mapExpression = buildSubstitutionMap(closureExpression);
String source = convertClosureToSource(closureExpression);
BlockStatement closureBlock = (BlockStatement) closureExpression.getCode();
Boolean asIs = false;
TupleExpression macroArguments = getMacroArguments(macroCall);
if (macroArguments == null) {
return true;
}
List<Expression> macroArgumentsExpressions = macroArguments.getExpressions();
if (macroArgumentsExpressions.size() == 2 || macroArgumentsExpressions.size() == 3) {
Expression asIsArgumentExpression = macroArgumentsExpressions.get(macroArgumentsExpressions.size() - 2);
if ((asIsArgumentExpression instanceof ConstantExpression)) {
ConstantExpression asIsConstantExpression = (ConstantExpression) asIsArgumentExpression;
if (!(asIsConstantExpression.getValue() instanceof Boolean)) {
addError("AsIs argument value should be boolean", asIsConstantExpression);
return true;
}
asIs = (Boolean) asIsConstantExpression.getValue();
}
}
macroArgumentsExpressions.remove(macroArgumentsExpressions.size() - 1);
macroArgumentsExpressions.add(new ConstantExpression(source));
macroArgumentsExpressions.add(mapExpression);
macroArgumentsExpressions.add(new ClassExpression(ClassHelper.makeWithoutCaching(MacroBuilder.getMacroValue(closureBlock, asIs).getClass(), false)));
macroCall.setObjectExpression(new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"));
macroCall.setSpreadSafe(false);
macroCall.setSafe(false);
macroCall.setImplicitThis(false);
return true;
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class AnnotationVisitor method transformInlineConstants.
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() || !type.isResolved())
return exp;
try {
Field field = type.getTypeClass().getField(pe.getPropertyAsString());
if (field != null && Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) {
return new ConstantExpression(field.get(null));
}
} catch (Exception e) {
// ignore, leave property expression in place and we'll report later
}
}
} else if (exp instanceof ListExpression) {
ListExpression le = (ListExpression) exp;
ListExpression result = new ListExpression();
for (Expression e : le.getExpressions()) {
result.addExpression(transformInlineConstants(e));
}
return result;
}
return exp;
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class AnnotationVisitor method visitExpression.
protected void visitExpression(String attrName, Expression attrExp, ClassNode attrType) {
if (attrType.isArray()) {
// check needed as @Test(attr = {"elem"}) passes through the parser
if (attrExp instanceof ListExpression) {
ListExpression le = (ListExpression) attrExp;
visitListExpression(attrName, le, attrType.getComponentType());
} else if (attrExp instanceof ClosureExpression) {
addError("Annotation list attributes must use Groovy notation [el1, el2]", attrExp);
} else {
// treat like a singleton list as per Java
ListExpression listExp = new ListExpression();
listExp.addExpression(attrExp);
if (annotation != null) {
annotation.setMember(attrName, listExp);
}
visitExpression(attrName, listExp, attrType);
}
} else if (ClassHelper.isPrimitiveType(attrType)) {
visitConstantExpression(attrName, getConstantExpression(attrExp, attrType), ClassHelper.getWrapper(attrType));
} else if (ClassHelper.STRING_TYPE.equals(attrType)) {
visitConstantExpression(attrName, getConstantExpression(attrExp, attrType), ClassHelper.STRING_TYPE);
} else if (ClassHelper.CLASS_Type.equals(attrType)) {
if (!(attrExp instanceof ClassExpression || attrExp instanceof ClosureExpression)) {
addError("Only classes and closures can be used for attribute '" + attrName + "'", attrExp);
}
} else if (attrType.isDerivedFrom(ClassHelper.Enum_Type)) {
if (attrExp instanceof PropertyExpression) {
visitEnumExpression(attrName, (PropertyExpression) attrExp, attrType);
} else {
addError("Expected enum value for attribute " + attrName, attrExp);
}
} else if (isValidAnnotationClass(attrType)) {
if (attrExp instanceof AnnotationConstantExpression) {
visitAnnotationExpression(attrName, (AnnotationConstantExpression) attrExp, attrType);
} else {
addError("Expected annotation of type '" + attrType.getName() + "' for attribute " + attrName, attrExp);
}
} else {
addError("Unexpected type " + attrType.getName(), attrExp);
}
}
Aggregations