Search in sources :

Example 81 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.

the class Java5 method configureAnnotation.

private void configureAnnotation(AnnotationNode node, Annotation annotation) {
    Class type = annotation.annotationType();
    if (type == Retention.class) {
        Retention r = (Retention) annotation;
        RetentionPolicy value = r.value();
        setRetentionPolicy(value, node);
        node.setMember("value", new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(RetentionPolicy.class, false)), value.toString()));
    } else if (type == Target.class) {
        Target t = (Target) annotation;
        ElementType[] elements = t.value();
        ListExpression elementExprs = new ListExpression();
        for (ElementType element : elements) {
            elementExprs.addExpression(new PropertyExpression(new ClassExpression(ClassHelper.ELEMENT_TYPE_TYPE), element.name()));
        }
        node.setMember("value", elementExprs);
    } else {
        Method[] declaredMethods;
        try {
            declaredMethods = type.getDeclaredMethods();
        } catch (SecurityException se) {
            declaredMethods = new Method[0];
        }
        for (Method declaredMethod : declaredMethods) {
            try {
                Object value = declaredMethod.invoke(annotation);
                Expression valueExpression = annotationValueToExpression(value);
                if (valueExpression == null)
                    continue;
                node.setMember(declaredMethod.getName(), valueExpression);
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }
    }
}
Also used : ElementType(java.lang.annotation.ElementType) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Method(java.lang.reflect.Method) Retention(java.lang.annotation.Retention) RetentionPolicy(java.lang.annotation.RetentionPolicy) InvocationTargetException(java.lang.reflect.InvocationTargetException) Target(java.lang.annotation.Target) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 82 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.

the class InvokeDynamicWriter method coerce.

@Override
public void coerce(ClassNode from, ClassNode target) {
    ClassNode wrapper = ClassHelper.getWrapper(target);
    makeIndyCall(invokeMethod, EmptyExpression.INSTANCE, false, false, "asType", new ClassExpression(wrapper));
    if (ClassHelper.boolean_TYPE.equals(target) || ClassHelper.Boolean_TYPE.equals(target)) {
        writeIndyCast(ClassHelper.OBJECT_TYPE, target);
    } else {
        BytecodeHelper.doCast(controller.getMethodVisitor(), wrapper);
        controller.getOperandStack().replace(wrapper);
        controller.getOperandStack().doGroovyCast(target);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 83 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.

the class CallSiteWriter method generateCreateCallSiteArray.

private void generateCreateCallSiteArray() {
    List<String> callSiteInitMethods = new LinkedList<String>();
    int index = 0;
    int methodIndex = 0;
    final int size = callSites.size();
    final int maxArrayInit = 5000;
    // create array initialization methods
    while (index < size) {
        methodIndex++;
        String methodName = "$createCallSiteArray_" + methodIndex;
        callSiteInitMethods.add(methodName);
        MethodVisitor mv = controller.getClassVisitor().visitMethod(MOD_PRIVSS, methodName, "([Ljava/lang/String;)V", null, null);
        controller.setMethodVisitor(mv);
        mv.visitCode();
        int methodLimit = size;
        // check if the next block is over the max allowed
        if ((methodLimit - index) > maxArrayInit) {
            methodLimit = index + maxArrayInit;
        }
        for (; index < methodLimit; index++) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitLdcInsn(index);
            mv.visitLdcInsn(callSites.get(index));
            mv.visitInsn(AASTORE);
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    // create base createCallSiteArray method
    MethodVisitor mv = controller.getClassVisitor().visitMethod(MOD_PRIVSS, CREATE_CSA_METHOD, GET_CALLSITEARRAY_DESC, null, null);
    controller.setMethodVisitor(mv);
    mv.visitCode();
    mv.visitLdcInsn(size);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
    mv.visitVarInsn(ASTORE, 0);
    for (String methodName : callSiteInitMethods) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESTATIC, controller.getInternalClassName(), methodName, "([Ljava/lang/String;)V", false);
    }
    mv.visitTypeInsn(NEW, CALLSITE_ARRAY_CLASS);
    mv.visitInsn(DUP);
    controller.getAcg().visitClassExpression(new ClassExpression(controller.getClassNode()));
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, CALLSITE_ARRAY_CLASS, "<init>", "(Ljava/lang/Class;[Ljava/lang/String;)V", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) LinkedList(java.util.LinkedList) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 84 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.

the class StatementMetaTypeChooser method resolveType.

public ClassNode resolveType(final Expression exp, final ClassNode current) {
    if (exp instanceof ClassExpression)
        return ClassHelper.CLASS_Type;
    OptimizingStatementWriter.StatementMeta meta = (OptimizingStatementWriter.StatementMeta) exp.getNodeMetaData(OptimizingStatementWriter.StatementMeta.class);
    ClassNode type = null;
    if (meta != null)
        type = meta.type;
    if (type != null)
        return type;
    if (exp instanceof VariableExpression) {
        VariableExpression ve = (VariableExpression) exp;
        if (ve.isClosureSharedVariable())
            return ve.getType();
        type = ve.getOriginType();
        if (ve.getAccessedVariable() instanceof FieldNode) {
            FieldNode fn = (FieldNode) ve.getAccessedVariable();
            if (!fn.getDeclaringClass().equals(current))
                return fn.getOriginType();
        }
    } else if (exp instanceof Variable) {
        Variable v = (Variable) exp;
        type = v.getOriginType();
    } else {
        type = exp.getType();
    }
    return type.redirect();
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) Variable(org.codehaus.groovy.ast.Variable) FieldNode(org.codehaus.groovy.ast.FieldNode) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 85 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.

the class AbstractASTTransformation method getClassList.

@Deprecated
public List<ClassNode> getClassList(AnnotationNode anno, String name) {
    List<ClassNode> list = new ArrayList<ClassNode>();
    Expression expr = anno.getMember(name);
    if (expr != null && expr instanceof ListExpression) {
        final ListExpression listExpression = (ListExpression) expr;
        for (Expression itemExpr : listExpression.getExpressions()) {
            if (itemExpr != null && itemExpr instanceof ClassExpression) {
                ClassNode cn = itemExpr.getType();
                if (cn != null)
                    list.add(cn);
            }
        }
    } else if (expr != null && expr instanceof ClassExpression) {
        ClassNode cn = expr.getType();
        if (cn != null)
            list.add(cn);
    }
    return list;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArrayList(java.util.ArrayList) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Aggregations

ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)97 Expression (org.codehaus.groovy.ast.expr.Expression)59 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)57 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)55 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)52 ClassNode (org.codehaus.groovy.ast.ClassNode)48 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)41 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)40 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)37 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)33 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)31 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)27 MethodNode (org.codehaus.groovy.ast.MethodNode)18 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)17 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)17 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)17 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)16 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)16 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)16 FieldNode (org.codehaus.groovy.ast.FieldNode)15