Java Code Examples for org.codehaus.groovy.ast.ClassNode#getDeclaredMethods()

The following examples show how to use org.codehaus.groovy.ast.ClassNode#getDeclaredMethods() . 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: DataSet.java    From groovy with Apache License 2.0 6 votes vote down vote up
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);
                }
            }
        }
    }
}
 
Example 2
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static List<MethodNode> findSetters(final ClassNode cn, final String setterName, final 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<>();
            }
            result.add(method);
        }
    }
    if (result == null) {
        ClassNode parent = cn.getSuperClass();
        if (parent != null) {
            return findSetters(parent, setterName, voidOnly);
        }
        return Collections.emptyList();
    }
    return result;
}
 
Example 3
Source File: ExtendedVerifier.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void visitOverride(AnnotatedNode node, AnnotationNode visited) {
    ClassNode annotationType = visited.getClassNode();
    if (annotationType.isResolved() && annotationType.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);
            }
        }
    }
}
 
Example 4
Source File: StaticTypesClosureWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 5
Source File: AstUtils.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static MethodNode getGeneratedClosureImplMethod(ClassNode classNode) {
    if (!classNode.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type)) {
        throw new IllegalArgumentException("expecting generated closure class node");
    }

    List<MethodNode> doCallMethods = classNode.getDeclaredMethods("doCall");
    return doCallMethods.get(0);
}
 
Example 6
Source File: UnionTypeClassNode.java    From groovy with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 7
Source File: AutoImplementASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: GeneralUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static boolean hasDeclaredMethod(final ClassNode cNode, final String name, final int argsCount) {
    List<MethodNode> methods = cNode.getDeclaredMethods(name);
    for (MethodNode method : methods) {
        Parameter[] params = method.getParameters();
        if (params != null && params.length == argsCount) {
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: ExtendedVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static MethodNode getDeclaredMethodCorrected(Map genericsSpec, MethodNode mn, ClassNode correctedNext) {
    for (MethodNode declared : correctedNext.getDeclaredMethods(mn.getName())) {
        MethodNode corrected = correctToGenericsSpec(genericsSpec, declared);
        if (ParameterUtils.parametersEqual(corrected.getParameters(), mn.getParameters())) {
            return corrected;
        }
    }
    return null;
}
 
Example 10
Source File: StaticTypesCallSiteWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private MethodNode getCompatibleMethod(final ClassNode current, final String getAt, final 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;
}
 
Example 11
Source File: AstUtils.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static MethodNode getGeneratedClosureImplMethod(ClassNode classNode) {
    if (!classNode.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type)) {
        throw new IllegalArgumentException("expecting generated closure class node");
    }

    List<MethodNode> doCallMethods = classNode.getDeclaredMethods("doCall");
    return doCallMethods.get(0);
}