Search in sources :

Example 16 with PackageDeclaration

use of org.eclipse.jdt.core.dom.PackageDeclaration in project bndtools by bndtools.

the class ImportPackageQuickFixProcessor method getCorrections.

@Override
public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IProblemLocation[] locations) throws CoreException {
    Set<String> pkgs = new HashSet<>(locations.length * 2 + 1);
    for (IProblemLocation location : locations) {
        Name name;
        switch(location.getProblemId()) {
            case IProblem.IsClassPathCorrect:
                name = getPackageFromIsClassPathCorrect(context, location);
                break;
            case IProblem.ImportNotFound:
                name = getPackageFromImportNotFound(context, location);
                break;
            case IProblem.UndefinedType:
                name = getPackageFromUndefinedType(context, location);
                break;
            default:
                continue;
        }
        if (name == null) {
            continue;
        }
        final String pkg = name.getFullyQualifiedName();
        // Don't suggest adding a bundle to fix missing package in import if current Compilation Unit
        // is already part of that package.
        final PackageDeclaration ourPkg = context.getASTRoot().getPackage();
        if (ourPkg != null && pkg.equals(ourPkg.getName().getFullyQualifiedName())) {
            continue;
        }
        pkgs.add(pkg);
    }
    if (pkgs.isEmpty()) {
        return null;
    }
    return getSuggestions(pkgs, context);
}
Also used : IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration) HashSet(java.util.HashSet) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 17 with PackageDeclaration

use of org.eclipse.jdt.core.dom.PackageDeclaration in project sts4 by spring-projects.

the class ImportRewrite method getAddedImportsInsertLocation.

/**
 * Reads the positions of each existing import declaration along with any associated comments,
 * and returns these in a list whose iteration order reflects the existing order of the imports
 * in the compilation unit.
 */
private int getAddedImportsInsertLocation() {
    List<ImportDeclaration> importDeclarations = astRoot.imports();
    if (importDeclarations == null) {
        importDeclarations = Collections.emptyList();
    }
    List<Comment> comments = astRoot.getCommentList();
    int currentCommentIndex = 0;
    // Skip over package and file header comments (see https://bugs.eclipse.org/121428).
    ImportDeclaration firstImport = importDeclarations.get(0);
    PackageDeclaration packageDeclaration = astRoot.getPackage();
    int firstImportStartPosition = packageDeclaration == null ? firstImport.getStartPosition() : astRoot.getExtendedStartPosition(packageDeclaration) + astRoot.getExtendedLength(packageDeclaration);
    while (currentCommentIndex < comments.size() && comments.get(currentCommentIndex).getStartPosition() < firstImportStartPosition) {
        currentCommentIndex++;
    }
    int previousExtendedEndPosition = -1;
    for (ImportDeclaration currentImport : importDeclarations) {
        int extendedEndPosition = astRoot.getExtendedStartPosition(currentImport) + astRoot.getExtendedLength(currentImport);
        int commentAfterImportIndex = currentCommentIndex;
        while (commentAfterImportIndex < comments.size() && comments.get(commentAfterImportIndex).getStartPosition() < extendedEndPosition) {
            commentAfterImportIndex++;
        }
        currentCommentIndex = commentAfterImportIndex;
        previousExtendedEndPosition = extendedEndPosition;
    }
    return previousExtendedEndPosition;
}
Also used : Comment(org.eclipse.jdt.core.dom.Comment) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration)

Aggregations

PackageDeclaration (org.eclipse.jdt.core.dom.PackageDeclaration)17 ImportDeclaration (org.eclipse.jdt.core.dom.ImportDeclaration)9 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)8 HashSet (java.util.HashSet)6 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 ASTParser (org.eclipse.jdt.core.dom.ASTParser)4 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Set (java.util.Set)3 Name (org.eclipse.jdt.core.dom.Name)3 NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)3 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)3 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)3 HashMap (java.util.HashMap)2 Nullable (javax.annotation.Nullable)2 AST (org.eclipse.jdt.core.dom.AST)2 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)2