Example usage for org.eclipse.jdt.core.dom BodyDeclaration setJavadoc

List of usage examples for org.eclipse.jdt.core.dom BodyDeclaration setJavadoc

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom BodyDeclaration setJavadoc.

Prototype

public void setJavadoc(Javadoc docComment) 

Source Link

Document

Sets or clears the doc comment node.

Usage

From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java

License:Open Source License

@Override
protected void updateUID(Object element, Object correspondingElement) {
    if (element instanceof BodyDeclaration) {
        BodyDeclaration node = (BodyDeclaration) element;
        Javadoc javadoc = node.getJavadoc();
        // if it doesn't have any doc, create it
        if (javadoc == null) {
            javadoc = node.getAST().newJavadoc();
            node.setJavadoc(javadoc);
        }/*w  w w.j a v a  2s . c  om*/
        // first remove the existing flower tag, this way we also make sure that it's the last tag
        // note: if we only change the id, the rewriter won't format it correctly
        for (Object obj : javadoc.tags()) {
            if (FLOWER_UID.equals(((TagElement) obj).getTagName())) {
                javadoc.tags().remove(obj);
                break;
            }
        }
        // create new tag element for UID
        TagElement tag = javadoc.getAST().newTagElement();
        tag.setTagName(FLOWER_UID);
        javadoc.tags().add(tag);
        TextElement text = javadoc.getAST().newTextElement();
        tag.fragments().add(text);
        EObject eObject = (EObject) correspondingElement;
        text.setText(eObject.eResource().getURIFragment(eObject));
        System.out.println(javadoc);
    }
}

From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java

License:Open Source License

protected void setJavaDoc(Object element, Object docComment) {
    if (element instanceof BodyDeclaration) {
        BodyDeclaration node = (BodyDeclaration) element;
        ASTParser parser = ASTParser.newParser(AST.JLS4);
        parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
        parser.setSource(("/** " + docComment + "*/ int x;").toCharArray());
        TypeDeclaration type = (TypeDeclaration) parser.createAST(null);
        BodyDeclaration x = (BodyDeclaration) type.bodyDeclarations().get(0);
        Javadoc javadoc = x.getJavadoc();
        node.setJavadoc((Javadoc) ASTNode.copySubtree(node.getAST(), javadoc));
    }/*  w  w  w  . ja v a 2s. co  m*/
}

From source file:org.decojer.cavaj.transformers.TrOutline.java

License:Open Source License

private static void decompileField(@Nonnull final F f, @Nonnull final CU cu) {
    if (checkFieldIgnore(f, cu)) {
        return;//from  w w  w .j a  va  2  s .c  om
    }
    final String name = f.getName();
    final T t = f.getT();
    final AST ast = cu.getAst();

    final boolean isEnum = f.isEnum();

    // decompile BodyDeclaration, possible subtypes:
    // FieldDeclaration, EnumConstantDeclaration
    final BodyDeclaration fieldDeclaration;
    if (isEnum) {
        fieldDeclaration = ast.newEnumConstantDeclaration();
        ((EnumConstantDeclaration) fieldDeclaration).setName(newSimpleName(name, ast));
    } else {
        final VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
        variableDeclarationFragment.setName(newSimpleName(name, ast));
        fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
    }
    f.setAstNode(fieldDeclaration);

    // decompile synthetic Javadoc-comment if no annotation set
    if (f.isSynthetic()) {
        final Javadoc javadoc = ast.newJavadoc();
        final TagElement tagElement = ast.newTagElement();
        tagElement.setTagName("is synthetic");
        javadoc.tags().add(tagElement);
        fieldDeclaration.setJavadoc(javadoc);
    }
    // decompile deprecated Javadoc-tag if no annotation set
    if (f.getAf(AF.DEPRECATED) && !Annotations.isDeprecatedAnnotation(f.getAs())) {
        final Javadoc javadoc = ast.newJavadoc();
        final TagElement tagElement = ast.newTagElement();
        tagElement.setTagName("@deprecated");
        javadoc.tags().add(tagElement);
        fieldDeclaration.setJavadoc(javadoc);
    }

    final List<IExtendedModifier> modifiers = fieldDeclaration.modifiers();
    assert modifiers != null;

    // decompile annotations, add annotation modifiers before other modifiers, order preserved
    // in source code generation through Eclipse JDT
    if (f.getAs() != null) {
        Annotations.decompileAnnotations(f.getAs(), modifiers, t);
    }

    final boolean isInterfaceMember = t.isInterface();

    // decompile modifier flags, public is default for enum and interface
    if (f.getAf(AF.PUBLIC) && !isEnum && !isInterfaceMember) {
        modifiers.add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
    }
    if (f.getAf(AF.PRIVATE)) {
        modifiers.add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
    }
    if (f.getAf(AF.PROTECTED)) {
        modifiers.add(ast.newModifier(ModifierKeyword.PROTECTED_KEYWORD));
    }
    // static is default for enum and interface
    if (f.isStatic() && !isEnum && !isInterfaceMember) {
        modifiers.add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
    }
    // final is default for enum and interface
    if (f.getAf(AF.FINAL) && !isEnum && !isInterfaceMember) {
        modifiers.add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
    }
    if (f.getAf(AF.VOLATILE)) {
        modifiers.add(ast.newModifier(ModifierKeyword.VOLATILE_KEYWORD));
    }
    if (f.getAf(AF.TRANSIENT)) {
        modifiers.add(ast.newModifier(ModifierKeyword.TRANSIENT_KEYWORD));
    }
    // not for enum constant declaration
    if (fieldDeclaration instanceof FieldDeclaration) {
        ((FieldDeclaration) fieldDeclaration).setType(newType(f.getValueT(), t));
        final Object value = f.getValue();
        if (value != null) {
            // only final, non static - no arrays, class types
            final Expression expr = newLiteral(f.getValueT(), value, t, null);
            final VariableDeclarationFragment variableDeclarationFragment = (VariableDeclarationFragment) ((FieldDeclaration) fieldDeclaration)
                    .fragments().get(0);
            variableDeclarationFragment.setInitializer(expr);
        }
    }
}

From source file:org.decojer.cavaj.transformers.TrOutline.java

License:Open Source License

private static void decompileMethod(@Nonnull final M m, @Nonnull final CU cu, final boolean strictFp) {
    if (checkMethodIgnore(m, cu)) {
        return;/*from   w w w  .j a  va  2 s .  c om*/
    }
    final String name = m.getName();
    final T t = m.getT();
    assert t != null : "decompile method cannot be dynamic";
    final AST ast = cu.getAst();

    final boolean isAnnotationMember = t.getAf(AF.ANNOTATION);

    // decompile BodyDeclaration, possible subtypes:
    // MethodDeclaration (method or constructor),
    // AnnotationTypeMemberDeclaration (all methods in @interface) or
    // Initializer (static {})
    final BodyDeclaration methodDeclaration;
    if (m.isInitializer()) {
        methodDeclaration = ast.newInitializer();
    } else if (m.isConstructor()) {
        // MethodDeclaration with type declaration name as name
        methodDeclaration = ast.newMethodDeclaration();
        ((MethodDeclaration) methodDeclaration).setConstructor(true);
        ((MethodDeclaration) methodDeclaration).setName(newSimpleName(
                cu.check(DFlag.START_TD_ONLY) || t.isAnonymous() ? t.getPName() : t.getSimpleName(), ast));
    } else if (isAnnotationMember) {
        // AnnotationTypeMemberDeclaration
        methodDeclaration = ast.newAnnotationTypeMemberDeclaration();
        ((AnnotationTypeMemberDeclaration) methodDeclaration).setName(newSimpleName(name, ast));
        // check if default value (e.g.: byte byteTest() default 2;)
        if (m.getAnnotationDefaultValue() != null) {
            final Expression expression = Annotations
                    .decompileAnnotationDefaultValue(m.getAnnotationDefaultValue(), t);
            if (expression != null) {
                ((AnnotationTypeMemberDeclaration) methodDeclaration).setDefault(expression);
            }
        }
    } else {
        // MethodDeclaration
        methodDeclaration = ast.newMethodDeclaration();
        ((MethodDeclaration) methodDeclaration).setName(newSimpleName(name, ast));
    }
    m.setAstNode(methodDeclaration);

    // decompile synthetic Javadoc-comment if no annotation set
    if (m.isSynthetic()) {
        final Javadoc javadoc = ast.newJavadoc();
        final TagElement tagElement = ast.newTagElement();
        tagElement.setTagName("is synthetic");
        javadoc.tags().add(tagElement);
        methodDeclaration.setJavadoc(javadoc);
    }
    // decompile deprecated Javadoc-tag if no annotation set
    if (m.getAf(AF.DEPRECATED) && !Annotations.isDeprecatedAnnotation(m.getAs())) {
        final Javadoc javadoc = ast.newJavadoc();
        final TagElement tagElement = ast.newTagElement();
        tagElement.setTagName("@deprecated");
        javadoc.tags().add(tagElement);
        methodDeclaration.setJavadoc(javadoc);
    }

    final List<IExtendedModifier> modifiers = methodDeclaration.modifiers();
    assert modifiers != null;

    // decompile annotations:
    // add annotation modifiers before other modifiers, order preserved in
    // source code generation through Eclipse JDT
    if (m.getAs() != null) {
        Annotations.decompileAnnotations(m.getAs(), modifiers, t);
    }

    final boolean isInterfaceMember = t.isInterface();

    // decompile modifier flags:
    // interfaces can have default methods since JVM 8
    if (isInterfaceMember && m.getCfg() != null && !m.isStatic()) {
        if (t.isBelow(Version.JVM_8)) {
            log.warn("Default methods are not known before JVM 8! Adding default keyword anyway, check this.");
        }
        modifiers.add(ast.newModifier(ModifierKeyword.DEFAULT_KEYWORD));
    }
    // public is default for interface and annotation type declarations
    if (m.getAf(AF.PUBLIC) && !isAnnotationMember && !isInterfaceMember) {
        modifiers.add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
    }
    if (m.getAf(AF.PRIVATE)) {
        modifiers.add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
    }
    if (m.getAf(AF.PROTECTED)) {
        modifiers.add(ast.newModifier(ModifierKeyword.PROTECTED_KEYWORD));
    }
    if (m.isStatic()) {
        modifiers.add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
    }
    if (m.getAf(AF.FINAL)) {
        modifiers.add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
    }
    if (m.getAf(AF.SYNCHRONIZED)) {
        modifiers.add(ast.newModifier(ModifierKeyword.SYNCHRONIZED_KEYWORD));
    }
    if (m.getAf(AF.BRIDGE)) {
        // TODO
    }
    if (m.getAf(AF.NATIVE)) {
        modifiers.add(ast.newModifier(ModifierKeyword.NATIVE_KEYWORD));
    }
    // abstract is default for interface and annotation type declarations
    if (m.getAf(AF.ABSTRACT) && !isAnnotationMember && !isInterfaceMember) {
        modifiers.add(ast.newModifier(ModifierKeyword.ABSTRACT_KEYWORD));
    }
    if (m.getAf(AF.STRICTFP) && !strictFp) {
        modifiers.add(ast.newModifier(ModifierKeyword.STRICTFP_KEYWORD));
    }
    /*
     * AF.CONSTRUCTOR, AF.DECLARED_SYNCHRONIZED nothing, Dalvik only?
     */
    if (methodDeclaration instanceof MethodDeclaration) {
        decompileTypeParams(m.getTypeParams(), ((MethodDeclaration) methodDeclaration).typeParameters(), t);
        decompileMethodParams(m);
        if (!m.getAf(AF.ABSTRACT) && !m.getAf(AF.NATIVE)) {
            // create method block for valid syntax, abstract and native methods have none
            final Block block = ast.newBlock();
            ((MethodDeclaration) methodDeclaration).setBody(block);
            final CFG cfg = m.getCfg();
            if (cfg != null) {
                // could have no CFG because of empty or incomplete read code attribute
                cfg.setBlock(block);
            }
        }
    } else if (methodDeclaration instanceof Initializer) {
        // Initializer (static{}) has block per default
        assert ((Initializer) methodDeclaration).getBody() != null;

        final CFG cfg = m.getCfg();
        if (cfg != null) {
            // could have no CFG because of empty or incomplete read code attribute
            cfg.setBlock(((Initializer) methodDeclaration).getBody());
        }
    } else if (methodDeclaration instanceof AnnotationTypeMemberDeclaration) {
        assert m.getParamTs().length == 0;

        ((AnnotationTypeMemberDeclaration) methodDeclaration).setType(newType(m.getReturnT(), t));
    }
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

public void convert(org.eclipse.jdt.internal.compiler.ast.Javadoc javadoc, BodyDeclaration bodyDeclaration) {
    if (bodyDeclaration.getJavadoc() == null) {
        if (javadoc != null) {
            if (this.commentMapper == null || !this.commentMapper.hasSameTable(this.commentsTable)) {
                this.commentMapper = new DefaultCommentMapper(this.commentsTable);
            }/*from   w  w  w.  j a  v  a  2s . c o  m*/
            Comment comment = this.commentMapper.getComment(javadoc.sourceStart);
            if (comment != null && comment.isDocComment() && comment.getParent() == null) {
                Javadoc docComment = (Javadoc) comment;
                if (this.resolveBindings) {
                    recordNodes(docComment, javadoc);
                    // resolve member and method references binding
                    Iterator tags = docComment.tags().listIterator();
                    while (tags.hasNext()) {
                        recordNodes(javadoc, (TagElement) tags.next());
                    }
                }
                bodyDeclaration.setJavadoc(docComment);
            }
        }
    }
}

From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java

License:Open Source License

/**
 * Sets new {@link Javadoc} comment for {@link BodyDeclaration}.
 * //from w w w  .  j  a  v a2 s  .  com
 * @param declaration
 *          the {@link BodyDeclaration} to adds comment to.
 * @param lines
 *          the lines for {@link Javadoc} comment, may be <code>null</code> if {@link Javadoc}
 *          should be removed.
 * 
 * @return the added {@link Javadoc} object, or <code>null</code> if {@link Javadoc} was removed.
 */
public Javadoc setJavadoc(BodyDeclaration declaration, String[] lines) throws Exception {
    Javadoc oldJavadoc = declaration.getJavadoc();
    // set new JavaDoc
    if (lines != null) {
        int position = declaration.getStartPosition();
        // prepare code generation constants
        AstCodeGeneration generation = getGeneration();
        String eol = generation.getEndOfLine();
        String indent = getWhitespaceToLeft(declaration.getStartPosition(), false);
        // prepare source for comment
        String comment;
        {
            StringBuilder sb = new StringBuilder();
            sb.append("/**");
            sb.append(eol);
            for (String line : lines) {
                sb.append(indent);
                sb.append(" * ");
                sb.append(line);
                sb.append(eol);
            }
            sb.append(indent);
            sb.append(" */");
            comment = sb.toString();
        }
        // prepare JavaDoc
        Javadoc javadoc;
        {
            BodyDeclaration tmpMethod = getParser().parseBodyDeclaration(position,
                    comment + " void __wbp_tmpMethod() {}");
            javadoc = tmpMethod.getJavadoc();
            tmpMethod.setJavadoc(null);
        }
        // set JavaDoc
        if (oldJavadoc != null) {
            int oldLength = oldJavadoc.getLength();
            replaceSubstring(position, oldLength, comment);
        } else {
            comment += eol + indent;
            replaceSubstring(position, 0, comment);
            declaration.setSourceRange(position, comment.length() + declaration.getLength());
        }
        declaration.setJavadoc(javadoc);
        return javadoc;
    }
    // remove existing JavaDoc
    if (oldJavadoc != null) {
        int sourceBegin = oldJavadoc.getStartPosition();
        int sourceEnd = sourceBegin + oldJavadoc.getLength();
        sourceEnd = indexOfAnyBut(" \t\r\n", sourceEnd);
        replaceSubstring(sourceBegin, sourceEnd - sourceBegin, "");
        declaration.setJavadoc(null);
    }
    return null;
}

From source file:org.flowerplatform.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java

License:Open Source License

@Override
protected void updateUID(Object element, Object correspondingElement) {
    if (element instanceof BodyDeclaration) {
        BodyDeclaration node = (BodyDeclaration) element;
        Javadoc javadoc = node.getJavadoc();
        // if it doesn't have any doc, create it
        if (javadoc == null) {
            javadoc = node.getAST().newJavadoc();
            node.setJavadoc(javadoc);
        }//  w  w  w  .j  a va 2  s .  c  o  m
        // first remove the existing flower tag, this way we also make sure that it's the last tag
        // note: if we only change the id, the rewriter won't format it correctly
        for (Object obj : javadoc.tags()) {
            if (FLOWER_UID.equals(((TagElement) obj).getTagName())) {
                javadoc.tags().remove(obj);
                break;
            }
        }
        // create new tag element for UID
        TagElement tag = javadoc.getAST().newTagElement();
        tag.setTagName(FLOWER_UID);
        javadoc.tags().add(tag);
        TextElement text = javadoc.getAST().newTextElement();
        tag.fragments().add(text);
        text.setText(Utils.getFragment(((Node) correspondingElement).getNodeUri()));
        System.out.println(javadoc);
    }
}

From source file:org.flowerplatform.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java

License:Open Source License

protected void setJavaDoc(Object element, String docComment) {
    if (element instanceof BodyDeclaration) {
        BodyDeclaration node = (BodyDeclaration) element;
        try {// www .j  ava2  s  .  com
            Class ast = node.getAST().getClass();
            Field rewriterField = ast.getDeclaredField("rewriter");
            rewriterField.setAccessible(true);
            Object rewriter = rewriterField.get(node.getAST());
            Field storeField = rewriter.getClass().getDeclaredField("nodeStore");
            storeField.setAccessible(true);
            NodeInfoStore store = (NodeInfoStore) storeField.get(rewriter);
            ASTNode javadoc = store.newPlaceholderNode(ASTNode.JAVADOC);
            store.markAsStringPlaceholder(javadoc, docComment);
            node.setJavadoc((Javadoc) javadoc);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException
                | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}