Skip to content

Commit

Permalink
When deduping methods that are inherited from an interface, only chec…
Browse files Browse the repository at this point in the history
…k the types to decide equality.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=116304582
  • Loading branch information
alanrussian authored and cgruber committed Mar 4, 2016
1 parent 4f9e653 commit 30cea62
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.auto.common.MoreTypes;
import com.google.auto.value.AutoValue;
import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;

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

import javax.lang.model.type.TypeMirror;

/**
Expand Down Expand Up @@ -93,7 +95,10 @@ static FactoryDescriptor create(
providersBuilder.build());
}

/** Removes methods with matching signatures from the set of ImplmentationMethods. */
/**
* Removes methods with matching signatures from the set of
* {@link ImplementationMethodDescriptor}s.
*/
private static ImmutableSet<ImplementationMethodDescriptor> dedupeMethods(
ImmutableSet<FactoryMethodDescriptor> methodDescriptors,
ImmutableSet<ImplementationMethodDescriptor> implementationMethodDescriptors) {
Expand All @@ -105,13 +110,23 @@ private static ImmutableSet<ImplementationMethodDescriptor> dedupeMethods(
for (ImplementationMethodDescriptor implementationMethod : implementationMethodDescriptors) {
for (FactoryMethodDescriptor factoryMethod : methodDescriptors) {
if (implementationMethod.name().equals(factoryMethod.name())
&& Iterables.elementsEqual(
&& parameterTypesEqual(
implementationMethod.passedParameters(), factoryMethod.passedParameters())) {
dedupedMethods.remove(implementationMethod);
break;
}
}
}
return ImmutableSet.copyOf(dedupedMethods);
}

/**
* Returns whether the two {@link Iterable}s of {@link Parameter}s are equal solely by type.
*/
private static boolean parameterTypesEqual(
Iterable<Parameter> first, Iterable<Parameter> second) {

return MoreTypes.equivalence().pairwise().equivalent(
Iterables.transform(first, Parameter.parameterToType),
Iterables.transform(second, Parameter.parameterToType));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@

import com.google.auto.common.MoreTypes;
import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import java.util.List;
import java.util.Set;

import javax.inject.Provider;
import javax.inject.Qualifier;
import javax.lang.model.element.AnnotationMirror;
Expand All @@ -37,6 +40,14 @@

@AutoValue
abstract class Parameter {

static Function<Parameter, TypeMirror> parameterToType = new Function<Parameter, TypeMirror>() {
@Override
public TypeMirror apply(Parameter parameter) {
return parameter.type();
}
};

abstract TypeMirror type();
abstract String name();
abstract boolean providerOfType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package tests;

import java.util.List;
import javax.annotation.Generated;
import javax.inject.Inject;

Expand All @@ -32,8 +33,12 @@ public FactoryImplementingCreateMethod.ConcreteClass create() {
return new FactoryImplementingCreateMethod.ConcreteClass();
}

public FactoryImplementingCreateMethod.ConcreteClass create(int a) {
return new FactoryImplementingCreateMethod.ConcreteClass(a);
public FactoryImplementingCreateMethod.ConcreteClass create(int aDifferentArgumentName) {
return new FactoryImplementingCreateMethod.ConcreteClass(aDifferentArgumentName);
}

public FactoryImplementingCreateMethod.ConcreteClass create(List<Integer> genericWithDifferentArgumentName) {
return new FactoryImplementingCreateMethod.ConcreteClass(genericWithDifferentArgumentName);
}

public FactoryImplementingCreateMethod.ConcreteClass create(int a, boolean b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@

import com.google.auto.factory.AutoFactory;

import java.util.List;

final class FactoryImplementingCreateMethod {

interface Interface {}

interface FactoryInterfaceWithCreateMethod {
Interface create();

// Parameters names have to match unfortunately.
Interface create(int a);

Interface create(List<Integer> generic);
}

@AutoFactory(implementing = FactoryInterfaceWithCreateMethod.class)
Expand All @@ -34,7 +37,10 @@ public static class ConcreteClass implements Interface {
ConcreteClass() {}

// Will generate a method with a signature that matches one from the interface.
ConcreteClass(int a) {}
ConcreteClass(int aDifferentArgumentName) {}

// Will generate a method with a signature that matches one from the interface.
ConcreteClass(List<Integer> genericWithDifferentArgumentName) {}

ConcreteClass(int a, boolean b) {}
}
Expand Down

0 comments on commit 30cea62

Please sign in to comment.