Java Code Examples for org.codehaus.groovy.ast.expr.ClassExpression#getType()

The following examples show how to use org.codehaus.groovy.ast.expr.ClassExpression#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: ExpressionUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * The attribute values of annotations must be primitive, String or Enum constants.
 * In various places, such constants can be seen during type resolution but won't be
 * readily accessible in later phases, e.g. they might be embedded into constructor code.
 * This method transforms constants that would appear in annotations early so they aren't lost.
 * Subsequent processing determines whether they are valid, this method simply retains
 * the constant value as a constant expression.
 *
 * @param exp the original expression
 * @return the converted expression
 */
public static Expression transformInlineConstants(final Expression exp) {
    if (exp instanceof PropertyExpression) {
        PropertyExpression pe = (PropertyExpression) exp;
        if (pe.getObjectExpression() instanceof ClassExpression) {
            ClassExpression ce = (ClassExpression) pe.getObjectExpression();
            ClassNode type = ce.getType();
            FieldNode field = ClassNodeUtils.getField(type, pe.getPropertyAsString());
            if (type.isEnum() && field != null && field.isEnum()) return exp;
            Expression constant = findConstant(field);
            if (constant != null) return constant;
        }
    } else if (exp instanceof BinaryExpression) {
        BinaryExpression be = (BinaryExpression) exp;
        be.setLeftExpression(transformInlineConstants(be.getLeftExpression()));
        be.setRightExpression(transformInlineConstants(be.getRightExpression()));
        return be;
    } else if (exp instanceof ListExpression) {
        ListExpression origList = (ListExpression) exp;
        ListExpression newList = new ListExpression();
        boolean changed = false;
        for (Expression e : origList.getExpressions()) {
            Expression transformed = transformInlineConstants(e);
            newList.addExpression(transformed);
            if (transformed != e) changed = true;
        }
        if (changed) {
            newList.setSourcePosition(origList);
            return newList;
        }
        return origList;
    }

    return exp;
}
 
Example 3
Source File: AnnotationCollectorTransform.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static ClassNode getSerializeClass(ClassNode alias) {
    List<AnnotationNode> annotations = alias.getAnnotations(ClassHelper.make(AnnotationCollector.class));
    if (!annotations.isEmpty()) {
        AnnotationNode annotationNode = annotations.get(0);
        Expression member = annotationNode.getMember("serializeClass");
        if (member instanceof ClassExpression) {
            ClassExpression ce = (ClassExpression) member;
            if (!ce.getType().getName().equals(AnnotationCollector.class.getName())) {
                alias = ce.getType();
            }
        }
    }
    return alias;
}
 
Example 4
Source File: CategoryASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static ClassNode getTargetClass(SourceUnit source, AnnotationNode annotation) {
    Expression value = annotation.getMember("value");
    if (!(value instanceof ClassExpression)) {
        //noinspection ThrowableInstanceNeverThrown
        source.getErrorCollector().addErrorAndContinue("@groovy.lang.Category must define 'value' which is the class to apply this category to", annotation, source);
        return null;
    } else {
        ClassExpression ce = (ClassExpression) value;
        return ce.getType();
    }
}
 
Example 5
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitClassExpression(final ClassExpression expression) {
    ClassNode type = expression.getType();
    MethodVisitor mv = controller.getMethodVisitor();
    if (BytecodeHelper.isClassLiteralPossible(type) || BytecodeHelper.isSameCompilationUnit(controller.getClassNode(), type)) {
        if (controller.getClassNode().isInterface()) {
            InterfaceHelperClassNode interfaceClassLoadingClass = controller.getInterfaceClassLoadingClass();
            if (BytecodeHelper.isClassLiteralPossible(interfaceClassLoadingClass)) {
                BytecodeHelper.visitClassLiteral(mv, interfaceClassLoadingClass);
                controller.getOperandStack().push(ClassHelper.CLASS_Type);
                return;
            }
        } else {
            BytecodeHelper.visitClassLiteral(mv, type);
            controller.getOperandStack().push(ClassHelper.CLASS_Type);
            return;
        }
    }
    String staticFieldName = getStaticFieldName(type);
    referencedClasses.put(staticFieldName, type);

    String internalClassName = controller.getInternalClassName();
    if (controller.getClassNode().isInterface()) {
        internalClassName = BytecodeHelper.getClassInternalName(controller.getInterfaceClassLoadingClass());
        mv.visitFieldInsn(GETSTATIC, internalClassName, staticFieldName, "Ljava/lang/Class;");
    } else {
        mv.visitMethodInsn(INVOKESTATIC, internalClassName, "$get$" + staticFieldName, "()Ljava/lang/Class;", false);
    }
    controller.getOperandStack().push(ClassHelper.CLASS_Type);
}