Search in sources :

Example 26 with GroovyBugError

use of org.codehaus.groovy.GroovyBugError in project gcontracts by andresteingress.

the class AnnotationProcessorVisitor method createAnnotationProcessor.

private AnnotationProcessor createAnnotationProcessor(AnnotationNode annotationNode) {
    ClassExpression annotationProcessingAnno = null;
    List<AnnotationNode> annotations = annotationNode.getClassNode().redirect().getAnnotations();
    for (AnnotationNode anno : annotations) {
        Class typeClass = anno.getClassNode().getTypeClass();
        if (typeClass.getName().equals("org.gcontracts.annotations.meta.AnnotationProcessorImplementation")) {
            annotationProcessingAnno = (ClassExpression) anno.getMember("value");
            break;
        }
    }
    if (annotationProcessingAnno == null)
        throw new GroovyBugError("Annotation processing class could not be found! This indicates a bug in GContracts, please file an issue!");
    try {
        final Class clz = Class.forName(annotationProcessingAnno.getType().getTypeClass().getName());
        return (AnnotationProcessor) clz.newInstance();
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    } catch (ClassNotFoundException e) {
    }
    throw new GroovyBugError("Annotation processing class could not be instantiated! This indicates a bug in GContracts, please file an issue!");
}
Also used : GroovyBugError(org.codehaus.groovy.GroovyBugError) AnnotationProcessor(org.gcontracts.common.spi.AnnotationProcessor)

Example 27 with GroovyBugError

use of org.codehaus.groovy.GroovyBugError 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());
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 28 with GroovyBugError

use of org.codehaus.groovy.GroovyBugError in project groovy-core by groovy.

the class GeneratorContext method encodeAsValidClassName.

public static String encodeAsValidClassName(String name) {
    final int l = name.length();
    StringBuilder b = null;
    int lastEscape = -1;
    for (int i = 0; i < l; ++i) {
        final int encodeIndex = name.charAt(i) - MIN_ENCODING;
        if (encodeIndex >= 0 && encodeIndex < CHARACTERS_TO_ENCODE.length) {
            if (CHARACTERS_TO_ENCODE[encodeIndex]) {
                if (b == null) {
                    b = new StringBuilder(name.length() + 3);
                    b.append(name, 0, i);
                } else {
                    b.append(name, lastEscape + 1, i);
                }
                b.append('_');
                lastEscape = i;
            }
        }
    }
    if (b == null)
        return name.toString();
    if (lastEscape == -1)
        throw new GroovyBugError("unexpected escape char control flow in " + name);
    b.append(name, lastEscape + 1, l);
    return b.toString();
}
Also used : GroovyBugError(org.codehaus.groovy.GroovyBugError)

Example 29 with GroovyBugError

use of org.codehaus.groovy.GroovyBugError in project groovy-core by groovy.

the class AsmClassGenerator method visitAnnotationDefaultExpression.

void visitAnnotationDefaultExpression(AnnotationVisitor av, ClassNode type, Expression exp) {
    if (exp instanceof ClosureExpression) {
        ClassNode closureClass = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) exp, ACC_PUBLIC);
        Type t = Type.getType(BytecodeHelper.getTypeDescription(closureClass));
        av.visit(null, t);
    } else if (type.isArray()) {
        ListExpression list = (ListExpression) exp;
        AnnotationVisitor avl = av.visitArray(null);
        ClassNode componentType = type.getComponentType();
        for (Expression lExp : list.getExpressions()) {
            visitAnnotationDefaultExpression(avl, componentType, lExp);
        }
    } else if (ClassHelper.isPrimitiveType(type) || type.equals(ClassHelper.STRING_TYPE)) {
        ConstantExpression constExp = (ConstantExpression) exp;
        av.visit(null, constExp.getValue());
    } else if (ClassHelper.CLASS_Type.equals(type)) {
        ClassNode clazz = exp.getType();
        Type t = Type.getType(BytecodeHelper.getTypeDescription(clazz));
        av.visit(null, t);
    } else if (type.isDerivedFrom(ClassHelper.Enum_Type)) {
        PropertyExpression pExp = (PropertyExpression) exp;
        ClassExpression cExp = (ClassExpression) pExp.getObjectExpression();
        String desc = BytecodeHelper.getTypeDescription(cExp.getType());
        String name = pExp.getPropertyAsString();
        av.visitEnum(null, desc, name);
    } else if (type.implementsInterface(ClassHelper.Annotation_TYPE)) {
        AnnotationConstantExpression avExp = (AnnotationConstantExpression) exp;
        AnnotationNode value = (AnnotationNode) avExp.getValue();
        AnnotationVisitor avc = av.visitAnnotation(null, BytecodeHelper.getTypeDescription(avExp.getType()));
        visitAnnotationAttributes(value, avc);
    } else {
        throw new GroovyBugError("unexpected annotation type " + type.getName());
    }
    av.visitEnd();
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) Type(org.objectweb.asm.Type) GenericsType(org.codehaus.groovy.ast.GenericsType) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor)

Example 30 with GroovyBugError

use of org.codehaus.groovy.GroovyBugError in project groovy-core by groovy.

the class Expression method transformExpressions.

/**
     * Transforms the list of expressions, and checks that all transformed expressions have the given type.
     *
     * @return a new list of transformed expressions
     */
protected <T extends Expression> List<T> transformExpressions(List<? extends Expression> expressions, ExpressionTransformer transformer, Class<T> transformedType) {
    List<T> list = new ArrayList<T>(expressions.size());
    for (Expression expr : expressions) {
        Expression transformed = transformer.transform(expr);
        if (!transformedType.isInstance(transformed))
            throw new GroovyBugError(String.format("Transformed expression should have type %s but has type %s", transformedType, transformed.getClass()));
        list.add(transformedType.cast(transformed));
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) GroovyBugError(org.codehaus.groovy.GroovyBugError)

Aggregations

GroovyBugError (org.codehaus.groovy.GroovyBugError)71 ClassNode (org.codehaus.groovy.ast.ClassNode)31 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)11 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)11 MethodNode (org.codehaus.groovy.ast.MethodNode)11 GenericsType (org.codehaus.groovy.ast.GenericsType)9 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)8 FieldNode (org.codehaus.groovy.ast.FieldNode)8 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)8 MethodVisitor (org.objectweb.asm.MethodVisitor)8 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)7 Closure (groovy.lang.Closure)6 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 LinkedList (java.util.LinkedList)5 Expression (org.codehaus.groovy.ast.expr.Expression)5 GroovyClassLoader (groovy.lang.GroovyClassLoader)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Collection (java.util.Collection)4