Java Code Examples for org.codehaus.groovy.ast.ClassNode#EMPTY_ARRAY

The following examples show how to use org.codehaus.groovy.ast.ClassNode#EMPTY_ARRAY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TemplateASTTransformer.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void createConstructor(final ClassNode classNode) {
    Parameter[] params = new Parameter[]{
            new Parameter(MarkupTemplateEngine.MARKUPTEMPLATEENGINE_CLASSNODE, "engine"),
            new Parameter(ClassHelper.MAP_TYPE.getPlainNodeReference(), "model"),
            new Parameter(ClassHelper.MAP_TYPE.getPlainNodeReference(), "modelTypes"),
            new Parameter(TEMPLATECONFIG_CLASSNODE, "tplConfig")
    };
    List<Expression> vars = new LinkedList<Expression>();
    for (Parameter param : params) {
        vars.add(new VariableExpression(param));
    }
    ExpressionStatement body = new ExpressionStatement(
            new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(vars)));
    ConstructorNode ctor = new ConstructorNode(Opcodes.ACC_PUBLIC, params, ClassNode.EMPTY_ARRAY, body);
    classNode.addConstructor(ctor);
}
 
Example 2
Source File: AbstractExtensionMethodCache.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void accumulate(Map<String, List<MethodNode>> accumulator, boolean isStatic, MethodNode metaMethod,
                               Function<MethodNode, String> mapperFunction) {

    Parameter[] types = metaMethod.getParameters();
    Parameter[] parameters = new Parameter[types.length - 1];
    System.arraycopy(types, 1, parameters, 0, parameters.length);
    ExtensionMethodNode node = new ExtensionMethodNode(
            metaMethod,
            metaMethod.getName(),
            metaMethod.getModifiers(),
            metaMethod.getReturnType(),
            parameters,
            ClassNode.EMPTY_ARRAY, null,
            isStatic);
    node.setGenericsTypes(metaMethod.getGenericsTypes());
    ClassNode declaringClass = types[0].getType();
    node.setDeclaringClass(declaringClass);

    String key = mapperFunction.apply(metaMethod);

    List<MethodNode> nodes = accumulator.computeIfAbsent(key, k -> new ArrayList<>());
    nodes.add(node);
}
 
Example 3
Source File: AbstractTypeCheckingExtension.java    From groovy with Apache License 2.0 6 votes vote down vote up
public MethodNode newMethod(final String name,
                            final Callable<ClassNode> returnType) {
    MethodNode node = new MethodNode(name,
            Opcodes.ACC_PUBLIC,
            ClassHelper.OBJECT_TYPE,
            Parameter.EMPTY_ARRAY,
            ClassNode.EMPTY_ARRAY,
            EmptyStatement.INSTANCE) {
        @Override
        public ClassNode getReturnType() {
            try {
                return returnType.call();
            } catch (Exception e) {
                return super.getReturnType();
            }
        }
    };
    generatedMethods.add(node);
    return node;
}
 
Example 4
Source File: AbstractTypeCheckingExtension.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Used to instruct the type checker that the call is a dynamic method call.
 * Calling this method automatically sets the handled flag to true.
 * @param call the method call which is a dynamic method call
 * @param returnType the expected return type of the dynamic call
 * @return a virtual method node with the same name as the expected call
 */
public MethodNode makeDynamic(MethodCall call, ClassNode returnType) {
    TypeCheckingContext.EnclosingClosure enclosingClosure = context.getEnclosingClosure();
    MethodNode enclosingMethod = context.getEnclosingMethod();
    ((ASTNode)call).putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, returnType);
    if (enclosingClosure!=null) {
        enclosingClosure.getClosureExpression().putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, Boolean.TRUE);
    } else {
        enclosingMethod.putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, Boolean.TRUE);
    }
    setHandled(true);
    if (debug) {
        LOG.info("Turning "+call.getText()+" into a dynamic method call returning "+returnType.toString(false));
    }
    return new MethodNode(call.getMethodAsString(), 0, returnType, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, EmptyStatement.INSTANCE);
}
 
Example 5
Source File: TraitASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static MethodNode createInitMethod(final boolean isStatic, final ClassNode cNode, final ClassNode helper) {
    MethodNode initializer = new MethodNode(
            isStatic?Traits.STATIC_INIT_METHOD:Traits.INIT_METHOD,
            ACC_STATIC | ACC_PUBLIC | ACC_SYNTHETIC,
            ClassHelper.VOID_TYPE,
            new Parameter[]{createSelfParameter(cNode, isStatic)},
            ClassNode.EMPTY_ARRAY,
            new BlockStatement()
    );
    helper.addMethod(initializer);

    // Cannot add static compilation of init method because of GROOVY-7217, see example 2 of test case
    //AnnotationNode an = new AnnotationNode(TraitComposer.COMPILESTATIC_CLASSNODE);
    //initializer.addAnnotation(an);
    //cNode.addTransform(StaticCompileTransformation.class, an);

    return initializer;
}
 
Example 6
Source File: GenericsUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static ClassNode resolveClassNode(final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage, final ClassNode parsedNode) {
    ClassNode dummyClass = new ClassNode("dummy", 0, ClassHelper.OBJECT_TYPE);
    dummyClass.setModule(new ModuleNode(sourceUnit));
    dummyClass.setGenericsTypes(mn.getDeclaringClass().getGenericsTypes());
    MethodNode dummyMN = new MethodNode(
            "dummy",
            0,
            parsedNode,
            Parameter.EMPTY_ARRAY,
            ClassNode.EMPTY_ARRAY,
            EmptyStatement.INSTANCE
    );
    dummyMN.setGenericsTypes(mn.getGenericsTypes());
    dummyClass.addMethod(dummyMN);
    ResolveVisitor visitor = new ResolveVisitor(compilationUnit) {
        @Override
        public void addError(final String msg, final ASTNode expr) {
            sourceUnit.addError(new IncorrectTypeHintException(mn, msg, usage.getLineNumber(), usage.getColumnNumber()));
        }
    };
    visitor.startResolving(dummyClass, sourceUnit);
    return dummyMN.getReturnType();
}
 
Example 7
Source File: EnumCompletionVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Add map and no-arg constructor or mirror those of the superclass (i.e. base enum).
 */
private static void addImplicitConstructors(ClassNode enumClass, boolean aic) {
    if (aic) {
        ClassNode sn = enumClass.getSuperClass();
        List<ConstructorNode> sctors = new ArrayList<ConstructorNode>(sn.getDeclaredConstructors());
        if (sctors.isEmpty()) {
            addMapConstructors(enumClass);
        } else {
            for (ConstructorNode constructorNode : sctors) {
                ConstructorNode init = new ConstructorNode(ACC_PUBLIC, constructorNode.getParameters(), ClassNode.EMPTY_ARRAY, new BlockStatement());
                enumClass.addConstructor(init);
            }
        }
    } else {
        addMapConstructors(enumClass);
    }
}
 
Example 8
Source File: AbstractTypeCheckingExtension.java    From groovy with Apache License 2.0 5 votes vote down vote up
public MethodNode newMethod(final String name, final ClassNode returnType) {
    MethodNode node = new MethodNode(name,
            Opcodes.ACC_PUBLIC,
            returnType,
            Parameter.EMPTY_ARRAY,
            ClassNode.EMPTY_ARRAY,
            EmptyStatement.INSTANCE);
    generatedMethods.add(node);
    return node;
}
 
Example 9
Source File: Verifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void visitGetter(PropertyNode node, Statement getterBlock, int getterModifiers, String getterName) {
    MethodNode getter =
            new MethodNode(getterName, getterModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
    getter.setSynthetic(true);
    addPropertyMethod(getter);
    visitMethod(getter);
}
 
Example 10
Source File: BindableASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a setter method with the given body.
 *
 * @param declaringClass the class to which we will add the setter
 * @param propertyNode          the field to back the setter
 * @param setterName     the name of the setter
 * @param setterBlock    the statement representing the setter block
 */
protected void createSetterMethod(ClassNode declaringClass, PropertyNode propertyNode, String setterName, Statement setterBlock) {
    MethodNode setter = new MethodNode(
            setterName,
            PropertyNodeUtils.adjustPropertyModifiersForMethod(propertyNode),
            ClassHelper.VOID_TYPE,
            params(param(propertyNode.getType(), "value")),
            ClassNode.EMPTY_ARRAY,
            setterBlock);
    setter.setSynthetic(true);
    // add it to the class
    addGeneratedMethod(declaringClass, setter);
}
 
Example 11
Source File: MapConstructorASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static void createNoArgConstructor(ClassNode cNode, int modifiers) {
    Statement body = stmt(ctorX(ClassNode.THIS, args(new MapExpression())));
    ConstructorNode consNode = new ConstructorNode(modifiers, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, body);
    markAsGenerated(cNode, consNode);
    cNode.addConstructor(consNode);
}
 
Example 12
Source File: UnionTypeClassNode.java    From groovy with Apache License 2.0 4 votes vote down vote up
public UnionTypeClassNode(ClassNode... classNodes) {
    super("<UnionType:" + asArrayDescriptor(classNodes) + ">", 0, ClassHelper.OBJECT_TYPE);
    delegates = classNodes == null ? ClassNode.EMPTY_ARRAY : classNodes;
}