Skip to content

Commit

Permalink
Avoid ambiguous imports with @UseImportPolicy(STATIC_IMPORT_ALWAYS)
Browse files Browse the repository at this point in the history
These changes prevent Refaster rules from introducing a static import that
conflicts with an existing static import. Without these changes the new test
case fails with the following error:

```
com/google/errorprone/refaster/testdata/StaticImportClashTemplate.java:27: error: reference to reverseOrder is ambiguous
    return reverseOrder();
           ^
  both method <T>reverseOrder() in java.util.Comparator and method <T>reverseOrder() in java.util.Collections match
```
  • Loading branch information
Stephan202 committed Dec 7, 2022
1 parent 6ddbaa4 commit d548298
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.function.Predicate.not;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
Expand All @@ -36,6 +37,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
Expand All @@ -59,7 +61,7 @@ public JCExpression classReference(
// Special handling to ensure that the pretty-printer always recognizes Refaster references
return inliner.maker().Ident(inliner.asName("Refaster"));
}
ImmutableSet<String> allImports = getAllImports(inliner, WhichImports.NON_STATIC);
ImmutableSet<String> allImports = getAllImports(inliner, WhichImports.NON_STATIC, i -> true);
/*
* Check if topLevelClazz or fullyQualifiedClazz is already imported.
* If fullyQualifiedClazz is imported, return the class name.
Expand Down Expand Up @@ -198,9 +200,18 @@ public JCExpression staticReference(
return IMPORT_TOP_LEVEL.staticReference(
inliner, topLevelClazz, fullyQualifiedClazz, member);
}
// Check to see if the reference is already static-imported.
String importableName = fullyQualifiedClazz + "." + member;
if (!getAllImports(inliner, WhichImports.STATIC).contains(importableName)) {
// Check to see if the reference (or a conflicting reference) is already static-imported.
String importSuffix = "." + member;
ImmutableSet<String> relatedImports =
getAllImports(inliner, WhichImports.STATIC, i -> i.endsWith(importSuffix));
String importableName = fullyQualifiedClazz + importSuffix;
if (!relatedImports.stream().allMatch(importableName::equals)) {
// A conflicting identifier is already statically imported.
return IMPORT_TOP_LEVEL.staticReference(
inliner, topLevelClazz, fullyQualifiedClazz, member);
}
if (relatedImports.isEmpty()) {
// The reference is not yet statically imported.
inliner.addStaticImport(importableName);
}
return inliner.maker().Ident(inliner.asName(member));
Expand Down Expand Up @@ -260,7 +271,8 @@ boolean existingImportMatches(JCImport jcImport) {
* Returns the set of imports that already exist of the import type (both in the source file and
* in the pending list of imports to add).
*/
private static ImmutableSet<String> getAllImports(Inliner inliner, WhichImports whichImports) {
private static ImmutableSet<String> getAllImports(
Inliner inliner, WhichImports whichImports, Predicate<String> filter) {
return Streams.concat(
whichImports.getExistingImports(inliner),
Optional.ofNullable(inliner.getContext())
Expand All @@ -270,6 +282,7 @@ private static ImmutableSet<String> getAllImports(Inliner inliner, WhichImports
.orElse(Stream.of())
.filter(whichImports::existingImportMatches)
.map(imp -> imp.getQualifiedIdentifier().toString()))
.filter(filter)
.collect(toImmutableSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,9 @@ public void staticImportClassToken() throws IOException {
public void suppressWarnings() throws IOException {
runTest("SuppressWarningsTemplate");
}

@Test
public void staticImportClash() throws IOException {
runTest("StaticImportClashTemplate");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2022 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone.refaster.testdata;

import static java.util.Collections.reverseOrder;

import java.util.Comparator;

/** Test data for {@code StaticImportClashTemplate}. */
public class StaticImportClashTemplate {
Comparator<Integer> example() {
return reverseOrder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2022 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone.refaster.testdata;

import static java.util.Collections.reverseOrder;

import java.util.Comparator;

/** Test data for {@code StaticImportClashTemplate}. */
public class StaticImportClashTemplate {
Comparator<Integer> example() {
return Comparator.reverseOrder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2022 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.errorprone.refaster.testdata.template;

import static java.util.Comparator.reverseOrder;

import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.util.Collections;
import java.util.Comparator;

/** Example template that may cause a static import clash. */
final class StaticImportClashTemplate<T extends Comparable<? super T>> {
@BeforeTemplate
Comparator<T> before() {
return Collections.reverseOrder();
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
Comparator<T> after() {
return reverseOrder();
}
}

0 comments on commit d548298

Please sign in to comment.