Skip to content

Commit

Permalink
Disambiguate provider field's names in generated factories
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129441115
  • Loading branch information
ronshapiro authored and eamonnmcmanus committed Aug 30, 2016
1 parent 4a052a8 commit fe75dbf
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map.Entry;

import java.util.Set;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.type.TypeMirror;

Expand Down Expand Up @@ -56,6 +56,22 @@ public boolean matches(char c) {
abstract boolean allowSubclasses();
abstract ImmutableMap<Key, ProviderField> providers();

private static class UniqueNameSet {
private final Set<String> uniqueNames = new HashSet<String>();

/**
* Generates a unique name using {@code base}. If {@code base} has not yet been added, it will
* be returned as-is. If your {@code base} is healthy, this will always return {@code base}.
*/
String getUniqueName(CharSequence base) {
String name = base.toString();
for (int differentiator = 2; !uniqueNames.add(name); differentiator++) {
name = base.toString() + differentiator;
}
return name;
}
}

static FactoryDescriptor create(
String name,
TypeMirror extendingType,
Expand All @@ -72,6 +88,7 @@ static FactoryDescriptor create(
}
}
ImmutableMap.Builder<Key, ProviderField> providersBuilder = ImmutableMap.builder();
UniqueNameSet uniqueNames = new UniqueNameSet();
for (Entry<Key, Collection<Parameter>> entry :
parametersForProviders.build().asMap().entrySet()) {
Key key = entry.getKey();
Expand All @@ -81,11 +98,16 @@ static FactoryDescriptor create(
case 1:
Parameter parameter = Iterables.getOnlyElement(entry.getValue());
providersBuilder.put(
key, ProviderField.create(parameter.name() + "Provider", key, parameter.nullable()));
key,
ProviderField.create(
uniqueNames.getUniqueName(parameter.name() + "Provider"),
key,
parameter.nullable()));
break;
default:
String providerName =
invalidIdentifierCharacters.replaceFrom(key.toString(), '_') + "Provider";
uniqueNames.getUniqueName(
invalidIdentifierCharacters.replaceFrom(key.toString(), '_') + "Provider");
Optional<AnnotationMirror> nullable = Optional.absent();
for (Parameter param : entry.getValue()) {
nullable = nullable.or(param.nullable());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
import static com.google.testing.compile.JavaSourcesSubject.assertThat;
import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;

/**
Expand Down Expand Up @@ -375,4 +376,15 @@ public void simpleClassProvidedProviderDeps() {
.and().generatesSources(
JavaFileObjects.forResource("expected/ProviderArgumentToCreateMethodFactory.java"));
}

@Test public void multipleFactoriesConflictingParameterNames() {
assertThat(
JavaFileObjects.forResource("good/MultipleFactoriesConflictingParameterNames.java"),
JavaFileObjects.forResource("support/AQualifier.java"))
.processedWith(new AutoFactoryProcessor())
.compilesWithoutError()
.and().generatesSources(
JavaFileObjects.forResource(
"expected/MultipleFactoriesConflictingParameterNamesFactory.java"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2016 Google, Inc.
*
* 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 tests;

import com.google.auto.factory.internal.Preconditions;
import javax.annotation.Generated;
import javax.inject.Inject;
import javax.inject.Provider;

@Generated(
value = "com.google.auto.factory.processor.AutoFactoryProcessor",
comments = "https://github.com/google/auto/tree/master/factory"
)
final class MultipleFactoriesConflictingParameterNamesFactory {

private final Provider<String> stringProvider;
private final Provider<Object> java_lang_ObjectProvider;
private final Provider<String> stringProvider2;
private final Provider<Object> _tests_AQualifier_java_lang_ObjectProvider;

@Inject
MultipleFactoriesConflictingParameterNamesFactory(
Provider<String> stringProvider,
Provider<Object> java_lang_ObjectProvider,
@AQualifier Provider<String> stringProvider2,
@AQualifier Provider<Object> _tests_AQualifier_java_lang_ObjectProvider) {
this.stringProvider = Preconditions.checkNotNull(stringProvider, 1);
this.java_lang_ObjectProvider = Preconditions.checkNotNull(java_lang_ObjectProvider, 2);
this.stringProvider2 = Preconditions.checkNotNull(stringProvider2, 3);
this._tests_AQualifier_java_lang_ObjectProvider =
Preconditions.checkNotNull(_tests_AQualifier_java_lang_ObjectProvider, 4);
}

MultipleFactoriesConflictingParameterNames create(Object unused) {
return new MultipleFactoriesConflictingParameterNames(
Preconditions.checkNotNull(stringProvider.get(), 1),
Preconditions.checkNotNull(java_lang_ObjectProvider.get(), 2),
java_lang_ObjectProvider,
Preconditions.checkNotNull(unused, 4));
}

MultipleFactoriesConflictingParameterNames create() {
return new MultipleFactoriesConflictingParameterNames(
Preconditions.checkNotNull(stringProvider2.get(), 1),
Preconditions.checkNotNull(_tests_AQualifier_java_lang_ObjectProvider.get(), 2),
_tests_AQualifier_java_lang_ObjectProvider);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2016 Google, Inc.
*
* 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 tests;

import com.google.auto.factory.AutoFactory;
import com.google.auto.factory.Provided;
import javax.inject.Provider;

class MultipleFactoriesConflictingParameterNames {

@AutoFactory
MultipleFactoriesConflictingParameterNames(
@Provided String string,
@Provided Object duplicatedKey_nameDoesntMatter,
@Provided Provider<Object> duplicatedKeyProvider_nameDoesntMatter,
// used to disambiguate with the second constructor since qualifiers aren't part of the type
// system
Object unused) {}

@AutoFactory
MultipleFactoriesConflictingParameterNames(
@Provided @AQualifier String string,
@Provided @AQualifier Object qualifiedDuplicatedKey_nameDoesntMatter,
@Provided @AQualifier Provider<Object> qualifiedDuplicatedKeyProvider_nameDoesntMatter) {}
}

0 comments on commit fe75dbf

Please sign in to comment.