Example usage for org.eclipse.jdt.core.dom PackageDeclaration getStartPosition

List of usage examples for org.eclipse.jdt.core.dom PackageDeclaration getStartPosition

Introduction

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

Prototype

public final int getStartPosition() 

Source Link

Document

Returns the character index into the original source file indicating where the source fragment corresponding to this node begins.

Usage

From source file:br.uff.ic.gems.resources.ast.Visitor.java

public int beginLine(PackageDeclaration node) {
    int beginLine = cu.getLineNumber(node.getStartPosition());

    Javadoc javadoc = node.getJavadoc();
    int javadocBegin = INVALID_LINE;

    if (javadoc != null) {
        javadocBegin = cu.getLineNumber(javadoc.getStartPosition());
    }//w ww  .  j av  a 2  s  .  co m

    if (beginLine == javadocBegin) {
        beginLine = cu.getLineNumber(javadoc.getStartPosition() + javadoc.getLength() + 1);
    }

    return beginLine;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

public int beginColunm(PackageDeclaration node) {
    int beginColumn = cu.getColumnNumber(node.getStartPosition());

    Javadoc javadoc = node.getJavadoc();
    int javadocBegin = INVALID_JAVADOC;

    if (javadoc != null) {
        javadocBegin = cu.getColumnNumber(javadoc.getStartPosition());
    }/*from w  ww. j a v a2s  .c  o  m*/

    if (beginColumn == javadocBegin) {
        beginColumn = cu.getColumnNumber(javadoc.getStartPosition() + javadoc.getLength() + 1);
    }

    return beginColumn;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(PackageDeclaration node) {
    int beginLine = beginLine(node);
    int endLine = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int beginColumn = beginColunm(node);
    int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    languageConstructs.add(//from  w ww .j  a v  a  2s.  c  o  m
            new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine, beginColumn, endColumn));

    return true;
}

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   ww  w . jav a2  s .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);
        }
    }
}