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

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

Introduction

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

Prototype

public final int getLength() 

Source Link

Document

Returns the length in characters of the original source file indicating where the source fragment corresponding to this node ends.

Usage

From source file:de.ovgu.featureide.munge.signatures.MungeSignatureBuilder.java

License:Open Source License

private static Collection<AbstractSignature> parse(ProjectSignatures projectSignatures, ASTNode root) {
    final HashMap<AbstractSignature, AbstractSignature> map = new HashMap<>();

    final CompilationUnit cu = (CompilationUnit) root;
    final PackageDeclaration pckgDecl = cu.getPackage();
    final String packageName = (pckgDecl == null) ? null : pckgDecl.getName().getFullyQualifiedName();
    List<?> l = cu.getCommentList();
    List<Javadoc> cl = new LinkedList<>();
    for (Object object : l) {
        if (object instanceof Javadoc) {
            Javadoc comment = (Javadoc) object;
            cl.add(comment);/*w  w  w .j a v  a 2s  . c o  m*/
        }
    }

    final ListIterator<Javadoc> it = cl.listIterator();
    final FeatureDataConstructor featureDataConstructor = new FeatureDataConstructor(projectSignatures,
            FeatureDataConstructor.TYPE_PP);

    root.accept(new ASTVisitor() {
        private BodyDeclaration curDeclaration = null;
        private PreprocessorFeatureData curfeatureData = null;
        private String lastComment = null;
        private MethodDeclaration lastCommentedMethod = null;

        @Override
        public boolean visit(Javadoc node) {
            if (curDeclaration != null) {
                final StringBuilder sb = new StringBuilder();
                while (it.hasNext()) {
                    final Javadoc comment = it.next();
                    if (comment.getStartPosition() <= curDeclaration.getStartPosition()) {
                        sb.append(comment);
                        sb.append("\n");
                    } else {
                        it.previous();
                        break;
                    }
                }
                lastComment = sb.toString();

                curfeatureData.setComment(lastComment);
                lastCommentedMethod = (curDeclaration instanceof MethodDeclaration)
                        ? (MethodDeclaration) curDeclaration
                        : null;
            }
            return false;
        }

        private void attachFeatureData(AbstractSignature curSignature, BodyDeclaration curDeclaration) {
            this.curDeclaration = curDeclaration;
            final Javadoc javadoc = curDeclaration.getJavadoc();
            final int startPosition = (javadoc == null) ? curDeclaration.getStartPosition()
                    : curDeclaration.getStartPosition() + javadoc.getLength();
            curfeatureData = (PreprocessorFeatureData) featureDataConstructor.create(null,
                    unit.getLineNumber(startPosition),
                    unit.getLineNumber(curDeclaration.getStartPosition() + curDeclaration.getLength()));
            curSignature.setFeatureData(curfeatureData);
            map.put(curSignature, curSignature);
        }

        @Override
        public boolean visit(CompilationUnit unit) {
            this.unit = unit;
            return true;
        }

        CompilationUnit unit = null;

        @Override
        public boolean visit(MethodDeclaration node) {
            int pos = unit.getLineNumber(node.getBody().getStartPosition());
            int end = unit.getLineNumber(node.getBody().getStartPosition() + node.getBody().getLength());
            final MungeMethodSignature methodSignature = new MungeMethodSignature(getParent(node.getParent()),
                    node.getName().getIdentifier(), node.getModifiers(), node.getReturnType2(),
                    node.parameters(), node.isConstructor(), pos, end);

            attachFeatureData(methodSignature, node);

            if (node.getJavadoc() == null && lastCommentedMethod != null
                    && lastCommentedMethod.getName().equals(node.getName())) {
                curfeatureData.setComment(lastComment);
            } else {
                lastCommentedMethod = null;
            }
            return true;
        }

        private AbstractClassSignature getParent(ASTNode astnode) {
            final AbstractClassSignature sig;
            if (astnode instanceof TypeDeclaration) {
                final TypeDeclaration node = (TypeDeclaration) astnode;
                sig = new MungeClassSignature(null, node.getName().getIdentifier(), node.getModifiers(),
                        node.isInterface() ? "interface" : "class", packageName);
            } else {
                return null;
            }
            AbstractClassSignature uniqueSig = (AbstractClassSignature) map.get(sig);
            if (uniqueSig == null) {
                visit((TypeDeclaration) astnode);
            }
            return uniqueSig;
        }

        @Override
        public boolean visit(FieldDeclaration node) {
            for (Iterator<?> it = node.fragments().iterator(); it.hasNext();) {
                VariableDeclarationFragment fragment = (VariableDeclarationFragment) it.next();

                final MungeFieldSignature fieldSignature = new MungeFieldSignature(getParent(node.getParent()),
                        fragment.getName().getIdentifier(), node.getModifiers(), node.getType());

                attachFeatureData(fieldSignature, node);
            }

            return true;
        }

        @Override
        public boolean visit(TypeDeclaration node) {
            final MungeClassSignature classSignature = new MungeClassSignature(getParent(node.getParent()),
                    node.getName().getIdentifier(), node.getModifiers(),
                    node.isInterface() ? "interface" : "class", packageName);

            attachFeatureData(classSignature, node);

            return super.visit(node);
        }

    });
    return map.keySet();
}

From source file:org.eclipse.pde.api.tools.ui.internal.markers.NodeFinder.java

License:Open Source License

/**
 * Visit the {@link BodyDeclaration} node to see if it is at the specified
 * position/*from   w  w  w  .ja  va 2s  .  c  o m*/
 * 
 * @param bodyDeclaration
 * @return
 */
private boolean visitNode(BodyDeclaration bodyDeclaration) {
    int start = bodyDeclaration.getStartPosition();
    int end = bodyDeclaration.getLength() - 1 + start;
    switch (bodyDeclaration.getNodeType()) {
    case ASTNode.TYPE_DECLARATION:
    case ASTNode.ENUM_DECLARATION:
    case ASTNode.ANNOTATION_TYPE_DECLARATION:
        if (start <= this.position && this.position <= end) {
            this.declaration = bodyDeclaration;
            return true;
        }
        return false;
    case ASTNode.ENUM_CONSTANT_DECLARATION:
    case ASTNode.FIELD_DECLARATION:
    case ASTNode.METHOD_DECLARATION:
    case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
        if (start <= this.position && this.position <= end) {
            this.declaration = bodyDeclaration;
        }
        return false;
    default:
        return false;
    }
}

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

License:Open Source License

/**
 * Removes given {@link BodyDeclaration}.
 */// w  w  w  .  ja  va 2  s  .  c  o  m
public void removeBodyDeclaration(BodyDeclaration declaration) throws Exception {
    List<BodyDeclaration> declarations;
    if (declaration.getParent() instanceof TypeDeclaration) {
        TypeDeclaration typeDeclaration = (TypeDeclaration) declaration.getParent();
        declarations = DomGenerics.bodyDeclarations(typeDeclaration);
    } else {
        declarations = DomGenerics.bodyDeclarations((AnonymousClassDeclaration) declaration.getParent());
    }
    // prepare start of source to remove
    int startIndex;
    {
        int index = declarations.indexOf(declaration);
        if (index != 0) {
            BodyDeclaration prevDeclaration = declarations.get(index - 1);
            startIndex = prevDeclaration.getStartPosition() + prevDeclaration.getLength();
            startIndex = skipWhitespaceEOLCToRight(startIndex);
        } else {
            if (declaration.getParent() instanceof TypeDeclaration) {
                TypeDeclaration typeDeclaration = (TypeDeclaration) declaration.getParent();
                startIndex = indexOfAny("{", typeDeclaration.getName().getStartPosition()) + 1;
            } else {
                startIndex = indexOfAny("{", declaration.getParent().getStartPosition()) + 1;
            }
        }
    }
    // prepare end of source to remove
    int endIndex;
    {
        endIndex = declaration.getStartPosition() + declaration.getLength();
        endIndex = skipWhitespaceEOLCToRight(endIndex);
    }
    // remove declaration and corresponding source
    declarations.remove(declaration);
    replaceSubstring(startIndex, endIndex - startIndex, "");
}

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 ww  .  ja  v  a2s. 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;
}

From source file:org.eclipse.wb.tests.designer.core.util.ast.AstEditorTest.java

License:Open Source License

private void check_ASTParser_parseBodyDeclaration(int position, String source) throws Exception {
    BodyDeclaration declaration = m_lastEditor.getParser().parseBodyDeclaration(position, source);
    assertNotNull(declaration);//www .ja v a 2s . c  o m
    assertEquals(position, declaration.getStartPosition());
    assertEquals(source.length(), declaration.getLength());
}