Skip to content

Commit

Permalink
Fix exception in use comparator cleanup (#1137)
Browse files Browse the repository at this point in the history
- fixes #915
- add wildcard support to ComparingOnCriteriaOperation.buildMethod()
  similar to what was added to buildField()
- add to existing test case in CleanUpTest1d8 for comparator cleanup
  • Loading branch information
jjohnstn committed Jan 29, 2024
1 parent 25dea83 commit d63daef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2023 IBM Corporation and others.
* Copyright (c) 2020, 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 @@ -2005,6 +2005,8 @@ public void testComparingOnCriteria() throws Exception {
+ "import java.util.List;\n" //
+ "import java.util.Locale;\n" //
+ "import java.util.TreeSet;\n" //
+ "import java.util.Map.Entry;\n" //
+ "import java.util.stream.Stream;\n" //
+ "\n" //
+ "public class E {\n" //
+ " private Comparator<Date> refactorField = new Comparator<Date>() {\n" //
Expand Down Expand Up @@ -2406,6 +2408,10 @@ public void testComparingOnCriteria() throws Exception {
+ " return listToSort;\n" //
+ " }\n" //
+ "\n" //
+ " void wildcardMethod() {\n" //
+ " Stream.<Entry<String, String>>of().sorted((entry1, entry2) -> entry1.getKey().compareTo(entry2.getKey()));\n"//
+ " }\n" //
+ "\n" //
+ " public class FooBar {\n" //
+ " public String value;\n" //
+ " }\n" //
Expand All @@ -2424,6 +2430,8 @@ public void testComparingOnCriteria() throws Exception {
+ "import java.util.List;\n" //
+ "import java.util.Locale;\n" //
+ "import java.util.TreeSet;\n" //
+ "import java.util.Map.Entry;\n" //
+ "import java.util.stream.Stream;\n" //
+ "\n" //
+ "public class E {\n" //
+ " private Comparator<Date> refactorField = Comparator.comparing(Date::toString);\n" //
Expand Down Expand Up @@ -2618,6 +2626,10 @@ public void testComparingOnCriteria() throws Exception {
+ " return listToSort;\n" //
+ " }\n" //
+ "\n" //
+ " void wildcardMethod() {\n" //
+ " Stream.<Entry<String, String>>of().sorted(Comparator.comparing(Entry<String, String>::getKey));\n" //
+ " }\n" //
+ "\n" //
+ " public class FooBar {\n" //
+ " public String value;\n" //
+ " }\n" //
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2023 Fabrice TIERCELIN and others.
* Copyright (c) 2021, 2024 Fabrice TIERCELIN 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 @@ -550,10 +550,15 @@ public void rewriteASTInternal(final CompilationUnitRewrite cuRewrite, final Lin
}

private TypeMethodReference buildMethod(final ITypeBinding type, final MethodInvocation method, final ASTRewrite rewrite, final AST ast, final ImportRewrite importRewrite) {
String comparedClassNameText= importRewrite.addImport((type.getBound() != null && !type.isUpperbound()) ? type.getBound() : type.getErasure());

TypeMethodReference typeMethodRef= ast.newTypeMethodReference();
typeMethodRef.setType(ast.newSimpleType(ASTNodeFactory.newName(ast, comparedClassNameText)));
if (type.isWildcardType()) {
ITypeBinding bound= type.getBound();
Type boundType= importRewrite.addImport(bound, ast, importRewrite.getDefaultImportRewriteContext(), TypeLocation.TYPE_BOUND);
typeMethodRef.setType(boundType);
} else {
String comparedClassNameText= importRewrite.addImport(type.getErasure());
typeMethodRef.setType(ast.newSimpleType(ASTNodeFactory.newName(ast, comparedClassNameText)));
}
typeMethodRef.setName(ASTNodes.createMoveTarget(rewrite, method.getName()));
return typeMethodRef;
}
Expand Down

0 comments on commit d63daef

Please sign in to comment.