예제 #1
0
 public static boolean isConstructorMethod(MethodNode declaredMethod) {
   return declaredMethod.isStatic()
       && declaredMethod.isPublic()
       && declaredMethod.getName().equals("initialize")
       && declaredMethod.getParameters().length >= 1
       && declaredMethod
           .getParameters()[0]
           .getType()
           .equals(AbstractGrailsArtefactTransformer.OBJECT_CLASS);
 }
 private void checkMethodForWeakerAccessPrivileges(MethodNode mn, ClassNode cn) {
   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.isPublic())) {
       addWeakerAccessError(cn, mn, params, superMethod);
       return;
     }
   }
 }
 private void checkOverloadingPrivateAndPublic(MethodNode node) {
   if (isConstructor(node)) return;
   boolean hasPrivate = false;
   boolean hasPublic = false;
   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) {
     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);
   }
 }
 private void addWeakerAccessError(
     ClassNode cn, MethodNode method, Parameter[] parameters, MethodNode superMethod) {
   StringBuilder msg = new StringBuilder();
   msg.append(method.getName());
   msg.append("(");
   boolean needsComma = false;
   for (Parameter parameter : parameters) {
     if (needsComma) {
       msg.append(",");
     } else {
       needsComma = true;
     }
     msg.append(parameter.getType());
   }
   msg.append(") in ");
   msg.append(cn.getName());
   msg.append(" cannot override ");
   msg.append(superMethod.getName());
   msg.append(" in ");
   msg.append(superMethod.getDeclaringClass().getName());
   msg.append("; attempting to assign weaker access privileges; was ");
   msg.append(superMethod.isPublic() ? "public" : "protected");
   addError(msg.toString(), method);
 }
예제 #5
0
 private boolean isPublicInstanceMethod(MethodNode methodNode) {
   return !methodNode.isSynthetic() && !methodNode.isStatic() && methodNode.isPublic();
 }
예제 #6
0
 /**
  * Tests whether the ClasNode implements the specified method name.
  *
  * @param classNode The ClassNode
  * @param methodName The method name
  * @return True if it does implement the method
  */
 public static boolean implementsZeroArgMethod(ClassNode classNode, String methodName) {
   MethodNode method = classNode.getDeclaredMethod(methodName, Parameter.EMPTY_ARRAY);
   return method != null && (method.isPublic() || method.isProtected()) && !method.isAbstract();
 }