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

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

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ImportDeclaration 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:com.windowtester.eclipse.ui.convert.WTConvertAPIContext.java

License:Open Source License

/**
 * Construct a new import declaration//from w ww .  ja  va  2  s.c o  m
 * 
 * @param typeName the fully qualified type name (not <code>null</code>, not empty)
 * @param isStatic <code>true</code> if the import should be a static import
 * @return the import declaration (not <code>null</code>)
 */
private ImportDeclaration newImport(String typeName, boolean isStatic) {
    ImportDeclaration newImport = compUnit.getAST().newImportDeclaration();
    newImport.setStatic(isStatic);
    int start = 7; // 7 = position after the "import" keyword and one space
    if (isStatic)
        start += 7; // add length of "static" keyword and one space
    newImport.setName(newName(typeName, start));
    newImport.setSourceRange(0, newImport.toString().trim().length());
    return newImport;
}

From source file:com.windowtester.eclipse.ui.convert.WTConvertAPIContextNodeListTest.java

License:Open Source License

public void testInsert() throws Exception {
    testAPI("contextAPI/nodelist", "NewContactSwingTest.txt", "insert", new APITest() {
        public void before(WTConvertAPIContext context) {
        }// w  w  w  .j a  v  a  2 s .c om

        @SuppressWarnings("unchecked")
        public void test(WTConvertAPIContext context, CompilationUnit compUnit) {
            Name newName = context.newName("com.windowtester.runtime.swing.locator.JButtonLocator", 7);
            ImportDeclaration newImport = compUnit.getAST().newImportDeclaration();
            newImport.setName(newName);
            newImport.setSourceRange(0, newImport.toString().trim().length());
            context.insert(compUnit, compUnit.imports(), 0, newImport);
        }

        public void after(WTConvertAPIContext context) {
        }
    });
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

public ImportDeclaration convertImport(org.eclipse.jdt.internal.compiler.ast.ImportReference importReference) {
    final ImportDeclaration importDeclaration = new ImportDeclaration(this.ast);
    final boolean onDemand = (importReference.bits
            & org.eclipse.jdt.internal.compiler.ast.ASTNode.OnDemand) != 0;
    final char[][] tokens = importReference.tokens;
    int length = importReference.tokens.length;
    final long[] positions = importReference.sourcePositions;
    if (length > 1) {
        importDeclaration.setName(setQualifiedNameNameAndSourceRanges(tokens, positions, importReference));
    } else {//from   w  w  w . j  av  a2  s . c  o m
        final SimpleName name = new SimpleName(this.ast);
        name.internalSetIdentifier(new String(tokens[0]));
        final int start = (int) (positions[0] >>> 32);
        final int end = (int) (positions[0] & 0xFFFFFFFF);
        name.setSourceRange(start, end - start + 1);
        name.index = 1;
        importDeclaration.setName(name);
        if (this.resolveBindings) {
            recordNodes(name, importReference);
        }
    }
    importDeclaration.setSourceRange(importReference.declarationSourceStart,
            importReference.declarationEnd - importReference.declarationSourceStart + 1);
    importDeclaration.setOnDemand(onDemand);
    int modifiers = importReference.modifiers;
    if (modifiers != ClassFileConstants.AccDefault) {
        switch (this.ast.apiLevel) {
        case AST.JLS2_INTERNAL:
            importDeclaration.setFlags(importDeclaration.getFlags() | ASTNode.MALFORMED);
            break;
        default:
            if (modifiers == ClassFileConstants.AccStatic) {
                importDeclaration.setStatic(true);
            } else {
                importDeclaration.setFlags(importDeclaration.getFlags() | ASTNode.MALFORMED);
            }
        }
    }
    if (this.resolveBindings) {
        recordNodes(importDeclaration, importReference);
    }
    return importDeclaration;
}