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

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

Introduction

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

Prototype

public final void setSourceRange(int startPosition, int length) 

Source Link

Document

Sets the source range of the original source file where the source fragment corresponding to this node was found.

Usage

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  ww w .  j  a v  a  2  s. c o  m
 * @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;
}