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

The following examples show how to use org.codehaus.groovy.ast.MethodNode#isSynthetic() . 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: Verifier.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static void checkForDuplicateMethods(ClassNode cn) {
    Set<String> descriptors = new HashSet<String>();
    for (MethodNode mn : cn.getMethods()) {
        if (mn.isSynthetic()) continue;
        String mySig = methodDescriptorWithoutReturnType(mn);
        if (descriptors.contains(mySig)) {
            if (mn.isScriptBody() || mySig.equals(scriptBodySignatureWithoutReturnType(cn))) {
                throw new RuntimeParserException("The method " + mn.getText() +
                        " is a duplicate of the one declared for this script's body code", sourceOf(mn));
            } else {
                throw new RuntimeParserException("The method " + mn.getText() +
                        " duplicates another method of the same signature", sourceOf(mn));
            }
        }
        descriptors.add(mySig);
    }
}
 
Example 2
Source File: Verifier.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static ASTNode sourceOf(MethodNode methodNode) {
    if (methodNode.getLineNumber() < 1) {
        ClassNode declaringClass = methodNode.getDeclaringClass();
        if (methodNode.isSynthetic()) {
            String propertyName = getPropertyName(methodNode);
            if (propertyName != null) {
                PropertyNode propertyNode = declaringClass.getProperty(propertyName);
                if (propertyNode != null && propertyNode.getLineNumber() > 0) {
                    return propertyNode;
                }
            }
        }
        return declaringClass;
    }
    return methodNode;
}
 
Example 3
Source File: InnerClassCompletionVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void addSyntheticMethod(final InnerClassNode node, final String methodName, final int modifiers,
        final ClassNode returnType, final Parameter[] parameters, final BiConsumer<BlockStatement, Parameter[]> consumer) {
    MethodNode method = node.getMethod(methodName, parameters);
    if (method != null) {
        // GROOVY-8914: pre-compiled classes lose synthetic boolean - TODO fix earlier as per GROOVY-4346 then remove extra check here
        if (isStatic(node) && !method.isSynthetic() && (method.getModifiers() & ACC_SYNTHETIC) == 0) {
            // if there is a user-defined methodNode, add compiler error and continue
            addError("\"" + methodName + "\" implementations are not supported on static inner classes as " +
                "a synthetic version of \"" + methodName + "\" is added during compilation for the purpose " +
                "of outer class delegation.",
                method);
        }
        return;
    }

    BlockStatement methodBody = block();
    consumer.accept(methodBody, parameters);
    node.addSyntheticMethod(methodName, modifiers, returnType, parameters, ClassNode.EMPTY_ARRAY, methodBody);
}
 
Example 4
Source File: GroovyVirtualSourceProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void genMethod(ClassNode clazz, MethodNode methodNode, PrintWriter out, boolean ignoreSynthetic) {
    String name = methodNode.getName();
    if ((ignoreSynthetic && methodNode.isSynthetic()) || name.startsWith("super$")) { // NOI18N
        return;
    }
// </netbeans>
    if (methodNode.getName().equals("<clinit>")) {
        return;
    }
    if (!clazz.isInterface()) {
        printModifiers(out, methodNode.getModifiers());
    }
    printType(methodNode.getReturnType(), out);
    out.print(" ");
    out.print(methodNode.getName());

    printParams(methodNode, out);

    ClassNode[] exceptions = methodNode.getExceptions();
    if (exceptions != null && exceptions.length > 0) {
        out.print(" throws ");
        for (int i = 0; i < exceptions.length; i++) {
            if (i > 0) {
                out.print(", ");
            }
            printType(exceptions[i], out);
        }
    }

    if ((methodNode.getModifiers() & Opcodes.ACC_ABSTRACT) != 0) {
        out.println(";");
    } else {
        out.print(" { ");
        ClassNode retType = methodNode.getReturnType();
        printReturn(out, retType);
        out.println("}");
    }
}
 
Example 5
Source File: SecureASTCustomizer.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected static List<MethodNode> filterMethods(ClassNode owner) {
    List<MethodNode> result = new LinkedList<>();
    List<MethodNode> methods = owner.getMethods();
    for (MethodNode method : methods) {
        if (method.getDeclaringClass() == owner && !method.isSynthetic()) {
            if ("main".equals(method.getName()) || "run".equals(method.getName()) && owner.isScriptBody()) continue;
            result.add(method);
        }
    }
    return result;
}
 
Example 6
Source File: AutoFinalASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void processConstructorOrMethod(MethodNode mNode, ClassCodeVisitorSupport visitor) {
    if (!isEnabled(mNode)) return;
    if (mNode.isSynthetic()) return;
    Parameter[] origParams = mNode.getParameters();
    for (Parameter p : origParams) {
        p.setModifiers(p.getModifiers() | Modifier.FINAL);
    }
    visitor.visitMethod(mNode);
}
 
Example 7
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void removeSyntheticMethodIfOne(final List<MethodNode> toBeRemoved, final MethodNode one, final MethodNode two) {
    if (one.isSynthetic() && !two.isSynthetic()) {
        toBeRemoved.add(one);
    } else if (two.isSynthetic() && !one.isSynthetic()) {
        toBeRemoved.add(two);
    }
}
 
Example 8
Source File: FromAbstractTypeMethods.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static List<ClassNode[]> extractSignaturesFromMethods(final ClassNode cn) {
    List<MethodNode> methods = cn.getAllDeclaredMethods();
    List<ClassNode[]> signatures = new LinkedList<ClassNode[]>();
    for (MethodNode method : methods) {
        if (!method.isSynthetic() && method.isAbstract()) {
            extractParametersFromMethod(signatures, method);
        }
    }
    return signatures;
}
 
Example 9
Source File: JavaStubGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void printMethods(PrintWriter out, ClassNode classNode, boolean isEnum) {
    if (!isEnum) printConstructors(out, classNode);

    @SuppressWarnings("unchecked")
    List<MethodNode> methods = (List) propertyMethods.clone();
    methods.addAll(classNode.getMethods());
    for (MethodNode method : methods) {
        if (isEnum && method.isSynthetic()) {
            // skip values() method and valueOf(String)
            String name = method.getName();
            Parameter[] params = method.getParameters();
            if (name.equals("values") && params.length == 0) continue;
            if (name.equals("valueOf") &&
                    params.length == 1 &&
                    params[0].getType().equals(ClassHelper.STRING_TYPE)) {
                continue;
            }
        }
        printMethod(out, classNode, method);
    }

    // print the methods from traits
    for (ClassNode trait : findTraits(classNode)) {
        Map<String, ClassNode> generics = trait.isUsingGenerics() ? createGenericsSpec(trait) : null;
        List<MethodNode> traitMethods = trait.getMethods();
        for (MethodNode traitOrigMethod : traitMethods) {
            // GROOVY-9606: replace method return type and parameter type placeholder with resolved type from trait generics
            MethodNode traitMethod = correctToGenericsSpec(generics, traitOrigMethod);
            MethodNode existingMethod = classNode.getMethod(traitMethod.getName(), traitMethod.getParameters());
            if (existingMethod != null) continue;
            for (MethodNode propertyMethod : propertyMethods) {
                if (propertyMethod.getName().equals(traitMethod.getName())) {
                    boolean sameParams = sameParameterTypes(propertyMethod, traitMethod);
                    if (sameParams) {
                        existingMethod = propertyMethod;
                        break;
                    }
                }
            }
            if (existingMethod != null) continue;
            boolean isCandidate = isCandidateTraitMethod(trait, traitMethod);
            if (!isCandidate) continue;
            printMethod(out, classNode, traitMethod);
        }
    }
}
 
Example 10
Source File: JavaStubGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
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("}");
    }
}