Example usage for org.eclipse.jdt.core.dom CompilationUnit getExtendedLength

List of usage examples for org.eclipse.jdt.core.dom CompilationUnit getExtendedLength

Introduction

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

Prototype

public int getExtendedLength(ASTNode node) 

Source Link

Document

Returns the extended source length of the given node.

Usage

From source file:edu.brown.cs.bubbles.bedrock.BedrockEditor.java

License:Open Source License

/********************************************************************************/

void getTextRegions(String proj, String bid, String file, String cls, boolean pfx, boolean statics,
        boolean compunit, boolean imports, boolean pkgfg, boolean topdecls, boolean fields, boolean all,
        IvyXmlWriter xw) throws BedrockException {
    if (file == null) {
        file = getFileFromClass(proj, cls);
    }//from ww w .  jav  a 2 s.c  o  m

    FileData fd = findFile(proj, file, bid, null);
    if (fd == null)
        throw new BedrockException("Can't find file " + file + " in " + proj);

    CompilationUnit cu = fd.getDefaultRoot(bid);
    if (cu == null)
        throw new BedrockException("Can't get compilation unit for " + file);

    List<?> typs = cu.types();
    AbstractTypeDeclaration atd = findTypeDecl(cls, typs);
    int start = 0;
    if (atd != null && atd != typs.get(0))
        start = cu.getExtendedStartPosition(atd);

    if (compunit) {
        xw.begin("RANGE");
        xw.field("PATH", file);
        xw.field("START", 0);
        int ln = fd.getLength();
        if (ln < 0) {
            File f = new File(file);
            ln = (int) f.length();
        }
        xw.field("END", ln);
        xw.end("RANGE");
    }

    if (pfx && atd != null) {
        int xpos = cu.getExtendedStartPosition(atd);
        int xlen = cu.getExtendedLength(atd);
        int spos = atd.getStartPosition();
        int len = atd.getLength();
        int epos = -1;
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            int apos = cu.getExtendedStartPosition(an);
            if (epos < 0 || epos >= apos)
                epos = apos - 1;
        }
        if (epos < 0) { // no body declarations
            xw.begin("RANGE");
            xw.field("PATH", file);
            xw.field("START", start);
            xw.field("END", xpos + xlen);
            xw.end("RANGE");
        } else {
            xw.begin("RANGE");
            xw.field("PATH", file);
            xw.field("START", start);
            xw.field("END", epos);
            xw.end("RANGE");
            xw.begin("RANGE");
            xw.field("PATH", file);
            xw.field("START", spos + len - 1);
            xw.field("END", xpos + xlen);
            xw.end("RANGE");
        }
    }

    if (pkgfg) {
        PackageDeclaration pkg = cu.getPackage();
        if (pkg != null) {
            outputRange(cu, pkg, file, xw);
        }
    }

    if (imports) {
        for (Iterator<?> it = cu.imports().iterator(); it.hasNext();) {
            ImportDeclaration id = (ImportDeclaration) it.next();
            outputRange(cu, id, file, xw);
        }
    }

    if (topdecls && atd != null) {
        int spos = atd.getStartPosition();
        int len = atd.getLength();
        int epos = -1;
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            int apos = cu.getExtendedStartPosition(an);
            if (epos < 0 || epos >= apos)
                epos = apos - 1;
        }
        if (epos < 0) { // no body declarations
            xw.begin("RANGE");
            xw.field("PATH", file);
            xw.field("START", spos);
            xw.field("END", spos + len);
            xw.end("RANGE");
        } else {
            xw.begin("RANGE");
            xw.field("PATH", file);
            xw.field("START", spos);
            xw.field("END", epos);
            xw.end("RANGE");
        }
    }

    if ((statics || all) && atd != null) {
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            if (an.getNodeType() == ASTNode.INITIALIZER) {
                outputRange(cu, an, file, xw);
            }
        }
    }

    if (fields && atd != null) {
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            switch (an.getNodeType()) {
            case ASTNode.FIELD_DECLARATION:
                outputRange(cu, an, file, xw);
                break;
            case ASTNode.ENUM_CONSTANT_DECLARATION:
                outputRange(cu, an, file, xw);
                break;
            }
        }
        if (atd instanceof EnumDeclaration) {
            for (Object o : ((EnumDeclaration) atd).enumConstants()) {
                ASTNode an = (ASTNode) o;
                switch (an.getNodeType()) {
                case ASTNode.FIELD_DECLARATION:
                    outputRange(cu, an, file, xw);
                    break;
                case ASTNode.ENUM_CONSTANT_DECLARATION:
                    outputRange(cu, an, file, xw);
                    break;
                }
            }
        }
    }

    if (all && atd != null) {
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            IJavaElement elt = null;
            switch (an.getNodeType()) {
            case ASTNode.ANNOTATION_TYPE_DECLARATION:
            case ASTNode.ENUM_DECLARATION:
            case ASTNode.TYPE_DECLARATION:
                AbstractTypeDeclaration atdecl = (AbstractTypeDeclaration) an;
                ITypeBinding atbnd = atdecl.resolveBinding();
                if (atbnd != null)
                    elt = atbnd.getJavaElement();
                break;
            case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
                break;
            case ASTNode.ENUM_CONSTANT_DECLARATION:
                EnumConstantDeclaration ecdecl = (EnumConstantDeclaration) an;
                IVariableBinding ecbnd = ecdecl.resolveVariable();
                if (ecbnd != null)
                    elt = ecbnd.getJavaElement();
                break;
            case ASTNode.FIELD_DECLARATION:
                FieldDeclaration fdecl = (FieldDeclaration) an;
                for (Iterator<?> it = fdecl.fragments().iterator(); it.hasNext();) {
                    VariableDeclarationFragment vdf = (VariableDeclarationFragment) it.next();
                    IVariableBinding vbnd = vdf.resolveBinding();
                    if (vbnd != null) {
                        IJavaElement velt = vbnd.getJavaElement();
                        if (velt != null)
                            BedrockUtil.outputJavaElement(velt, xw);
                    }
                }
                break;
            case ASTNode.INITIALIZER:
                break;
            case ASTNode.METHOD_DECLARATION:
                MethodDeclaration mdecl = (MethodDeclaration) an;
                IMethodBinding mbnd = mdecl.resolveBinding();
                if (mbnd != null)
                    elt = mbnd.getJavaElement();
                break;
            default:
                break;
            }
            if (elt != null)
                BedrockUtil.outputJavaElement(elt, false, xw);
        }
    }
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockEditor.java

License:Open Source License

private void outputRange(CompilationUnit cu, ASTNode an, String file, IvyXmlWriter xw) {
    int xpos = cu.getExtendedStartPosition(an);
    int xlen = cu.getExtendedLength(an);
    xw.begin("RANGE");
    xw.field("PATH", file);
    xw.field("START", xpos);
    xw.field("END", xpos + xlen);
    xw.end("RANGE");
}

From source file:edu.brown.cs.bubbles.rebase.java.RebaseJavaRoot.java

License:Open Source License

private RebaseJavaSymbol getContainer(CompilationUnit cu, int soff, int eoff) {
    for (Object o : cu.types()) {
        AbstractTypeDeclaration atd = (AbstractTypeDeclaration) o;
        int spos = cu.getExtendedStartPosition(atd);
        if (spos <= soff && spos + cu.getExtendedLength(atd) >= eoff) {
            return getContainer(cu, atd, soff, eoff);
        }//from w ww .j a  v a2 s .c  om
    }

    return null;
}

From source file:edu.brown.cs.bubbles.rebase.java.RebaseJavaRoot.java

License:Open Source License

private RebaseJavaSymbol getContainer(CompilationUnit cu, AbstractTypeDeclaration atd, int soff, int eoff) {
    RebaseJavaSymbol sym = RebaseJavaAst.getDefinition(atd);

    for (Object o : atd.bodyDeclarations()) {
        BodyDeclaration bd = (BodyDeclaration) o;
        int spos = cu.getExtendedStartPosition(bd);
        if (spos <= soff && spos + cu.getExtendedLength(bd) >= eoff) {
            switch (bd.getNodeType()) {
            case ASTNode.TYPE_DECLARATION:
            case ASTNode.ANNOTATION_TYPE_DECLARATION:
            case ASTNode.ENUM_DECLARATION:
                return getContainer(cu, (AbstractTypeDeclaration) bd, soff, eoff);
            case ASTNode.ENUM_CONSTANT_DECLARATION:
            case ASTNode.FIELD_DECLARATION:
            case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
            case ASTNode.INITIALIZER:
            case ASTNode.METHOD_DECLARATION:
                return sym;
            }//from ww w .j av a  2 s . com
        }
    }

    return sym;
}

From source file:edu.brown.cs.bubbles.rebase.newjava.RebaseJcompProject.java

License:Open Source License

static void outputNameData(RebaseFile rf, JcompSymbol js, IvyXmlWriter xw) {
    ASTNode an = js.getDefinitionNode();
    if (an == null)
        return;//from w  w  w.  j a v  a  2s .c om

    xw.begin("ITEM");
    xw.field("PROJECT", rf.getProjectName());
    xw.field("PATH", rf.getFileName());
    CompilationUnit cu = (CompilationUnit) an.getRoot();
    xw.field("QNAME", js.getFullReportName());
    int spos = cu.getExtendedStartPosition(an);
    int len = cu.getExtendedLength(an);
    xw.field("STARTOFFSET", spos);
    xw.field("LENGTH", len);
    xw.field("ENDOFFSET", spos + len);
    String hdl = js.getHandle(rf.getProjectName());
    if (hdl != null)
        xw.field("HANDLE", hdl);

    if (rf.getSource() != null) {
        String s6 = rf.getSource().getS6Source();
        if (s6 != null)
            xw.field("S6", s6);
    }

    switch (js.getSymbolKind()) {
    case FIELD:
        if (js.isEnumSymbol()) {
            xw.field("NAME", js.getName());
            xw.field("TYPE", "EnumConstants");
            xw.field("FLAGS", js.getModifiers());
        }
        break;
    case CONSTRUCTOR:
        xw.field("NAME", js.getReportName());
        xw.field("TYPE", "Constructor");
        xw.field("FLAGS", js.getModifiers());
        break;
    case METHOD:
        xw.field("NAME", js.getReportName());
        xw.field("TYPE", "Function");
        xw.field("FLAGS", js.getModifiers());
        break;
    case CLASS:
    case INTERFACE:
    case ENUM:
        String typ = "Class";
        if (js.getType() == null)
            typ = "Class";
        else if (js.getType().isInterfaceType())
            typ = "Interface";
        else if (js.getType().isEnumType())
            typ = "Enum";
        else if (js.getType().isThrowable())
            typ = "Exception";
        xw.field("NAME", js.getFullName());
        xw.field("TYPE", typ);
        xw.field("FLAGS", js.getModifiers());
        break;
    case ANNOTATION:
    case LOCAL:
    case NONE:
    case PACKAGE:
        break;
    }
    xw.end("ITEM");
}

From source file:edu.brown.cs.bubbles.rebase.newjava.RebaseJcompSemantics.java

License:Open Source License

/********************************************************************************/

@Override/*from w w w . j a  v  a 2  s . c o m*/
public void getTextRegions(String text, String cls, boolean pfx, boolean statics, boolean compunit,
        boolean imports, boolean pkgfg, boolean topdecls, boolean fields, boolean all, IvyXmlWriter xw)
        throws RebaseException {
    CompilationUnit cu = getAstNode();
    if (cu == null)
        throw new RebaseException("Can't get compilation unit for " + getFile().getFileName());

    List<?> typs = cu.types();
    AbstractTypeDeclaration atd = findTypeDecl(cls, typs);
    int start = 0;
    if (atd != null && atd != typs.get(0))
        start = cu.getExtendedStartPosition(atd);

    if (compunit) {
        xw.begin("RANGE");
        xw.field("PATH", getFile().getFileName());
        xw.field("START", 0);
        int ln = text.length();
        xw.field("END", ln);
        xw.end("RANGE");
    }

    if (pfx && atd != null) {
        int xpos = cu.getExtendedStartPosition(atd);
        int xlen = cu.getExtendedLength(atd);
        int spos = atd.getStartPosition();
        int len = atd.getLength();
        int epos = -1;
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            int apos = cu.getExtendedStartPosition(an);
            if (epos < 0 || epos >= apos)
                epos = apos - 1;
        }
        if (epos < 0) { // no body declarations
            xw.begin("RANGE");
            xw.field("PATH", getFile().getFileName());
            xw.field("START", start);
            xw.field("END", xpos + xlen);
            xw.end("RANGE");
        } else {
            xw.begin("RANGE");
            xw.field("PATH", getFile().getFileName());
            xw.field("START", start);
            xw.field("END", epos);
            xw.end("RANGE");
            xw.begin("RANGE");
            xw.field("PATH", getFile().getFileName());
            xw.field("START", spos + len - 1);
            xw.field("END", xpos + xlen);
            xw.end("RANGE");
        }
    }

    if (pkgfg) {
        PackageDeclaration pkg = cu.getPackage();
        if (pkg != null) {
            outputRange(cu, pkg, xw);
        }
    }

    if (imports) {
        for (Iterator<?> it = cu.imports().iterator(); it.hasNext();) {
            ImportDeclaration id = (ImportDeclaration) it.next();
            outputRange(cu, id, xw);
        }
    }

    if (topdecls && atd != null) {
        int spos = atd.getStartPosition();
        int len = atd.getLength();
        int epos = -1;
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            int apos = cu.getExtendedStartPosition(an);
            if (epos < 0 || epos >= apos)
                epos = apos - 1;
        }
        if (epos < 0) { // no body declarations
            xw.begin("RANGE");
            xw.field("PATH", getFile().getFileName());
            xw.field("START", spos);
            xw.field("END", spos + len);
            xw.end("RANGE");
        } else {
            xw.begin("RANGE");
            xw.field("PATH", getFile().getFileName());
            xw.field("START", spos);
            xw.field("END", epos);
            xw.end("RANGE");
        }
    }

    if ((statics || all) && atd != null) {
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            if (an.getNodeType() == ASTNode.INITIALIZER) {
                outputRange(cu, an, xw);
            }
        }
    }

    if (fields && atd != null) {
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            if (an.getNodeType() == ASTNode.FIELD_DECLARATION) {
                outputRange(cu, an, xw);
            }
        }
    }

    if (all && atd != null) {
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            JcompSymbol elt = null;
            switch (an.getNodeType()) {
            case ASTNode.ANNOTATION_TYPE_DECLARATION:
            case ASTNode.ENUM_DECLARATION:
            case ASTNode.TYPE_DECLARATION:
                elt = JcompAst.getDefinition(an);
                break;
            case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
                break;
            case ASTNode.ENUM_CONSTANT_DECLARATION:
                elt = JcompAst.getDefinition(an);
                break;
            case ASTNode.FIELD_DECLARATION:
                FieldDeclaration fdecl = (FieldDeclaration) an;
                for (Iterator<?> it = fdecl.fragments().iterator(); it.hasNext();) {
                    VariableDeclarationFragment vdf = (VariableDeclarationFragment) it.next();
                    JcompSymbol velt = JcompAst.getDefinition(vdf);
                    if (velt != null)
                        RebaseJcompProject.outputNameData(getFile(), velt, xw);
                }
                break;
            case ASTNode.INITIALIZER:
                break;
            case ASTNode.METHOD_DECLARATION:
                elt = JcompAst.getDefinition(an);
                break;
            default:
                break;
            }
            if (elt != null)
                RebaseJcompProject.outputNameData(getFile(), elt, xw);
        }
    }
}

From source file:edu.brown.cs.bubbles.rebase.newjava.RebaseJcompSemantics.java

License:Open Source License

private void outputRange(CompilationUnit cu, ASTNode an, IvyXmlWriter xw) {
    int xpos = cu.getExtendedStartPosition(an);
    int xlen = cu.getExtendedLength(an);
    xw.begin("RANGE");
    xw.field("PATH", getFile().getFileName());
    xw.field("START", xpos);
    xw.field("END", xpos + xlen);
    xw.end("RANGE");
}

From source file:edu.brown.cs.ivy.jcomp.JcompProjectImpl.java

License:Open Source License

/********************************************************************************/

@Override//from w  w w  . j  a va2s .c o m
public JcompSymbol getContainer(JcompSemantics js, int soff, int eoff) {
    CompilationUnit cu = js.getRootNode();
    if (cu == null)
        return null;

    for (Object o : cu.types()) {
        AbstractTypeDeclaration atd = (AbstractTypeDeclaration) o;
        int spos = cu.getExtendedStartPosition(atd);
        if (spos <= soff && spos + cu.getExtendedLength(atd) >= eoff) {
            return getContainer(cu, atd, soff, eoff);
        }
    }

    return null;
}

From source file:edu.brown.cs.ivy.jcomp.JcompProjectImpl.java

License:Open Source License

private JcompSymbol getContainer(CompilationUnit cu, AbstractTypeDeclaration atd, int soff, int eoff) {
    JcompSymbol sym = JcompAst.getDefinition(atd);

    for (Object o : atd.bodyDeclarations()) {
        BodyDeclaration bd = (BodyDeclaration) o;
        int spos = cu.getExtendedStartPosition(bd);
        if (spos <= soff && spos + cu.getExtendedLength(bd) >= eoff) {
            switch (bd.getNodeType()) {
            case ASTNode.TYPE_DECLARATION:
            case ASTNode.ANNOTATION_TYPE_DECLARATION:
            case ASTNode.ENUM_DECLARATION:
                return getContainer(cu, (AbstractTypeDeclaration) bd, soff, eoff);
            case ASTNode.ENUM_CONSTANT_DECLARATION:
            case ASTNode.FIELD_DECLARATION:
            case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
            case ASTNode.INITIALIZER:
            case ASTNode.METHOD_DECLARATION:
                return sym;
            }/*from  ww w  .  j  av  a2s .  c o m*/
        }
    }

    return sym;
}

From source file:org.eclipse.modisco.java.discoverer.internal.io.java.CommentsManager.java

License:Open Source License

/**
 * @param jdtNode/*from   w ww.j  a va 2 s .co  m*/
 * @param cuJdtNode
 * @param visitor
 * @return
 */
private List<Comment> computeListOfcommentsAfter(final org.eclipse.jdt.core.dom.ASTNode jdtNode,
        final CompilationUnit cuJdtNode, final JDTVisitor visitor) {
    List<Comment> result = new ArrayList<Comment>();
    int index = cuJdtNode.lastTrailingCommentIndex(jdtNode);
    if (index != -1) {
        /*
         * we have to retrieve all comments which start position is more
         * than basic end position of jdt node. Or the opposite way, all
         * comments which ended before the extended end position of jdt
         * node.
         */
        int endPosition = cuJdtNode.getExtendedStartPosition(jdtNode) + cuJdtNode.getExtendedLength(jdtNode);
        for (int i = index; i > -1; i--) {
            org.eclipse.jdt.core.dom.ASTNode jdtComment = (org.eclipse.jdt.core.dom.ASTNode) cuJdtNode
                    .getCommentList().get(i);
            int commentEndPosition = (cuJdtNode.getExtendedStartPosition(jdtComment)
                    + cuJdtNode.getExtendedLength(jdtComment));
            if (this.debug) {
                System.out.println("end position = " + endPosition); //$NON-NLS-1$
                System.out.println("comment end position = " + commentEndPosition); //$NON-NLS-1$
            }
            String whitespaces = null;
            if (endPosition > commentEndPosition) {
                whitespaces = visitor.getJavaContent().substring(commentEndPosition, endPosition).trim();
            }
            if ((whitespaces == null) || (whitespaces.length() == 0)) {
                // shift the end position to manage further comments
                endPosition = cuJdtNode.getExtendedStartPosition(jdtComment);
                Comment comment = visitor.getCommentsBinding().get(jdtComment);
                if (comment != null) {
                    result.add(0, comment);
                }
            } else {
                // stop iteration (we are inside or before the jdt node)
                i = -1;
            }
        }
    }
    if (this.debug) {
        System.out.println("number of comments after the node = " + result.size()); //$NON-NLS-1$
    }
    return result;
}