@Override public List<MethodNode> getDeclaredMethods(final String name) { List<MethodNode> nodes = new LinkedList<MethodNode>(); for (ClassNode delegate : delegates) { List<MethodNode> methods = delegate.getDeclaredMethods(name); if (methods != null) nodes.addAll(methods); } return nodes; }
public static boolean hasDeclaredMethod(ClassNode cNode, String name, int argsCount) { List<MethodNode> ms = cNode.getDeclaredMethods(name); for (MethodNode m : ms) { Parameter[] paras = m.getParameters(); if (paras != null && paras.length == argsCount) { return true; } } return false; }
@Override public List<MethodNode> getDeclaredMethods(String name) { lazyInitMembers(); return super.getDeclaredMethods(name); }
public static void deleteMethod(ClassNode clazz, MethodNode method) { // as of 1.8.6, this is the best possible implementation clazz.getMethods().remove(method); clazz.getDeclaredMethods(method.getName()).remove(method); }
/** * This methods creates a list of all methods with this name of the * current class and of all super classes * @return the methods list * @see #getDeclaredMethods(String) */ public List<MethodNode> getMethods(String name) { List<MethodNode> answer = new ArrayList<MethodNode>(); ClassNode node = this; while (node != null) { answer.addAll(node.getDeclaredMethods(name)); node = node.getSuperClass(); } return answer; }
/** * Finds a method matching the given name and parameters in this class. * * @return the method matching the given name and parameters or null */ public MethodNode getDeclaredMethod(String name, Parameter[] parameters) { for (MethodNode method : getDeclaredMethods(name)) { if (parametersEqual(method.getParameters(), parameters)) { return method; } } return null; }
private MethodNode getOrAddStaticConstructorNode() { MethodNode method = null; List declaredMethods = getDeclaredMethods("<clinit>"); if (declaredMethods.isEmpty()) { method = addMethod("<clinit>", ACC_STATIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BlockStatement()); method.setSynthetic(true); } else { method = (MethodNode) declaredMethods.get(0); } return method; }
/** * This methods returns a list of all methods of the given name * defined in the current class * @return the method list * @see #getMethods(String) */ public List<MethodNode> getDeclaredMethods(String name) { if (redirect!=null) return redirect().getDeclaredMethods(name); lazyClassInit(); return methods.getNotNull(name); }
public static List<MethodNode> findSetters(ClassNode cn, String setterName, boolean voidOnly) { List<MethodNode> result = null; for (MethodNode method : cn.getDeclaredMethods(setterName)) { if (setterName.equals(method.getName()) && (!voidOnly || VOID_TYPE == method.getReturnType()) && method.getParameters().length == 1) { if (result == null) { result = new LinkedList<MethodNode>(); } result.add(method); } } if (result == null) { ClassNode parent = cn.getSuperClass(); if (parent != null) { return findSetters(parent, setterName, voidOnly); } return Collections.emptyList(); } return result; }
public MethodNode getSetterMethod(String setterName, boolean voidOnly) { for (MethodNode method : getDeclaredMethods(setterName)) { if (setterName.equals(method.getName()) && (!voidOnly || ClassHelper.VOID_TYPE==method.getReturnType()) && method.getParameters().length == 1) { return method; } } ClassNode parent = getSuperClass(); if (parent!=null) return parent.getSetterMethod(setterName, voidOnly); return null; }
private static void visit(Closure closure, CodeVisitorSupport visitor) { if (closure != null) { ClassNode classNode = closure.getMetaClass().getClassNode(); if (classNode == null) { throw new GroovyRuntimeException( "DataSet unable to evaluate expression. AST not available for closure: " + closure.getMetaClass().getTheClass().getName() + ". Is the source code on the classpath?"); } List methods = classNode.getDeclaredMethods("doCall"); if (!methods.isEmpty()) { MethodNode method = (MethodNode) methods.get(0); if (method != null) { Statement statement = method.getCode(); if (statement != null) { statement.visit(visitor); } } } } }
public void visitMethod(final MethodNode node) { if (visitor!=null) visitor.visitMethod(node); ClassNode declaringClass = node.getDeclaringClass(); if (declaringClass!=null){ if (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(declaringClass, ClassHelper.LIST_TYPE)) { List<MethodNode> properties = declaringClass.getDeclaredMethods(node.getName()); boolean spread = true; for (MethodNode mn : properties) { if (node==mn) { spread = false; break; } } // it's no real property but a property of the component pexp.setSpreadSafe(spread); } rType.set(declaringClass); } }
private static MethodNode getDeclaredMethodCorrected(Map genericsSpec, MethodNode mn, ClassNode correctedNext) { for (MethodNode orig : correctedNext.getDeclaredMethods(mn.getName())) { MethodNode method = correctToGenericsSpec(genericsSpec, orig); if (ParameterUtils.parametersEqual(method.getParameters(), mn.getParameters())) { return method; } } return null; }
private static MethodNode getDeclaredMethodCorrected(Map<String, ClassNode> genericsSpec, MethodNode origMethod, ClassNode correctedClass) { for (MethodNode nameMatch : correctedClass.getDeclaredMethods(origMethod.getName())) { MethodNode correctedMethod = correctToGenericsSpec(genericsSpec, nameMatch); if (ParameterUtils.parametersEqual(correctedMethod.getParameters(), origMethod.getParameters())) { return correctedMethod; } } return null; }
private MethodNode getCompatibleMethod(ClassNode current, String getAt, ClassNode aType) { // TODO this really should find "best" match or find all matches and complain about ambiguity if more than one // TODO handle getAt with more than one parameter // TODO handle default getAt methods on Java 8 interfaces for (MethodNode methodNode : current.getDeclaredMethods("getAt")) { if (methodNode.getParameters().length == 1) { ClassNode paramType = methodNode.getParameters()[0].getType(); if (aType.isDerivedFrom(paramType) || aType.declaresInterface(paramType)) { return methodNode; } } } return null; }
@Override protected ClassNode createClosureClass(final ClosureExpression expression, final int mods) { ClassNode closureClass = super.createClosureClass(expression, mods); List<MethodNode> methods = closureClass.getDeclaredMethods("call"); List<MethodNode> doCall = closureClass.getMethods("doCall"); if (doCall.size() != 1) { throw new GroovyBugError("Expected to find one (1) doCall method on generated closure, but found " + doCall.size()); } MethodNode doCallMethod = doCall.get(0); if (methods.isEmpty() && doCallMethod.getParameters().length == 1) { createDirectCallMethod(closureClass, doCallMethod); } MethodTargetCompletionVisitor visitor = new MethodTargetCompletionVisitor(doCallMethod); Object dynamic = expression.getNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION); if (dynamic != null) { doCallMethod.putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, dynamic); } for (MethodNode method : methods) { visitor.visitMethod(method); } closureClass.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, Boolean.TRUE); return closureClass; }
public MethodNode getGetterMethod(String getterName, boolean searchSuperClasses) { MethodNode getterMethod = null; boolean booleanReturnOnly = getterName.startsWith("is"); for (MethodNode method : getDeclaredMethods(getterName)) { if (getterName.equals(method.getName()) && ClassHelper.VOID_TYPE!=method.getReturnType() && method.getParameters().length == 0 && (!booleanReturnOnly || ClassHelper.Boolean_TYPE.equals(ClassHelper.getWrapper(method.getReturnType())))) { // GROOVY-7363: There can be multiple matches for a getter returning a generic parameter type, due to // the generation of a bridge method. The real getter is really the non-bridge, non-synthetic one as it // has the most specific and exact return type of the two. Picking the bridge method results in loss of // type information, as it down-casts the return type to the lower bound of the generic parameter. if (getterMethod == null || getterMethod.isSynthetic()) { getterMethod = method; } } } if (getterMethod != null) return getterMethod; if (searchSuperClasses) { ClassNode parent = getSuperClass(); if (parent != null) return parent.getGetterMethod(getterName); } return null; }
private void visitOverride(AnnotatedNode node, AnnotationNode visited) { ClassNode annotationClassNode = visited.getClassNode(); if (annotationClassNode.isResolved() && annotationClassNode.getName().equals("java.lang.Override")) { if (node instanceof MethodNode && !Boolean.TRUE.equals(node.getNodeMetaData(Verifier.DEFAULT_PARAMETER_GENERATED))) { boolean override = false; MethodNode origMethod = (MethodNode) node; ClassNode cNode = origMethod.getDeclaringClass(); if (origMethod.hasDefaultValue()) { List<MethodNode> variants = cNode.getDeclaredMethods(origMethod.getName()); for (MethodNode m : variants) { if (m.getAnnotations().contains(visited) && isOverrideMethod(m)) { override = true; break; } } } else { override = isOverrideMethod(origMethod); } if (!override) { addError("Method '" + origMethod.getName() + "' from class '" + cNode.getName() + "' does not override " + "method from its superclass or interfaces but is annotated with @Override.", visited); } } } }
/** * This methods creates a list of all methods with this name of the * current class and of all super classes * @return the methods list * @see #getDeclaredMethods(String) */ public List getMethods(String name) { List answer = new ArrayList(getDeclaredMethods(name)); ClassNode parent = getSuperClass(); if (parent!=null) answer.addAll(parent.getMethods(name)); return answer; }
/** * This methods returns a list of all methods of the given name * defined in the current class * @return the method list * @see #getMethods(String) */ public List getDeclaredMethods(String name) { if (!redirect().lazyInitDone) redirect().lazyClassInit(); if (redirect!=null) return redirect().getDeclaredMethods(name); return methods.getNotNull(name); }