Example usage for org.eclipse.jdt.internal.compiler.ast ImportReference ImportReference

List of usage examples for org.eclipse.jdt.internal.compiler.ast ImportReference ImportReference

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast ImportReference ImportReference.

Prototype

public ImportReference(char[][] tokens, long[] sourcePositions, boolean onDemand, int modifiers) 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryTypeConverter.java

License:Open Source License

public ImportReference[] buildImports(ClassFileReader reader) {
    // add remaining references to the list of type names
    // (code extracted from BinaryIndexer#extractReferenceFromConstantPool(...))
    int[] constantPoolOffsets = reader.getConstantPoolOffsets();
    int constantPoolCount = constantPoolOffsets.length;
    for (int i = 0; i < constantPoolCount; i++) {
        int tag = reader.u1At(constantPoolOffsets[i]);
        char[] name = null;
        switch (tag) {
        case ClassFileConstants.MethodRefTag:
        case ClassFileConstants.InterfaceMethodRefTag:
            int constantPoolIndex = reader.u2At(constantPoolOffsets[i] + 3);
            int utf8Offset = constantPoolOffsets[reader.u2At(constantPoolOffsets[constantPoolIndex] + 3)];
            name = reader.utf8At(utf8Offset + 3, reader.u2At(utf8Offset + 1));
            break;
        case ClassFileConstants.ClassTag:
            utf8Offset = constantPoolOffsets[reader.u2At(constantPoolOffsets[i] + 1)];
            name = reader.utf8At(utf8Offset + 3, reader.u2At(utf8Offset + 1));
            break;
        }//from  w ww  . j ava  2s.  c o m
        if (name == null || (name.length > 0 && name[0] == '['))
            break; // skip over array references
        this.typeNames.add(CharOperation.splitOn('/', name));
    }

    // convert type names into import references
    int typeNamesLength = this.typeNames.size();
    ImportReference[] imports = new ImportReference[typeNamesLength];
    char[][][] set = this.typeNames.set;
    int index = 0;
    for (int i = 0, length = set.length; i < length; i++) {
        char[][] typeName = set[i];
        if (typeName != null) {
            imports[index++] = new ImportReference(typeName, new long[typeName.length]/*dummy positions*/,
                    false/*not on demand*/, 0);
        }
    }
    return imports;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.BinaryTypeConverter.java

License:Open Source License

/**
 * Convert a binary type into an AST type declaration and put it in the given compilation unit.
 *//* w  ww .  j  a  v a  2  s . c  o  m*/
public TypeDeclaration buildTypeDeclaration(IType type, CompilationUnitDeclaration compilationUnit)
        throws JavaModelException {
    PackageFragment pkg = (PackageFragment) type.getPackageFragment();
    char[][] packageName = Util.toCharArrays(pkg.names);

    if (packageName.length > 0) {
        compilationUnit.currentPackage = new ImportReference(packageName, new long[] { 0 }, false,
                ClassFileConstants.AccDefault);
    }

    /* convert type */
    TypeDeclaration typeDeclaration = convert(type, null, null);

    IType alreadyComputedMember = type;
    IType parent = type.getDeclaringType();
    TypeDeclaration previousDeclaration = typeDeclaration;
    while (parent != null) {
        TypeDeclaration declaration = convert(parent, alreadyComputedMember, previousDeclaration);

        alreadyComputedMember = parent;
        previousDeclaration = declaration;
        parent = parent.getDeclaringType();
    }

    compilationUnit.types = new TypeDeclaration[] { previousDeclaration };

    return typeDeclaration;
}

From source file:org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.java

License:Open Source License

protected void createImports(ModuleNode moduleNode) {
    List<ImportNode> importNodes = moduleNode.getImports();
    List<ImportNode> importPackages = ImportNodeCompatibilityWrapper.getStarImports(moduleNode);
    Map<String, ImportNode> importStatics = ImportNodeCompatibilityWrapper.getStaticImports(moduleNode);
    Map<String, ImportNode> importStaticStars = ImportNodeCompatibilityWrapper.getStaticStarImports(moduleNode);
    if (importNodes.size() > 0 || importPackages.size() > 0 || importStatics.size() > 0
            || importStaticStars.size() > 0) {
        List<ImportReference> importReferences = new ArrayList<ImportReference>();
        for (ImportNode importNode : importNodes) {
            char[][] splits = CharOperation.splitOn('.', importNode.getClassName().toCharArray());
            ImportReference ref = null;//  w ww. j av a  2s. c  o  m
            ClassNode type = importNode.getType();
            int typeStartOffset = startOffset(type);
            int typeEndOffset = endOffset(type);
            if (typeStartOffset == 0) {
                // not a real import, a fake one created during recovery
                continue;
            }
            if (importNode.getAlias() != null && importNode.getAlias().length() > 0) {
                // FIXASC will need extra positional info for the 'as' and the alias
                ref = new AliasImportReference(importNode.getAlias().toCharArray(), splits,
                        positionsFor(splits, typeStartOffset, typeEndOffset), false,
                        ClassFileConstants.AccDefault);
            } else {
                ref = new ImportReference(splits, positionsFor(splits, typeStartOffset, typeEndOffset), false,
                        ClassFileConstants.AccDefault);
            }
            ref.sourceEnd = Math.max(typeEndOffset - 1, ref.sourceStart); // For error reporting, Eclipse wants -1
            int start = importNode.getStart();
            ref.declarationSourceStart = start;
            int end = importNode.getEnd();
            ref.declarationSourceEnd = end;

            ref.declarationEnd = ref.sourceEnd;
            importReferences.add(ref);
        }

        for (ImportNode importPackage : importPackages) {
            String importText = importPackage.getText();

            // when calculating these offsets, assume no extraneous whitespace
            int packageStartOffset = importPackage.getStart() + "import ".length();
            int packageEndOffset = packageStartOffset + importText.length() - "import ".length()
                    - ".*".length();

            char[][] splits = CharOperation.splitOn('.', importPackage.getPackageName()
                    .substring(0, importPackage.getPackageName().length() - 1).toCharArray());
            ImportReference ref = new ImportReference(splits,
                    positionsFor(splits, packageStartOffset, packageEndOffset), true,
                    ClassFileConstants.AccDefault);
            // import * style only have slocs for the entire ImportNode and not for the embedded type
            ref.sourceEnd = packageEndOffset;
            ref.declarationSourceStart = importPackage.getStart();
            ref.declarationSourceEnd = importPackage.getEnd();
            ref.declarationEnd = ref.sourceEnd;
            importReferences.add(ref);
        }
        for (Map.Entry<String, ImportNode> importStatic : importStatics.entrySet()) {
            ImportNode importNode = importStatic.getValue();
            String importName = importNode.getClassName() + "." + importStatic.getKey();
            char[][] splits = CharOperation.splitOn('.', importName.toCharArray());

            ImportReference ref = null;
            ClassNode type = importNode.getType();
            int typeStartOffset = startOffset(type);
            int typeEndOffset = endOffset(type);
            if (importNode.getAlias() != null && importNode.getAlias().length() > 0) {
                // FIXASC will need extra positional info for the 'as' and the alias
                ref = new AliasImportReference(importNode.getAlias().toCharArray(), splits,
                        positionsFor(splits, typeStartOffset, typeEndOffset), false,
                        ClassFileConstants.AccDefault | ClassFileConstants.AccStatic);
            } else {
                ref = new ImportReference(splits, positionsFor(splits, typeStartOffset, typeEndOffset), false,
                        ClassFileConstants.AccDefault | ClassFileConstants.AccStatic);
            }
            ref.sourceEnd = Math.max(typeEndOffset - 1, ref.sourceStart); // For error reporting, Eclipse wants -1
            ref.declarationSourceStart = importNode.getStart();
            ref.declarationSourceEnd = importNode.getEnd();
            ref.declarationEnd = ref.sourceEnd;
            importReferences.add(ref);
        }
        for (Map.Entry<String, ImportNode> importStaticStar : importStaticStars.entrySet()) {
            String classname = importStaticStar.getKey();
            ImportNode importNode = importStaticStar.getValue();
            ClassNode importedType = importNode.getType();
            int typeStartOffset = importedType != null ? startOffset(importedType) : 0;
            int typeEndOffset = importedType != null ? endOffset(importedType) : 0;
            char[][] splits = CharOperation.splitOn('.', classname.toCharArray());
            ImportReference ref = new ImportReference(splits,
                    positionsFor(splits, typeStartOffset, typeEndOffset), true,
                    ClassFileConstants.AccDefault | ClassFileConstants.AccStatic);
            ref.sourceEnd = Math.max(typeEndOffset - 1, ref.sourceStart); // For error reporting, Eclipse wants -1
            ref.declarationSourceStart = importNode.getStart();
            ref.declarationSourceEnd = importNode.getEnd();
            ref.declarationEnd = ref.sourceEnd;
            importReferences.add(ref);
        }

        // ensure proper lexical order
        if (importReferences.size() != 0) {
            imports = importReferences.toArray(new ImportReference[importReferences.size()]);
            Arrays.sort(imports, new Comparator<ImportReference>() {
                public int compare(ImportReference left, ImportReference right) {
                    return left.sourceStart - right.sourceStart;
                }
            });
            for (ImportReference ref : imports) {
                if (ref.declarationSourceStart > 0
                        && (ref.declarationEnd - ref.declarationSourceStart + 1) < 0) {
                    throw new IllegalStateException(
                            "Import reference alongside class " + moduleNode.getClasses().get(0)
                                    + " will trigger later failure: " + ref.toString() + " declSourceStart="
                                    + ref.declarationSourceStart + " declEnd=" + +ref.declarationEnd);
                }

            }
        }
    }
}

From source file:org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.java

License:Open Source License

/**
 * Build a JDT package declaration based on the groovy one
 *///from www .  j  ava  2 s.  co m
private void createPackageDeclaration(ModuleNode moduleNode) {
    if (moduleNode.hasPackageName()) {
        PackageNode packageNode = moduleNode.getPackage();// Node();
        String packageName = moduleNode.getPackageName();
        if (packageName.endsWith(".")) {
            packageName = packageName.substring(0, packageName.length() - 1);
        }
        long start = startOffset(packageNode);
        long end = endOffset(packageNode);
        char[][] packageReference = CharOperation.splitOn('.', packageName.toCharArray());
        currentPackage = new ImportReference(packageReference, positionsFor(packageReference, start, end), true,
                ClassFileConstants.AccDefault);
        currentPackage.declarationSourceStart = currentPackage.sourceStart;
        currentPackage.declarationSourceEnd = currentPackage.sourceEnd;
        currentPackage.declarationEnd = currentPackage.sourceEnd;

        // FIXASC (M3) not right, there may be spaces between package keyword and decl. Just the first example of position
        // problems
        currentPackage.declarationSourceStart = currentPackage.sourceStart - "package ".length();
        currentPackage.declarationEnd = currentPackage.declarationSourceEnd = currentPackage.sourceEnd;
    }
}

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitStructureRequestor.java

License:Open Source License

/**
 * @since 1.6//ww w  . ja  v a2s. c  o m
 */
public void acceptPackage(org.aspectj.org.eclipse.jdt.internal.compiler.ast.ImportReference ir) {
    ImportReference dup = new ImportReference(ir.tokens, ir.sourcePositions,
            (ir.bits & org.aspectj.org.eclipse.jdt.internal.compiler.ast.ASTNode.OnDemand) != 0, ir.modifiers);
    dup.declarationSourceStart = ir.declarationSourceStart;
    dup.declarationSourceEnd = ir.declarationSourceEnd;
    super.acceptPackage(dup);
}

From source file:org.eclipse.ajdt.core.parserbridge.AJSourceElementParser2.java

License:Open Source License

protected void consumeStaticImportOnDemandDeclarationName() {
    // TypeImportOnDemandDeclarationName ::= 'import' 'static' Name '.' '*'
    /* push an ImportRef build from the last name 
    stored in the identifier stack. */

    ImportReference impt;/*from ww w . j  a v a2 s. c o m*/
    int length;
    char[][] tokens = new char[length = this.identifierLengthStack[this.identifierLengthPtr--]][];
    this.identifierPtr -= length;
    long[] positions = new long[length];
    System.arraycopy(this.identifierStack, this.identifierPtr + 1, tokens, 0, length);
    System.arraycopy(this.identifierPositionStack, this.identifierPtr + 1, positions, 0, length);
    pushOnAstStack(impt = new ImportReference(tokens, positions, true, ClassFileConstants.AccStatic));

    this.modifiers = ClassFileConstants.AccDefault;
    this.modifiersSourceStart = -1; // <-- see comment into modifiersFlag(int)

    if (this.currentToken == TokenNameSEMICOLON) {
        impt.declarationSourceEnd = this.scanner.currentPosition - 1;
    } else {
        impt.declarationSourceEnd = impt.sourceEnd;
    }
    impt.declarationEnd = impt.declarationSourceEnd;
    //this.endPosition is just before the ;
    impt.declarationSourceStart = this.intStack[this.intPtr--];

    if (!this.statementRecoveryActivated && options.sourceLevel < ClassFileConstants.JDK1_5
            && this.lastErrorEndPositionBeforeRecovery < this.scanner.currentPosition) {
        impt.modifiers = ClassFileConstants.AccDefault; // convert the static import reference to a non-static importe reference
        this.problemReporter().invalidUsageOfStaticImports(impt);
    }

    // recovery
    if (this.currentElement != null) {
        this.lastCheckPoint = impt.declarationSourceEnd + 1;
        this.currentElement = this.currentElement.add(impt, 0);
        this.lastIgnoredToken = -1;
        this.restartRecovery = true; // used to avoid branching back into the regular automaton      
    }
    if (reportReferenceInfo) {
        requestor.acceptTypeReference(impt.tokens, impt.sourceStart, impt.sourceEnd);
    }
}

From source file:org.eclipse.ajdt.core.parserbridge.AJSourceElementParser2.java

License:Open Source License

protected void consumeTypeImportOnDemandDeclarationName() {
    // TypeImportOnDemandDeclarationName ::= 'import' Name '.' '*'
    /* push an ImportRef build from the last name 
    stored in the identifier stack. */

    ImportReference impt;/*from   www  . ja va 2s.  com*/
    int length;
    char[][] tokens = new char[length = this.identifierLengthStack[this.identifierLengthPtr--]][];
    this.identifierPtr -= length;
    long[] positions = new long[length];
    System.arraycopy(this.identifierStack, this.identifierPtr + 1, tokens, 0, length);
    System.arraycopy(this.identifierPositionStack, this.identifierPtr + 1, positions, 0, length);
    pushOnAstStack(impt = new ImportReference(tokens, positions, true, ClassFileConstants.AccDefault));

    impt.trailingStarPosition = this.intStack[this.intPtr--];
    if (this.currentToken == TokenNameSEMICOLON) {
        impt.declarationSourceEnd = this.scanner.currentPosition - 1;
    } else {
        impt.declarationSourceEnd = impt.sourceEnd;
    }
    impt.declarationEnd = impt.declarationSourceEnd;
    //this.endPosition is just before the ;
    impt.declarationSourceStart = this.intStack[this.intPtr--];

    // recovery
    if (this.currentElement != null) {
        this.lastCheckPoint = impt.declarationSourceEnd + 1;
        this.currentElement = this.currentElement.add(impt, 0);
        this.lastIgnoredToken = -1;
        this.restartRecovery = true; // used to avoid branching back into the regular automaton      
    }
    if (reportReferenceInfo) {
        requestor.acceptUnknownReference(impt.tokens, impt.sourceStart, impt.sourceEnd);
    }
}

From source file:org.eclipse.ajdt.core.parserbridge.AJSourceElementParser2.java

License:Open Source License

protected ImportReference newImportReference(char[][] tokens, long[] positions, boolean onDemand, int mod) {
    return new ImportReference(tokens, positions, onDemand, mod);
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumePackageDeclarationName() {
    // PackageDeclarationName ::= 'package' Name
    /* build an ImportRef build from the last name
    stored in the identifier stack. */

    ImportReference impt;/*  w w  w. ja  v a2s .c  o m*/
    int length;
    char[][] tokens = new char[length = this.identifierLengthStack[this.identifierLengthPtr--]][];
    this.identifierPtr -= length;
    long[] positions = new long[length];
    System.arraycopy(this.identifierStack, ++this.identifierPtr, tokens, 0, length);
    System.arraycopy(this.identifierPositionStack, this.identifierPtr--, positions, 0, length);

    impt = new ImportReference(tokens, positions, false, ClassFileConstants.AccDefault);
    this.compilationUnit.currentPackage = impt;

    if (this.currentToken == TokenNameSEMICOLON) {
        impt.declarationSourceEnd = this.scanner.currentPosition - 1;
    } else {
        impt.declarationSourceEnd = impt.sourceEnd;
    }
    impt.declarationEnd = impt.declarationSourceEnd;
    //this.endPosition is just before the ;
    impt.declarationSourceStart = this.intStack[this.intPtr--];

    // get possible comment source start
    if (this.javadoc != null) {
        impt.declarationSourceStart = this.javadoc.sourceStart;
    }

    // recovery
    if (this.currentElement != null) {
        this.lastCheckPoint = impt.declarationSourceEnd + 1;
        this.restartRecovery = true; // used to avoid branching back into the regular automaton
    }
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumePackageDeclarationNameWithModifiers() {
    // PackageDeclarationName ::= Modifiers 'package' Name
    /* build an ImportRef build from the last name
    stored in the identifier stack. */

    ImportReference impt;//  w ww  .  j  ava 2  s.  c om
    int length;
    char[][] tokens = new char[length = this.identifierLengthStack[this.identifierLengthPtr--]][];
    this.identifierPtr -= length;
    long[] positions = new long[length];
    System.arraycopy(this.identifierStack, ++this.identifierPtr, tokens, 0, length);
    System.arraycopy(this.identifierPositionStack, this.identifierPtr--, positions, 0, length);

    int packageModifiersSourceStart = this.intStack[this.intPtr--]; // we don't need the modifiers start
    int packageModifiers = this.intStack[this.intPtr--];

    impt = new ImportReference(tokens, positions, false, packageModifiers);
    this.compilationUnit.currentPackage = impt;
    // consume annotations
    if ((length = this.expressionLengthStack[this.expressionLengthPtr--]) != 0) {
        System.arraycopy(this.expressionStack, (this.expressionPtr -= length) + 1,
                impt.annotations = new Annotation[length], 0, length);
        impt.declarationSourceStart = packageModifiersSourceStart;
        this.intPtr--; // we don't need the position of the 'package keyword
    } else {
        impt.declarationSourceStart = this.intStack[this.intPtr--];
        // get possible comment source start
        if (this.javadoc != null) {
            impt.declarationSourceStart = this.javadoc.sourceStart;
        }
    }

    if (this.currentToken == TokenNameSEMICOLON) {
        impt.declarationSourceEnd = this.scanner.currentPosition - 1;
    } else {
        impt.declarationSourceEnd = impt.sourceEnd;
    }
    impt.declarationEnd = impt.declarationSourceEnd;

    // recovery
    if (this.currentElement != null) {
        this.lastCheckPoint = impt.declarationSourceEnd + 1;
        this.restartRecovery = true; // used to avoid branching back into the regular automaton
    }
}