Search in sources :

Example 11 with IProblemLocation

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

the class ImportPackageQuickFixProcessorTest method getProblems.

/**
 * Processes "marked source" to generate an AST with associated {@link IProblemLocation} instances. The "marked
 * source" consists of plain Java source, with "`" to mark the start of a problem and "'" to mark the end. This
 * syntax will obviously not handle source with character literals well (or strings with embedded ' characters), but
 * that doesn't matter for this test usage. Marked problems can be nested, but cannot overlap as the closing "'" is
 * always paired with the most recent starting "`".<br>
 * The IDs for the problems are taken from the supplied <tt>problemIds</tt> array. They are applied in the order
 * that the closing "'" characters are found. If the number of supplied problemIds is not equal to the number of
 * problems marked in the source, the method will assert.<br>
 * The method finally calls {@link #setupAST(String)} to set up the AST corresponding to the supplied source after
 * the markers have been stripped.
 *
 * @param markedSource The string containing the marked-up Java source.
 * @param problemIds Array of problem IDs used when building the IProblemLocation objects. It is expected that the
 *            number of elements in this array will match the number of "`'" pairs in the marked source.
 * @return The {@link List} of {@link IProblemLocation} objects that correspond to the markers in the source.
 */
private List<IProblemLocation> getProblems(String markedSource, IProblemLocation... srcProblems) {
    List<IProblemLocation> problems = new ArrayList<>(srcProblems.length);
    StringBuilder source = new StringBuilder(markedSource.length());
    Deque<Integer> starts = new ArrayDeque<>();
    int problemIndex = 0;
    int j = 0;
    for (int i = 0; i < markedSource.length(); i++) {
        char current = markedSource.charAt(i);
        switch(current) {
            case '`':
                starts.push(j);
                break;
            case '\'':
                int currentStart = starts.isEmpty() ? 0 : starts.pop();
                assertThat(problemIndex).as("getProblems() problem count").isLessThan(srcProblems.length);
                final IProblemLocation srcProb = srcProblems[problemIndex++];
                problems.add(new ProblemLocation(currentStart, j - currentStart, srcProb.getProblemId(), srcProb.getProblemArguments(), srcProb.isError(), srcProb.getMarkerType()));
                break;
            default:
                source.append(current);
                j++;
                break;
        }
    }
    assertThat(problems).as("getProblems() array length").hasSize(srcProblems.length);
    setupAST(source.toString());
    return problems;
}
Also used : ArrayList(java.util.ArrayList) 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) ArrayDeque(java.util.ArrayDeque)

Example 12 with IProblemLocation

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

the class QuickAssistService method getProblemLocations.

public IProblemLocation getProblemLocations(ICompilationUnit liveEditUnit, int problemID, int offset, int length) {
    final ASTParser parser = ASTParser.newParser(AST.JLS4);
    // Parse the class as a compilation unit.
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(liveEditUnit);
    parser.setResolveBindings(true);
    // Return the compiled class as a compilation unit
    final CompilationUnit unit = (CompilationUnit) parser.createAST(null);
    IProblem[] problems = unit.getProblems();
    if (problemID > 0) {
        return filterOutProblems(problems, problemID);
    } else {
        IProblemLocation[] locations = convertProblems(unit.getProblems());
        for (IProblemLocation iProblemLocation : locations) {
            if (offset >= iProblemLocation.getOffset() && offset <= (iProblemLocation.getOffset() + iProblemLocation.getLength())) {
                return iProblemLocation;
            }
        }
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 13 with IProblemLocation

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

the class TypeParametersFix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, boolean insertInferredTypeArguments, boolean removeRedundantTypeArguments) {
    IProblem[] problems = compilationUnit.getProblems();
    IProblemLocation[] locations = new IProblemLocation[problems.length];
    for (int i = 0; i < problems.length; i++) {
        locations[i] = new ProblemLocation(problems[i]);
    }
    return createCleanUp(compilationUnit, locations, insertInferredTypeArguments, removeRedundantTypeArguments);
}
Also used : IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) ProblemLocation(org.eclipse.jdt.internal.ui.text.correction.ProblemLocation) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 14 with IProblemLocation

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

the class UnimplementedCodeFix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit root, boolean addMissingMethod, boolean makeTypeAbstract, IProblemLocation[] problems) {
    Assert.isLegal(!addMissingMethod || !makeTypeAbstract);
    if (!addMissingMethod && !makeTypeAbstract)
        return null;
    if (problems.length == 0)
        return null;
    ArrayList<CompilationUnitRewriteOperation> operations = new ArrayList<CompilationUnitRewriteOperation>();
    for (int i = 0; i < problems.length; i++) {
        IProblemLocation problem = problems[i];
        if (addMissingMethod) {
            ASTNode typeNode = getSelectedTypeNode(root, problem);
            if (typeNode != null && !isTypeBindingNull(typeNode)) {
                operations.add(new AddUnimplementedMethodsOperation(typeNode));
            }
        } else {
            ASTNode typeNode = getSelectedTypeNode(root, problem);
            if (typeNode instanceof TypeDeclaration) {
                operations.add(new MakeTypeAbstractOperation((TypeDeclaration) typeNode));
            }
        }
    }
    if (operations.size() == 0)
        return null;
    String label;
    if (addMissingMethod) {
        label = CorrectionMessages.UnimplementedMethodsCorrectionProposal_description;
    } else {
        label = CorrectionMessages.UnimplementedCodeFix_MakeAbstractFix_label;
    }
    return new UnimplementedCodeFix(label, root, operations.toArray(new CompilationUnitRewriteOperation[operations.size()]));
}
Also used : ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 15 with IProblemLocation

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

the class UnusedCodeFix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, boolean removeUnusedPrivateMethods, boolean removeUnusedPrivateConstructors, boolean removeUnusedPrivateFields, boolean removeUnusedPrivateTypes, boolean removeUnusedLocalVariables, boolean removeUnusedImports, boolean removeUnusedCast) {
    IProblem[] problems = compilationUnit.getProblems();
    IProblemLocation[] locations = new IProblemLocation[problems.length];
    for (int i = 0; i < problems.length; i++) {
        locations[i] = new ProblemLocation(problems[i]);
    }
    return createCleanUp(compilationUnit, locations, removeUnusedPrivateMethods, removeUnusedPrivateConstructors, removeUnusedPrivateFields, removeUnusedPrivateTypes, removeUnusedLocalVariables, removeUnusedImports, removeUnusedCast);
}
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) IProblem(org.eclipse.jdt.core.compiler.IProblem)

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