Search in sources :

Example 31 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class UnresolvedElementsSubProcessor method getAnnotationMemberProposals.

public static void getAnnotationMemberProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    CompilationUnit astRoot = context.getASTRoot();
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    Annotation annotation;
    String memberName;
    if (selectedNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
        if (selectedNode.getParent().getLocationInParent() != NormalAnnotation.VALUES_PROPERTY) {
            return;
        }
        annotation = (Annotation) selectedNode.getParent().getParent();
        memberName = ((SimpleName) selectedNode).getIdentifier();
    } else if (selectedNode.getLocationInParent() == SingleMemberAnnotation.VALUE_PROPERTY) {
        annotation = (Annotation) selectedNode.getParent();
        //$NON-NLS-1$
        memberName = "value";
    } else {
        return;
    }
    ITypeBinding annotBinding = annotation.resolveTypeBinding();
    if (annotBinding == null) {
        return;
    }
    if (annotation instanceof NormalAnnotation) {
        // similar names
        IMethodBinding[] otherMembers = annotBinding.getDeclaredMethods();
        for (int i = 0; i < otherMembers.length; i++) {
            IMethodBinding binding = otherMembers[i];
            String curr = binding.getName();
            int relevance = NameMatcher.isSimilarName(memberName, curr) ? IProposalRelevance.CHANGE_TO_ATTRIBUTE_SIMILAR_NAME : IProposalRelevance.CHANGE_TO_ATTRIBUTE;
            String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_UnresolvedElementsSubProcessor_changetoattribute_description, BasicElementLabels.getJavaElementName(curr));
            proposals.add(new RenameNodeCorrectionProposal(label, cu, problem.getOffset(), problem.getLength(), curr, relevance));
        }
    }
    if (annotBinding.isFromSource()) {
        ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, annotBinding);
        if (targetCU != null) {
            String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_UnresolvedElementsSubProcessor_createattribute_description, BasicElementLabels.getJavaElementName(memberName));
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PUBLIC);
            proposals.add(new NewAnnotationMemberProposal(label, targetCU, selectedNode, annotBinding, IProposalRelevance.CREATE_ATTRIBUTE, image));
        }
    }
}
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) Image(org.eclipse.swt.graphics.Image) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) Annotation(org.eclipse.jdt.core.dom.Annotation) RenameNodeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.RenameNodeCorrectionProposal) NewAnnotationMemberProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.NewAnnotationMemberProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation)

Example 32 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class AddTypeParameterProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
    ASTNode declNode = null;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
        createImportRewrite(fAstRoot);
    } else {
        CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
        createImportRewrite(newRoot);
    }
    AST ast = declNode.getAST();
    TypeParameter newTypeParam = ast.newTypeParameter();
    newTypeParam.setName(ast.newSimpleName(fTypeParamName));
    if (fBounds != null && fBounds.length > 0) {
        List<Type> typeBounds = newTypeParam.typeBounds();
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, getImportRewrite());
        for (int i = 0; i < fBounds.length; i++) {
            Type newBound = getImportRewrite().addImport(fBounds[i], ast, importRewriteContext);
            typeBounds.add(newBound);
        }
    }
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ListRewrite listRewrite;
    Javadoc javadoc;
    List<TypeParameter> otherTypeParams;
    if (declNode instanceof TypeDeclaration) {
        TypeDeclaration declaration = (TypeDeclaration) declNode;
        listRewrite = rewrite.getListRewrite(declaration, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
        otherTypeParams = declaration.typeParameters();
        javadoc = declaration.getJavadoc();
    } else {
        MethodDeclaration declaration = (MethodDeclaration) declNode;
        listRewrite = rewrite.getListRewrite(declNode, MethodDeclaration.TYPE_PARAMETERS_PROPERTY);
        otherTypeParams = declaration.typeParameters();
        javadoc = declaration.getJavadoc();
    }
    listRewrite.insertLast(newTypeParam, null);
    if (javadoc != null && otherTypeParams != null) {
        ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
        Set<String> previousNames = JavadocTagsSubProcessor.getPreviousTypeParamNames(otherTypeParams, null);
        String name = '<' + fTypeParamName + '>';
        TagElement newTag = ast.newTagElement();
        newTag.setTagName(TagElement.TAG_PARAM);
        TextElement text = ast.newTextElement();
        text.setText(name);
        newTag.fragments().add(text);
        JavadocTagsSubProcessor.insertTag(tagsRewriter, newTag, previousNames);
    }
    return rewrite;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Type(org.eclipse.jdt.core.dom.Type) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) TextElement(org.eclipse.jdt.core.dom.TextElement) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 33 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class LocalCorrectionsQuickFixTest17 method testRemoveRedundantTypeArguments2.

@Test
public void testRemoveRedundantTypeArguments2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.HashMap;\n");
    buf.append("import java.util.Map;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        Map<String,String> a = new HashMap<String,String>();\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.util.HashMap;\n");
    buf.append("import java.util.Map;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        Map<String,String> a = new HashMap<>();\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 34 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class LocalCorrectionsQuickFixTest17 method testUncaughtExceptionTryWithResources4.

@Test
public void testUncaughtExceptionTryWithResources4() throws Exception {
    //https://bugs.eclipse.org/bugs/show_bug.cgi?id=351464
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar() throws MyException {\n");
    buf.append("        throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) throws IOException {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar() throws MyException {\n");
    buf.append("        throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) throws IOException, MyException {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(1);
    String preview2 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar() throws MyException {\n");
    buf.append("        throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) throws IOException {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar();\n");
    buf.append("        } catch (MyException e) {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(2);
    String preview3 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar() throws MyException {\n");
    buf.append("        throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) throws IOException {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            try {\n");
    buf.append("                bar();\n");
    buf.append("            } catch (MyException e) {\n");
    buf.append("            }\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected3 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2, preview3 }, new String[] { expected1, expected2, expected3 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 35 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class LocalCorrectionsQuickFixTest17 method testUncaughtExceptionTryWithResources1.

@Test
public void testUncaughtExceptionTryWithResources1() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar(1);\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    //quick fix on 1st problem
    ArrayList proposals = collectCorrections(cu, astRoot, 3, 0);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview1 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) throws FileNotFoundException, IOException {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar(1);\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(1);
    String preview2 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar(1);\n");
    buf.append("        } catch (FileNotFoundException e) {\n");
    buf.append("        } catch (IOException e) {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    proposal = (CUCorrectionProposal) proposals.get(2);
    String preview3 = getPreviewContent(proposal);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.io.FileInputStream;\n");
    buf.append("import java.io.FileNotFoundException;\n");
    buf.append("import java.io.IOException;\n");
    buf.append("class MyException extends Exception {\n");
    buf.append("    static final long serialVersionUID = 1L;\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(int n) throws IllegalArgumentException, MyException {\n");
    buf.append("        if (n == 1)\n");
    buf.append("            throw new IllegalArgumentException();\n");
    buf.append("        else\n");
    buf.append("            throw new MyException();\n");
    buf.append("    }\n");
    buf.append("    void foo(String name, boolean b) {\n");
    buf.append("        try (FileInputStream fis = new FileInputStream(name)) {\n");
    buf.append("            bar(1);\n");
    buf.append("        } catch (FileNotFoundException | IOException e) {\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected3 = buf.toString();
    assertEqualStringsIgnoreOrder(new String[] { preview1, preview2, preview3 }, new String[] { expected1, expected2, expected3 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)632 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)557 ArrayList (java.util.ArrayList)453 Test (org.junit.Test)441 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)435 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)228 Hashtable (java.util.Hashtable)137 ASTNode (org.eclipse.jdt.core.dom.ASTNode)90 ASTParser (org.eclipse.jdt.core.dom.ASTParser)44 SimpleName (org.eclipse.jdt.core.dom.SimpleName)36 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)35 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)35 Ignore (org.junit.Ignore)34 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)29 Type (org.eclipse.jdt.core.dom.Type)27 AST (org.eclipse.jdt.core.dom.AST)23 Expression (org.eclipse.jdt.core.dom.Expression)21 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)19 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)19