Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prefix formatting on IsEmptyCallOnCollections #1112

Closed
aegershman opened this issue Oct 15, 2021 · 2 comments
Closed

Fix prefix formatting on IsEmptyCallOnCollections #1112

aegershman opened this issue Oct 15, 2021 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@aegershman
Copy link
Contributor

aegershman commented Oct 15, 2021

Problem

Describe the issue you are experiencing.

see diff

Expected behavior

Describe what you expected to see.

see diff

Example diff

   public static List<CorrelationId> getVariablesSetAndUsed(RelNode rel0,
       RelNode rel1) {
     Set<CorrelationId> set = getVariablesSet(rel0);
-    if (set.size() == 0) {
+    if (set.isEmpty()) {
       return ImmutableList.of();
     }
     Set<CorrelationId> used = getVariablesUsed(rel1);
-    if (used.size() == 0) {
+    if (used.isEmpty()) {
       return ImmutableList.of();
     }
     final List<CorrelationId> result = new ArrayList<>();
   @Experimental
   public static RelNode propagateRelHints(RelNode originalRel, RelNode equiv) {
     if (!(originalRel instanceof Hintable)
-        || ((Hintable) originalRel).getHints().size() == 0) {
+        ||((Hintable) originalRel).getHints().isEmpty()) {
       return equiv;
     }
     final RelShuttle shuttle = new SubTreeHintPropagateShuttle(
         rightKeys,
         filterNulls,
         nonEquiList);
-    return nonEquiList.size() == 0;
+    returnnonEquiList.isEmpty();
   }
 
   /**
 
     private static RelHint copyWithAppendPath(RelHint hint,
         List<Integer> appendPaths) {
-      if (appendPaths.size() == 0) {
+      if (appendPaths.isEmpty()) {
         return hint;
       } else {
         List<Integer> newPath = new ArrayList<>(hint.inheritPath);
     private static RelNode resetHints(Hintable hintable) {
       if (hintable.getHints().size() > 0) {
         final List<RelHint> resetHints = hintable.getHints().stream()
-            .filter(hint -> hint.inheritPath.size() == 0)
+            .filter(hint ->hint.inheritPath.isEmpty())
             .collect(Collectors.toList());
         return hintable.withHints(resetHints);
       } else {

Recipes in example diff:

  • org.openrewrite.java.cleanup.IsEmptyCallOnCollections

References:

  • Recipe ID: OHafc
  • Recipe Name: "Use Collections#isEmpty() instead of comparing size()"
  • Repository: apache/calcite
  • Created at Fri Oct 15 2021 01:39:26 GMT-0500 (Central Daylight Time)
@aegershman
Copy link
Contributor Author

notes on parsing error when in the context of lambdas:

J.Binary is an Expression and thus doesn't currently have coordinates. So when we return the generated J.Binary it's at a location for the .size() method invocation within that Binary.

In lieu of being able to replace the J.Binary using the J.Binary coordinates directly, we'll manually set the prefix. We could include J.Binary Coordinates, or expand Coordinates to include Coordinates for Expressions, but at the time of writing this that is a bigger scope than manually setting the prefix here.


This leads to a parsing issue when replacing the result within a lambda, b/c the end result is expected to be an Expression. Something close to this stub is generated, which is not a statement / doesn't appear to be compileable in a way to get extracted. Checking

class Test {
    static <T> Stream<List<T>> method(Stream<List<T>> stream) {
        if (true) {
            s -> {
                if (true) {
                    Object o =
                            /*__TEMPLATE__*/__P__.<java.util.Collection>/*__p1__*/p().isEmpty()
                }
                return null;
            };
        }
        return null;
    }
}

@aegershman aegershman changed the title Formatting on IsEmptyCallOnCollections Parsing errors resultant from IsEmptyCallOnCollections Oct 16, 2021
@aegershman aegershman changed the title Parsing errors resultant from IsEmptyCallOnCollections Formatting on IsEmptyCallOnCollections Oct 17, 2021
@aegershman aegershman changed the title Formatting on IsEmptyCallOnCollections Fix prefix formatting on IsEmptyCallOnCollections Oct 17, 2021
@aegershman aegershman added this to the 7.16.0 milestone Oct 17, 2021
@aegershman
Copy link
Contributor Author

Moving other content to #1120

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant