use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class StaticImportVisitor 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;
Expression constant = findConstant(getField(type, pe.getPropertyAsString()));
if (constant != null)
return constant;
}
} 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 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.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);
}
Aggregations