Skip to content

Commit

Permalink
Fixed tests failing after requiring minimal 1.8 compliance, part 3
Browse files Browse the repository at this point in the history
- GenerateToStringTest
- AnnotateAssistTest1d5
- AnnotateAssistTest1d8
- AssistQuickFixTest
- AssistQuickFixTest1d7

See eclipse-jdt/eclipse.jdt.core#2536
  • Loading branch information
iloveeclipse committed Jul 25, 2024
1 parent 1d3de40 commit 754889c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ private void setCompilerLevels(boolean is50orHigher, boolean is60orHigher) throw
jp.setRawClasspath(newCP.toArray(new IClasspathEntry[newCP.size()]), null);
}
if (is60orHigher) {
JavaProjectHelper.addRTJar16(jp);
JavaProjectHelper.addRTJar18(jp);
}
if (!is50orHigher) {
JavaProjectHelper.addRTJar13(jp);
JavaProjectHelper.addRTJar18(jp);
}
}

Expand Down Expand Up @@ -511,6 +511,7 @@ public void concatArrayLimit1_4() throws Exception {
+ " HashMap hashMap;\r\n"
+ " Collection wildCollection;\r\n"
+ " Collection integerCollection;\r\n"
+ " @Override\r\n"
+ " public String toString() {\r\n"
+ " final int maxLen = 10;\r\n"
+ " return \"A [AArray=\" + (AArray != null ? arrayToString(AArray, AArray.length, maxLen) : null) + \", aBool=\" + aBool + \", anA=\" + anA + \", floatArray=\" + (floatArray != null ? arrayToString(floatArray, floatArray.length, maxLen) : null) + \", hashMap=\" + (hashMap != null ? toString(hashMap.entrySet(), maxLen) : null) + \", intArray=\" + (intArray != null ? arrayToString(intArray, intArray.length, maxLen) : null) + \", integerCollection=\" + (integerCollection != null ? toString(integerCollection, maxLen) : null) + \", list=\" + (list != null ? toString(list, maxLen) : null) + \", object=\" + object + \", stringArray=\" + (stringArray != null ? arrayToString(stringArray, stringArray.length, maxLen) : null) + \", wildCollection=\" + (wildCollection != null ? toString(wildCollection, maxLen) : null) + \", charArrayMethod()=\" + (charArrayMethod() != null ? arrayToString(charArrayMethod(), charArrayMethod().length, maxLen) : null) + \", floatArrayMethod()=\"\r\n"
Expand Down Expand Up @@ -571,6 +572,7 @@ public void concatArrayLimit1_4Unique() throws Exception {
+ " Object len;\r\n"
+ " Object collection;\r\n"
+ " Object array;\r\n"
+ " @Override\r\n"
+ " public String toString() {\r\n"
+ " final int maxLen2 = 10;\r\n"
+ " return \"A [aBool=\" + aBool + \", intArray=\" + (intArray != null ? arrayToString(intArray, intArray.length, maxLen2) : null) + \", stringArray=\" + (stringArray != null ? arrayToString(stringArray, stringArray.length, maxLen2) : null) + \", AArray=\" + (AArray != null ? arrayToString(AArray, AArray.length, maxLen2) : null) + \", list=\" + (list != null ? toString(list, maxLen2) : null) + \", hashMap=\" + (hashMap != null ? toString(hashMap.entrySet(), maxLen2) : null) + \", wildCollection=\" + (wildCollection != null ? toString(wildCollection, maxLen2) : null) + \", integerCollection=\" + (integerCollection != null ? toString(integerCollection, maxLen2) : null) + \", builder=\" + builder + \", buffer=\" + buffer + \", maxLen=\" + maxLen + \", len=\" + len + \", collection=\" + collection + \", array=\" + array + \"]\";\r\n"
Expand Down Expand Up @@ -1351,7 +1353,9 @@ public void builderArrayLimit1_4() throws Exception {
String expected= "package p;\r\n" + "\r\n" + "import java.util.Collection;\r\n" + "import java.util.HashMap;\r\n" + "import java.util.Iterator;\r\n" + "import java.util.List;\r\n" + "\r\n"
+ "public class A {\r\n" + "\r\n" + " boolean aBool;\r\n" + " Object object;\r\n" + " A anA;\r\n" + " int[] intArray;\r\n" + " float[] floatArray;\r\n" + " String[] stringArray;\r\n"
+ " A[] AArray;\r\n" + " char[] charArrayMethod() {\r\n" + " return new char[0];\r\n" + " }\r\n" + " float[] floatArrayMethod() {\r\n" + " return null;\r\n" + " }\r\n"
+ " List list;\r\n" + " HashMap hashMap;\r\n" + " Collection wildCollection;\r\n" + " Collection integerCollection;\r\n" + " public String toString() {\r\n"
+ " List list;\r\n" + " HashMap hashMap;\r\n" + " Collection wildCollection;\r\n" + " Collection integerCollection;\r\n"
+ " @Override\r\n"
+ " public String toString() {\r\n"
+ " final int maxLen = 10;\r\n" + " StringBuffer buffer = new StringBuffer();\r\n" + " buffer.append(\"A [AArray=\");\r\n"
+ " buffer.append(AArray != null ? arrayToString(AArray, AArray.length, maxLen) : null);\r\n" + " buffer.append(\", aBool=\");\r\n" + " buffer.append(aBool);\r\n"
+ " buffer.append(\", anA=\");\r\n" + " buffer.append(anA);\r\n" + " buffer.append(\", floatArray=\");\r\n"
Expand Down Expand Up @@ -1403,7 +1407,9 @@ public void builderArrayLimit1_4Unique() throws Exception {
String expected= "package p;\r\n" + "\r\n" + "import java.util.Collection;\r\n" + "import java.util.HashMap;\r\n" + "import java.util.Iterator;\r\n" + "import java.util.List;\r\n" + "\r\n"
+ "public class A {\r\n" + "\r\n" + " boolean aBool;\r\n" + " int[] intArray;\r\n" + " String[] stringArray;\r\n" + " A[] AArray;\r\n" + " List list;\r\n" + " HashMap hashMap;\r\n"
+ " Collection wildCollection;\r\n" + " Collection integerCollection;\r\n" + " Object builder;\r\n" + " Object buffer;\r\n" + " Object maxLen;\r\n" + " Object len;\r\n"
+ " Object collection;\r\n" + " Object array;\r\n" + " public String toString() {\r\n" + " final int maxLen2 = 10;\r\n" + " StringBuffer buffer2 = new StringBuffer();\r\n"
+ " Object collection;\r\n" + " Object array;\r\n"
+ " @Override\r\n"
+ " public String toString() {\r\n" + " final int maxLen2 = 10;\r\n" + " StringBuffer buffer2 = new StringBuffer();\r\n"
+ " buffer2.append(\"A [aBool=\");\r\n" + " buffer2.append(aBool);\r\n" + " buffer2.append(\", intArray=\");\r\n"
+ " buffer2.append(intArray != null ? arrayToString(intArray, intArray.length, maxLen2) : null);\r\n" + " buffer2.append(\", stringArray=\");\r\n"
+ " buffer2.append(stringArray != null ? arrayToString(stringArray, stringArray.length, maxLen2) : null);\r\n" + " buffer2.append(\", AArray=\");\r\n"
Expand Down Expand Up @@ -1883,6 +1889,7 @@ public void chainedBuilderArrayLimit1_4() throws Exception {
+ " HashMap hashMap;\r\n"
+ " Collection wildCollection;\r\n"
+ " Collection integerCollection;\r\n"
+ " @Override\r\n"
+ " public String toString() {\r\n"
+ " final int maxLen = 10;\r\n"
+ " StringBuffer buffer = new StringBuffer();\r\n"
Expand Down Expand Up @@ -2452,6 +2459,7 @@ public void formatLimit1_4() throws Exception {
+ " HashMap<Integer, String> hashMap;\r\n"
+ " Collection<?> wildCollection;\r\n"
+ " Collection<Integer> integerCollection;\r\n"
+ " @Override\r\n"
+ " public String toString() {\r\n"
+ " final int maxLen = 10;\r\n"
+ " return MessageFormat.format(\"A [AArray={0}, aBool={1}, anA={2}, floatArray={3}, hashMap={4}, intArray={5}, integerCollection={6}, list={7}, object={8}, stringArray={9}, wildCollection={10}, charArrayMethod()={11}, floatArrayMethod()={12}]\", new Object[]{AArray, new Boolean(aBool), anA, floatArray, hashMap != null ? toString(hashMap.entrySet(), maxLen) : null, intArray, integerCollection != null ? toString(integerCollection, maxLen) : null, list != null ? toString(list, maxLen) : null, object, stringArray, wildCollection != null ? toString(wildCollection, maxLen) : null, charArrayMethod(), floatArrayMethod()});\r\n"
Expand Down Expand Up @@ -3418,6 +3426,7 @@ public void builderArrayLimit1_4Prefixes() throws Exception {
+ " HashMap<Integer, String> hashMap;\r\n"
+ " Collection<?> wildCollection;\r\n"
+ " Collection<Integer> integerCollection;\r\n"
+ " @Override\r\n"
+ " public String toString() {\r\n"
+ " final int l_maxLen_l = 10;\r\n"
+ " StringBuffer l_buffer_l = new StringBuffer();\r\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public interface MyMap<K,V> {
}
"""
};
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_5, null);
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, null);
IType type= fJProject1.findType(MY_MAP_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);

Expand Down Expand Up @@ -173,7 +173,7 @@ public interface MyMap<K,V> {
}
"""
};
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_5, null);
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, null);
IType type= fJProject1.findType(MY_MAP_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);

Expand Down Expand Up @@ -239,7 +239,7 @@ public interface MyMap<K,V> {
}
"""
};
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_5, null);
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, null);
IType type= fJProject1.findType(MY_MAP_PATH.replace('/', '.'));
IFile annotationFile= fJProject1.getProject().getFile(new Path(ANNOTATION_PATH).append(MY_MAP_PATH + ".eea"));
String initialContent=
Expand Down Expand Up @@ -395,7 +395,7 @@ public interface X {
}
"""
};
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_5, null);
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, null);

IFile annotationFile= fJProject1.getProject().getFile(new Path(ANNOTATION_PATH).append(X_PATH + ".eea"));
String initialContent=
Expand Down Expand Up @@ -472,7 +472,7 @@ public X(String p) {}
}
"""
};
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_5, null);
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, null);

IFile annotationFile= fJProject1.getProject().getFile(new Path(ANNOTATION_PATH).append(X_PATH + ".eea"));
String initialContent=
Expand Down Expand Up @@ -548,7 +548,7 @@ public class Node<V> {
}
"""
};
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_5, null);
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, null);
IType type= fJProject1.findType(NODE_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);

Expand Down Expand Up @@ -613,7 +613,7 @@ public class Node<V> {
}
"""
};
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_5, null);
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, null);
IType type= fJProject1.findType(NODE_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);

Expand Down Expand Up @@ -682,7 +682,7 @@ public class Missing {
"}\n"
};

addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_5, unitResult -> !new Path(MISSINGPATH + ".java").equals(new Path(String.valueOf(unitResult.getFileName()))));
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, unitResult -> !new Path(MISSINGPATH + ".java").equals(new Path(String.valueOf(unitResult.getFileName()))));
IType type= fJProject1.findType(CLASS2_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ public class Node {
}
"""
};
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_5, null);
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, null);
IType type= fJProject1.findType(NODE_PATH.replace('/', '.'));
JavaEditor javaEditor= openInClassfileEditor(type, true, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2990,7 +2990,7 @@ public void run() {\s
AssistContext context= getCorrectionContext(cu, str1.indexOf(str), 0);
List<IJavaCompletionProposal> proposals= collectAssists(context, false);

assertNumberOfProposals(proposals, 1);
assertNumberOfProposals(proposals, 2);
assertCorrectLabels(proposals);

CUCorrectionProposal proposal= (CUCorrectionProposal) proposals.get(0);
Expand Down Expand Up @@ -7374,7 +7374,7 @@ public void run() {
AssistContext context= getCorrectionContext(cu, offset, 1);
List<IJavaCompletionProposal> proposals= collectAssists(context, false);

assertNumberOfProposals(proposals, 1);
assertNumberOfProposals(proposals, 2);
assertCorrectLabels(proposals);

String str1= """
Expand Down Expand Up @@ -7789,48 +7789,6 @@ public void foo() {
assertProposalPreviewEquals(str1, NLS.bind(CorrectionMessages.QuickAssistProcessor_convert_to_string_buffer_description, "StringBuilder"), proposals);
}

@Test
public void testConvertToStringBufferJava14() throws Exception {

Map<String, String> oldOptions= fJProject1.getOptions(false);
Map<String, String> newOptions= new HashMap<>(oldOptions);
JavaCore.setComplianceOptions(JavaCore.VERSION_1_4, newOptions);
fJProject1.setOptions(newOptions);

try {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String str= """
package test1;
public class A {
public void foo() {
System.out.println("foo"+"bar");
}
}
""";
ICompilationUnit cu= pack1.createCompilationUnit("A.java", str, false, null);

AssistContext context= getCorrectionContext(cu, str.indexOf("\"+\""), 0);
List<IJavaCompletionProposal> proposals= collectAssists(context, false);

assertCorrectLabels(proposals);

String str1= """
package test1;
public class A {
public void foo() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("foo");
stringBuffer.append("bar");
System.out.println(stringBuffer.toString());
}
}
""";
assertProposalPreviewEquals(str1, NLS.bind(CorrectionMessages.QuickAssistProcessor_convert_to_string_buffer_description, "StringBuffer"), proposals);
} finally {
fJProject1.setOptions(oldOptions);
}
}

@Test
public void testConvertToStringBufferExisting1() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
Expand Down Expand Up @@ -7928,7 +7886,7 @@ public void foo(Object o1, Object o2) {
public class A {
public void foo(Object o1, Object o2) {
System.out.println(MessageFormat.format("foo{0} \\"bar\\" {1}", new Object[]{o1, o2}));
System.out.println(MessageFormat.format("foo{0} \\"bar\\" {1}", o1, o2));
}
}
""";
Expand Down Expand Up @@ -7991,7 +7949,7 @@ public void foo(Object o1, Object o2) {
public class A {
public void foo(Object o1, Object o2) {
System.out.println(MessageFormat.format("foo{0} \\"bar\\" ", new Object[]{new Integer(1)}));
System.out.println(MessageFormat.format("foo{0} \\"bar\\" ", 1));
}
}
""";
Expand Down Expand Up @@ -9435,7 +9393,7 @@ <T extends Iterable<? super Number> & Comparable<Number>> Iterable<T> getIterabl
import java.util.List;
public class E {
void foo() {
for (Iterable<? super Number> iterable : getIterable()) {
for (Comparable comparable : getIterable()) {
\s
}
}
Expand All @@ -9451,9 +9409,9 @@ <T extends Iterable<? super Number> & Comparable<Number>> Iterable<T> getIterabl
import java.util.List;
public class E {
void foo() {
for (Iterator<? extends Iterable<? super Number>> iterator = getIterable()
.iterator(); iterator.hasNext();) {
Iterable<? super Number> iterable = iterator.next();
for (Iterator<Comparable> iterator = getIterable().iterator(); iterator
.hasNext();) {
Comparable comparable = iterator.next();
\s
}
}
Expand Down Expand Up @@ -9754,59 +9712,6 @@ void foo(Collection collection) {
}
}

@Test
public void testGenerateForLowVersion() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String str= """
package test1;
import java.util.Collection;
public class E {
void foo(Collection collection) {
collection
}
}
""";
ICompilationUnit cu= pack1.createCompilationUnit("E.java", str, false, null);

Map<String, String> saveOptions= fJProject1.getOptions(false);
Map<String, String> newOptions= new HashMap<>();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_4, newOptions);
newOptions.put(DefaultCodeFormatterConstants.FORMATTER_PUT_EMPTY_STATEMENT_ON_NEW_LINE, "true");
try {
fJProject1.setOptions(newOptions);

String selection= "collection";
AssistContext context= getCorrectionContext(cu, str.lastIndexOf(selection) + selection.length(), 0);
List<IJavaCompletionProposal> proposals= collectAssists(context, false);

assertNumberOfProposals(proposals, 5);
assertProposalDoesNotExist(proposals, CorrectionMessages.QuickAssistProcessor_generate_enhanced_for_loop);
assertCorrectLabels(proposals);

String[] expected= new String[1];

// no generics should be added to iterator since the version is too low
String str1= """
package test1;
import java.util.Collection;
import java.util.Iterator;
public class E {
void foo(Collection collection) {
for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
Object object = iterator.next();
\s
}
}
}
""";
expected[0]= str1;

assertExpectedExistInProposals(proposals, expected);
} finally {
fJProject1.setOptions(saveOptions);
}
}

@Test
public void testGenerateForArray() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
Expand Down Expand Up @@ -10005,7 +9910,7 @@ static Set raw(int i) {

Map<String, String> saveOptions= fJProject1.getOptions(false);
Map<String, String> newOptions= new HashMap<>();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, newOptions);
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, newOptions);
newOptions.put(DefaultCodeFormatterConstants.FORMATTER_PUT_EMPTY_STATEMENT_ON_NEW_LINE, "true");
try {
fJProject1.setOptions(newOptions);
Expand Down Expand Up @@ -10103,7 +10008,7 @@ static Set raw(int i) {

Map<String, String> saveOptions= fJProject1.getOptions(false);
Map<String, String> newOptions= new HashMap<>();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, newOptions);
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, newOptions);
newOptions.put(DefaultCodeFormatterConstants.FORMATTER_PUT_EMPTY_STATEMENT_ON_NEW_LINE, "true");
try {
fJProject1.setOptions(newOptions);
Expand Down Expand Up @@ -10175,7 +10080,7 @@ static Date[] get() {

Map<String, String> saveOptions= fJProject1.getOptions(false);
Map<String, String> newOptions= new HashMap<>();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, newOptions);
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, newOptions);
newOptions.put(DefaultCodeFormatterConstants.FORMATTER_PUT_EMPTY_STATEMENT_ON_NEW_LINE, "true");
try {
fJProject1.setOptions(newOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,39 +277,6 @@ void foo() {
assertProposalDoesNotExist(proposals, CONVERT_TO_A_SINGLE_MULTI_CATCH_BLOCK);
}

@Test
public void testConvertToMultiCatch6() throws Exception {
//Quick assist should not be offered in 1.5 mode
JavaProjectHelper.set15CompilerOptions(fJProject1);
try {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String str= """
package test1;
public class E {
void foo() {
try {
System.out.println("foo");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
""";
ICompilationUnit cu= pack1.createCompilationUnit("E.java", str, false, null);

int offset= str.indexOf("catch");
AssistContext context= getCorrectionContext(cu, offset, 0);
assertNoErrors(context);
List<IJavaCompletionProposal> proposals= collectAssists(context, false);

assertProposalDoesNotExist(proposals, CONVERT_TO_A_SINGLE_MULTI_CATCH_BLOCK);
} finally {
JavaProjectHelper.set17CompilerOptions(fJProject1);
}
}

@Test
public void testUnrollMultiCatch1() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
Expand Down

0 comments on commit 754889c

Please sign in to comment.