Search in sources :

Example 6 with IPackageFragment

use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.

the class RenamePackageProcessor method checkTypeNameConflicts.

private RefactoringStatus checkTypeNameConflicts(IPackageFragmentRoot root, String newName, Set<String> topLevelTypeNames) throws CoreException {
    IPackageFragment otherPack = root.getPackageFragment(newName);
    if (fPackage.equals(otherPack))
        return null;
    ICompilationUnit[] cus = otherPack.getCompilationUnits();
    RefactoringStatus result = new RefactoringStatus();
    for (int i = 0; i < cus.length; i++) {
        result.merge(checkTypeNameConflicts(cus[i], topLevelTypeNames));
    }
    return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 7 with IPackageFragment

use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.

the class CorrectPackageDeclarationProposal method getName.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeCorrectionProposal#getName()
	 */
@Override
public String getName() {
    ICompilationUnit cu = getCompilationUnit();
    IPackageFragment parentPack = (IPackageFragment) cu.getParent();
    try {
        IPackageDeclaration[] decls = cu.getPackageDeclarations();
        if (parentPack.isDefaultPackage() && decls.length > 0) {
            return Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_remove_description, BasicElementLabels.getJavaElementName(decls[0].getElementName()));
        }
        if (!parentPack.isDefaultPackage() && decls.length == 0) {
            return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_add_description, JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT)));
        }
    } catch (JavaModelException e) {
        JavaPlugin.log(e);
    }
    return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_change_description, JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT)));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration)

Example 8 with IPackageFragment

use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.

the class AssistQuickFixTest17 method testUnrollMultiCatch2.

@Test
public void testUnrollMultiCatch2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        try {\n");
    buf.append("            System.out.println(\"foo\");\n");
    buf.append("        } catch (IllegalArgumentException | NullPointerException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        } catch (RuntimeException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("catch");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    assertNoErrors(context);
    List proposals = collectAssists(context, false);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        try {\n");
    buf.append("            System.out.println(\"foo\");\n");
    buf.append("        } catch (IllegalArgumentException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        } catch (NullPointerException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        } catch (RuntimeException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) List(java.util.List) Test(org.junit.Test)

Example 9 with IPackageFragment

use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.

the class AssistQuickFixTest17 method testPickoutTypeFromMulticatch2.

@Test
public void testPickoutTypeFromMulticatch2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | java.lang.NoSuchMethodException | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String string = "MethodException";
    int offset = buf.toString().indexOf(string);
    AssistContext context = getCorrectionContext(cu, offset, 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 4);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() throws java.lang.NoSuchMethodException {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        } catch (java.lang.NoSuchMethodException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected3 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1, expected2, expected3 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) List(java.util.List) Test(org.junit.Test)

Example 10 with IPackageFragment

use of org.eclipse.jdt.core.IPackageFragment in project che by eclipse.

the class AssistQuickFixTest18 method testConvertToAnonymousClassCreation1.

@Test
public void testConvertToAnonymousClassCreation1() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("interface I {\n");
    buf.append("    void method();\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(I i) {\n");
    buf.append("    }\n");
    buf.append("    void foo() {\n");
    buf.append("        bar(() -> {\n");
    buf.append("            System.out.println();\n");
    buf.append("            System.out.println();\n");
    buf.append("        });\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("->");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    assertNoErrors(context);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 4);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("interface I {\n");
    buf.append("    void method();\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(I i) {\n");
    buf.append("    }\n");
    buf.append("    void foo() {\n");
    buf.append("        bar(new I() {\n");
    buf.append("            @Override\n");
    buf.append("            public void method() {\n");
    buf.append("                System.out.println();\n");
    buf.append("                System.out.println();\n");
    buf.append("            }\n");
    buf.append("        });\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) List(java.util.List) Test(org.junit.Test)

Aggregations

IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1638 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1456 Test (org.junit.Test)1439 ArrayList (java.util.ArrayList)767 List (java.util.List)454 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)436 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)384 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)284 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)165 Hashtable (java.util.Hashtable)136 IJavaElement (org.eclipse.jdt.core.IJavaElement)72 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)67 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)67 IJavaProject (org.eclipse.jdt.core.IJavaProject)59 Ignore (org.junit.Ignore)56 IType (org.eclipse.jdt.core.IType)55 JavaModelException (org.eclipse.jdt.core.JavaModelException)38 IResource (org.eclipse.core.resources.IResource)34 IFile (org.eclipse.core.resources.IFile)33 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)33