Search in sources :

Example 16 with IProblemLocation

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

the class CodeStyleFix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProblemLocation[] problems, boolean addThisQualifier, boolean changeNonStaticAccessToStatic, boolean changeIndirectStaticAccessToDirect) {
    if (!addThisQualifier && !changeNonStaticAccessToStatic && !changeIndirectStaticAccessToDirect)
        return null;
    List<CompilationUnitRewriteOperation> operations = new ArrayList<CompilationUnitRewriteOperation>();
    if (addThisQualifier) {
        for (int i = 0; i < problems.length; i++) {
            IProblemLocation problem = problems[i];
            if (problem.getProblemId() == IProblem.UnqualifiedFieldAccess) {
                AddThisQualifierOperation operation = getUnqualifiedFieldAccessResolveOperation(compilationUnit, problem);
                if (operation != null)
                    operations.add(operation);
            }
        }
    }
    addToStaticAccessOperations(compilationUnit, problems, changeNonStaticAccessToStatic, changeIndirectStaticAccessToDirect, operations);
    if (operations.isEmpty())
        return null;
    CompilationUnitRewriteOperation[] operationsArray = operations.toArray(new CompilationUnitRewriteOperation[operations.size()]);
    return new CodeStyleFix(FixMessages.CodeStyleFix_change_name, compilationUnit, operationsArray);
}
Also used : ArrayList(java.util.ArrayList) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation)

Example 17 with IProblemLocation

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

the class CodeStyleFix method addToStaticAccessOperations.

private static void addToStaticAccessOperations(CompilationUnit compilationUnit, IProblemLocation[] problems, boolean changeNonStaticAccessToStatic, boolean changeIndirectStaticAccessToDirect, List<CompilationUnitRewriteOperation> result) {
    if (!changeNonStaticAccessToStatic && !changeIndirectStaticAccessToDirect)
        return;
    HashMap<ASTNode, Block> createdBlocks = new HashMap<ASTNode, Block>();
    for (int i = 0; i < problems.length; i++) {
        IProblemLocation problem = problems[i];
        boolean isNonStaticAccess = changeNonStaticAccessToStatic && isNonStaticAccess(problem);
        boolean isIndirectStaticAccess = changeIndirectStaticAccessToDirect && isIndirectStaticAccess(problem);
        if (isNonStaticAccess || isIndirectStaticAccess) {
            ToStaticAccessOperation[] nonStaticAccessInformation = createToStaticAccessOperations(compilationUnit, createdBlocks, problem, true);
            if (nonStaticAccessInformation != null) {
                ToStaticAccessOperation op = nonStaticAccessInformation[0];
                Expression qualifier = op.fQualifier;
                if (!(qualifier instanceof MethodInvocation) || !isMethodArgument(qualifier)) {
                    for (Iterator<CompilationUnitRewriteOperation> it = result.iterator(); it.hasNext(); ) {
                        // see bug 346230
                        CompilationUnitRewriteOperation oper = it.next();
                        if (oper instanceof CodeStyleFix.AddThisQualifierOperation && ((CodeStyleFix.AddThisQualifierOperation) oper).fName.equals(qualifier)) {
                            result.remove(oper);
                            break;
                        }
                    }
                    result.add(op);
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block)

Example 18 with IProblemLocation

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

the class Java50Fix method createCleanUp.

public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, boolean addOverrideAnnotation, boolean addOverrideInterfaceAnnotation, boolean addDeprecatedAnnotation, boolean rawTypeReference) {
    ICompilationUnit cu = (ICompilationUnit) compilationUnit.getJavaElement();
    if (!JavaModelUtil.is50OrHigher(cu.getJavaProject()))
        return null;
    if (!addOverrideAnnotation && !addDeprecatedAnnotation && !rawTypeReference)
        return null;
    List<CompilationUnitRewriteOperation> operations = new ArrayList<CompilationUnitRewriteOperation>();
    IProblem[] problems = compilationUnit.getProblems();
    IProblemLocation[] locations = new IProblemLocation[problems.length];
    for (int i = 0; i < problems.length; i++) {
        locations[i] = new ProblemLocation(problems[i]);
    }
    if (addOverrideAnnotation)
        createAddOverrideAnnotationOperations(compilationUnit, addOverrideInterfaceAnnotation, locations, operations);
    if (addDeprecatedAnnotation)
        createAddDeprecatedAnnotationOperations(compilationUnit, locations, operations);
    if (rawTypeReference)
        createRawTypeReferenceOperations(compilationUnit, locations, operations);
    if (operations.size() == 0)
        return null;
    String fixName;
    if (rawTypeReference) {
        fixName = FixMessages.Java50Fix_add_type_parameters_change_name;
    } else {
        fixName = FixMessages.Java50Fix_add_annotations_change_name;
    }
    CompilationUnitRewriteOperation[] operationsArray = operations.toArray(new CompilationUnitRewriteOperation[operations.size()]);
    return new Java50Fix(fixName, compilationUnit, operationsArray);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ArrayList(java.util.ArrayList) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) IProblem(org.eclipse.jdt.core.compiler.IProblem) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) ProblemLocation(org.eclipse.jdt.internal.ui.text.correction.ProblemLocation)

Example 19 with IProblemLocation

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

the class Java50Fix method createAddOverrideAnnotationOperations.

private static void createAddOverrideAnnotationOperations(CompilationUnit compilationUnit, boolean addOverrideInterfaceAnnotation, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> result) {
    for (int i = 0; i < locations.length; i++) {
        IProblemLocation problem = locations[i];
        int problemId = problem.getProblemId();
        if (isMissingOverrideAnnotationProblem(problemId)) {
            if (!isMissingOverrideAnnotationInterfaceProblem(problemId) || addOverrideInterfaceAnnotation) {
                ASTNode selectedNode = problem.getCoveringNode(compilationUnit);
                if (selectedNode != null) {
                    ASTNode declaringNode = getDeclaringNode(selectedNode);
                    if (declaringNode instanceof BodyDeclaration) {
                        BodyDeclaration declaration = (BodyDeclaration) declaringNode;
                        AnnotationRewriteOperation operation = new AnnotationRewriteOperation(declaration, OVERRIDE);
                        result.add(operation);
                    }
                }
            }
        }
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation)

Example 20 with IProblemLocation

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

the class Java50Fix method createRawTypeReferenceOperations.

private static SimpleType createRawTypeReferenceOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> operations) {
    if (hasFatalError(compilationUnit))
        return null;
    List<SimpleType> result = new ArrayList<SimpleType>();
    for (int i = 0; i < locations.length; i++) {
        IProblemLocation problem = locations[i];
        if (isRawTypeReferenceProblem(problem.getProblemId())) {
            ASTNode node = problem.getCoveredNode(compilationUnit);
            if (node instanceof ClassInstanceCreation) {
                Type rawReference = (Type) node.getStructuralProperty(ClassInstanceCreation.TYPE_PROPERTY);
                if (isRawTypeReference(rawReference)) {
                    result.add((SimpleType) rawReference);
                }
            } else if (node instanceof SimpleName) {
                ASTNode rawReference = node.getParent();
                if (isRawTypeReference(rawReference)) {
                    ASTNode parent = rawReference.getParent();
                    if (!(parent instanceof ArrayType || parent instanceof ParameterizedType))
                        result.add((SimpleType) rawReference);
                }
            } else if (node instanceof MethodInvocation) {
                MethodInvocation invocation = (MethodInvocation) node;
                SimpleType rawReference = getRawReference(invocation, compilationUnit);
                if (rawReference != null) {
                    result.add(rawReference);
                }
            }
        }
    }
    if (result.size() == 0)
        return null;
    SimpleType[] types = result.toArray(new SimpleType[result.size()]);
    operations.add(new AddTypeParametersOperation(types));
    return types[0];
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

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