Search in sources :

Example 21 with GroovyBugError

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

the class StaticTypeCheckingVisitor method getSignaturesFromHint.

private List<ClassNode[]> getSignaturesFromHint(final ClosureExpression expression, final MethodNode selectedMethod, final Expression hintClass, final Expression options) {
    // initialize hints
    List<ClassNode[]> closureSignatures = null;
    try {
        ClassLoader transformLoader = getTransformLoader();
        @SuppressWarnings("unchecked") Class<? extends ClosureSignatureHint> hint = (Class<? extends ClosureSignatureHint>) transformLoader.loadClass(hintClass.getText());
        ClosureSignatureHint hintInstance = hint.newInstance();
        closureSignatures = hintInstance.getClosureSignatures(selectedMethod instanceof ExtensionMethodNode ? ((ExtensionMethodNode) selectedMethod).getExtensionMethodNode() : selectedMethod, typeCheckingContext.source, typeCheckingContext.compilationUnit, convertToStringArray(options), expression);
    } catch (ClassNotFoundException e) {
        throw new GroovyBugError(e);
    } catch (InstantiationException e) {
        throw new GroovyBugError(e);
    } catch (IllegalAccessException e) {
        throw new GroovyBugError(e);
    }
    return closureSignatures;
}
Also used : ClosureSignatureHint(groovy.transform.stc.ClosureSignatureHint) GroovyBugError(org.codehaus.groovy.GroovyBugError)

Example 22 with GroovyBugError

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

the class GroovyTypeCheckingExtensionSupport method handleAmbiguousMethods.

@Override
@SuppressWarnings("unchecked")
public List<MethodNode> handleAmbiguousMethods(final List<MethodNode> nodes, final Expression origin) {
    List<Closure> onMethodSelection = eventHandlers.get("handleAmbiguousMethods");
    List<MethodNode> methodList = nodes;
    if (onMethodSelection != null) {
        Iterator<Closure> iterator = onMethodSelection.iterator();
        while (methodList.size() > 1 && iterator.hasNext()) {
            final Closure closure = iterator.next();
            Object result = safeCall(closure, methodList, origin);
            if (result != null) {
                if (result instanceof MethodNode) {
                    methodList = Collections.singletonList((MethodNode) result);
                } else if (result instanceof Collection) {
                    methodList = new LinkedList<MethodNode>((Collection<? extends MethodNode>) result);
                } else {
                    throw new GroovyBugError("Type checking extension returned unexpected method list: " + result);
                }
            }
        }
    }
    return methodList;
}
Also used : Closure(groovy.lang.Closure) MethodNode(org.codehaus.groovy.ast.MethodNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) Collection(java.util.Collection) LinkedList(java.util.LinkedList)

Example 23 with GroovyBugError

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

the class MetaClassImpl method makeReplacementMetaProperty.

private MetaProperty makeReplacementMetaProperty(MetaProperty mp, String propName, boolean isGetter, MetaMethod propertyMethod) {
    if (mp == null) {
        if (isGetter) {
            return new MetaBeanProperty(propName, propertyMethod.getReturnType(), propertyMethod, null);
        } else {
            //isSetter
            return new MetaBeanProperty(propName, propertyMethod.getParameterTypes()[0].getTheClass(), null, propertyMethod);
        }
    }
    if (mp instanceof CachedField) {
        CachedField mfp = (CachedField) mp;
        MetaBeanProperty mbp = new MetaBeanProperty(propName, mfp.getType(), isGetter ? propertyMethod : null, isGetter ? null : propertyMethod);
        mbp.setField(mfp);
        return mbp;
    } else if (mp instanceof MultipleSetterProperty) {
        MultipleSetterProperty msp = (MultipleSetterProperty) mp;
        if (isGetter) {
            msp.setGetter(propertyMethod);
        }
        return msp;
    } else if (mp instanceof MetaBeanProperty) {
        MetaBeanProperty mbp = (MetaBeanProperty) mp;
        if (isGetter) {
            mbp.setGetter(propertyMethod);
            return mbp;
        } else if (mbp.getSetter() == null || mbp.getSetter() == propertyMethod) {
            mbp.setSetter(propertyMethod);
            return mbp;
        } else {
            MultipleSetterProperty msp = new MultipleSetterProperty(propName);
            msp.setField(mbp.getField());
            msp.setGetter(mbp.getGetter());
            return msp;
        }
    } else {
        throw new GroovyBugError("unknown MetaProperty class used. Class is " + mp.getClass());
    }
}
Also used : MultipleSetterProperty(org.codehaus.groovy.runtime.metaclass.MultipleSetterProperty) GroovyBugError(org.codehaus.groovy.GroovyBugError) CachedField(org.codehaus.groovy.reflection.CachedField)

Example 24 with GroovyBugError

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

the class GenericsUtils method applyGenericsContextToPlaceHolders.

/**
     * transforms generics types from an old context to a new context using the given spec. This method assumes
     * all generics types will be placeholders. WARNING: The resulting generics types may or may not be placeholders
     * after the transformation.
     * @param genericsSpec the generics context information spec
     * @param oldPlaceHolders the old placeholders
     * @return the new generics types
     */
public static GenericsType[] applyGenericsContextToPlaceHolders(Map<String, ClassNode> genericsSpec, GenericsType[] oldPlaceHolders) {
    if (oldPlaceHolders == null || oldPlaceHolders.length == 0)
        return oldPlaceHolders;
    if (genericsSpec.isEmpty())
        return oldPlaceHolders;
    GenericsType[] newTypes = new GenericsType[oldPlaceHolders.length];
    for (int i = 0; i < oldPlaceHolders.length; i++) {
        GenericsType old = oldPlaceHolders[i];
        if (!old.isPlaceholder())
            throw new GroovyBugError("Given generics type " + old + " must be a placeholder!");
        ClassNode fromSpec = genericsSpec.get(old.getName());
        if (fromSpec != null) {
            if (fromSpec.isGenericsPlaceHolder()) {
                ClassNode[] upper = new ClassNode[] { fromSpec.redirect() };
                newTypes[i] = new GenericsType(fromSpec, upper, null);
            } else {
                newTypes[i] = new GenericsType(fromSpec);
            }
        } else {
            ClassNode[] upper = old.getUpperBounds();
            ClassNode[] newUpper = upper;
            if (upper != null && upper.length > 0) {
                ClassNode[] upperCorrected = new ClassNode[upper.length];
                for (int j = 0; j < upper.length; j++) {
                    upperCorrected[i] = correctToGenericsSpecRecurse(genericsSpec, upper[j]);
                }
                upper = upperCorrected;
            }
            ClassNode lower = old.getLowerBound();
            ClassNode newLower = correctToGenericsSpecRecurse(genericsSpec, lower);
            if (lower == newLower && upper == newUpper) {
                newTypes[i] = oldPlaceHolders[i];
            } else {
                ClassNode newPlaceHolder = ClassHelper.make(old.getName());
                GenericsType gt = new GenericsType(newPlaceHolder, newUpper, newLower);
                gt.setPlaceholder(true);
                newTypes[i] = gt;
            }
        }
    }
    return newTypes;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType) GroovyBugError(org.codehaus.groovy.GroovyBugError)

Example 25 with GroovyBugError

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

the class FieldASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    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 node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    if (parent instanceof DeclarationExpression) {
        DeclarationExpression de = (DeclarationExpression) parent;
        ClassNode cNode = de.getDeclaringClass();
        if (!cNode.isScript()) {
            addError("Annotation " + MY_TYPE_NAME + " can only be used within a Script.", parent);
            return;
        }
        candidate = de;
        // GROOVY-4548: temp fix to stop CCE until proper support is added
        if (de.isMultipleAssignmentDeclaration()) {
            addError("Annotation " + MY_TYPE_NAME + " not supported with multiple assignment notation.", parent);
            return;
        }
        VariableExpression ve = de.getVariableExpression();
        variableName = ve.getName();
        // set owner null here, it will be updated by addField
        fieldNode = new FieldNode(variableName, ve.getModifiers(), ve.getType(), null, de.getRightExpression());
        fieldNode.setSourcePosition(de);
        cNode.addField(fieldNode);
        // GROOVY-4833 : annotations that are not Groovy transforms should be transferred to the generated field
        // GROOVY-6112 : also copy acceptable Groovy transforms
        final List<AnnotationNode> annotations = de.getAnnotations();
        for (AnnotationNode annotation : annotations) {
            // GROOVY-6337 HACK: in case newly created field is @Lazy
            if (annotation.getClassNode().equals(LAZY_TYPE)) {
                LazyASTTransformation.visitField(annotation, fieldNode);
            }
            final ClassNode annotationClassNode = annotation.getClassNode();
            if (notTransform(annotationClassNode) || acceptableTransform(annotation)) {
                fieldNode.addAnnotation(annotation);
            }
        }
        super.visitClass(cNode);
        // GROOVY-5207 So that Closures can see newly added fields
        // (not super efficient for a very large class with many @Fields but we chose simplicity
        // and understandability of this solution over more complex but efficient alternatives)
        VariableScopeVisitor scopeVisitor = new VariableScopeVisitor(source);
        scopeVisitor.visitClass(cNode);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) GroovyBugError(org.codehaus.groovy.GroovyBugError) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) VariableScopeVisitor(org.codehaus.groovy.classgen.VariableScopeVisitor)

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