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

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

Introduction

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

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:lombok.eclipse.handlers.Eclipse.java

License:Open Source License

public static void deleteImport(final EclipseNode node, final String name, final boolean deleteStatic) {
    CompilationUnitDeclaration unit = (CompilationUnitDeclaration) node.top().get();
    List<ImportReference> newImports = new ArrayList<ImportReference>();
    for (ImportReference imp0rt : Each.elementIn(unit.imports)) {
        boolean delete = ((deleteStatic || !imp0rt.isStatic()) && imp0rt.toString().equals(name));
        if (!delete)
            newImports.add(imp0rt);//from  w  w w  . ja v a2  s  .c  o m
    }
    unit.imports = newImports.toArray(new ImportReference[newImports.size()]);
}

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;
            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;
            }//from   w  ww.  j  ava 2s .com
            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:spoon.support.compiler.jdt.JDTImportBuilder.java

License:Open Source License

void build() {
    // sets the imports of the Spoon compilation unit corresponding to `declarationUnit`

    if (declarationUnit.imports == null || declarationUnit.imports.length == 0) {
        return;/*from w  w  w .  j  av  a 2  s. co m*/
    }

    for (ImportReference importRef : declarationUnit.imports) {
        String importName = importRef.toString();
        if (!importRef.isStatic()) {
            if (importName.endsWith("*")) {
                int lastDot = importName.lastIndexOf('.');
                String packageName = importName.substring(0, lastDot);

                // only get package from the model by traversing from rootPackage the model
                // it does not use reflection to achieve that
                CtPackage ctPackage = this.factory.Package().get(packageName);

                if (ctPackage != null) {
                    this.imports.add(createImportWithPosition(ctPackage.getReference(), importRef));
                } else {
                    if (factory.getEnvironment().getNoClasspath()) {
                        this.imports.add(createUnresolvedImportWithPosition(importName, false, importRef));
                    }
                }

            } else {
                CtType klass = this.getOrLoadClass(importName);
                if (klass != null) {
                    this.imports.add(createImportWithPosition(klass.getReference(), importRef));
                } else {
                    if (factory.getEnvironment().getNoClasspath()) {
                        this.imports.add(createUnresolvedImportWithPosition(importName, false, importRef));
                    }
                }
            }
        } else {
            int lastDot = importName.lastIndexOf('.');
            String className = importName.substring(0, lastDot);
            String methodOrFieldName = importName.substring(lastDot + 1);

            CtType klass = this.getOrLoadClass(className);
            if (klass != null) {
                if ("*".equals(methodOrFieldName)) {
                    this.imports.add(createImportWithPosition(
                            factory.Type().createTypeMemberWildcardImportReference(klass.getReference()),
                            importRef));
                } else {
                    List<CtNamedElement> methodOrFields = klass
                            .getElements(new NamedElementFilter<>(CtNamedElement.class, methodOrFieldName));

                    if (!methodOrFields.isEmpty()) {
                        CtNamedElement methodOrField = methodOrFields.get(0);
                        this.imports.add(createImportWithPosition(methodOrField.getReference(), importRef));
                    }
                }
            } else {
                if (factory.getEnvironment().getNoClasspath()) {
                    this.imports.add(createUnresolvedImportWithPosition(importName, true, importRef));
                }
            }
        }
    }

    spoonUnit.setImports(this.imports);
}