コード例 #1
0
    private ICompletionProposal[] getJavaAnnotationFixes(IJavaAnnotation javaAnnotation) {
      ProblemLocation location =
          new ProblemLocation(position.getOffset(), position.getLength(), javaAnnotation);
      ICompilationUnit cu = javaAnnotation.getCompilationUnit();
      if (cu == null) return NO_PROPOSALS;

      ISourceViewer sourceViewer = null;
      if (viewer instanceof ISourceViewer) sourceViewer = (ISourceViewer) viewer;

      IInvocationContext context =
          new AssistContext(
              cu,
              sourceViewer,
              location.getOffset(),
              location.getLength(),
              SharedASTProvider.WAIT_ACTIVE_ONLY);
      if (!SpellingAnnotation.TYPE.equals(javaAnnotation.getType())
          && !hasProblem(context.getASTRoot().getProblems(), location)) return NO_PROPOSALS;

      ArrayList proposals = new ArrayList();
      JavaCorrectionProcessor.collectCorrections(
          context, new IProblemLocation[] {location}, proposals);
      Collections.sort(proposals, new CompletionProposalComparator());

      return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
    }
コード例 #2
0
  public static void getInvalidQualificationProposals(
      IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    if (!(node instanceof Name)) {
      return;
    }
    Name name = (Name) node;
    IBinding binding = name.resolveBinding();
    if (!(binding instanceof ITypeBinding)) {
      return;
    }
    ITypeBinding typeBinding = (ITypeBinding) binding;

    AST ast = node.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    rewrite.replace(name, ast.newName(typeBinding.getQualifiedName()), null);

    String label = CorrectionMessages.JavadocTagsSubProcessor_qualifylinktoinner_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal =
        new ASTRewriteCorrectionProposal(
            label,
            context.getCompilationUnit(),
            rewrite,
            IProposalRelevance.QUALIFY_INNER_TYPE_NAME,
            image);

    proposals.add(proposal);
  }
コード例 #3
0
 public boolean hasAssists(IInvocationContext context) throws CoreException {
   IProblem[] problems = context.getASTRoot().getProblems();
   for (int i = 0; i < problems.length; i++) {
     if (hasCorrections(context.getCompilationUnit(), problems[i].getID())) {
       return true;
     }
   }
   return false;
 }
コード例 #4
0
  public static void getMissingJavadocTagProposals(
      IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    if (node == null) {
      return;
    }
    node = ASTNodes.getNormalizedNode(node);

    BodyDeclaration bodyDeclaration = ASTResolving.findParentBodyDeclaration(node);
    if (bodyDeclaration == null) {
      return;
    }
    Javadoc javadoc = bodyDeclaration.getJavadoc();
    if (javadoc == null) {
      return;
    }

    String label;
    StructuralPropertyDescriptor location = node.getLocationInParent();
    if (location == SingleVariableDeclaration.NAME_PROPERTY) {
      label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_paramtag_description;
      if (node.getParent().getLocationInParent() != MethodDeclaration.PARAMETERS_PROPERTY) {
        return; // paranoia checks
      }
    } else if (location == TypeParameter.NAME_PROPERTY) {
      label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_paramtag_description;
      StructuralPropertyDescriptor parentLocation = node.getParent().getLocationInParent();
      if (parentLocation != MethodDeclaration.TYPE_PARAMETERS_PROPERTY
          && parentLocation != TypeDeclaration.TYPE_PARAMETERS_PROPERTY) {
        return; // paranoia checks
      }
    } else if (location == MethodDeclaration.RETURN_TYPE2_PROPERTY) {
      label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_returntag_description;
    } else if (location == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
      label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_throwstag_description;
    } else {
      return;
    }
    ASTRewriteCorrectionProposal proposal =
        new AddMissingJavadocTagProposal(
            label,
            context.getCompilationUnit(),
            bodyDeclaration,
            node,
            IProposalRelevance.ADD_MISSING_TAG);
    proposals.add(proposal);

    String label2 = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_allmissing_description;
    ASTRewriteCorrectionProposal addAllMissing =
        new AddAllMissingJavadocTagsProposal(
            label2,
            context.getCompilationUnit(),
            bodyDeclaration,
            IProposalRelevance.ADD_ALL_MISSING_TAGS);
    proposals.add(addAllMissing);
  }
 public FixCorrectionProposal(
     IProposableFix fix,
     ICleanUp cleanUp,
     int relevance,
     Image image,
     IInvocationContext context) {
   super(fix.getDisplayString(), context.getCompilationUnit(), null, relevance, image);
   fFix = fix;
   fCleanUp = cleanUp;
   fCompilationUnit = context.getASTRoot();
 }
コード例 #6
0
  public static void getUnusedAndUndocumentedParameterOrExceptionProposals(
      IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    IJavaProject project = cu.getJavaProject();

    if (!JavaCore.ENABLED.equals(project.getOption(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, true))) {
      return;
    }

    int problemId = problem.getProblemId();
    boolean isUnusedTypeParam = problemId == IProblem.UnusedTypeParameter;
    boolean isUnusedParam = problemId == IProblem.ArgumentIsNeverUsed || isUnusedTypeParam;
    String key =
        isUnusedParam
            ? JavaCore.COMPILER_PB_UNUSED_PARAMETER_INCLUDE_DOC_COMMENT_REFERENCE
            : JavaCore.COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_INCLUDE_DOC_COMMENT_REFERENCE;

    if (!JavaCore.ENABLED.equals(project.getOption(key, true))) {
      return;
    }

    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    if (node == null) {
      return;
    }

    BodyDeclaration bodyDecl = ASTResolving.findParentBodyDeclaration(node);
    if (bodyDecl == null || ASTResolving.getParentMethodOrTypeBinding(bodyDecl) == null) {
      return;
    }

    String label;
    if (isUnusedTypeParam) {
      label = CorrectionMessages.JavadocTagsSubProcessor_document_type_parameter_description;
    } else if (isUnusedParam) {
      label = CorrectionMessages.JavadocTagsSubProcessor_document_parameter_description;
    } else {
      node = ASTNodes.getNormalizedNode(node);
      label = CorrectionMessages.JavadocTagsSubProcessor_document_exception_description;
    }
    ASTRewriteCorrectionProposal proposal =
        new AddMissingJavadocTagProposal(
            label,
            context.getCompilationUnit(),
            bodyDecl,
            node,
            IProposalRelevance.DOCUMENT_UNUSED_ITEM);
    proposals.add(proposal);
  }
コード例 #7
0
  public static void getRemoveJavadocTagProposals(
      IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    while (node != null && !(node instanceof TagElement)) {
      node = node.getParent();
    }
    if (node == null) {
      return;
    }
    ASTRewrite rewrite = ASTRewrite.create(node.getAST());
    rewrite.remove(node, null);

    String label = CorrectionMessages.JavadocTagsSubProcessor_removetag_description;
    Image image =
        JavaPlugin.getDefault()
            .getWorkbench()
            .getSharedImages()
            .getImage(ISharedImages.IMG_TOOL_DELETE);
    proposals.add(
        new ASTRewriteCorrectionProposal(
            label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_TAG, image));
  }
コード例 #8
0
  public static void getMissingJavadocCommentProposals(
      IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals)
      throws CoreException {
    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    if (node == null) {
      return;
    }
    BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
    if (declaration == null) {
      return;
    }
    ICompilationUnit cu = context.getCompilationUnit();
    ITypeBinding binding = Bindings.getBindingOfParentType(declaration);
    if (binding == null) {
      return;
    }

    if (declaration instanceof MethodDeclaration) {
      MethodDeclaration methodDecl = (MethodDeclaration) declaration;
      IMethodBinding methodBinding = methodDecl.resolveBinding();
      IMethodBinding overridden = null;
      if (methodBinding != null) {
        overridden = Bindings.findOverriddenMethod(methodBinding, true);
      }

      String string =
          CodeGeneration.getMethodComment(
              cu, binding.getName(), methodDecl, overridden, String.valueOf('\n'));
      if (string != null) {
        String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_method_description;
        proposals.add(
            new AddJavadocCommentProposal(
                label,
                cu,
                IProposalRelevance.ADD_JAVADOC_METHOD,
                declaration.getStartPosition(),
                string));
      }
    } else if (declaration instanceof AbstractTypeDeclaration) {
      String typeQualifiedName = Bindings.getTypeQualifiedName(binding);
      String[] typeParamNames;
      if (declaration instanceof TypeDeclaration) {
        List<TypeParameter> typeParams = ((TypeDeclaration) declaration).typeParameters();
        typeParamNames = new String[typeParams.size()];
        for (int i = 0; i < typeParamNames.length; i++) {
          typeParamNames[i] = (typeParams.get(i)).getName().getIdentifier();
        }
      } else {
        typeParamNames = new String[0];
      }
      String string =
          CodeGeneration.getTypeComment(
              cu, typeQualifiedName, typeParamNames, String.valueOf('\n'));
      if (string != null) {
        String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_type_description;
        proposals.add(
            new AddJavadocCommentProposal(
                label,
                cu,
                IProposalRelevance.ADD_JAVADOC_TYPE,
                declaration.getStartPosition(),
                string));
      }
    } else if (declaration instanceof FieldDeclaration) {
      String comment = "/**\n *\n */\n"; // $NON-NLS-1$
      List<VariableDeclarationFragment> fragments = ((FieldDeclaration) declaration).fragments();
      if (fragments != null && fragments.size() > 0) {
        VariableDeclaration decl = fragments.get(0);
        String fieldName = decl.getName().getIdentifier();
        String typeName = binding.getName();
        comment = CodeGeneration.getFieldComment(cu, typeName, fieldName, String.valueOf('\n'));
      }
      if (comment != null) {
        String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_field_description;
        proposals.add(
            new AddJavadocCommentProposal(
                label,
                cu,
                IProposalRelevance.ADD_JAVADOC_FIELD,
                declaration.getStartPosition(),
                comment));
      }
    } else if (declaration instanceof EnumConstantDeclaration) {
      EnumConstantDeclaration enumDecl = (EnumConstantDeclaration) declaration;
      String id = enumDecl.getName().getIdentifier();
      String comment =
          CodeGeneration.getFieldComment(cu, binding.getName(), id, String.valueOf('\n'));
      String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_enumconst_description;
      proposals.add(
          new AddJavadocCommentProposal(
              label,
              cu,
              IProposalRelevance.ADD_JAVADOC_ENUM,
              declaration.getStartPosition(),
              comment));
    }
  }