Skip to content

Commit

Permalink
Sealedassist (#1817)
Browse files Browse the repository at this point in the history
* Add quick fix to add permitted types to switch

- add new LocalCorrectionsSubProcessor.addPermittedTypesProposal()
  method to add permitted types as cases to empty switch statement
  or switch expression
- modify QuickFixProcessor to call new method above
- add tests to QuickFixTest22
- fixes #1795
  • Loading branch information
jjohnstn authored Dec 4, 2024
1 parent 20aa0b7 commit 0de1bc1
Show file tree
Hide file tree
Showing 6 changed files with 442 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private CorrectionMessages() {
public static String LocalCorrectionsSubProcessor_surroundwith_trymulticatch_var_description;
public static String LocalCorrectionsSubProcessor_add_default_case_description;
public static String LocalCorrectionsSubProcessor_add_default_case_label_description;
public static String LocalCorrectionsSubProcessor_add_permitted_types_description;
public static String LocalCorrectionsSubProcessor_remove_default_case_description;
public static String LocalCorrectionsSubProcessor_add_missing_cases_description;
public static String LocalCorrectionsSubProcessor_addthrows_description;
Expand Down Expand Up @@ -350,6 +351,7 @@ private CorrectionMessages() {
public static String QuickAssistProcessor_invertequals_description;
public static String QuickAssistProcessor_typetoarrayInitializer_description;
public static String QuickAssistProcessor_createmethodinsuper_description;
public static String QuickAssistProcessor_create_permitted_switch_cases_desc;
public static String LinkedNamesAssistProposal_proposalinfo;
public static String LinkedNamesAssistProposal_description;
public static String QuickTemplateProcessor_surround_label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ LocalCorrectionsSubProcessor_surroundwith_trymulticatch_description=Surround wit
LocalCorrectionsSubProcessor_surroundwith_trymulticatch_var_description=Change type of ''{0}'' to ''{1}'' and surround with try/multi-catch
LocalCorrectionsSubProcessor_add_default_case_description=Add 'default' case
LocalCorrectionsSubProcessor_add_default_case_label_description=Add 'default' case label
LocalCorrectionsSubProcessor_add_permitted_types_description=Add permitted type cases
LocalCorrectionsSubProcessor_remove_default_case_description=Remove 'default' case
LocalCorrectionsSubProcessor_add_missing_cases_description=Add missing case statements
LocalCorrectionsSubProcessor_addthrows_description=Add throws declaration
Expand Down Expand Up @@ -452,6 +453,7 @@ QuickAssistProcessor_create_new_impl=Create new implementation of ''{0}''
QuickAssistProcessor_create_new_impl_desc=Open the New > Class wizard.
QuickAssistProcessor_create_new_interface_impl=Create new interface, extending ''{0}''
QuickAssistProcessor_create_new_interface_impl_desc=Open the New > Interface wizard.
QuickAssistProcessor_create_permitted_switch_cases_desc=Add permitted type case stubs.

LinkedNamesAssistProposal_proposalinfo=Link all references for a local rename (does not change references in other files)
LinkedNamesAssistProposal_description=Rename in file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012, 2023 IBM Corporation and others.
* Copyright (c) 2012, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -30,6 +30,7 @@ public interface IProposalRelevance {
int CREATE_NON_STATIC_ACCESS_USING_DECLARING_TYPE= 12;
int CREATE_INDIRECT_ACCESS_TO_STATIC= 12;
int CREATE_NON_STATIC_ACCESS_USING_INSTANCE_TYPE= 11;
int ADD_PERMITTED_TYPES= 11;

int REMOVE_UNUSED_CAST= 10;
int ADD_UNIMPLEMENTED_METHODS= 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,164 @@ public static void main(String[] args) {
assertEqualString(preview1, expected1);
}

@Test
public void testAddPermittedTypesToSwitchStatement() throws Exception {
fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin");
fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null);
JavaProjectHelper.set22CompilerOptions(fJProject1);

fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);

String test= """
package test;
public class TestSwitch {
sealed interface Foo<T> {
record FooImpl_a(String x) implements Foo<String> {
}
record FooImpl_b(String y, String z) implements Foo<String> {
}
final class FooImpl_c implements Foo {}
}
public static void main(String[] args) {
Foo<String> foo = getFoo();
switch (foo) {
}
}
private static Foo<String> getFoo() {
return new Foo.FooImpl_b("a", "b");
}
}
""";

ICompilationUnit cu= pack1.createCompilationUnit("TestSwitch.java", test, false, null);

CompilationUnit astRoot= getASTRoot(cu);
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1);
assertCorrectLabels(proposals);

CUCorrectionProposal proposal1= (CUCorrectionProposal) proposals.get(0);
String preview1= getPreviewContent(proposal1);

String expected1= """
package test;
public class TestSwitch {
sealed interface Foo<T> {
record FooImpl_a(String x) implements Foo<String> {
}
record FooImpl_b(String y, String z) implements Foo<String> {
}
final class FooImpl_c implements Foo {}
}
public static void main(String[] args) {
Foo<String> foo = getFoo();
switch (foo) {
case Foo.FooImpl_a(String x) -> {}
case Foo.FooImpl_b(String y, String z) -> {}
case Foo.FooImpl_c f -> {}
case null -> {}
default -> {}
}
}
private static Foo<String> getFoo() {
return new Foo.FooImpl_b("a", "b");
}
}
""";

assertEqualString(preview1, expected1);
}

@Test
public void testAddPermittedTypesToSwitchExpression() throws Exception {
fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin");
fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null);
JavaProjectHelper.set22CompilerOptions(fJProject1);

fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);

String test= """
package test;
public class TestSwitch {
sealed interface Foo<T> {
record FooImpl_a(String x) implements Foo<String> {
}
record FooImpl_b(String y, String z) implements Foo<String> {
}
final class FooImpl_c implements Foo {}
}
public static void main(String[] args) {
Foo<String> foo = getFoo();
int i = switch (foo) {
};
}
private static Foo<String> getFoo() {
return new Foo.FooImpl_b("a", "b");
}
}
""";

ICompilationUnit cu= pack1.createCompilationUnit("TestSwitch.java", test, false, null);

CompilationUnit astRoot= getASTRoot(cu);
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 2, 1);
assertCorrectLabels(proposals);

CUCorrectionProposal proposal1= (CUCorrectionProposal) proposals.get(0);
String preview1= getPreviewContent(proposal1);

String expected1= """
package test;
public class TestSwitch {
sealed interface Foo<T> {
record FooImpl_a(String x) implements Foo<String> {
}
record FooImpl_b(String y, String z) implements Foo<String> {
}
final class FooImpl_c implements Foo {}
}
public static void main(String[] args) {
Foo<String> foo = getFoo();
int i = switch (foo) {
case Foo.FooImpl_a(String x) -> 0;
case Foo.FooImpl_b(String y, String z) -> 0;
case Foo.FooImpl_c f -> 0;
case null -> 0;
default -> 0;
};
}
private static Foo<String> getFoo() {
return new Foo.FooImpl_b("a", "b");
}
}
""";

assertEqualString(preview1, expected1);
}

}
Loading

0 comments on commit 0de1bc1

Please sign in to comment.