Search in sources :

Example 21 with Parameter

use of org.codehaus.groovy.ast.Parameter in project groovy-core by groovy.

the class TraitTypeCheckingExtension method handleMissingMethod.

@Override
public List<MethodNode> handleMissingMethod(final ClassNode receiver, final String name, final ArgumentListExpression argumentList, final ClassNode[] argumentTypes, final MethodCall call) {
    String[] decomposed = Traits.decomposeSuperCallName(name);
    if (decomposed != null) {
        return convertToDynamicCall(call, receiver, decomposed, argumentTypes);
    }
    if (call instanceof MethodCallExpression) {
        MethodCallExpression mce = (MethodCallExpression) call;
        if (mce.getReceiver() instanceof VariableExpression) {
            VariableExpression var = (VariableExpression) mce.getReceiver();
            // GROOVY-7322
            // static method call in trait?
            ClassNode type = null;
            if (isStaticTraitReceiver(receiver, var)) {
                type = receiver.getGenericsTypes()[0].getType();
            } else if (isThisTraitReceiver(var)) {
                type = receiver;
            }
            if (type != null && Traits.isTrait(type)) {
                ClassNode helper = Traits.findHelper(type);
                Parameter[] params = new Parameter[argumentTypes.length + 1];
                params[0] = new Parameter(ClassHelper.CLASS_Type.getPlainNodeReference(), "staticSelf");
                for (int i = 1; i < params.length; i++) {
                    params[i] = new Parameter(argumentTypes[i - 1], "p" + i);
                }
                MethodNode method = helper.getDeclaredMethod(name, params);
                if (method != null) {
                    return Collections.singletonList(makeDynamic(call, method.getReturnType()));
                }
            }
        }
        ClassNode dynamic = mce.getNodeMetaData(TraitASTTransformation.DO_DYNAMIC);
        if (dynamic != null) {
            return Collections.singletonList(makeDynamic(call, dynamic));
        }
    }
    return NOTFOUND;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 22 with Parameter

use of org.codehaus.groovy.ast.Parameter in project groovy-core by groovy.

the class SuperCallTraitTransformer method transformBinaryExpression.

private Expression transformBinaryExpression(final BinaryExpression exp) {
    Expression trn = super.transform(exp);
    if (trn instanceof BinaryExpression) {
        BinaryExpression bin = (BinaryExpression) trn;
        Expression leftExpression = bin.getLeftExpression();
        if (bin.getOperation().getType() == Types.EQUAL && leftExpression instanceof PropertyExpression) {
            ClassNode traitReceiver = ((PropertyExpression) leftExpression).getObjectExpression().getNodeMetaData(SuperCallTraitTransformer.class);
            if (traitReceiver != null) {
                // A.super.foo = ...
                TraitHelpersTuple helpers = Traits.findHelpers(traitReceiver);
                ClassNode helper = helpers.getHelper();
                String setterName = MetaProperty.getSetterName(((PropertyExpression) leftExpression).getPropertyAsString());
                List<MethodNode> methods = helper.getMethods(setterName);
                for (MethodNode method : methods) {
                    Parameter[] parameters = method.getParameters();
                    if (parameters.length == 2 && parameters[0].getType().equals(traitReceiver)) {
                        ArgumentListExpression args = new ArgumentListExpression(new VariableExpression("this"), transform(exp.getRightExpression()));
                        MethodCallExpression setterCall = new MethodCallExpression(new ClassExpression(helper), setterName, args);
                        setterCall.setMethodTarget(method);
                        setterCall.setImplicitThis(false);
                        return setterCall;
                    }
                }
                return bin;
            }
        }
    }
    return trn;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Parameter(org.codehaus.groovy.ast.Parameter) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 23 with Parameter

use of org.codehaus.groovy.ast.Parameter in project groovy-core by groovy.

the class Java5 method configureClassNode.

public void configureClassNode(CompileUnit compileUnit, ClassNode classNode) {
    try {
        Class clazz = classNode.getTypeClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field f : fields) {
            ClassNode ret = makeClassNode(compileUnit, f.getGenericType(), f.getType());
            FieldNode fn = new FieldNode(f.getName(), f.getModifiers(), ret, classNode, null);
            setAnnotationMetaData(f.getAnnotations(), fn);
            classNode.addField(fn);
        }
        Method[] methods = clazz.getDeclaredMethods();
        for (Method m : methods) {
            ClassNode ret = makeClassNode(compileUnit, m.getGenericReturnType(), m.getReturnType());
            Parameter[] params = makeParameters(compileUnit, m.getGenericParameterTypes(), m.getParameterTypes(), m.getParameterAnnotations());
            ClassNode[] exceptions = makeClassNodes(compileUnit, m.getGenericExceptionTypes(), m.getExceptionTypes());
            MethodNode mn = new MethodNode(m.getName(), m.getModifiers(), ret, params, exceptions, null);
            mn.setSynthetic(m.isSynthetic());
            setMethodDefaultValue(mn, m);
            setAnnotationMetaData(m.getAnnotations(), mn);
            mn.setGenericsTypes(configureTypeVariable(m.getTypeParameters()));
            classNode.addMethod(mn);
        }
        Constructor[] constructors = clazz.getDeclaredConstructors();
        for (Constructor ctor : constructors) {
            Parameter[] params = makeParameters(compileUnit, ctor.getGenericParameterTypes(), ctor.getParameterTypes(), ctor.getParameterAnnotations());
            ClassNode[] exceptions = makeClassNodes(compileUnit, ctor.getGenericExceptionTypes(), ctor.getExceptionTypes());
            classNode.addConstructor(ctor.getModifiers(), params, exceptions, null);
        }
        Class sc = clazz.getSuperclass();
        if (sc != null)
            classNode.setUnresolvedSuperClass(makeClassNode(compileUnit, clazz.getGenericSuperclass(), sc));
        makeInterfaceTypes(compileUnit, classNode, clazz);
        setAnnotationMetaData(classNode.getTypeClass().getAnnotations(), classNode);
        PackageNode packageNode = classNode.getPackage();
        if (packageNode != null) {
            setAnnotationMetaData(classNode.getTypeClass().getPackage().getAnnotations(), packageNode);
        }
    } catch (NoClassDefFoundError e) {
        throw new NoClassDefFoundError("Unable to load class " + classNode.toString(false) + " due to missing dependency " + e.getMessage());
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) MethodNode(org.codehaus.groovy.ast.MethodNode) Parameter(org.codehaus.groovy.ast.Parameter) PackageNode(org.codehaus.groovy.ast.PackageNode)

Example 24 with Parameter

use of org.codehaus.groovy.ast.Parameter in project groovy-core by groovy.

the class Java5 method makeParameter.

private Parameter makeParameter(CompileUnit cu, Type type, Class cl, Annotation[] annotations, int idx) {
    ClassNode cn = makeClassNode(cu, type, cl);
    Parameter parameter = new Parameter(cn, "param" + idx);
    setAnnotationMetaData(annotations, parameter);
    return parameter;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) Parameter(org.codehaus.groovy.ast.Parameter)

Example 25 with Parameter

use of org.codehaus.groovy.ast.Parameter in project grails-core by grails.

the class DefaultASTValidateableHelper method getPropertiesToEnsureConstraintsFor.

/**
     * Retrieves a Map describing all of the properties which need to be constrained for the class
     * represented by classNode.  The keys in the Map will be property names and the values are the
     * type of the corresponding property.
     * 
     * @param classNode the class to inspect
     * @return a Map describing all of the properties which need to be constrained
     */
protected Map<String, ClassNode> getPropertiesToEnsureConstraintsFor(final ClassNode classNode) {
    final Map<String, ClassNode> fieldsToConstrain = new HashMap<String, ClassNode>();
    final List<FieldNode> allFields = classNode.getFields();
    for (final FieldNode field : allFields) {
        if (!field.isStatic()) {
            final PropertyNode property = classNode.getProperty(field.getName());
            if (property != null) {
                fieldsToConstrain.put(field.getName(), field.getType());
            }
        }
    }
    final Map<String, MethodNode> declaredMethodsMap = classNode.getDeclaredMethodsMap();
    for (Entry<String, MethodNode> methodEntry : declaredMethodsMap.entrySet()) {
        final MethodNode value = methodEntry.getValue();
        if (!value.isStatic() && value.isPublic() && classNode.equals(value.getDeclaringClass()) && value.getLineNumber() > 0) {
            Parameter[] parameters = value.getParameters();
            if (parameters == null || parameters.length == 0) {
                final String methodName = value.getName();
                if (methodName.startsWith("get")) {
                    final ClassNode returnType = value.getReturnType();
                    final String restOfMethodName = methodName.substring(3);
                    final String propertyName = GrailsNameUtils.getPropertyName(restOfMethodName);
                    fieldsToConstrain.put(propertyName, returnType);
                }
            }
        }
    }
    final ClassNode superClass = classNode.getSuperClass();
    if (!superClass.equals(new ClassNode(Object.class))) {
        fieldsToConstrain.putAll(getPropertiesToEnsureConstraintsFor(superClass));
    }
    return fieldsToConstrain;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodNode(org.codehaus.groovy.ast.MethodNode) HashMap(java.util.HashMap) PropertyNode(org.codehaus.groovy.ast.PropertyNode) Parameter(org.codehaus.groovy.ast.Parameter)

Aggregations

Parameter (org.codehaus.groovy.ast.Parameter)221 ClassNode (org.codehaus.groovy.ast.ClassNode)135 MethodNode (org.codehaus.groovy.ast.MethodNode)79 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)70 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)67 FieldNode (org.codehaus.groovy.ast.FieldNode)49 Expression (org.codehaus.groovy.ast.expr.Expression)47 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)47 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)44 Statement (org.codehaus.groovy.ast.stmt.Statement)34 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)33 ArrayList (java.util.ArrayList)32 LinkedList (java.util.LinkedList)32 ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)30 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)29 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)26 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)26 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)26 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)25 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)25