Search in sources :

Example 71 with ClassNode

use of org.codehaus.groovy.ast.ClassNode 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 72 with ClassNode

use of org.codehaus.groovy.ast.ClassNode 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 73 with ClassNode

use of org.codehaus.groovy.ast.ClassNode in project gradle by gradle.

the class MagicFieldTransform method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    ClassNode clazz = (ClassNode) nodes[1];
    clazz.addField("magicField", Modifier.PUBLIC, ClassHelper.STRING_TYPE, new ConstantExpression("magicValue"));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Example 74 with ClassNode

use of org.codehaus.groovy.ast.ClassNode in project gradle by gradle.

the class MagicInterfaceTransform method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    ClassNode clazz = (ClassNode) nodes[1];
    clazz.addInterface(new ClassNode(Marker.class));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode)

Example 75 with ClassNode

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

the class AbstractGrailsArtefactTransformer method performInstanceImplementationInjection.

protected void performInstanceImplementationInjection(SourceUnit source, ClassNode classNode, Map<String, ClassNode> genericsPlaceholders, Class instanceImplementation) {
    ClassNode implementationNode;
    final ConstructorCallExpression constructorCallExpression;
    try {
        implementationNode = GrailsASTUtils.replaceGenericsPlaceholders(ClassHelper.make(instanceImplementation), genericsPlaceholders);
        constructorCallExpression = GrailsASTUtils.hasZeroArgsConstructor(implementationNode) ? new ConstructorCallExpression(implementationNode, ZERO_ARGS) : null;
    } catch (Throwable e) {
        // this may well be ok, as we want to be able to compile against, for example, non servlet environments. In this case just bail out.
        return;
    }
    String apiInstanceProperty = INSTANCE_PREFIX + instanceImplementation.getSimpleName();
    Expression apiInstance = new VariableExpression(apiInstanceProperty, implementationNode);
    if (requiresStaticLookupMethod()) {
        final String lookupMethodName = CURRENT_PREFIX + instanceImplementation.getSimpleName();
        MethodNode lookupMethod = createStaticLookupMethod(classNode, implementationNode, apiInstanceProperty, lookupMethodName);
        apiInstance = new MethodCallExpression(new ClassExpression(classNode), lookupMethodName, ZERO_ARGS);
        ((MethodCallExpression) apiInstance).setMethodTarget(lookupMethod);
    } else if (requiresAutowiring()) {
        PropertyNode propertyNode = new PropertyNode(apiInstanceProperty, Modifier.PUBLIC, implementationNode, classNode, constructorCallExpression, null, null);
        propertyNode.addAnnotation(AUTO_WIRED_ANNOTATION);
        if (getMarkerAnnotation() != null) {
            propertyNode.addAnnotation(getMarkerAnnotation());
        }
        classNode.addProperty(propertyNode);
    } else {
        FieldNode fieldNode = classNode.getField(apiInstanceProperty);
        if (fieldNode == null || (Modifier.isPrivate(fieldNode.getModifiers()) && !fieldNode.getDeclaringClass().equals(classNode))) {
            fieldNode = new FieldNode(apiInstanceProperty, PRIVATE_STATIC_MODIFIER, implementationNode, classNode, constructorCallExpression);
            classNode.addField(fieldNode);
        }
    }
    while (!implementationNode.equals(AbstractGrailsArtefactTransformer.OBJECT_CLASS)) {
        List<MethodNode> declaredMethods = implementationNode.getMethods();
        for (MethodNode declaredMethod : declaredMethods) {
            if (GrailsASTUtils.isConstructorMethod(declaredMethod)) {
                GrailsASTUtils.addDelegateConstructor(classNode, declaredMethod, genericsPlaceholders);
            } else if (isCandidateInstanceMethod(classNode, declaredMethod)) {
                addDelegateInstanceMethod(classNode, apiInstance, declaredMethod, getMarkerAnnotation(), genericsPlaceholders);
            }
        }
        implementationNode = implementationNode.getSuperClass();
    }
    performInjectionInternal(apiInstanceProperty, source, classNode);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) FieldNode(org.codehaus.groovy.ast.FieldNode) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) 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) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Aggregations

ClassNode (org.codehaus.groovy.ast.ClassNode)869 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)348 MethodNode (org.codehaus.groovy.ast.MethodNode)193 GenericsType (org.codehaus.groovy.ast.GenericsType)154 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)148 Expression (org.codehaus.groovy.ast.expr.Expression)146 Parameter (org.codehaus.groovy.ast.Parameter)135 FieldNode (org.codehaus.groovy.ast.FieldNode)126 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)118 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)117 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)113 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)103 ArrayList (java.util.ArrayList)95 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)92 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)75 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)74 LinkedList (java.util.LinkedList)71 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)71 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)62 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)61