Skip to content

Commit

Permalink
Have SerializableAutoValueExtension declare that it is "isolating".
Browse files Browse the repository at this point in the history
Since it is a built-in extension, and since AutoValue itself only describes
itself as isolating if all extensions are, this omission meant that the
presence of AutoValue disabled incremental builds in Gradle.

Add a test to ensure that we don't forget this with future extensions.

Fixes #844.

RELNOTES=AutoValue is once again "isolating" for Gradle incremental compilation.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=310610697
  • Loading branch information
eamonnmcmanus committed May 8, 2020
1 parent 2bbe506 commit 44ca4dd
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.auto.common.MoreTypes;
import com.google.auto.service.AutoService;
import com.google.auto.value.extension.AutoValueExtension;
import com.google.auto.value.extension.AutoValueExtension.Context;
import com.google.auto.value.extension.serializable.serializer.SerializerFactoryLoader;
import com.google.auto.value.extension.serializable.serializer.interfaces.Serializer;
import com.google.auto.value.extension.serializable.serializer.interfaces.SerializerFactory;
Expand All @@ -45,6 +44,7 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
Expand All @@ -68,6 +68,11 @@ public boolean applicable(Context context) {
return hasSerializableInterface(context) && hasSerializableAutoValueAnnotation(context);
}

@Override
public IncrementalExtensionType incrementalType(ProcessingEnvironment processingEnvironment) {
return IncrementalExtensionType.ISOLATING;
}

@Override
public String generateClass(
Context context, String className, String classToExtend, boolean isFinal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public AutoValueProcessor() {
@VisibleForTesting
public AutoValueProcessor(Iterable<? extends AutoValueExtension> extensions) {
super(AUTO_VALUE_NAME);
this.extensions = ImmutableList.<AutoValueExtension>copyOf(extensions);
this.extensions = ImmutableList.copyOf(extensions);
this.loaderForExtensions = null;
}

Expand All @@ -98,25 +98,30 @@ public AutoValueProcessor(Iterable<? extends AutoValueExtension> extensions) {
private ImmutableList<AutoValueExtension> extensions;
private final ClassLoader loaderForExtensions;

@VisibleForTesting
static ImmutableList<AutoValueExtension> extensionsFromLoader(ClassLoader loader) {
return ImmutableList.copyOf(
Iterables.filter(
SimpleServiceLoader.load(AutoValueExtension.class, loader),
ext -> !ext.getClass().getName().equals(OLD_MEMOIZE_EXTENSION)));
}

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);

if (extensions == null) {
try {
extensions = ImmutableList.copyOf(
Iterables.filter(
SimpleServiceLoader.load(AutoValueExtension.class, loaderForExtensions),
ext -> !ext.getClass().getName().equals(OLD_MEMOIZE_EXTENSION)));
} catch (Throwable t) {
extensions = extensionsFromLoader(loaderForExtensions);
} catch (RuntimeException | Error e) {
StringBuilder warning = new StringBuilder();
warning.append(
"An exception occurred while looking for AutoValue extensions. "
+ "No extensions will function.");
if (t instanceof ServiceConfigurationError) {
"An exception occurred while looking for AutoValue extensions."
+ " No extensions will function.");
if (e instanceof ServiceConfigurationError) {
warning.append(" This may be due to a corrupt jar file in the compiler's classpath.");
}
warning.append("\n").append(Throwables.getStackTraceAsString(t));
warning.append("\n").append(Throwables.getStackTraceAsString(e));
errorReporter().reportWarning(warning.toString(), null);
extensions = ImmutableList.of();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 2020 Google LLC
*
* 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.auto.value.processor;

import static com.google.common.truth.Correspondence.transforming;
import static com.google.common.truth.Truth.assertThat;

import com.google.auto.value.extension.AutoValueExtension;
import com.google.auto.value.extension.AutoValueExtension.IncrementalExtensionType;
import com.google.auto.value.extension.memoized.processor.MemoizeExtension;
import com.google.auto.value.extension.serializable.processor.SerializableAutoValueExtension;
import com.google.common.collect.ImmutableList;
import javax.annotation.processing.ProcessingEnvironment;
import net.ltgt.gradle.incap.IncrementalAnnotationProcessorType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Tests of Gradle incrementality in the presence of extensions. See
* <a href="https://docs.gradle.org/5.0/userguide/java_plugin.html#sec:incremental_annotation_processing">
* incremental annotation processing</a> in the Gradle user guide.
*/
@RunWith(JUnit4.class)
public class IncrementalExtensionTest {
@Test
public void builtInExtensionsAreIsolating() {
ImmutableList<AutoValueExtension> builtInExtensions =
AutoValueProcessor.extensionsFromLoader(AutoValueProcessor.class.getClassLoader());
// These are the current built-in extensions. We will update this test if we add more.
// The (Object) cast is because otherwise the inferred type is Class<?>, and we can't match
// that <?> with the class literals here. Even if we cast them to Class<?> it will be a
// different <?>.
assertThat(builtInExtensions)
.comparingElementsUsing(transforming(e -> (Object) e.getClass(), "is class"))
.containsExactly(MemoizeExtension.class, SerializableAutoValueExtension.class);

AutoValueProcessor processor = new AutoValueProcessor(builtInExtensions);
assertThat(processor.getSupportedOptions())
.contains(IncrementalAnnotationProcessorType.ISOLATING.getProcessorOption());
}

@Test
public void customExtensionsAreNotIsolatingByDefault() {
AutoValueExtension nonIsolatingExtension = new NonIsolatingExtension();
assertThat(nonIsolatingExtension.incrementalType((ProcessingEnvironment) null))
.isEqualTo(IncrementalExtensionType.UNKNOWN);
ImmutableList<AutoValueExtension> extensions = ImmutableList.<AutoValueExtension>builder()
.addAll(AutoValueProcessor.extensionsFromLoader(AutoValueProcessor.class.getClassLoader()))
.add(nonIsolatingExtension)
.build();

AutoValueProcessor processor = new AutoValueProcessor(extensions);
assertThat(processor.getSupportedOptions())
.doesNotContain(IncrementalAnnotationProcessorType.ISOLATING.getProcessorOption());
}

@Test
public void customExtensionsCanBeIsolating() {
AutoValueExtension isolatingExtension = new IsolatingExtension();
assertThat(isolatingExtension.incrementalType((ProcessingEnvironment) null))
.isEqualTo(IncrementalExtensionType.ISOLATING);
ImmutableList<AutoValueExtension> extensions = ImmutableList.<AutoValueExtension>builder()
.addAll(AutoValueProcessor.extensionsFromLoader(AutoValueProcessor.class.getClassLoader()))
.add(isolatingExtension)
.build();

AutoValueProcessor processor = new AutoValueProcessor(extensions);
assertThat(processor.getSupportedOptions())
.contains(IncrementalAnnotationProcessorType.ISOLATING.getProcessorOption());
}

// Extensions are "UNKNOWN" by default.
private static class NonIsolatingExtension extends AutoValueExtension {
@Override
public String generateClass(
Context context, String className, String classToExtend, boolean isFinal) {
return null;
}
}

// Extensions are "ISOLATING" if they say they are.
private static class IsolatingExtension extends AutoValueExtension {
@Override
public IncrementalExtensionType incrementalType(ProcessingEnvironment processingEnvironment) {
return IncrementalExtensionType.ISOLATING;
}

@Override
public String generateClass(
Context context, String className, String classToExtend, boolean isFinal) {
return null;
}
}
}

0 comments on commit 44ca4dd

Please sign in to comment.