Java Code Examples for org.codehaus.groovy.ast.MethodNode#setModifiers()

The following examples show how to use org.codehaus.groovy.ast.MethodNode#setModifiers() . 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: TraitASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private MethodNode processMethod(ClassNode traitClass, ClassNode traitHelperClass, MethodNode methodNode, ClassNode fieldHelper, Collection<String> knownFields) {
    Parameter[] initialParams = methodNode.getParameters();
    Parameter[] newParams = new Parameter[initialParams.length + 1];
    newParams[0] = createSelfParameter(traitClass, methodNode.isStatic());
    System.arraycopy(initialParams, 0, newParams, 1, initialParams.length);
    final int mod = methodNode.isPrivate() ? ACC_PRIVATE : ACC_PUBLIC | (methodNode.isFinal() ? ACC_FINAL : 0);
    MethodNode mNode = new MethodNode(
            methodNode.getName(),
            mod | ACC_STATIC,
            methodNode.getReturnType(),
            newParams,
            methodNode.getExceptions(),
            processBody(new VariableExpression(newParams[0]), methodNode.getCode(), traitClass, traitHelperClass, fieldHelper, knownFields)
    );
    mNode.setSourcePosition(methodNode);
    mNode.addAnnotations(filterAnnotations(methodNode.getAnnotations()));
    mNode.setGenericsTypes(methodNode.getGenericsTypes());
    if (methodNode.isAbstract()) {
        mNode.setModifiers(ACC_PUBLIC | ACC_ABSTRACT);
    } else {
        methodNode.addAnnotation(new AnnotationNode(Traits.IMPLEMENTED_CLASSNODE));
    }
    methodNode.setCode(null);

    if (!methodNode.isPrivate() && !methodNode.isStatic()) {
        methodNode.setModifiers(ACC_PUBLIC | ACC_ABSTRACT);
    }
    return mNode;
}
 
Example 2
Source File: Verifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void addPropertyMethod(MethodNode method) {
    classNode.addMethod(method);
    markAsGenerated(classNode, method);
    // GROOVY-4415 / GROOVY-4645: check that there's no abstract method which corresponds to this one
    String methodName = method.getName();
    Parameter[] parameters = method.getParameters();
    ClassNode methodReturnType = method.getReturnType();
    for (MethodNode node : classNode.getAbstractMethods()) {
        if (!node.getDeclaringClass().equals(classNode)) continue;
        if (node.getName().equals(methodName) && node.getParameters().length == parameters.length) {
            if (parameters.length == 1) {
                // setter
                ClassNode abstractMethodParameterType = node.getParameters()[0].getType();
                ClassNode methodParameterType = parameters[0].getType();
                if (!methodParameterType.isDerivedFrom(abstractMethodParameterType) && !methodParameterType.implementsInterface(abstractMethodParameterType)) {
                    continue;
                }
            }
            ClassNode nodeReturnType = node.getReturnType();
            if (!methodReturnType.isDerivedFrom(nodeReturnType) && !methodReturnType.implementsInterface(nodeReturnType)) {
                continue;
            }
            // matching method, remove abstract status and use the same body
            node.setModifiers(node.getModifiers() ^ ACC_ABSTRACT);
            node.setCode(method.getCode());
        }
    }
}
 
Example 3
Source File: ExtendedVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void visitDeprecation(AnnotatedNode node, AnnotationNode visited) {
    if (visited.getClassNode().isResolved() && visited.getClassNode().getName().equals("java.lang.Deprecated")) {
        if (node instanceof MethodNode) {
            MethodNode mn = (MethodNode) node;
            mn.setModifiers(mn.getModifiers() | Opcodes.ACC_DEPRECATED);
        } else if (node instanceof FieldNode) {
            FieldNode fn = (FieldNode) node;
            fn.setModifiers(fn.getModifiers() | Opcodes.ACC_DEPRECATED);
        } else if (node instanceof ClassNode) {
            ClassNode cn = (ClassNode) node;
            cn.setModifiers(cn.getModifiers() | Opcodes.ACC_DEPRECATED);
        }
    }
}
 
Example 4
Source File: InitializerStrategy.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void createBuilderForAnnotatedMethod(BuilderASTTransformation transform, MethodNode mNode, AnnotationNode anno, boolean useSetters) {
    if (transform.getMemberValue(anno, "includes") != null || transform.getMemberValue(anno, "excludes") != null) {
        transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME +
                " processing: includes/excludes only allowed on classes", anno);
    }
    if (mNode instanceof ConstructorNode) {
        mNode.setModifiers(ACC_PRIVATE);
    } else {
        if (!mNode.isStatic()) {
            transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME +
                    " processing: method builders only allowed on static methods", anno);
        }
        mNode.setModifiers(ACC_SYNTHETIC | ACC_PRIVATE | ACC_STATIC);
    }
    ClassNode buildee = mNode.getDeclaringClass();
    Parameter[] parameters = mNode.getParameters();
    if (parameters.length == 0) {
        transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME +
                " processing: at least one parameter is required for this strategy", anno);
    }
    ClassNode builder = createInnerHelperClass(buildee, getBuilderClassName(buildee, anno), parameters.length);
    List<FieldNode> convertedFields = convertParamsToFields(builder, parameters);

    buildCommon(buildee, anno, convertedFields, builder);
    if (mNode instanceof ConstructorNode) {
        createBuildeeConstructors(transform, buildee, builder, convertedFields, false, useSetters);
    } else {
        createBuildeeMethods(buildee, mNode, builder, convertedFields);
    }
}
 
Example 5
Source File: PackageScopeASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static void revertVisibility(MethodNode mNode) {
    mNode.setModifiers(mNode.getModifiers() & ~ACC_PUBLIC);
}