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

Moe Sync #737

Merged
merged 7 commits into from
May 27, 2019
Merged
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 @@ -307,18 +307,10 @@ public void properlyDefersProcessing_rejectsElement() {

private static <K, V>
Correspondence<SetMultimap<K, V>, SetMultimap<K, String>> setMultimapValuesByString() {
return new Correspondence<SetMultimap<K, V>, SetMultimap<K, String>>() {
@Override
public boolean compare(SetMultimap<K, V> actual, SetMultimap<K, String> expected) {
return ImmutableSetMultimap.copyOf(transformValues(actual, Object::toString))
.equals(expected);
}

@Override
public String toString() {
return "is equivalent comparing multimap values by `toString()` to";
}
};
return Correspondence.from(
(actual, expected) ->
ImmutableSetMultimap.copyOf(transformValues(actual, Object::toString)).equals(expected),
"is equivalent comparing multimap values by `toString()` to");
}

@Test public void reportsMissingType() {
Expand Down
6 changes: 6 additions & 0 deletions factory/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
<version>1.0-rc4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.ltgt.gradle.incap</groupId>
<artifactId>incap</artifactId>
<version>0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.googlejavaformat</groupId>
<artifactId>google-java-format</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic.Kind;
import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;
import net.ltgt.gradle.incap.IncrementalAnnotationProcessorType;

/**
* The annotation processor that generates factories for {@link AutoFactory} annotations.
*
* @author Gregory Kick
*/
@IncrementalAnnotationProcessor(IncrementalAnnotationProcessorType.ISOLATING)
@AutoService(Processor.class)
public final class AutoFactoryProcessor extends AbstractProcessor {
private FactoryDescriptorGenerator factoryDescriptorGenerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public boolean matches(char c) {
abstract boolean allowSubclasses();
abstract ImmutableMap<Key, ProviderField> providers();

final AutoFactoryDeclaration declaration() {
return Iterables.getFirst(methodDescriptors(), null).declaration();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ final class FactoryWriter {
void writeFactory(final FactoryDescriptor descriptor)
throws IOException {
String factoryName = getSimpleName(descriptor.name()).toString();
TypeSpec.Builder factory = classBuilder(factoryName);
TypeSpec.Builder factory =
classBuilder(factoryName)
.addOriginatingElement(descriptor.declaration().targetType());
generatedAnnotationSpec(
elements,
sourceVersion,
Expand Down Expand Up @@ -106,7 +108,7 @@ private void addConstructorAndProviderFields(
Iterator<ProviderField> providerFields = descriptor.providers().values().iterator();
for (int argumentIndex = 1; providerFields.hasNext(); argumentIndex++) {
ProviderField provider = providerFields.next();
TypeName typeName = TypeName.get(provider.key().type()).box();
TypeName typeName = TypeName.get(provider.key().type().get()).box();
TypeName providerType = ParameterizedTypeName.get(ClassName.get(Provider.class), typeName);
factory.addField(providerType, provider.name(), PRIVATE, FINAL);
if (provider.key().qualifier().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
* @author Gregory Kick
*/
@AutoValue
// TODO(ronshapiro): reuse dagger.model.Key?
abstract class Key {

abstract TypeMirror type();
abstract Equivalence.Wrapper<TypeMirror> type();

abstract Optional<Equivalence.Wrapper<AnnotationMirror>> qualifierWrapper();

Optional<AnnotationMirror> qualifier() {
Expand Down Expand Up @@ -80,7 +82,8 @@ static Key create(
? MoreTypes.asDeclared(type).getTypeArguments().get(0)
: boxedType(type, types);
return new AutoValue_Key(
keyType, wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), qualifier));
MoreTypes.equivalence().wrap(keyType),
wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), qualifier));
}

/**
Expand All @@ -95,7 +98,7 @@ private static TypeMirror boxedType(TypeMirror type, Types types) {

@Override
public String toString() {
String typeQualifiedName = MoreTypes.asTypeElement(type()).toString();
String typeQualifiedName = MoreTypes.asTypeElement(type().get()).toString();
return qualifier().isPresent()
? qualifier().get() + "/" + typeQualifiedName
: typeQualifiedName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,21 @@
)
final class CheckerFrameworkNullableFactory {

private final Provider<String> providedNullableDeclProvider;
private final Provider<String> providedNullableTypeProvider;
private final Provider<String> java_lang_StringProvider;

@Inject
CheckerFrameworkNullableFactory(
Provider<String> providedNullableDeclProvider,
Provider<String> providedNullableTypeProvider) {
this.providedNullableDeclProvider = checkNotNull(providedNullableDeclProvider, 1);
this.providedNullableTypeProvider = checkNotNull(providedNullableTypeProvider, 2);
Provider<String> java_lang_StringProvider) {
this.java_lang_StringProvider = checkNotNull(java_lang_StringProvider, 1);
}

CheckerFrameworkNullable create(
@NullableDecl String nullableDecl, @NullableType String nullableType) {
return new CheckerFrameworkNullable(
nullableDecl,
providedNullableDeclProvider.get(),
java_lang_StringProvider.get(),
nullableType,
providedNullableTypeProvider.get());
java_lang_StringProvider.get());
}

private static <T> T checkNotNull(T reference, int argumentIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import static org.junit.Assert.fail;

import com.google.common.testing.EqualsTester;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -518,4 +522,83 @@ public void classAnnotationsCopiedIfCopyAnnotations() {
assertThat(ace.getClass().isAnnotationPresent(CopyTest.class)).isTrue();
assertThat(ace.getClass().getAnnotation(CopyTest.class).value()).isEqualTo(23);
}

@AutoOneOf(MaybeEmpty.Kind.class)
public abstract static class MaybeEmpty implements Serializable {
public enum Kind {
EMPTY, STRING,
}

public abstract Kind getKind();

public abstract void empty();

public abstract String string();

public static MaybeEmpty ofEmpty() {
return AutoOneOf_AutoOneOfTest_MaybeEmpty.empty();
}

public static MaybeEmpty ofString(String s) {
return AutoOneOf_AutoOneOfTest_MaybeEmpty.string(s);
}
}

@Test
public void voidPropertyIsSingleton() {
MaybeEmpty empty1 = MaybeEmpty.ofEmpty();
MaybeEmpty empty2 = MaybeEmpty.ofEmpty();
assertThat(empty1).isSameInstanceAs(empty2);
}

@Test
public void voidPropertyRemainsSingletonWhenDeserialized() throws Exception {
MaybeEmpty empty1 = MaybeEmpty.ofEmpty();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// We're still compiling this with -source 6, so we can't use try-with-resources.
ObjectOutputStream dos = new ObjectOutputStream(baos);
dos.writeObject(empty1);
dos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
MaybeEmpty empty2 = (MaybeEmpty) ois.readObject();
assertThat(empty2).isSameInstanceAs(empty1);
}

@Test
public void voidPropertyToString() {
MaybeEmpty empty = MaybeEmpty.ofEmpty();
assertThat(empty.toString()).isEqualTo("MaybeEmpty{empty}");
}

@Test
public void voidPropertyHashCodeIsIdentity() {
MaybeEmpty empty = MaybeEmpty.ofEmpty();
assertThat(empty.hashCode()).isEqualTo(System.identityHashCode(empty));
}

@Test
public void voidPropertyGetterDoesNothing() {
MaybeEmpty empty = MaybeEmpty.ofEmpty();
empty.empty();
}

@Test
public void voidPropertyNotEqualToNonVoid() {
MaybeEmpty empty = MaybeEmpty.ofEmpty();
MaybeEmpty notEmpty = MaybeEmpty.ofString("foo");
assertThat(empty).isNotEqualTo(notEmpty);
assertThat(notEmpty).isNotEqualTo(empty);
}

@Test
public void voidPropertyWrongType() {
MaybeEmpty notEmpty = MaybeEmpty.ofString("foo");
try {
notEmpty.empty();
fail();
} catch (UnsupportedOperationException e) {
assertThat(e).hasMessageThat().containsMatch("(?i:string)");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.ImmutableTable;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.SerializableTester;
Expand All @@ -53,7 +55,12 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.NoSuchElementException;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.annotation.Nullable;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -2220,15 +2227,89 @@ public void testBuilderWithCopyingSetters() {
BuilderWithCopyingSetters.Builder<Integer> builder = BuilderWithCopyingSetters.builder(23);

BuilderWithCopyingSetters<Integer> a = builder.setThings(ImmutableSet.of(1, 2)).build();
assertEquals(ImmutableSet.of(1, 2), a.things());
assertEquals(ImmutableList.of(17, 23.0), a.numbers());
assertEquals(ImmutableMap.of("foo", 23), a.map());
assertThat(a.things()).containsExactly(1, 2);
assertThat(a.numbers()).containsExactly(17, 23.0).inOrder();
assertThat(a.map()).containsExactly("foo", 23);

BuilderWithCopyingSetters<Integer> b = builder.setThings(Arrays.asList(1, 2)).build();
assertEquals(a, b);
assertThat(b).isEqualTo(a);

BuilderWithCopyingSetters<Integer> c = builder.setThings(1, 2).build();
assertEquals(a, c);
assertThat(c).isEqualTo(a);
}

@AutoValue
public abstract static class BuilderWithImmutableSorted<T extends Comparable<T>> {
public abstract ImmutableSortedSet<T> sortedSet();

public abstract ImmutableSortedMap<T, Integer> sortedMap();

public static <T extends Comparable<T>> Builder<T> builder() {
return new AutoValue_AutoValueTest_BuilderWithImmutableSorted.Builder<T>()
.setSortedSet(new TreeSet<T>())
.setSortedMap(new TreeMap<T, Integer>());
}

@AutoValue.Builder
public interface Builder<T extends Comparable<T>> {
@SuppressWarnings("unchecked")
Builder<T> setSortedSet(T... x);

Builder<T> setSortedSet(NavigableSet<T> x);

ImmutableSortedSet.Builder<T> sortedSetBuilder();

Builder<T> setSortedMap(SortedMap<T, Integer> x);

Builder<T> setSortedMap(NavigableMap<T, Integer> x);

ImmutableSortedMap.Builder<T, Integer> sortedMapBuilder();

BuilderWithImmutableSorted<T> build();
}
}

@Test
public void testBuilderWithImmutableSorted_Varargs() {
BuilderWithImmutableSorted<String> x =
BuilderWithImmutableSorted.<String>builder().setSortedSet("foo", "bar", "baz").build();
assertThat(x.sortedSet()).containsExactly("bar", "baz", "foo").inOrder();
}

@Test
public void testBuilderWithImmutableSorted_SetSet() {
BuilderWithImmutableSorted<String> x =
BuilderWithImmutableSorted.<String>builder()
.setSortedSet(new TreeSet<String>(String.CASE_INSENSITIVE_ORDER))
.build();
assertThat(x.sortedSet().comparator()).isEqualTo(String.CASE_INSENSITIVE_ORDER);
}

@Test
public void testBuilderWithImmutableSorted_SetMap() {
BuilderWithImmutableSorted<String> x =
BuilderWithImmutableSorted.<String>builder()
.setSortedMap(new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER))
.build();
assertThat(x.sortedMap().comparator()).isEqualTo(String.CASE_INSENSITIVE_ORDER);
}

@Test
public void testBuilderWithImmutableSorted_SetCollectionBuilder() {
BuilderWithImmutableSorted.Builder<String> builder =
BuilderWithImmutableSorted.<String>builder();
builder.sortedSetBuilder().add("is", "ea", "id");
BuilderWithImmutableSorted<String> x = builder.build();
assertThat(x.sortedSet()).containsExactly("ea", "id", "is").inOrder();
}

@Test
public void testBuilderWithImmutableSorted_MapCollectionBuilder() {
BuilderWithImmutableSorted.Builder<String> builder =
BuilderWithImmutableSorted.<String>builder();
builder.sortedMapBuilder().put("two", 2).put("one", 1);
BuilderWithImmutableSorted<String> x = builder.build();
assertThat(x.sortedMap()).containsExactly("one", 1, "two", 2).inOrder();
}

@AutoValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public AutoOneOfProcessor() {
super(AUTO_ONE_OF_NAME);
}

@Override
boolean propertiesCanBeVoid() {
return true;
}

@Override
void processType(TypeElement autoOneOfType) {
if (autoOneOfType.getKind() != ElementKind.CLASS) {
Expand Down Expand Up @@ -255,6 +260,10 @@ private void defineVarsForType(
propertySet(type, propertyMethods, ImmutableListMultimap.of(), ImmutableListMultimap.of());
vars.kindGetter = kindGetter.getSimpleName().toString();
vars.kindType = TypeEncoder.encode(kindGetter.getReturnType());
TypeElement javaIoSerializable = elementUtils().getTypeElement("java.io.Serializable");
vars.serializable =
javaIoSerializable != null // just in case
&& typeUtils().isAssignable(type.asType(), javaIoSerializable.asType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class AutoOneOfTemplateVars extends AutoValueOrOneOfTemplateVars {
/** Maps property names like {@code dog} to enum constants like {@code DOG}. */
Map<String, String> propertyToKind;

/** True if this {@code @AutoOneOf} class is Serializable. */
Boolean serializable;

private static final Template TEMPLATE = parsedTemplateForResource("autooneof.vm");

@Override
Expand Down
Loading