Search in sources :

Example 76 with ClassNode

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

the class AbstractGrailsArtefactTransformer method performStaticImplementationInjection.

protected void performStaticImplementationInjection(ClassNode classNode, Map<String, ClassNode> genericsPlaceholders, Class staticImplementation) {
    ClassNode staticImplementationNode = GrailsASTUtils.replaceGenericsPlaceholders(ClassHelper.make(staticImplementation), genericsPlaceholders);
    final List<MethodNode> declaredMethods = staticImplementationNode.getMethods();
    final String staticImplementationSimpleName = staticImplementation.getSimpleName();
    String apiInstanceProperty = STATIC_PREFIX + staticImplementationSimpleName;
    final String lookupMethodName = CURRENT_PREFIX + staticImplementationSimpleName;
    if (!requiresStaticLookupMethod()) {
        final ConstructorCallExpression constructorCallExpression = new ConstructorCallExpression(staticImplementationNode, ZERO_ARGS);
        addApiLookupFieldAndSetter(classNode, staticImplementationNode, apiInstanceProperty, constructorCallExpression);
    }
    MethodNode lookupMethod = createStaticLookupMethod(classNode, staticImplementationNode, apiInstanceProperty, lookupMethodName);
    MethodCallExpression apiLookupMethod = new MethodCallExpression(new ClassExpression(classNode), lookupMethodName, ZERO_ARGS);
    apiLookupMethod.setMethodTarget(lookupMethod);
    for (MethodNode declaredMethod : declaredMethods) {
        if (isStaticCandidateMethod(classNode, declaredMethod)) {
            addDelegateStaticMethod(classNode, apiLookupMethod, declaredMethod, genericsPlaceholders);
        }
    }
}
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) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 77 with ClassNode

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

the class AbstractGrailsArtefactTransformer method populateAutowiredApiLookupMethod.

protected MethodNode populateAutowiredApiLookupMethod(ClassNode classNode, ClassNode implementationNode, String apiProperty, String methodName, BlockStatement methodBody) {
    addApiLookupFieldAndSetter(classNode, implementationNode, apiProperty, null);
    VariableExpression apiVar = new VariableExpression(apiProperty, implementationNode);
    BlockStatement ifBlock = new BlockStatement();
    ArgumentListExpression arguments = new ArgumentListExpression();
    arguments.addExpression(new ConstantExpression("Method on class [" + classNode + "] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly."));
    ifBlock.addStatement(new ThrowStatement(new ConstructorCallExpression(new ClassNode(IllegalStateException.class), arguments)));
    BlockStatement elseBlock = new BlockStatement();
    elseBlock.addStatement(new ReturnStatement(apiVar));
    methodBody.addStatement(new IfStatement(new BooleanExpression(new BinaryExpression(apiVar, GrailsASTUtils.EQUALS_OPERATOR, GrailsASTUtils.NULL_EXPRESSION)), ifBlock, elseBlock));
    MethodNode methodNode = new MethodNode(methodName, PUBLIC_STATIC_MODIFIER, implementationNode, ZERO_PARAMETERS, null, methodBody);
    return methodNode;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement)

Example 78 with ClassNode

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

the class DefaultGrailsDomainClassInjector method createPropertiesForBelongsToOrHasOneExpression.

private Collection<PropertyNode> createPropertiesForBelongsToOrHasOneExpression(Expression e, ClassNode classNode) {
    List<PropertyNode> properties = new ArrayList<>();
    if (e instanceof MapExpression) {
        MapExpression me = (MapExpression) e;
        for (MapEntryExpression mme : me.getMapEntryExpressions()) {
            String key = mme.getKeyExpression().getText();
            final Expression expression = mme.getValueExpression();
            ClassNode type;
            if (expression instanceof ClassExpression) {
                type = expression.getType();
            } else {
                type = ClassHelper.make(expression.getText());
            }
            properties.add(new PropertyNode(key, Modifier.PUBLIC, type, classNode, null, null, null));
        }
    }
    return properties;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList)

Example 79 with ClassNode

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

the class DefaultGrailsDomainClassInjector method injectVersionProperty.

private void injectVersionProperty(ClassNode classNode) {
    final boolean hasVersion = GrailsASTUtils.hasOrInheritsProperty(classNode, GormProperties.VERSION);
    if (!hasVersion) {
        ClassNode parent = GrailsASTUtils.getFurthestUnresolvedParent(classNode);
        parent.addProperty(GormProperties.VERSION, Modifier.PUBLIC, new ClassNode(Long.class), null, null, null);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode)

Example 80 with ClassNode

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

the class TraitInjectionUtils method addTrait.

private static boolean addTrait(ClassNode classNode, Class trait) {
    boolean traitsAdded = false;
    boolean implementsTrait = false;
    boolean traitNotLoaded = false;
    ClassNode traitClassNode = ClassHelper.make(trait);
    try {
        implementsTrait = classNode.declaresInterface(traitClassNode);
    } catch (Throwable e) {
        // if we reach this point, the trait injector could not be loaded due to missing dependencies (for example missing servlet-api). This is ok, as we want to be able to compile against non-servlet environments.
        traitNotLoaded = true;
    }
    if (!implementsTrait && !traitNotLoaded) {
        final GenericsType[] genericsTypes = traitClassNode.getGenericsTypes();
        final Map<String, ClassNode> parameterNameToParameterValue = new LinkedHashMap<String, ClassNode>();
        if (genericsTypes != null) {
            for (GenericsType gt : genericsTypes) {
                parameterNameToParameterValue.put(gt.getName(), classNode);
            }
        }
        classNode.addInterface(GrailsASTUtils.replaceGenericsPlaceholders(traitClassNode, parameterNameToParameterValue, classNode));
        traitsAdded = true;
    }
    return traitsAdded;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

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