Search in sources :

Example 36 with IProblemLocation

use of org.eclipse.jdt.ui.text.java.IProblemLocation in project bndtools by bndtools.

the class ImportPackageQuickFixProcessorTest method getCorrections_forIsClassPath_withClassInMainNamespace_returnsNull.

@Test
public void getCorrections_forIsClassPath_withClassInMainNamespace_returnsNull() {
    List<IProblemLocation> locs = getProblems("package other; public class Test { Test2 test = new `Test2()'; }", new ProblemLocation(0, 0, IsClassPathCorrect, new String[] { "Test" }, true, JDT_PROBLEM));
    assertThat(proposalsFor(locs)).isNull();
}
Also used : IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) ProblemLocation(org.eclipse.jdt.internal.ui.text.correction.ProblemLocation) Test(org.junit.Test)

Example 37 with IProblemLocation

use of org.eclipse.jdt.ui.text.java.IProblemLocation 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 38 with IProblemLocation

use of org.eclipse.jdt.ui.text.java.IProblemLocation in project bndtools by bndtools.

the class PackageInfoBaselineQuickFixProcessor method getCorrections.

@Override
public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IProblemLocation[] locations) throws CoreException {
    ICompilationUnit compUnit = context.getCompilationUnit();
    IResource resource = compUnit.getResource();
    IMarker[] markers = resource.findMarkers(BndtoolsConstants.MARKER_JAVA_BASELINE, false, 1);
    for (IProblemLocation location : locations) {
        for (IMarker marker : markers) {
            int markerStart = marker.getAttribute(IMarker.CHAR_START, -1);
            int markerEnd = marker.getAttribute(IMarker.CHAR_END, -1);
            int markerLength = markerEnd - markerStart;
            if (location.getOffset() <= markerStart && markerStart < location.getOffset() + location.getLength()) {
                String newVersion = marker.getAttribute("suggestedVersion", null);
                if (newVersion != null) {
                    StringBuilder quotedVersion = new StringBuilder(newVersion.trim());
                    if (quotedVersion.charAt(0) != '"')
                        quotedVersion.insert(0, '"');
                    if (quotedVersion.charAt(quotedVersion.length() - 1) != '"')
                        quotedVersion.append('"');
                    CompilationUnitChange change = new CompilationUnitChange("Change package-info.java", compUnit);
                    change.setEdit(new ReplaceEdit(markerStart, markerLength, quotedVersion.toString()));
                    return new IJavaCompletionProposal[] { new ChangeCorrectionProposal("Change package version to " + newVersion, change, 1000) };
                }
            }
        }
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IMarker(org.eclipse.core.resources.IMarker) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) IResource(org.eclipse.core.resources.IResource) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 39 with IProblemLocation

use of org.eclipse.jdt.ui.text.java.IProblemLocation in project flux by eclipse.

the class QuickFixProcessor method getCorrections.

/* (non-Javadoc)
	 * @see IAssistProcessor#getCorrections(org.eclipse.jdt.internal.ui.text.correction.IAssistContext, org.eclipse.jdt.internal.ui.text.correction.IProblemLocation[])
	 */
public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IProblemLocation[] locations) throws CoreException {
    if (locations == null || locations.length == 0) {
        return null;
    }
    HashSet<Integer> handledProblems = new HashSet<Integer>(locations.length);
    ArrayList<ICommandAccess> resultingCollections = new ArrayList<ICommandAccess>();
    for (int i = 0; i < locations.length; i++) {
        IProblemLocation curr = locations[i];
        Integer id = new Integer(curr.getProblemId());
        if (handledProblems.add(id)) {
            process(context, curr, resultingCollections);
        }
    }
    return resultingCollections.toArray(new IJavaCompletionProposal[resultingCollections.size()]);
}
Also used : ICommandAccess(org.eclipse.jdt.ui.text.java.correction.ICommandAccess) ArrayList(java.util.ArrayList) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) HashSet(java.util.HashSet)

Aggregations

IProblemLocation (org.eclipse.jdt.ui.text.java.IProblemLocation)39 ArrayList (java.util.ArrayList)15 ProblemLocation (org.eclipse.jdt.internal.ui.text.correction.ProblemLocation)15 Test (org.junit.Test)10 IProblem (org.eclipse.jdt.core.compiler.IProblem)8 ASTNode (org.eclipse.jdt.core.dom.ASTNode)7 HashSet (java.util.HashSet)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 IJavaCompletionProposal (org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)4 IResource (org.eclipse.core.resources.IResource)2 CoreException (org.eclipse.core.runtime.CoreException)2 IStatus (org.eclipse.core.runtime.IStatus)2 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)2 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 ICommandAccess (org.eclipse.jdt.ui.text.java.correction.ICommandAccess)2 ArrayDeque (java.util.ArrayDeque)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 LinkedHashSet (java.util.LinkedHashSet)1