Search in sources :

Example 21 with IProblemLocation

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

the class NullAnnotationsFix method createRemoveRedundantNullAnnotationsOperations.

private static void createRemoveRedundantNullAnnotationsOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> result) {
    for (int i = 0; i < locations.length; i++) {
        IProblemLocation problem = locations[i];
        if (problem == null)
            // problem was filtered out by createCleanUp()
            continue;
        int problemId = problem.getProblemId();
        if (problemId == IProblem.RedundantNullAnnotation || problemId == IProblem.RedundantNullDefaultAnnotationPackage || problemId == IProblem.RedundantNullDefaultAnnotationType || problemId == IProblem.RedundantNullDefaultAnnotationMethod) {
            RemoveRedundantAnnotationRewriteOperation operation = new RemoveRedundantAnnotationRewriteOperation(compilationUnit, problem);
            result.add(operation);
        }
    }
}
Also used : IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) RemoveRedundantAnnotationRewriteOperation(org.eclipse.jdt.internal.corext.fix.NullAnnotationsRewriteOperations.RemoveRedundantAnnotationRewriteOperation)

Example 22 with IProblemLocation

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

the class NullAnnotationsFix method createAddNullAnnotationOperations.

private static void createAddNullAnnotationOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> result) {
    String nullableAnnotationName = getNullableAnnotationName(compilationUnit.getJavaElement(), false);
    String nonNullAnnotationName = getNonNullAnnotationName(compilationUnit.getJavaElement(), false);
    Set<String> handledPositions = new HashSet<String>();
    for (int i = 0; i < locations.length; i++) {
        IProblemLocation problem = locations[i];
        if (problem == null)
            // problem was filtered out by createCleanUp()
            continue;
        boolean isArgumentProblem = isComplainingAboutArgument(problem.getCoveredNode(compilationUnit));
        String annotationToAdd = nullableAnnotationName;
        String annotationToRemove = nonNullAnnotationName;
        // cf. createNullAnnotationInSignatureFix() but changeKind is constantly LOCAL
        switch(problem.getProblemId()) {
            case IProblem.IllegalDefinitionToNonNullParameter:
            case IProblem.IllegalRedefinitionToNonNullParameter:
                break;
            case IProblem.ParameterLackingNonNullAnnotation:
            case IProblem.IllegalReturnNullityRedefinition:
                annotationToAdd = nonNullAnnotationName;
                annotationToRemove = nullableAnnotationName;
                break;
            case IProblem.RequiredNonNullButProvidedNull:
            case IProblem.RequiredNonNullButProvidedPotentialNull:
            case IProblem.RequiredNonNullButProvidedUnknown:
            case IProblem.RequiredNonNullButProvidedSpecdNullable:
                if (isArgumentProblem) {
                    annotationToAdd = nonNullAnnotationName;
                    annotationToRemove = nullableAnnotationName;
                }
                break;
        }
        // when performing multiple changes we can only modify the one CU that the CleanUp infrastructure provides to the operation.
        SignatureAnnotationRewriteOperation fix = NullAnnotationsRewriteOperations.createAddAnnotationOperation(compilationUnit, problem, annotationToAdd, annotationToRemove, handledPositions, true, /*thisUnitOnly*/
        false, /*allowRemove*/
        isArgumentProblem, ChangeKind.LOCAL);
        if (fix != null) {
            if (annotationToAdd == nonNullAnnotationName) {
                fix.fRemoveIfNonNullByDefault = true;
                fix.fNonNullByDefaultName = getNonNullByDefaultAnnotationName(compilationUnit.getJavaElement(), false);
            }
            result.add(fix);
        }
    }
}
Also used : SignatureAnnotationRewriteOperation(org.eclipse.jdt.internal.corext.fix.NullAnnotationsRewriteOperations.SignatureAnnotationRewriteOperation) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) HashSet(java.util.HashSet)

Example 23 with IProblemLocation

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

the class NullAnnotationsFix method createCleanUp.

// Entry for NullAnnotationsCleanup:
public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProblemLocation[] locations, int problemID) {
    ICompilationUnit cu = (ICompilationUnit) compilationUnit.getJavaElement();
    if (!JavaModelUtil.is50OrHigher(cu.getJavaProject()))
        return null;
    List<CompilationUnitRewriteOperation> operations = new ArrayList<CompilationUnitRewriteOperation>();
    if (locations == null) {
        org.eclipse.jdt.core.compiler.IProblem[] problems = compilationUnit.getProblems();
        locations = new IProblemLocation[problems.length];
        for (int i = 0; i < problems.length; i++) {
            if (problems[i].getID() == problemID)
                locations[i] = new ProblemLocation(problems[i]);
        }
    }
    createAddNullAnnotationOperations(compilationUnit, locations, operations);
    createRemoveRedundantNullAnnotationsOperations(compilationUnit, locations, operations);
    if (operations.size() == 0)
        return null;
    CompilationUnitRewriteOperation[] operationsArray = operations.toArray(new CompilationUnitRewriteOperation[operations.size()]);
    return new NullAnnotationsFix(FixMessages.NullAnnotationsFix_add_annotation_change_name, compilationUnit, operationsArray);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ArrayList(java.util.ArrayList) 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 24 with IProblemLocation

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

the class PotentialProgrammingProblemsFix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, boolean addSerialVersionIds) {
    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, addSerialVersionIds);
}
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 25 with IProblemLocation

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

the class StringFix method createCleanUp.

private static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, boolean addNLSTag, boolean removeNLSTag, IProblemLocation[] problems) throws CoreException, JavaModelException {
    ICompilationUnit cu = (ICompilationUnit) compilationUnit.getJavaElement();
    if (!cu.isStructureKnown())
        //[clean up] 'Remove unnecessary $NLS-TAGS$' removes necessary ones in case of syntax errors: https://bugs.eclipse.org/bugs/show_bug.cgi?id=285814 : 
        return null;
    List<CategorizedTextEditGroup> result = new ArrayList<CategorizedTextEditGroup>();
    List<IProblemLocation> missingNLSProblems = new ArrayList<IProblemLocation>();
    for (int i = 0; i < problems.length; i++) {
        IProblemLocation problem = problems[i];
        if (addNLSTag && problem.getProblemId() == IProblem.NonExternalizedStringLiteral) {
            missingNLSProblems.add(problem);
        }
        if (removeNLSTag && problem.getProblemId() == IProblem.UnnecessaryNLSTag) {
            IBuffer buffer = cu.getBuffer();
            if (buffer != null) {
                TextEdit edit = StringFix.getReplace(problem.getOffset(), problem.getLength(), buffer, false);
                if (edit != null) {
                    String label = FixMessages.StringFix_RemoveNonNls_description;
                    result.add(new CategorizedTextEditGroup(label, edit, new GroupCategorySet(new GroupCategory(label, label, label))));
                }
            }
        }
    }
    if (!missingNLSProblems.isEmpty()) {
        int[] positions = new int[missingNLSProblems.size()];
        int i = 0;
        for (Iterator<IProblemLocation> iter = missingNLSProblems.iterator(); iter.hasNext(); ) {
            IProblemLocation problem = iter.next();
            positions[i] = problem.getOffset();
            i++;
        }
        //TODO nls
        //NLSUtil.createNLSEdits(cu, positions);
        TextEdit[] edits = null;
        if (edits != null) {
            for (int j = 0; j < edits.length; j++) {
                String label = FixMessages.StringFix_AddNonNls_description;
                result.add(new CategorizedTextEditGroup(label, edits[j], new GroupCategorySet(new GroupCategory(label, label, label))));
            }
        }
    }
    if (result.isEmpty())
        return null;
    //$NON-NLS-1$
    return new StringFix("", compilationUnit, result.toArray(new TextEditGroup[result.size()]));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) GroupCategorySet(org.eclipse.ltk.core.refactoring.GroupCategorySet) GroupCategory(org.eclipse.ltk.core.refactoring.GroupCategory) ArrayList(java.util.ArrayList) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) IBuffer(org.eclipse.jdt.core.IBuffer) CategorizedTextEditGroup(org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup) TextEdit(org.eclipse.text.edits.TextEdit) CategorizedTextEditGroup(org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

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