Search in sources :

Example 1 with ReplaceCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal in project che by eclipse.

the class ReturnTypeSubProcessor method addMissingReturnTypeProposals.

public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
    if (decl instanceof MethodDeclaration) {
        MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
        ReturnStatementCollector eval = new ReturnStatementCollector();
        decl.accept(eval);
        AST ast = astRoot.getAST();
        ITypeBinding typeBinding = eval.getTypeBinding(decl.getAST());
        typeBinding = Bindings.normalizeTypeBinding(typeBinding);
        if (typeBinding == null) {
            //$NON-NLS-1$
            typeBinding = ast.resolveWellKnownType("void");
        }
        if (typeBinding.isWildcardType()) {
            typeBinding = ASTResolving.normalizeWildcardType(typeBinding, true, ast);
        }
        ASTRewrite rewrite = ASTRewrite.create(ast);
        String label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE, image);
        ImportRewrite imports = proposal.createImportRewrite(astRoot);
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
        Type type = imports.addImport(typeBinding, ast, importRewriteContext);
        rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
        rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);
        Javadoc javadoc = methodDeclaration.getJavadoc();
        if (javadoc != null && typeBinding != null) {
            TagElement newTag = ast.newTagElement();
            newTag.setTagName(TagElement.TAG_RETURN);
            TextElement commentStart = ast.newTextElement();
            newTag.fragments().add(commentStart);
            JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
            //$NON-NLS-1$
            proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
        }
        //$NON-NLS-1$
        String key = "return_type";
        proposal.addLinkedPosition(rewrite.track(type), true, key);
        if (typeBinding != null) {
            ITypeBinding[] bindings = ASTResolving.getRelaxingTypes(ast, typeBinding);
            for (int i = 0; i < bindings.length; i++) {
                proposal.addLinkedPositionProposal(key, bindings[i]);
            }
        }
        proposals.add(proposal);
        // change to constructor
        ASTNode parentType = ASTResolving.findParentType(decl);
        if (parentType instanceof AbstractTypeDeclaration) {
            boolean isInterface = parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
            if (!isInterface) {
                String constructorName = ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
                ASTNode nameNode = methodDeclaration.getName();
                label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
                proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
            }
        }
    }
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Image(org.eclipse.swt.graphics.Image) TextElement(org.eclipse.jdt.core.dom.TextElement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) ReplaceCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal)

Example 2 with ReplaceCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal in project che by eclipse.

the class QuickFixProcessor method process.

private void process(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    int id = problem.getProblemId();
    if (id == 0) {
        // no proposals for none-problem locations
        return;
    }
    switch(id) {
        case IProblem.UnterminatedString:
            String quoteLabel = CorrectionMessages.JavaCorrectionProcessor_addquote_description;
            //$NON-NLS-1$
            int pos = moveBack(problem.getOffset() + problem.getLength(), problem.getOffset(), "\n\r", context.getCompilationUnit());
            //$NON-NLS-1$
            proposals.add(new ReplaceCorrectionProposal(quoteLabel, context.getCompilationUnit(), pos, 0, "\"", IProposalRelevance.ADD_QUOTE));
            break;
        case IProblem.UnusedImport:
        case IProblem.DuplicateImport:
        case IProblem.CannotImportPackage:
        case IProblem.ConflictingImport:
            ReorgCorrectionsSubProcessor.removeImportStatementProposals(context, problem, proposals);
            break;
        case IProblem.ImportNotFound:
            ReorgCorrectionsSubProcessor.importNotFoundProposals(context, problem, proposals);
            ReorgCorrectionsSubProcessor.removeImportStatementProposals(context, problem, proposals);
            break;
        case IProblem.UndefinedMethod:
            UnresolvedElementsSubProcessor.getMethodProposals(context, problem, false, proposals);
            break;
        case IProblem.UndefinedConstructor:
            UnresolvedElementsSubProcessor.getConstructorProposals(context, problem, proposals);
            break;
        case IProblem.UndefinedAnnotationMember:
            UnresolvedElementsSubProcessor.getAnnotationMemberProposals(context, problem, proposals);
            break;
        case IProblem.ParameterMismatch:
            UnresolvedElementsSubProcessor.getMethodProposals(context, problem, true, proposals);
            break;
        case IProblem.MethodButWithConstructorName:
            ReturnTypeSubProcessor.addMethodWithConstrNameProposals(context, problem, proposals);
            break;
        case IProblem.UndefinedField:
        case IProblem.UndefinedName:
        case IProblem.UnresolvedVariable:
            UnresolvedElementsSubProcessor.getVariableProposals(context, problem, null, proposals);
            break;
        case IProblem.AmbiguousType:
        case IProblem.JavadocAmbiguousType:
            UnresolvedElementsSubProcessor.getAmbiguosTypeReferenceProposals(context, problem, proposals);
            break;
        case IProblem.PublicClassMustMatchFileName:
            ReorgCorrectionsSubProcessor.getWrongTypeNameProposals(context, problem, proposals);
            break;
        case IProblem.PackageIsNotExpectedPackage:
            ReorgCorrectionsSubProcessor.getWrongPackageDeclNameProposals(context, problem, proposals);
            break;
        case IProblem.UndefinedType:
        case IProblem.JavadocUndefinedType:
            UnresolvedElementsSubProcessor.getTypeProposals(context, problem, proposals);
            break;
        case IProblem.TypeMismatch:
        case IProblem.ReturnTypeMismatch:
            TypeMismatchSubProcessor.addTypeMismatchProposals(context, problem, proposals);
            break;
        case IProblem.IncompatibleTypesInForeach:
            TypeMismatchSubProcessor.addTypeMismatchInForEachProposals(context, problem, proposals);
            break;
        case IProblem.IncompatibleReturnType:
            TypeMismatchSubProcessor.addIncompatibleReturnTypeProposals(context, problem, proposals);
            break;
        case IProblem.IncompatibleExceptionInThrowsClause:
            TypeMismatchSubProcessor.addIncompatibleThrowsProposals(context, problem, proposals);
            break;
        case IProblem.UnhandledException:
        case IProblem.UnhandledExceptionOnAutoClose:
            LocalCorrectionsSubProcessor.addUncaughtExceptionProposals(context, problem, proposals);
            break;
        case IProblem.UnreachableCatch:
        case IProblem.InvalidCatchBlockSequence:
        case IProblem.InvalidUnionTypeReferenceSequence:
            LocalCorrectionsSubProcessor.addUnreachableCatchProposals(context, problem, proposals);
            break;
        case IProblem.RedundantSuperinterface:
            LocalCorrectionsSubProcessor.addRedundantSuperInterfaceProposal(context, problem, proposals);
            break;
        case IProblem.VoidMethodReturnsValue:
            ReturnTypeSubProcessor.addVoidMethodReturnsProposals(context, problem, proposals);
            break;
        case IProblem.MethodReturnsVoid:
            ReturnTypeSubProcessor.addMethodRetunsVoidProposals(context, problem, proposals);
            break;
        case IProblem.MissingReturnType:
            ReturnTypeSubProcessor.addMissingReturnTypeProposals(context, problem, proposals);
            break;
        case IProblem.ShouldReturnValue:
        case IProblem.ShouldReturnValueHintMissingDefault:
            ReturnTypeSubProcessor.addMissingReturnStatementProposals(context, problem, proposals);
            break;
        case IProblem.NonExternalizedStringLiteral:
            LocalCorrectionsSubProcessor.addNLSProposals(context, problem, proposals);
            break;
        case IProblem.UnnecessaryNLSTag:
            LocalCorrectionsSubProcessor.getUnnecessaryNLSTagProposals(context, problem, proposals);
            break;
        case IProblem.NonStaticAccessToStaticField:
        case IProblem.NonStaticAccessToStaticMethod:
        case IProblem.NonStaticOrAlienTypeReceiver:
        case IProblem.IndirectAccessToStaticField:
        case IProblem.IndirectAccessToStaticMethod:
            LocalCorrectionsSubProcessor.addCorrectAccessToStaticProposals(context, problem, proposals);
            break;
        case IProblem.StaticMethodRequested:
        case IProblem.NonStaticFieldFromStaticInvocation:
        case IProblem.InstanceMethodDuringConstructorInvocation:
        case IProblem.InstanceFieldDuringConstructorInvocation:
            ModifierCorrectionSubProcessor.addNonAccessibleReferenceProposal(context, problem, proposals, ModifierCorrectionSubProcessor.TO_STATIC, IProposalRelevance.CHANGE_MODIFIER_TO_STATIC);
            break;
        case IProblem.NonBlankFinalLocalAssignment:
        case IProblem.DuplicateFinalLocalInitialization:
        case IProblem.FinalFieldAssignment:
        case IProblem.DuplicateBlankFinalFieldInitialization:
        case IProblem.AnonymousClassCannotExtendFinalClass:
        case IProblem.ClassExtendFinalClass:
            ModifierCorrectionSubProcessor.addNonAccessibleReferenceProposal(context, problem, proposals, ModifierCorrectionSubProcessor.TO_NON_FINAL, IProposalRelevance.REMOVE_FINAL_MODIFIER);
            break;
        case IProblem.InheritedMethodReducesVisibility:
        case IProblem.MethodReducesVisibility:
        case IProblem.OverridingNonVisibleMethod:
            ModifierCorrectionSubProcessor.addChangeOverriddenModifierProposal(context, problem, proposals, ModifierCorrectionSubProcessor.TO_VISIBLE);
            break;
        case IProblem.FinalMethodCannotBeOverridden:
            ModifierCorrectionSubProcessor.addChangeOverriddenModifierProposal(context, problem, proposals, ModifierCorrectionSubProcessor.TO_NON_FINAL);
            break;
        case IProblem.CannotOverrideAStaticMethodWithAnInstanceMethod:
            ModifierCorrectionSubProcessor.addChangeOverriddenModifierProposal(context, problem, proposals, ModifierCorrectionSubProcessor.TO_NON_STATIC);
            break;
        case IProblem.CannotHideAnInstanceMethodWithAStaticMethod:
        case IProblem.IllegalModifierForInterfaceMethod:
        case IProblem.IllegalModifierForInterface:
        case IProblem.IllegalModifierForClass:
        case IProblem.IllegalModifierForInterfaceField:
        case IProblem.UnexpectedStaticModifierForField:
        case IProblem.IllegalModifierCombinationFinalVolatileForField:
        case IProblem.IllegalModifierForMemberInterface:
        case IProblem.IllegalModifierForMemberClass:
        case IProblem.IllegalModifierForLocalClass:
        case IProblem.IllegalModifierForArgument:
        case IProblem.IllegalModifierForField:
        case IProblem.IllegalModifierForMethod:
        case IProblem.IllegalModifierForConstructor:
        case IProblem.IllegalModifierForVariable:
        case IProblem.IllegalModifierForEnum:
        case IProblem.IllegalModifierForEnumConstant:
        case IProblem.IllegalModifierForEnumConstructor:
        case IProblem.IllegalModifierForMemberEnum:
        case IProblem.IllegalVisibilityModifierForInterfaceMemberType:
        case IProblem.UnexpectedStaticModifierForMethod:
        case IProblem.IllegalModifierForInterfaceMethod18:
            ModifierCorrectionSubProcessor.addRemoveInvalidModifiersProposal(context, problem, proposals, IProposalRelevance.REMOVE_INVALID_MODIFIERS);
            break;
        case IProblem.NotVisibleField:
            GetterSetterCorrectionSubProcessor.addGetterSetterProposal(context, problem, proposals, IProposalRelevance.GETTER_SETTER_NOT_VISIBLE_FIELD);
            ModifierCorrectionSubProcessor.addNonAccessibleReferenceProposal(context, problem, proposals, ModifierCorrectionSubProcessor.TO_VISIBLE, IProposalRelevance.CHANGE_VISIBILITY);
            break;
        case IProblem.NotVisibleMethod:
        case IProblem.NotVisibleConstructor:
        case IProblem.NotVisibleType:
        case IProblem.JavadocNotVisibleType:
            ModifierCorrectionSubProcessor.addNonAccessibleReferenceProposal(context, problem, proposals, ModifierCorrectionSubProcessor.TO_VISIBLE, IProposalRelevance.CHANGE_VISIBILITY);
            break;
        case IProblem.BodyForAbstractMethod:
        case IProblem.AbstractMethodInAbstractClass:
        case IProblem.AbstractMethodInEnum:
        case IProblem.EnumAbstractMethodMustBeImplemented:
            ModifierCorrectionSubProcessor.addAbstractMethodProposals(context, problem, proposals);
            break;
        case IProblem.AbstractMethodsInConcreteClass:
            ModifierCorrectionSubProcessor.addAbstractTypeProposals(context, problem, proposals);
            break;
        case IProblem.AbstractMethodMustBeImplemented:
        case IProblem.EnumConstantMustImplementAbstractMethod:
            LocalCorrectionsSubProcessor.addUnimplementedMethodsProposals(context, problem, proposals);
            break;
        case IProblem.ShouldImplementHashcode:
            LocalCorrectionsSubProcessor.addMissingHashCodeProposals(context, problem, proposals);
            break;
        case IProblem.MissingValueForAnnotationMember:
            LocalCorrectionsSubProcessor.addValueForAnnotationProposals(context, problem, proposals);
            break;
        case IProblem.BodyForNativeMethod:
            ModifierCorrectionSubProcessor.addNativeMethodProposals(context, problem, proposals);
            break;
        case IProblem.MethodRequiresBody:
            ModifierCorrectionSubProcessor.addMethodRequiresBodyProposals(context, problem, proposals);
            break;
        case IProblem.OuterLocalMustBeFinal:
            ModifierCorrectionSubProcessor.addNonFinalLocalProposal(context, problem, proposals);
            break;
        case IProblem.UninitializedLocalVariable:
        case IProblem.UninitializedLocalVariableHintMissingDefault:
            LocalCorrectionsSubProcessor.addUninitializedLocalVariableProposal(context, problem, proposals);
            break;
        case IProblem.UnhandledExceptionInDefaultConstructor:
        case IProblem.UndefinedConstructorInDefaultConstructor:
        case IProblem.NotVisibleConstructorInDefaultConstructor:
            LocalCorrectionsSubProcessor.addConstructorFromSuperclassProposal(context, problem, proposals);
            break;
        case IProblem.UnusedPrivateMethod:
        case IProblem.UnusedPrivateConstructor:
        case IProblem.UnusedPrivateField:
        case IProblem.UnusedPrivateType:
        case IProblem.LocalVariableIsNeverUsed:
        case IProblem.ArgumentIsNeverUsed:
            LocalCorrectionsSubProcessor.addUnusedMemberProposal(context, problem, proposals);
            break;
        case IProblem.NeedToEmulateFieldReadAccess:
        case IProblem.NeedToEmulateFieldWriteAccess:
        case IProblem.NeedToEmulateMethodAccess:
        case IProblem.NeedToEmulateConstructorAccess:
            ModifierCorrectionSubProcessor.addNonAccessibleReferenceProposal(context, problem, proposals, ModifierCorrectionSubProcessor.TO_NON_PRIVATE, IProposalRelevance.CHANGE_VISIBILITY_TO_NON_PRIVATE);
            break;
        case IProblem.SuperfluousSemicolon:
            LocalCorrectionsSubProcessor.addSuperfluousSemicolonProposal(context, problem, proposals);
            break;
        case IProblem.UnnecessaryCast:
            LocalCorrectionsSubProcessor.addUnnecessaryCastProposal(context, problem, proposals);
            break;
        case IProblem.UnnecessaryInstanceof:
            LocalCorrectionsSubProcessor.addUnnecessaryInstanceofProposal(context, problem, proposals);
            break;
        case IProblem.UnusedMethodDeclaredThrownException:
        case IProblem.UnusedConstructorDeclaredThrownException:
            LocalCorrectionsSubProcessor.addUnnecessaryThrownExceptionProposal(context, problem, proposals);
            break;
        case IProblem.UnqualifiedFieldAccess:
            GetterSetterCorrectionSubProcessor.addGetterSetterProposal(context, problem, proposals, IProposalRelevance.GETTER_SETTER_UNQUALIFIED_FIELD_ACCESS);
            LocalCorrectionsSubProcessor.addUnqualifiedFieldAccessProposal(context, problem, proposals);
            break;
        case IProblem.Task:
            proposals.add(new TaskMarkerProposal(context.getCompilationUnit(), problem, 10));
            break;
        case IProblem.JavadocMissing:
            JavadocTagsSubProcessor.getMissingJavadocCommentProposals(context, problem, proposals);
            break;
        case IProblem.JavadocMissingParamTag:
        case IProblem.JavadocMissingReturnTag:
        case IProblem.JavadocMissingThrowsTag:
            JavadocTagsSubProcessor.getMissingJavadocTagProposals(context, problem, proposals);
            break;
        case IProblem.JavadocInvalidThrowsClassName:
        case IProblem.JavadocDuplicateThrowsClassName:
        case IProblem.JavadocDuplicateReturnTag:
        case IProblem.JavadocDuplicateParamName:
        case IProblem.JavadocInvalidParamName:
        case IProblem.JavadocUnexpectedTag:
        case IProblem.JavadocInvalidTag:
            JavadocTagsSubProcessor.getRemoveJavadocTagProposals(context, problem, proposals);
            break;
        case IProblem.JavadocInvalidMemberTypeQualification:
            JavadocTagsSubProcessor.getInvalidQualificationProposals(context, problem, proposals);
            break;
        case IProblem.LocalVariableHidingLocalVariable:
        case IProblem.LocalVariableHidingField:
        case IProblem.FieldHidingLocalVariable:
        case IProblem.FieldHidingField:
        case IProblem.ArgumentHidingLocalVariable:
        case IProblem.ArgumentHidingField:
        case IProblem.UseAssertAsAnIdentifier:
        case IProblem.UseEnumAsAnIdentifier:
        case IProblem.RedefinedLocal:
        case IProblem.RedefinedArgument:
        case IProblem.DuplicateField:
        case IProblem.DuplicateMethod:
        case IProblem.DuplicateTypeVariable:
        case IProblem.DuplicateNestedType:
            LocalCorrectionsSubProcessor.addInvalidVariableNameProposals(context, problem, proposals);
            break;
        case IProblem.NoMessageSendOnArrayType:
            UnresolvedElementsSubProcessor.getArrayAccessProposals(context, problem, proposals);
            break;
        case IProblem.InvalidOperator:
            LocalCorrectionsSubProcessor.getInvalidOperatorProposals(context, problem, proposals);
            break;
        case IProblem.MissingSerialVersion:
            SerialVersionSubProcessor.getSerialVersionProposals(context, problem, proposals);
            break;
        case IProblem.UnnecessaryElse:
            LocalCorrectionsSubProcessor.getUnnecessaryElseProposals(context, problem, proposals);
            break;
        case IProblem.SuperclassMustBeAClass:
            LocalCorrectionsSubProcessor.getInterfaceExtendsClassProposals(context, problem, proposals);
            break;
        case IProblem.CodeCannotBeReached:
        case IProblem.DeadCode:
            LocalCorrectionsSubProcessor.getUnreachableCodeProposals(context, problem, proposals);
            break;
        case IProblem.InvalidUsageOfTypeParameters:
        case IProblem.InvalidUsageOfStaticImports:
        case IProblem.InvalidUsageOfForeachStatements:
        case IProblem.InvalidUsageOfTypeArguments:
        case IProblem.InvalidUsageOfEnumDeclarations:
        case IProblem.InvalidUsageOfVarargs:
        case IProblem.InvalidUsageOfAnnotations:
        case IProblem.InvalidUsageOfAnnotationDeclarations:
            ReorgCorrectionsSubProcessor.getNeedHigherComplianceProposals(context, problem, proposals, JavaCore.VERSION_1_5);
            break;
        case IProblem.DiamondNotBelow17:
            TypeArgumentMismatchSubProcessor.getInferDiamondArgumentsProposal(context, problem, proposals);
        //$FALL-THROUGH$
        case IProblem.AutoManagedResourceNotBelow17:
        case IProblem.MultiCatchNotBelow17:
        case IProblem.PolymorphicMethodNotBelow17:
        case IProblem.BinaryLiteralNotBelow17:
        case IProblem.UnderscoresInLiteralsNotBelow17:
        case IProblem.SwitchOnStringsNotBelow17:
            ReorgCorrectionsSubProcessor.getNeedHigherComplianceProposals(context, problem, proposals, JavaCore.VERSION_1_7);
            break;
        case IProblem.ExplicitThisParameterNotBelow18:
        case IProblem.DefaultMethodNotBelow18:
        case IProblem.StaticInterfaceMethodNotBelow18:
        case IProblem.LambdaExpressionNotBelow18:
        case IProblem.MethodReferenceNotBelow18:
        case IProblem.ConstructorReferenceNotBelow18:
        case IProblem.IntersectionCastNotBelow18:
        case IProblem.InvalidUsageOfTypeAnnotations:
            ReorgCorrectionsSubProcessor.getNeedHigherComplianceProposals(context, problem, proposals, JavaCore.VERSION_1_8);
            break;
        case IProblem.NonGenericType:
            TypeArgumentMismatchSubProcessor.removeMismatchedArguments(context, problem, proposals);
            break;
        case IProblem.MissingOverrideAnnotation:
        case IProblem.MissingOverrideAnnotationForInterfaceMethodImplementation:
            ModifierCorrectionSubProcessor.addOverrideAnnotationProposal(context, problem, proposals);
            break;
        case IProblem.MethodMustOverride:
        case IProblem.MethodMustOverrideOrImplement:
            ModifierCorrectionSubProcessor.removeOverrideAnnotationProposal(context, problem, proposals);
            break;
        case IProblem.FieldMissingDeprecatedAnnotation:
        case IProblem.MethodMissingDeprecatedAnnotation:
        case IProblem.TypeMissingDeprecatedAnnotation:
            ModifierCorrectionSubProcessor.addDeprecatedAnnotationProposal(context, problem, proposals);
            break;
        case IProblem.OverridingDeprecatedMethod:
            ModifierCorrectionSubProcessor.addOverridingDeprecatedMethodProposal(context, problem, proposals);
            break;
        case IProblem.IsClassPathCorrect:
            ReorgCorrectionsSubProcessor.getIncorrectBuildPathProposals(context, problem, proposals);
            break;
        case IProblem.ForbiddenReference:
        case IProblem.DiscouragedReference:
            ReorgCorrectionsSubProcessor.getAccessRulesProposals(context, problem, proposals);
            break;
        case IProblem.AssignmentHasNoEffect:
            LocalCorrectionsSubProcessor.getAssignmentHasNoEffectProposals(context, problem, proposals);
            break;
        case IProblem.UnsafeTypeConversion:
        case IProblem.RawTypeReference:
        case IProblem.UnsafeRawMethodInvocation:
            LocalCorrectionsSubProcessor.addDeprecatedFieldsToMethodsProposals(context, problem, proposals);
        //$FALL-THROUGH$
        case IProblem.UnsafeElementTypeConversion:
            LocalCorrectionsSubProcessor.addTypePrametersToRawTypeReference(context, problem, proposals);
            break;
        case IProblem.RedundantSpecificationOfTypeArguments:
            LocalCorrectionsSubProcessor.addRemoveRedundantTypeArgumentsProposals(context, problem, proposals);
            break;
        case IProblem.FallthroughCase:
            LocalCorrectionsSubProcessor.addFallThroughProposals(context, problem, proposals);
            break;
        case IProblem.UnhandledWarningToken:
            SuppressWarningsSubProcessor.addUnknownSuppressWarningProposals(context, problem, proposals);
            break;
        case IProblem.UnusedWarningToken:
            SuppressWarningsSubProcessor.addRemoveUnusedSuppressWarningProposals(context, problem, proposals);
            break;
        case IProblem.MissingEnumConstantCase:
        case IProblem.MissingEnumDefaultCase:
            LocalCorrectionsSubProcessor.getMissingEnumConstantCaseProposals(context, problem, proposals);
            break;
        case IProblem.MissingDefaultCase:
            LocalCorrectionsSubProcessor.addMissingDefaultCaseProposal(context, problem, proposals);
            break;
        case IProblem.MissingEnumConstantCaseDespiteDefault:
            LocalCorrectionsSubProcessor.getMissingEnumConstantCaseProposals(context, problem, proposals);
            LocalCorrectionsSubProcessor.addCasesOmittedProposals(context, problem, proposals);
            break;
        case IProblem.MissingSynchronizedModifierInInheritedMethod:
            ModifierCorrectionSubProcessor.addSynchronizedMethodProposal(context, problem, proposals);
            break;
        case IProblem.UnusedObjectAllocation:
            LocalCorrectionsSubProcessor.getUnusedObjectAllocationProposals(context, problem, proposals);
            break;
        case IProblem.MethodCanBeStatic:
        case IProblem.MethodCanBePotentiallyStatic:
            ModifierCorrectionSubProcessor.addStaticMethodProposal(context, problem, proposals);
            break;
        case IProblem.PotentialHeapPollutionFromVararg:
            VarargsWarningsSubProcessor.addAddSafeVarargsProposals(context, problem, proposals);
            break;
        case IProblem.UnsafeGenericArrayForVarargs:
            VarargsWarningsSubProcessor.addAddSafeVarargsToDeclarationProposals(context, problem, proposals);
            break;
        case IProblem.SafeVarargsOnFixedArityMethod:
        case IProblem.SafeVarargsOnNonFinalInstanceMethod:
            VarargsWarningsSubProcessor.addRemoveSafeVarargsProposals(context, problem, proposals);
            break;
        case IProblem.IllegalReturnNullityRedefinition:
        case IProblem.IllegalDefinitionToNonNullParameter:
        case IProblem.IllegalRedefinitionToNonNullParameter:
            boolean isArgProblem = id != IProblem.IllegalReturnNullityRedefinition;
            NullAnnotationsCorrectionProcessor.addNullAnnotationInSignatureProposal(context, problem, proposals, ChangeKind.LOCAL, isArgProblem);
            NullAnnotationsCorrectionProcessor.addNullAnnotationInSignatureProposal(context, problem, proposals, ChangeKind.OVERRIDDEN, isArgProblem);
            break;
        case IProblem.RequiredNonNullButProvidedSpecdNullable:
        case IProblem.RequiredNonNullButProvidedUnknown:
            NullAnnotationsCorrectionProcessor.addExtractCheckedLocalProposal(context, problem, proposals);
        //$FALL-THROUGH$
        case IProblem.RequiredNonNullButProvidedNull:
        case IProblem.RequiredNonNullButProvidedPotentialNull:
        case IProblem.ParameterLackingNonNullAnnotation:
        case IProblem.ParameterLackingNullableAnnotation:
            NullAnnotationsCorrectionProcessor.addReturnAndArgumentTypeProposal(context, problem, ChangeKind.LOCAL, proposals);
            NullAnnotationsCorrectionProcessor.addReturnAndArgumentTypeProposal(context, problem, ChangeKind.TARGET, proposals);
            break;
        case IProblem.SpecdNonNullLocalVariableComparisonYieldsFalse:
        case IProblem.RedundantNullCheckOnSpecdNonNullLocalVariable:
            IJavaProject prj = context.getCompilationUnit().getJavaProject();
            if (prj != null && JavaCore.ENABLED.equals(prj.getOption(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, true))) {
                NullAnnotationsCorrectionProcessor.addReturnAndArgumentTypeProposal(context, problem, ChangeKind.LOCAL, proposals);
            }
            break;
        case IProblem.RedundantNullAnnotation:
        case IProblem.RedundantNullDefaultAnnotationPackage:
        case IProblem.RedundantNullDefaultAnnotationType:
        case IProblem.RedundantNullDefaultAnnotationMethod:
            NullAnnotationsCorrectionProcessor.addRemoveRedundantAnnotationProposal(context, problem, proposals);
            break;
        case IProblem.UnusedTypeParameter:
            LocalCorrectionsSubProcessor.addUnusedTypeParameterProposal(context, problem, proposals);
            break;
        case IProblem.NullableFieldReference:
            NullAnnotationsCorrectionProcessor.addExtractCheckedLocalProposal(context, problem, proposals);
            break;
        case IProblem.ConflictingNullAnnotations:
        case IProblem.ConflictingInheritedNullAnnotations:
            NullAnnotationsCorrectionProcessor.addReturnAndArgumentTypeProposal(context, problem, ChangeKind.LOCAL, proposals);
            NullAnnotationsCorrectionProcessor.addReturnAndArgumentTypeProposal(context, problem, ChangeKind.INVERSE, proposals);
            break;
        default:
    }
    if (JavaModelUtil.is50OrHigher(context.getCompilationUnit().getJavaProject())) {
        SuppressWarningsSubProcessor.addSuppressWarningsProposals(context, problem, proposals);
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) TaskMarkerProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.TaskMarkerProposal) ReplaceCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal)

Example 3 with ReplaceCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal in project che by eclipse.

the class LocalCorrectionsSubProcessor method addSuperfluousSemicolonProposal.

public static void addSuperfluousSemicolonProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    String label = CorrectionMessages.LocalCorrectionsSubProcessor_removesemicolon_description;
    ReplaceCorrectionProposal //$NON-NLS-1$
    proposal = new ReplaceCorrectionProposal(label, context.getCompilationUnit(), problem.getOffset(), problem.getLength(), "", IProposalRelevance.REMOVE_SEMICOLON);
    proposals.add(proposal);
}
Also used : ReplaceCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal)

Example 4 with ReplaceCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal in project che by eclipse.

the class UnresolvedElementsSubProcessor method getMethodProposals.

public static void getMethodProposals(IInvocationContext context, IProblemLocation problem, boolean isOnlyParameterMismatch, Collection<ICommandAccess> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    SimpleName nameNode = (SimpleName) selectedNode;
    List<Expression> arguments;
    Expression sender;
    boolean isSuperInvocation;
    ASTNode invocationNode = nameNode.getParent();
    if (invocationNode instanceof MethodInvocation) {
        MethodInvocation methodImpl = (MethodInvocation) invocationNode;
        arguments = methodImpl.arguments();
        sender = methodImpl.getExpression();
        isSuperInvocation = false;
    } else if (invocationNode instanceof SuperMethodInvocation) {
        SuperMethodInvocation methodImpl = (SuperMethodInvocation) invocationNode;
        arguments = methodImpl.arguments();
        sender = methodImpl.getQualifier();
        isSuperInvocation = true;
    } else {
        return;
    }
    String methodName = nameNode.getIdentifier();
    int nArguments = arguments.size();
    // corrections
    IBinding[] bindings = (new ScopeAnalyzer(astRoot)).getDeclarationsInScope(nameNode, ScopeAnalyzer.METHODS);
    HashSet<String> suggestedRenames = new HashSet<String>();
    for (int i = 0; i < bindings.length; i++) {
        IMethodBinding binding = (IMethodBinding) bindings[i];
        String curr = binding.getName();
        if (!curr.equals(methodName) && binding.getParameterTypes().length == nArguments && NameMatcher.isSimilarName(methodName, curr) && suggestedRenames.add(curr)) {
            String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changemethod_description, BasicElementLabels.getJavaElementName(curr));
            proposals.add(new RenameNodeCorrectionProposal(label, context.getCompilationUnit(), problem.getOffset(), problem.getLength(), curr, IProposalRelevance.CHANGE_METHOD));
        }
    }
    suggestedRenames = null;
    if (isOnlyParameterMismatch) {
        ArrayList<IMethodBinding> parameterMismatchs = new ArrayList<IMethodBinding>();
        for (int i = 0; i < bindings.length; i++) {
            IMethodBinding binding = (IMethodBinding) bindings[i];
            if (binding.getName().equals(methodName)) {
                parameterMismatchs.add(binding);
            }
        }
        addParameterMissmatchProposals(context, problem, parameterMismatchs, invocationNode, arguments, proposals);
    }
    if (sender == null) {
        addStaticImportFavoriteProposals(context, nameNode, true, proposals);
    }
    // new method
    addNewMethodProposals(cu, astRoot, sender, arguments, isSuperInvocation, invocationNode, methodName, proposals);
    if (!isOnlyParameterMismatch && !isSuperInvocation && sender != null) {
        addMissingCastParentsProposal(cu, (MethodInvocation) invocationNode, proposals);
    }
    if (!isSuperInvocation && sender == null && invocationNode.getParent() instanceof ThrowStatement) {
        //$NON-NLS-1$ // do it the manual way, copting all the arguments is nasty
        String str = "new ";
        String label = CorrectionMessages.UnresolvedElementsSubProcessor_addnewkeyword_description;
        int relevance = Character.isUpperCase(methodName.charAt(0)) ? IProposalRelevance.ADD_NEW_KEYWORD_UPPERCASE : IProposalRelevance.ADD_NEW_KEYWORD;
        ReplaceCorrectionProposal proposal = new ReplaceCorrectionProposal(label, cu, invocationNode.getStartPosition(), 0, str, relevance);
        proposals.add(proposal);
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) RenameNodeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.RenameNodeCorrectionProposal) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) HashSet(java.util.HashSet) ReplaceCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal)

Example 5 with ReplaceCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal in project flux by eclipse.

the class QuickFixProcessor method process.

private void process(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    int id = problem.getProblemId();
    if (id == 0) {
        // no proposals for none-problem locations
        return;
    }
    switch(id) {
        case IProblem.UnterminatedString:
            String quoteLabel = CorrectionMessages.JavaCorrectionProcessor_addquote_description;
            // $NON-NLS-1$
            int pos = moveBack(problem.getOffset() + problem.getLength(), problem.getOffset(), "\n\r", context.getCompilationUnit());
            // $NON-NLS-1$
            proposals.add(new ReplaceCorrectionProposal(quoteLabel, context.getCompilationUnit(), pos, 0, "\"", IProposalRelevance.ADD_QUOTE));
            break;
        case IProblem.CannotHideAnInstanceMethodWithAStaticMethod:
        case IProblem.IllegalModifierForInterfaceMethod:
        case IProblem.IllegalModifierForInterface:
        case IProblem.IllegalModifierForClass:
        case IProblem.IllegalModifierForInterfaceField:
        case IProblem.UnexpectedStaticModifierForField:
        case IProblem.IllegalModifierCombinationFinalVolatileForField:
        case IProblem.IllegalModifierForMemberInterface:
        case IProblem.IllegalModifierForMemberClass:
        case IProblem.IllegalModifierForLocalClass:
        case IProblem.IllegalModifierForArgument:
        case IProblem.IllegalModifierForField:
        case IProblem.IllegalModifierForMethod:
        case IProblem.IllegalModifierForConstructor:
        case IProblem.IllegalModifierForVariable:
        case IProblem.IllegalModifierForEnum:
        case IProblem.IllegalModifierForEnumConstant:
        case IProblem.IllegalModifierForEnumConstructor:
        case IProblem.IllegalModifierForMemberEnum:
        case IProblem.IllegalVisibilityModifierForInterfaceMemberType:
        case IProblem.UnexpectedStaticModifierForMethod:
        case IProblem.IllegalModifierForInterfaceMethod18:
            ModifierCorrectionSubProcessor.addRemoveInvalidModifiersProposal(context, problem, proposals, IProposalRelevance.REMOVE_INVALID_MODIFIERS);
            break;
        default:
    }
}
Also used : ReplaceCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal)

Aggregations

ReplaceCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 AST (org.eclipse.jdt.core.dom.AST)1 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)1 AnnotationTypeDeclaration (org.eclipse.jdt.core.dom.AnnotationTypeDeclaration)1 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 Expression (org.eclipse.jdt.core.dom.Expression)1 IBinding (org.eclipse.jdt.core.dom.IBinding)1 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 Javadoc (org.eclipse.jdt.core.dom.Javadoc)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)1 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)1