Java Code Examples for org.codehaus.groovy.ast.MethodNode#isPrivate()
The following examples show how to use
org.codehaus.groovy.ast.MethodNode#isPrivate() .
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: TraitReceiverTransformer.java From groovy with Apache License 2.0 | 6 votes |
private Expression transformMethodCallOnThis(final MethodCallExpression call) { Expression method = call.getMethod(); Expression arguments = call.getArguments(); if (method instanceof ConstantExpression) { String methodName = method.getText(); List<MethodNode> methods = traitClass.getMethods(methodName); for (MethodNode methodNode : methods) { if (methodName.equals(methodNode.getName()) && methodNode.isPrivate()) { if (inClosure) { return transformPrivateMethodCallOnThisInClosure(call, arguments, methodName); } return transformPrivateMethodCallOnThis(call, arguments, methodName); } } } if (inClosure) { return transformMethodCallOnThisInClosure(call); } return transformMethodCallOnThisFallBack(call, method, arguments); }
Example 2
Source File: ClassCompletionVerifier.java From groovy with Apache License 2.0 | 6 votes |
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 3
Source File: GroovyNodeToStringUtils.java From groovy-language-server with Apache License 2.0 | 5 votes |
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 4
Source File: TraitASTTransformation.java From groovy with Apache License 2.0 | 5 votes |
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 5
Source File: ClassCompletionVerifier.java From groovy with Apache License 2.0 | 5 votes |
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 6
Source File: ClassCompletionVerifier.java From groovy with Apache License 2.0 | 5 votes |
private void checkAbstractMethodVisibility(ClassNode node) { // we only do check abstract classes (including enums), no interfaces or non-abstract classes if (!isAbstract(node.getModifiers()) || isInterface(node.getModifiers())) return; for (MethodNode method : node.getAbstractMethods()) { if (method.isPrivate()) { addError("Method '" + method.getName() + "' from " + getDescription(node) + " must not be private as it is declared as an abstract method.", method); } } }
Example 7
Source File: ClassCompletionVerifier.java From groovy with Apache License 2.0 | 5 votes |
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 8
Source File: StaticTypeCheckingSupport.java From groovy with Apache License 2.0 | 4 votes |
/** * 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 9
Source File: BeanUtils.java From groovy with Apache License 2.0 | 4 votes |
public static void addPseudoProperties(ClassNode origType, ClassNode cNode, List<PropertyNode> result, Set<String> names, boolean includeStatic, boolean includePseudoGetters, boolean includePseudoSetters) { if (!includePseudoGetters && !includePseudoSetters) return; List<MethodNode> methods = cNode.getAllDeclaredMethods(); for (MethodNode mNode : methods) { if (!includeStatic && mNode.isStatic()) continue; if (hasAnnotation(mNode, INTERNAL_TYPE)) continue; String name = mNode.getName(); if ((name.length() <= 3 && !name.startsWith(IS_PREFIX)) || name.equals("getClass") || name.equals("getMetaClass") || name.equals("getDeclaringClass")) { // Optimization: skip invalid propertyNames continue; } if (mNode.getDeclaringClass() != origType && mNode.isPrivate()) { // skip private super methods continue; } int paramCount = mNode.getParameters().length; ClassNode paramType = mNode.getReturnType(); String propName = null; Statement getter = null; Statement setter = null; if (paramCount == 0) { if (includePseudoGetters && name.startsWith(GET_PREFIX)) { // Simple getter propName = decapitalize(name.substring(3)); getter = mNode.getCode(); } else if (includePseudoGetters && name.startsWith(IS_PREFIX) && paramType.equals(ClassHelper.boolean_TYPE)) { // boolean getter propName = decapitalize(name.substring(2)); getter = mNode.getCode(); } } else if (paramCount == 1) { if (includePseudoSetters && name.startsWith(SET_PREFIX)) { // Simple setter propName = decapitalize(name.substring(3)); setter = mNode.getCode(); paramType = mNode.getParameters()[0].getType(); } } if (propName != null) { addIfMissing(cNode, result, names, mNode, paramType, propName, getter, setter); } } }
Example 10
Source File: InvocationWriter.java From groovy with Apache License 2.0 | 4 votes |
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; }
Example 11
Source File: JavaStubGenerator.java From groovy with Apache License 2.0 | 4 votes |
private void printMethod(PrintWriter out, ClassNode clazz, MethodNode methodNode) { if (methodNode.getName().equals("<clinit>")) return; if (methodNode.isPrivate() || !Utilities.isJavaIdentifier(methodNode.getName())) return; if (methodNode.isSynthetic() && methodNode.getName().equals("$getStaticMetaClass")) return; printAnnotations(out, methodNode); if (!isInterfaceOrTrait(clazz)) { int modifiers = methodNode.getModifiers(); if (isDefaultTraitImpl(methodNode)) { modifiers ^= Opcodes.ACC_ABSTRACT; } printModifiers(out, modifiers & ~(clazz.isEnum() ? Opcodes.ACC_ABSTRACT : 0)); } printGenericsBounds(out, methodNode.getGenericsTypes()); out.print(" "); printType(out, methodNode.getReturnType()); out.print(" "); out.print(methodNode.getName()); printParams(out, methodNode); ClassNode[] exceptions = methodNode.getExceptions(); printExceptions(out, exceptions); if (Traits.isTrait(clazz)) { out.println(";"); } else if (isAbstract(methodNode) && !clazz.isEnum()) { if (clazz.isAnnotationDefinition() && methodNode.hasAnnotationDefault()) { Statement fs = methodNode.getFirstStatement(); if (fs instanceof ExpressionStatement) { ExpressionStatement es = (ExpressionStatement) fs; Expression re = es.getExpression(); out.print(" default "); ClassNode rt = methodNode.getReturnType(); boolean classReturn = ClassHelper.CLASS_Type.equals(rt) || (rt.isArray() && ClassHelper.CLASS_Type.equals(rt.getComponentType())); if (re instanceof ListExpression) { out.print("{ "); ListExpression le = (ListExpression) re; boolean first = true; for (Expression expression : le.getExpressions()) { if (first) first = false; else out.print(", "); printValue(out, expression, classReturn); } out.print(" }"); } else { printValue(out, re, classReturn); } } } out.println(";"); } else { out.print(" { "); ClassNode retType = methodNode.getReturnType(); printReturn(out, retType); out.println("}"); } }