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

Avoid ambiguous imports with @UseImportPolicy(STATIC_IMPORT_ALWAYS) #3584

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -37,6 +38,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 @@ -60,7 +62,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 @@ -199,9 +201,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 @@ -261,7 +272,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 @@ -271,6 +283,7 @@ private static ImmutableSet<String> getAllImports(Inliner inliner, WhichImports
.orElse(Stream.of())
.filter(whichImports::existingImportMatches)
.map(imp -> getQualifiedIdentifier(imp).toString()))
.filter(filter)
.collect(toImmutableSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,9 @@ public void suppressWarnings() throws IOException {
public void typeArgumentsMethodInvocation() throws IOException {
runTest("TypeArgumentsMethodInvocationTemplate");
}

@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();
}
}