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

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

Introduction

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

Prototype

public int getExtendedStartPosition(ASTNode node) 

Source Link

Document

Returns the extended start position 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 w  w  w  .  jav a2  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 va 2 s.  c o  m*/
    }

    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;
            }//  ww  w.jav a  2 s .c o m
        }
    }

    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;// w w  w. j  av a 2s  .c  o  m

    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/*  w w  w.jav a2  s .  c om*/
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//  w  ww .j  av a  2s  .  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 a  v a 2  s.  c  o  m
        }
    }

    return sym;
}

From source file:org.eclipse.jdt.internal.core.CopyResourceElementsOperation.java

License:Open Source License

private void updatePackageStatement(CompilationUnit astCU, String[] pkgName, ASTRewrite rewriter,
        ICompilationUnit cu) throws JavaModelException {
    boolean defaultPackage = pkgName.length == 0;
    AST ast = astCU.getAST();//from  w w w.  ja v  a2s  .  c om
    if (defaultPackage) {
        // remove existing package statement
        PackageDeclaration pkg = astCU.getPackage();
        if (pkg != null) {
            int pkgStart;
            Javadoc javadoc = pkg.getJavadoc();
            if (javadoc != null) {
                pkgStart = javadoc.getStartPosition() + javadoc.getLength() + 1;
            } else {
                pkgStart = pkg.getStartPosition();
            }
            int extendedStart = astCU.getExtendedStartPosition(pkg);
            if (pkgStart != extendedStart) {
                // keep the comments associated with package declaration
                // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757
                String commentSource = cu.getSource().substring(extendedStart, pkgStart);
                ASTNode comment = rewriter.createStringPlaceholder(commentSource, ASTNode.PACKAGE_DECLARATION);
                rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, comment, null);
            } else {
                rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, null, null);
            }
        }
    } else {
        org.eclipse.jdt.core.dom.PackageDeclaration pkg = astCU.getPackage();
        if (pkg != null) {
            // rename package statement
            Name name = ast.newName(pkgName);
            rewriter.set(pkg, PackageDeclaration.NAME_PROPERTY, name, null);
        } else {
            // create new package statement
            pkg = ast.newPackageDeclaration();
            pkg.setName(ast.newName(pkgName));
            rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, pkg, null);
        }
    }
}