Example usage for org.objectweb.asm Opcodes ACC_ABSTRACT

List of usage examples for org.objectweb.asm Opcodes ACC_ABSTRACT

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ACC_ABSTRACT.

Prototype

int ACC_ABSTRACT

To view the source code for org.objectweb.asm Opcodes ACC_ABSTRACT.

Click Source Link

Usage

From source file:org.codehaus.groovy.antlr.AntlrParserPlugin.java

License:Apache License

protected void constructorDef(AST constructorDef) {
    List<AnnotationNode> annotations = new ArrayList<>();
    AST node = constructorDef.getFirstChild();
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_STATIC, "static");
        checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_FINAL, "final");
        checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_ABSTRACT, "abstract");
        checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_NATIVE, "native");
        node = node.getNextSibling();//from  w  w  w  .  j a v  a 2 s. co  m
    }

    assertNodeType(PARAMETERS, node);
    Parameter[] parameters = parameters(node);
    if (parameters == null)
        parameters = Parameter.EMPTY_ARRAY;
    node = node.getNextSibling();

    ClassNode[] exceptions = ClassNode.EMPTY_ARRAY;
    if (isType(LITERAL_throws, node)) {
        AST throwsNode = node.getFirstChild();
        List<ClassNode> exceptionList = new ArrayList<>();
        throwsList(throwsNode, exceptionList);
        exceptions = exceptionList.toArray(exceptions);
        node = node.getNextSibling();
    }

    assertNodeType(SLIST, node);
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    ConstructorNode constructorNode = classNode.addConstructor(modifiers, parameters, exceptions, null);
    MethodNode oldMethod = methodNode;
    methodNode = constructorNode;
    Statement code = statementList(node);
    methodNode = oldMethod;
    constructorNode.setCode(code);
    constructorNode.setSyntheticPublic(syntheticPublic);
    constructorNode.addAnnotations(annotations);
    configureAST(constructorNode, constructorDef);
}

From source file:org.codehaus.groovy.antlr.AntlrParserPlugin.java

License:Apache License

protected int modifiers(AST modifierNode, List<AnnotationNode> annotations, int defaultModifiers) {
    assertNodeType(MODIFIERS, modifierNode);

    boolean access = false;
    int answer = 0;

    for (AST node = modifierNode.getFirstChild(); node != null; node = node.getNextSibling()) {
        int type = node.getType();
        switch (type) {
        case STATIC_IMPORT:
            // ignore
            break;

        // annotations
        case ANNOTATION:
            annotations.add(annotation(node));
            break;

        // core access scope modifiers
        case LITERAL_private:
            answer = setModifierBit(node, answer, Opcodes.ACC_PRIVATE);
            access = setAccessTrue(node, access);
            break;

        case LITERAL_protected:
            answer = setModifierBit(node, answer, Opcodes.ACC_PROTECTED);
            access = setAccessTrue(node, access);
            break;

        case LITERAL_public:
            answer = setModifierBit(node, answer, Opcodes.ACC_PUBLIC);
            access = setAccessTrue(node, access);
            break;

        // other modifiers
        case ABSTRACT:
            answer = setModifierBit(node, answer, Opcodes.ACC_ABSTRACT);
            break;

        case FINAL:
            answer = setModifierBit(node, answer, Opcodes.ACC_FINAL);
            break;

        case LITERAL_native:
            answer = setModifierBit(node, answer, Opcodes.ACC_NATIVE);
            break;

        case LITERAL_static:
            answer = setModifierBit(node, answer, Opcodes.ACC_STATIC);
            break;

        case STRICTFP:
            answer = setModifierBit(node, answer, Opcodes.ACC_STRICT);
            break;

        case LITERAL_synchronized:
            answer = setModifierBit(node, answer, Opcodes.ACC_SYNCHRONIZED);
            break;

        case LITERAL_transient:
            answer = setModifierBit(node, answer, Opcodes.ACC_TRANSIENT);
            break;

        case LITERAL_volatile:
            answer = setModifierBit(node, answer, Opcodes.ACC_VOLATILE);
            break;

        default:/*w w  w .  j  a  v  a 2 s .com*/
            unknownAST(node);
        }
    }
    if (!access) {
        answer |= defaultModifiers;
        // ACC_SYNTHETIC isn't used here, use it as a special flag
        if (defaultModifiers == Opcodes.ACC_PUBLIC)
            answer |= Opcodes.ACC_SYNTHETIC;
    }
    return answer;
}

From source file:org.codehaus.groovy.eclipse.codeassist.processors.NewMethodCompletionProcessor.java

License:Apache License

private void createMethod(MethodNode method, StringBuffer completion) {
    //// Modifiers
    // flush uninteresting modifiers
    int insertedModifiers = method.getModifiers()
            & ~(Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_PUBLIC);
    ASTNode.printModifiers(insertedModifiers, completion);

    //// Type parameters
    // ignore too difficult and not really needed for Groovy

    //        GenericsType[] typeVariableBindings = method.getGenericsTypes();
    //        if(typeVariableBindings != null && typeVariableBindings.length != 0) {
    //            completion.append('<');
    //            for (int i = 0; i < typeVariableBindings.length; i++) {
    //                if(i != 0) {
    //                    completion.append(',');
    //                    completion.append(' ');
    //                }
    //                createTypeVariable(typeVariableBindings[i], completion);
    //            }
    //            completion.append('>');
    //            completion.append(' ');
    //        }/*w  w w. ja v a  2  s  . c  o  m*/

    //// Return type
    createType(method.getReturnType(), completion, false);
    completion.append(' ');

    //// Selector
    completion.append(method.getName());

    completion.append('(');

    ////Parameters
    Parameter[] parameters = method.getParameters();
    int length = parameters.length;
    for (int i = 0; i < length; i++) {
        if (i != 0) {
            completion.append(',');
            completion.append(' ');
        }
        createType(parameters[i].getType(), completion, true);
        completion.append(' ');
        completion.append(parameters[i].getName());
    }

    completion.append(')');

    //// Exceptions
    ClassNode[] exceptions = method.getExceptions();

    if (exceptions != null && exceptions.length > 0) {
        completion.append(' ');
        completion.append("throws");
        completion.append(' ');
        for (int i = 0; i < exceptions.length; i++) {
            if (i != 0) {
                completion.append(' ');
                completion.append(',');
            }
            createType(exceptions[i], completion, false);
        }
    }
}

From source file:org.codehaus.groovy.parser.antlr4.ASTBuilder.java

License:Apache License

public ClassNode parseClassDeclaration(final GroovyLangParser.ClassDeclarationContext ctx) {
    boolean isEnum = asBoolean(ctx.KW_ENUM());

    final ClassNode outerClass = asBoolean(classNodeStack) ? classNodeStack.peek() : null;
    ClassNode[] interfaces = asBoolean(ctx.implementsClause())
            ? collect(ctx.implementsClause().genericClassNameExpression(), new Closure<ClassNode>(this, this) {
                public ClassNode doCall(GroovyLangParser.GenericClassNameExpressionContext it) {
                    return parseExpression(it);
                }//  w  w w.  j  av a2  s.  c  om
            }).toArray(new ClassNode[0])
            : new ClassNode[0];

    ClassNode classNode;
    String packageName = moduleNode.getPackageName();
    packageName = packageName != null && asBoolean(packageName) ? packageName : "";

    if (isEnum) {
        classNode = EnumHelper.makeEnumNode(
                asBoolean(outerClass) ? ctx.IDENTIFIER().getText() : packageName + ctx.IDENTIFIER().getText(),
                Modifier.PUBLIC, interfaces, outerClass);
    } else {
        if (outerClass != null) {
            String name = outerClass.getName() + "$" + String.valueOf(ctx.IDENTIFIER());
            classNode = new InnerClassNode(outerClass, name, Modifier.PUBLIC, ClassHelper.OBJECT_TYPE);
        } else {
            classNode = new ClassNode(packageName + String.valueOf(ctx.IDENTIFIER()), Modifier.PUBLIC,
                    ClassHelper.OBJECT_TYPE);
        }
    }

    setupNodeLocation(classNode, ctx);

    if (asBoolean(ctx.KW_TRAIT())) {
        attachTraitTransformAnnotation(classNode);
    }

    attachAnnotations(classNode, ctx.annotationClause());

    //        moduleNode.addClass(classNode);
    classes.add(classNode);
    if (asBoolean(ctx.extendsClause())) {
        if (asBoolean(ctx.KW_INTERFACE()) && !asBoolean(ctx.AT())) { // interface(NOT annotation)
            List<ClassNode> interfaceList = new LinkedList<ClassNode>();
            for (GroovyLangParser.GenericClassNameExpressionContext genericClassNameExpressionContext : ctx
                    .extendsClause().genericClassNameExpression()) {
                interfaceList.add(parseExpression(genericClassNameExpressionContext));
            }
            (classNode).setInterfaces(interfaceList.toArray(new ClassNode[0]));
            (classNode).setSuperClass(ClassHelper.OBJECT_TYPE);
        } else {
            (classNode).setSuperClass(parseExpression(ctx.extendsClause().genericClassNameExpression(0)));
        }

    }

    if (asBoolean(ctx.implementsClause())) {
        (classNode).setInterfaces(interfaces);
    }

    if (!isEnum) {
        (classNode).setGenericsTypes(parseGenericDeclaration(ctx.genericDeclarationList()));
        (classNode).setUsingGenerics(
                (classNode.getGenericsTypes() != null && classNode.getGenericsTypes().length != 0)
                        || (classNode).getSuperClass().isUsingGenerics() || DefaultGroovyMethods
                                .any(classNode.getInterfaces(), new Closure<Boolean>(this, this) {
                                    public Boolean doCall(ClassNode it) {
                                        return it.isUsingGenerics();
                                    }
                                }));
    }

    classNode.setModifiers(
            parseClassModifiers(ctx.classModifier()) | (isEnum ? (Opcodes.ACC_ENUM | Opcodes.ACC_FINAL)
                    : ((asBoolean(ctx.KW_INTERFACE()) ? Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT : 0))));

    classNode.setSyntheticPublic((classNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0);
    classNode.setModifiers(classNode.getModifiers() & ~Opcodes.ACC_SYNTHETIC);// FIXME Magic with synthetic modifier.

    if (asBoolean(ctx.AT())) {
        classNode.addInterface(ClassHelper.Annotation_TYPE);
        classNode.setModifiers(classNode.getModifiers() | Opcodes.ACC_ANNOTATION);
    }

    classNodeStack.add(classNode);
    parseClassBody(classNode, ctx.classBody());
    classNodeStack.pop();

    if (classNode.isInterface()) { // FIXME why interface has null mixin
        try {
            // FIXME Hack with visibility.
            Field field = ClassNode.class.getDeclaredField("mixins");
            field.setAccessible(true);
            field.set(classNode, null);
        } catch (IllegalAccessException e) {
            log.warning(createExceptionMessage(e));
        } catch (NoSuchFieldException e) {
            log.warning(createExceptionMessage(e));
        }
    }

    this.attachDocCommentAsMetaData(classNode, ctx);

    return classNode;
}

From source file:org.codehaus.groovy.parser.antlr4.ASTBuilder.java

License:Apache License

public AnnotatedNode parseMember(ClassNode classNode, GroovyLangParser.MethodDeclarationContext ctx) {
    if (isTrait(classNode)) {
        if (null == ctx.blockStatementWithCurve() && !ctx.modifierAndDefSet.contains(ABSTRACT)) {
            throw createParsingFailedException(new InvalidSyntaxException(
                    "You defined a method without body. Try adding a body, or declare it abstract.", ctx));
        }//from ww  w.j  ava 2 s . c  o  m
    }

    return parseMethodDeclaration(classNode, ctx, new Closure<MethodNode>(this, this) {
        public MethodNode doCall(ClassNode classNode, GroovyLangParser.MethodDeclarationContext ctx,
                String methodName, int modifiers, ClassNode returnType, Parameter[] params,
                ClassNode[] exceptions, Statement statement) {
            modifiers |= classNode.isInterface() ? Opcodes.ACC_ABSTRACT : 0;

            if (ctx.KW_DEFAULT() != null) {
                statement = new ExpressionStatement(parseExpression(ctx.annotationParameter()));
            }

            MethodNode methodNode;
            if (asBoolean(ctx.IDENTIFIER()) // constructor's name should only be defined by IDENTIFIER
                    && !(asBoolean(ctx.typeDeclaration()) || asBoolean(ctx.genericClassNameExpression())) // constructor should not has return type
                    && asBoolean(ctx.blockStatementWithCurve()) // constructor should have block statement
                    && methodName.equals(ctx.className) // constructor should has same name with class's
                    && (ctx.modifierAndDefSet.isEmpty()
                            || (1 == ctx.modifierAndDefSet.size() && CONSTRUCTOR_VISIBILITY_MODIFIER_SET
                                    .contains(ctx.modifierAndDefSet.toArray()[0]))) // constructor's modifier should has only public, protected, private and default(nothing)
            ) { // constructor

                methodNode = classNode.addConstructor(modifiers, params, exceptions, statement);
            } else { // method
                methodNode = classNode.addMethod(methodName, modifiers, returnType, params, exceptions,
                        statement);
            }

            methodNode.setGenericsTypes(parseGenericDeclaration(ctx.genericDeclarationList()));

            if (ctx.KW_DEFAULT() != null) {
                methodNode.setAnnotationDefault(true);
            }

            return methodNode;
        }
    });
}

From source file:org.codehaus.groovy.parser.antlr4.ASTBuilder.java

License:Apache License

public int parseClassModifiers(List<? extends GroovyLangParser.ClassModifierContext> ctxs) {
    List<TerminalNode> visibilityModifiers = new LinkedList<TerminalNode>();
    int modifiers = 0;
    for (int i = 0; i < ctxs.size(); i++) {
        for (Object ctx : ctxs.get(i).children) {
            ParseTree child = null;// w  ww.jav a  2 s . co  m
            if (ctx instanceof List) {
                List list = (List) ctx;
                assert list.size() == 1;
                child = (ParseTree) list.get(0);
            } else
                child = (ParseTree) ctx;

            assert child instanceof TerminalNode;
            switch (((TerminalNode) child).getSymbol().getType()) {
            case GroovyLangLexer.VISIBILITY_MODIFIER:
                visibilityModifiers.add((TerminalNode) child);
                break;
            case GroovyLangLexer.KW_STATIC:
                modifiers |= checkModifierDuplication(modifiers, Opcodes.ACC_STATIC, (TerminalNode) child);
                break;
            case GroovyLangLexer.KW_ABSTRACT:
                modifiers |= checkModifierDuplication(modifiers, Opcodes.ACC_ABSTRACT, (TerminalNode) child);
                break;
            case GroovyLangLexer.KW_FINAL:
                modifiers |= checkModifierDuplication(modifiers, Opcodes.ACC_FINAL, (TerminalNode) child);
                break;
            case GroovyLangLexer.KW_STRICTFP:
                modifiers |= checkModifierDuplication(modifiers, Opcodes.ACC_STRICT, (TerminalNode) child);
                break;
            }
        }
    }

    if (asBoolean(visibilityModifiers))
        modifiers |= parseVisibilityModifiers(visibilityModifiers, 0);
    else
        modifiers |= Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC;
    return modifiers;
}

From source file:org.codehaus.groovy.parser.antlr4.ASTBuilder.java

License:Apache License

/**
 * Traverse through modifiers, and combine them in one int value. Raise an error if there is multiple occurrences of same modifier.
 *
 * @param ctxList                   modifiers list.
 * @param defaultVisibilityModifier Default visibility modifier. Can be null. Applied if providen, and no visibility modifier exists in the ctxList.
 * @return tuple of int modifier and boolean flag, signalising visibility modifiers presence(true if there is visibility modifier in list, false otherwise).
 * @see #checkModifierDuplication(int, int, TerminalNode)
 *///from ww w.  java2  s. co  m
public List<Object> parseModifiers(List<? extends GroovyLangParser.MemberModifierContext> ctxList,
        Integer defaultVisibilityModifier) {
    int modifiers = 0;
    boolean hasVisibilityModifier = false;
    for (GroovyLangParser.MemberModifierContext it : ctxList) {
        TerminalNode child = (TerminalNode) it.getChild(0);
        switch (child.getSymbol().getType()) {
        case GroovyLangLexer.KW_STATIC:
            modifiers |= checkModifierDuplication(modifiers, Opcodes.ACC_STATIC, child);
            break;
        case GroovyLangLexer.KW_ABSTRACT:
            modifiers |= checkModifierDuplication(modifiers, Opcodes.ACC_ABSTRACT, child);
            break;
        case GroovyLangLexer.KW_FINAL:
            modifiers |= checkModifierDuplication(modifiers, Opcodes.ACC_FINAL, child);
            break;
        case GroovyLangLexer.KW_NATIVE:
            modifiers |= checkModifierDuplication(modifiers, Opcodes.ACC_NATIVE, child);
            break;
        case GroovyLangLexer.KW_SYNCHRONIZED:
            modifiers |= checkModifierDuplication(modifiers, Opcodes.ACC_SYNCHRONIZED, child);
            break;
        case GroovyLangLexer.KW_TRANSIENT:
            modifiers |= checkModifierDuplication(modifiers, Opcodes.ACC_TRANSIENT, child);
            break;
        case GroovyLangLexer.KW_VOLATILE:
            modifiers |= checkModifierDuplication(modifiers, Opcodes.ACC_VOLATILE, child);
            break;
        case GroovyLangLexer.VISIBILITY_MODIFIER:
            modifiers |= parseVisibilityModifiers(child);
            hasVisibilityModifier = true;
            break;
        }
    }
    if (!hasVisibilityModifier && defaultVisibilityModifier != null)
        modifiers |= defaultVisibilityModifier;

    return new LinkedList<Object>(Arrays.asList(modifiers, hasVisibilityModifier));
}

From source file:org.codehaus.groovy.tools.javac.JavaStubGenerator.java

License:Apache License

private void printClassContents(PrintWriter out, ClassNode classNode) {
    if (classNode instanceof InnerClassNode && ((InnerClassNode) classNode).isAnonymous()) {
        // if it is an anonymous inner class, don't generate the stub code for it.
        return;//  ww w . j  a v  a 2s . c o m
    }
    try {
        Verifier verifier = new Verifier() {
            @Override
            public void visitClass(ClassNode node) {
                List<Statement> savedStatements = new ArrayList<>(node.getObjectInitializerStatements());
                super.visitClass(node);
                node.getObjectInitializerStatements().addAll(savedStatements);

                for (ClassNode trait : findTraits(node)) {
                    // GROOVY-9031: replace property type placeholder with resolved type from trait generics
                    Map<String, ClassNode> generics = trait.isUsingGenerics() ? createGenericsSpec(trait)
                            : null;
                    for (PropertyNode traitProperty : trait.getProperties()) {
                        ClassNode traitPropertyType = traitProperty.getType();
                        traitProperty.setType(correctToGenericsSpec(generics, traitPropertyType));
                        super.visitProperty(traitProperty);
                        traitProperty.setType(traitPropertyType);
                    }
                }
            }

            private Iterable<ClassNode> findTraits(ClassNode node) {
                Set<ClassNode> traits = new LinkedHashSet<>();

                LinkedList<ClassNode> todo = new LinkedList<>();
                Collections.addAll(todo, node.getInterfaces());
                while (!todo.isEmpty()) {
                    ClassNode next = todo.removeLast();
                    if (Traits.isTrait(next))
                        traits.add(next);
                    Collections.addAll(todo, next.getInterfaces());
                }

                return traits;
            }

            @Override
            public void visitConstructor(ConstructorNode node) {
                Statement stmt = node.getCode();
                if (stmt != null) {
                    stmt.visit(new VerifierCodeVisitor(getClassNode()));
                }
            }

            @Override
            public void visitProperty(PropertyNode node) {
                // GROOVY-8233 skip static properties for traits since they don't make the interface
                if (!node.isStatic() || !Traits.isTrait(node.getDeclaringClass())) {
                    super.visitProperty(node);
                }
            }

            public void addCovariantMethods(ClassNode cn) {
            }

            protected void addInitialization(ClassNode node) {
            }

            protected void addPropertyMethod(MethodNode method) {
                doAddMethod(method);
            }

            protected void addReturnIfNeeded(MethodNode node) {
            }

            protected MethodNode addMethod(ClassNode node, boolean shouldBeSynthetic, String name,
                    int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions,
                    Statement code) {
                return doAddMethod(new MethodNode(name, modifiers, returnType, parameters, exceptions, code));
            }

            protected void addConstructor(Parameter[] newParams, ConstructorNode ctor, Statement code,
                    ClassNode node) {
                if (code instanceof ExpressionStatement) {//GROOVY-4508
                    Statement temp = code;
                    code = new BlockStatement();
                    ((BlockStatement) code).addStatement(temp);
                }
                ConstructorNode ctrNode = new ConstructorNode(ctor.getModifiers(), newParams,
                        ctor.getExceptions(), code);
                ctrNode.setDeclaringClass(node);
                constructors.add(ctrNode);
            }

            protected void addDefaultParameters(DefaultArgsAction action, MethodNode method) {
                final Parameter[] parameters = method.getParameters();
                final Expression[] saved = new Expression[parameters.length];
                for (int i = 0; i < parameters.length; i++) {
                    if (parameters[i].hasInitialExpression())
                        saved[i] = parameters[i].getInitialExpression();
                }
                super.addDefaultParameters(action, method);
                for (int i = 0; i < parameters.length; i++) {
                    if (saved[i] != null)
                        parameters[i].setInitialExpression(saved[i]);
                }
            }

            private MethodNode doAddMethod(MethodNode method) {
                String sig = method.getTypeDescriptor();

                if (propertyMethodsWithSigs.containsKey(sig))
                    return method;

                propertyMethods.add(method);
                propertyMethodsWithSigs.put(sig, method);

                return method;
            }

            @Override
            protected void addDefaultConstructor(ClassNode node) {
                // not required for stub generation
            }

            @Override
            protected FinalVariableAnalyzer.VariableNotFinalCallback getFinalVariablesCallback() {
                return null;
            }
        };
        int origNumConstructors = classNode.getDeclaredConstructors().size();
        verifier.visitClass(classNode);
        // undo unwanted side-effect of verifier
        if (origNumConstructors == 0 && classNode.getDeclaredConstructors().size() == 1) {
            classNode.getDeclaredConstructors().clear();
        }
        currentModule = classNode.getModule();

        boolean isInterface = isInterfaceOrTrait(classNode);
        boolean isEnum = classNode.isEnum();
        boolean isAnnotationDefinition = classNode.isAnnotationDefinition();
        printAnnotations(out, classNode);
        printModifiers(out, classNode.getModifiers() & ~(isInterface ? Opcodes.ACC_ABSTRACT : 0)
                & ~(isEnum ? Opcodes.ACC_FINAL | Opcodes.ACC_ABSTRACT : 0));

        if (isInterface) {
            if (isAnnotationDefinition) {
                out.print("@");
            }
            out.print("interface ");
        } else if (isEnum) {
            out.print("enum ");
        } else {
            out.print("class ");
        }

        String className = classNode.getNameWithoutPackage();
        if (classNode instanceof InnerClassNode)
            className = className.substring(className.lastIndexOf("$") + 1);
        out.println(className);
        printGenericsBounds(out, classNode, true);

        ClassNode superClass = classNode.getUnresolvedSuperClass(false);

        if (!isInterface && !isEnum) {
            out.print("  extends ");
            printType(out, superClass);
        }

        ClassNode[] interfaces = classNode.getInterfaces();
        if (interfaces != null && interfaces.length > 0 && !isAnnotationDefinition) {
            if (isInterface) {
                out.println("  extends");
            } else {
                out.println("  implements");
            }
            for (int i = 0; i < interfaces.length - 1; ++i) {
                out.print("    ");
                printType(out, interfaces[i]);
                out.print(",");
            }
            out.print("    ");
            printType(out, interfaces[interfaces.length - 1]);
        }
        out.println(" {");

        printFields(out, classNode);
        printMethods(out, classNode, isEnum);

        for (Iterator<InnerClassNode> inner = classNode.getInnerClasses(); inner.hasNext();) {
            // GROOVY-4004: Clear the methods from the outer class so that they don't get duplicated in inner ones
            propertyMethods.clear();
            propertyMethodsWithSigs.clear();
            constructors.clear();
            printClassContents(out, inner.next());
        }

        out.println("}");
    } finally {
        propertyMethods.clear();
        propertyMethodsWithSigs.clear();
        constructors.clear();
        currentModule = null;
    }
}

From source file:org.codehaus.groovy.tools.javac.JavaStubGenerator.java

License:Apache License

private void printMethod(PrintWriter out, ClassNode clazz, MethodNode methodNode) {
    if (methodNode.getName().equals("<clinit>"))
        return;//from   w ww. ja  v  a2s . co  m
    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();
    for (int i = 0; i < exceptions.length; i++) {
        ClassNode exception = exceptions[i];
        if (i == 0) {
            out.print("throws ");
        } else {
            out.print(", ");
        }
        printType(out, exception);
    }

    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("}");
    }
}

From source file:org.codehaus.groovy.tools.javac.JavaStubGenerator.java

License:Apache License

private static boolean isAbstract(final MethodNode methodNode) {
    if (isDefaultTraitImpl(methodNode)) {
        return false;
    }//from  w w  w.  ja  va 2  s.  c om
    return (methodNode.getModifiers() & Opcodes.ACC_ABSTRACT) != 0;
}