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

The following examples show how to use org.codehaus.groovy.ast.MethodNode#isPublic() . 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: AbstractExtensionMethodCache.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void scan(Map<String, List<MethodNode>> accumulator, Iterable<Class> allClasses, boolean isStatic) {
    Predicate<MethodNode> methodFilter = getMethodFilter();
    Function<MethodNode, String> methodMapper = getMethodMapper();

    for (Class dgmLikeClass : allClasses) {
        ClassNode cn = makeWithoutCaching(dgmLikeClass, true);
        for (MethodNode methodNode : cn.getMethods()) {
            if (!(methodNode.isStatic() && methodNode.isPublic()) || methodNode.getParameters().length == 0) continue;
            if (methodFilter.test(methodNode)) continue;

            accumulate(accumulator, isStatic, methodNode, methodMapper);
        }
    }
}
 
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: OptimizingStatementWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static MethodNode selectConstructor(final ClassNode node, final Parameter[] parameters) {
    List<ConstructorNode> ctors = node.getDeclaredConstructors();
    MethodNode result = null;
    for (ConstructorNode ctor : ctors) {
        if (parametersEqual(ctor.getParameters(), parameters)) {
            result = ctor;
            break;
        }
    }
    return (result != null && result.isPublic() ? result : null);
}
 
Example 6
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 7
Source File: InvocationWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected boolean writeDirectMethodCall(final MethodNode target, final boolean implicitThis, final Expression receiver, final TupleExpression args) {
    if (target == null) return false;

    String methodName = target.getName();
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();
    ClassNode declaringClass = target.getDeclaringClass();
    ClassNode classNode = controller.getClassNode();

    MethodVisitor mv = controller.getMethodVisitor();
    int opcode = INVOKEVIRTUAL;
    if (target.isStatic()) {
        opcode = INVOKESTATIC;
    } else if (declaringClass.isInterface()) {
        opcode = INVOKEINTERFACE;
    } else if (target.isPrivate() || AsmClassGenerator.isSuperExpression(receiver)) {
        opcode = INVOKESPECIAL;
    }

    // handle receiver
    int argumentsToRemove = 0;
    if (opcode != INVOKESTATIC) {
        if (receiver != null) {
            // load receiver if not static invocation
            // todo: fix inner class case
            if (implicitThis
                    && classNode.getOuterClass() != null
                    && !classNode.isDerivedFrom(declaringClass)
                    && !classNode.implementsInterface(declaringClass)) {
                // we are calling an outer class method
                compileStack.pushImplicitThis(false);
                if (controller.isInGeneratedFunction()) {
                    new VariableExpression("thisObject").visit(controller.getAcg());
                } else {
                    Expression expr = new PropertyExpression(new ClassExpression(declaringClass), "this");
                    expr.visit(controller.getAcg());
                }
            } else {
                compileStack.pushImplicitThis(implicitThis);
                receiver.visit(controller.getAcg());
            }
            operandStack.doGroovyCast(declaringClass);
            compileStack.popImplicitThis();
            argumentsToRemove += 1;
        } else {
            mv.visitIntInsn(ALOAD,0);
            operandStack.push(classNode);
            argumentsToRemove += 1;
        }
    }

    int stackSize = operandStack.getStackLength();

    String owner = BytecodeHelper.getClassInternalName(declaringClass);
    ClassNode receiverType = receiver != null ? controller.getTypeChooser().resolveType(receiver, classNode) : declaringClass;
    if (opcode == INVOKEVIRTUAL && ClassHelper.OBJECT_TYPE.equals(declaringClass)) {
        // avoid using a narrowed type if the method is defined on object because it can interfere
        // with delegate type inference in static compilation mode and trigger a ClassCastException
        receiverType = declaringClass;
    }
    if (opcode == INVOKEVIRTUAL) {
        if (!receiverType.equals(declaringClass)
                && !ClassHelper.OBJECT_TYPE.equals(declaringClass)
                && !receiverType.isArray()
                && !receiverType.isInterface()
                && !ClassHelper.isPrimitiveType(receiverType) // e.g int.getClass()
                && receiverType.isDerivedFrom(declaringClass)) {

            owner = BytecodeHelper.getClassInternalName(receiverType);
            ClassNode top = operandStack.getTopOperand();
            if (!receiverType.equals(top)) {
                mv.visitTypeInsn(CHECKCAST, owner);
            }
        } else if (target.isPublic()
                && (!receiverType.equals(declaringClass) && !Modifier.isPublic(declaringClass.getModifiers()))
                && receiverType.isDerivedFrom(declaringClass) && !Objects.equals(receiverType.getPackageName(), classNode.getPackageName())) {
            // GROOVY-6962: package private class, public method
            owner = BytecodeHelper.getClassInternalName(receiverType);
        }
    }

    loadArguments(args.getExpressions(), target.getParameters());

    String desc = BytecodeHelper.getMethodDescriptor(target.getReturnType(), target.getParameters());
    mv.visitMethodInsn(opcode, owner, methodName, desc, declaringClass.isInterface());
    ClassNode ret = target.getReturnType().redirect();
    if (ret == ClassHelper.VOID_TYPE) {
        ret = ClassHelper.OBJECT_TYPE;
        mv.visitInsn(ACONST_NULL);
    }
    argumentsToRemove += (operandStack.getStackLength()-stackSize);
    controller.getOperandStack().remove(argumentsToRemove);
    controller.getOperandStack().push(ret);
    return true;
}