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

The following examples show how to use org.codehaus.groovy.ast.MethodNode#isProtected() . 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: ClassCompletionVerifier.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void checkOverloadingPrivateAndPublic(MethodNode node) {
    if (isConstructor(node)) return;
    boolean hasPrivate = node.isPrivate();
    boolean hasPublic = node.isPublic();
    for (MethodNode method : currentClass.getMethods(node.getName())) {
        if (method == node) continue;
        if (!method.getDeclaringClass().equals(node.getDeclaringClass())) continue;
        if (method.isPublic() || method.isProtected()) {
            hasPublic = true;
        } else {
            hasPrivate = true;
        }
        if (hasPrivate && hasPublic) break;
    }
    if (hasPrivate && hasPublic) {
        addError("Mixing private and public/protected methods of the same name causes multimethods to be disabled and is forbidden to avoid surprising behaviour. Renaming the private methods will solve the problem.", node);
    }
}
 
Example 2
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 3
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void checkInterfaceMethodVisibility(ClassNode node) {
    if (!node.isInterface()) return;
    for (MethodNode method : node.getMethods()) {
        if (method.isPrivate()) {
            addError("Method '" + method.getName() + "' is private but should be public in " + getDescription(currentClass) + ".", method);
        } else if (method.isProtected()) {
            addError("Method '" + method.getName() + "' is protected but should be public in " + getDescription(currentClass) + ".", method);
        }
    }
}
 
Example 4
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void checkMethodForWeakerAccessPrivileges(MethodNode mn, ClassNode cn) {
    if (mn.isPublic()) return;
    Parameter[] params = mn.getParameters();
    for (MethodNode superMethod : cn.getSuperClass().getMethods(mn.getName())) {
        Parameter[] superParams = superMethod.getParameters();
        if (!hasEqualParameterTypes(params, superParams)) continue;
        if ((mn.isPrivate() && !superMethod.isPrivate())
                || (mn.isProtected() && !superMethod.isProtected() && !superMethod.isPackageScope() && !superMethod.isPrivate())
                || (!mn.isPrivate() && !mn.isProtected() && !mn.isPublic() && (superMethod.isPublic() || superMethod.isProtected()))) {
            addWeakerAccessError(cn, mn, params, superMethod);
            return;
        }
    }
}
 
Example 5
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
/**
 * Filter methods according to visibility
 *
 * @param methodNodeList method nodes to filter
 * @param enclosingClassNode the enclosing class
 * @return filtered method nodes
 * @since 3.0.0
 */
public static List<MethodNode> filterMethodsByVisibility(final List<MethodNode> methodNodeList, final ClassNode enclosingClassNode) {
    if (!asBoolean(methodNodeList)) {
        return StaticTypeCheckingVisitor.EMPTY_METHODNODE_LIST;
    }

    List<MethodNode> result = new LinkedList<>();

    boolean isEnclosingInnerClass = enclosingClassNode instanceof InnerClassNode;
    List<ClassNode> outerClasses = enclosingClassNode.getOuterClasses();

    outer:
    for (MethodNode methodNode : methodNodeList) {
        if (methodNode instanceof ExtensionMethodNode) {
            result.add(methodNode);
            continue;
        }

        ClassNode declaringClass = methodNode.getDeclaringClass();

        if (isEnclosingInnerClass) {
            for (ClassNode outerClass : outerClasses) {
                if (outerClass.isDerivedFrom(declaringClass)) {
                    if (outerClass.equals(declaringClass)) {
                        result.add(methodNode);
                        continue outer;
                    } else {
                        if (methodNode.isPublic() || methodNode.isProtected()) {
                            result.add(methodNode);
                            continue outer;
                        }
                    }
                }
            }
        }

        if (declaringClass instanceof InnerClassNode) {
            if (declaringClass.getOuterClasses().contains(enclosingClassNode)) {
                result.add(methodNode);
                continue;
            }
        }

        if (methodNode.isPrivate() && !enclosingClassNode.equals(declaringClass)) {
            continue;
        }
        if (methodNode.isProtected()
                && !enclosingClassNode.isDerivedFrom(declaringClass)
                && !samePackageName(enclosingClassNode, declaringClass)) {
            continue;
        }
        if (methodNode.isPackageScope() && !samePackageName(enclosingClassNode, declaringClass)) {
            continue;
        }

        result.add(methodNode);
    }

    return result;
}
 
Example 6
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to make a direct method call on a bridge method, if it exists.
 */
protected boolean tryBridgeMethod(final MethodNode target, final Expression receiver, final boolean implicitThis, final TupleExpression args, final ClassNode thisClass) {
    ClassNode lookupClassNode;
    if (target.isProtected()) {
        lookupClassNode = controller.getClassNode();
        while (lookupClassNode != null && !lookupClassNode.isDerivedFrom(target.getDeclaringClass())) {
            lookupClassNode = lookupClassNode.getOuterClass();
        }
        if (lookupClassNode == null) {
            return false;
        }
    } else {
        lookupClassNode = target.getDeclaringClass().redirect();
    }
    Map<MethodNode, MethodNode> bridges = lookupClassNode.getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_BRIDGE_METHODS);
    MethodNode bridge = bridges == null ? null : bridges.get(target);
    if (bridge != null) {
        Expression fixedReceiver = receiver;
        if (implicitThis) {
            if (!controller.isInGeneratedFunction()) {
                fixedReceiver = propX(classX(lookupClassNode), "this");
            } else if (thisClass != null) {
                ClassNode current = thisClass.getOuterClass();
                fixedReceiver = varX("thisObject", current);
                // adjust for multiple levels of nesting if needed
                while (current.getOuterClass() != null && !lookupClassNode.equals(current)) {
                    FieldNode thisField = current.getField("this$0");
                    current = current.getOuterClass();
                    if (thisField != null) {
                        fixedReceiver = propX(fixedReceiver, "this$0");
                        fixedReceiver.setType(current);
                    }
                }
            }
        }
        ArgumentListExpression newArgs = args(target.isStatic() ? nullX() : fixedReceiver);
        for (Expression expression : args.getExpressions()) {
            newArgs.addExpression(expression);
        }
        return writeDirectMethodCall(bridge, implicitThis, fixedReceiver, newArgs);
    }
    return false;
}