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

The following examples show how to use org.codehaus.groovy.ast.MethodNode#isFinal() . 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: GroovyNodeToStringUtils.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public static String methodToString(MethodNode methodNode, ASTNodeVisitor ast) {
	if (methodNode instanceof ConstructorNode) {
		return constructorToString((ConstructorNode) methodNode, ast);
	}
	StringBuilder builder = new StringBuilder();
	if (methodNode.isPublic()) {
		if (!methodNode.isSyntheticPublic()) {
			builder.append("public ");
		}
	} else if (methodNode.isProtected()) {
		builder.append("protected ");
	} else if (methodNode.isPrivate()) {
		builder.append("private ");
	}

	if (methodNode.isStatic()) {
		builder.append("static ");
	}

	if (methodNode.isFinal()) {
		builder.append("final ");
	}
	ClassNode returnType = methodNode.getReturnType();
	builder.append(returnType.getNameWithoutPackage());
	builder.append(" ");
	builder.append(methodNode.getName());
	builder.append("(");
	builder.append(parametersToString(methodNode.getParameters(), ast));
	builder.append(")");
	return builder.toString();
}
 
Example 2
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 3
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void checkMethodsForIncorrectModifiers(ClassNode cn) {
    if (!cn.isInterface()) return;
    for (MethodNode method : cn.getMethods()) {
        if (method.isFinal()) {
            addError("The " + getDescription(method) + " from " + getDescription(cn) +
                    " must not be final. It is by definition abstract.", method);
        }
        if (method.isStatic() && !isConstructor(method)) {
            addError("The " + getDescription(method) + " from " + getDescription(cn) +
                    " must not be static. Only fields may be static in an interface.", method);
        }
    }
}
 
Example 4
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void checkMethodsForOverridingFinal(ClassNode cn) {
    for (MethodNode method : cn.getMethods()) {
        Parameter[] params = method.getParameters();
        for (MethodNode superMethod : cn.getSuperClass().getMethods(method.getName())) {
            Parameter[] superParams = superMethod.getParameters();
            if (!hasEqualParameterTypes(params, superParams)) continue;
            if (!superMethod.isFinal()) break;
            addInvalidUseOfFinalError(method, params, superMethod.getDeclaringClass());
            return;
        }
    }
}