com.google.auto.value.extension.AutoValueExtension Java Examples

The following examples show how to use com.google.auto.value.extension.AutoValueExtension. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AutoValueProcessor.java    From auto with Apache License 2.0 6 votes vote down vote up
private int writeExtensions(
    TypeElement type,
    ExtensionContext context,
    ImmutableList<AutoValueExtension> applicableExtensions) {
  int writtenSoFar = 0;
  for (AutoValueExtension extension : applicableExtensions) {
    String parentFqName = generatedSubclassName(type, writtenSoFar + 1);
    String parentSimpleName = TypeSimplifier.simpleNameOf(parentFqName);
    String classFqName = generatedSubclassName(type, writtenSoFar);
    String classSimpleName = TypeSimplifier.simpleNameOf(classFqName);
    boolean isFinal = (writtenSoFar == 0);
    String source = extension.generateClass(context, classSimpleName, parentSimpleName, isFinal);
    if (source != null) {
      source = Reformatter.fixup(source);
      writeSourceFile(classFqName, source, type);
      writtenSoFar++;
    }
  }
  return writtenSoFar;
}
 
Example #2
Source File: IncrementalExtensionTest.java    From auto with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #3
Source File: ExtensionTest.java    From auto with Apache License 2.0 6 votes vote down vote up
private void doTestNoCode(AutoValueExtension... extensions) {
  JavaFileObject javaFileObject =
      JavaFileObjects.forSourceLines(
          "foo.bar.Baz",
          "package foo.bar;",
          "",
          "import com.google.auto.value.AutoValue;",
          "",
          "@AutoValue",
          "public abstract class Baz {",
          "  public abstract String foo();",
          "",
          "  public static Baz create(String foo) {",
          "    return new AutoValue_Baz(foo);",
          "  }",
          "}");
  Compilation compilation =
      javac()
          .withProcessors(new AutoValueProcessor(ImmutableList.copyOf(extensions)))
          .compile(javaFileObject);
  assertThat(compilation).succeededWithoutWarnings();
  assertThat(compilation)
      .generatedFile(StandardLocation.SOURCE_OUTPUT, "foo.bar", "Side_Baz.java");
}
 
Example #4
Source File: ColumnProperty.java    From auto-value-cursor with Apache License 2.0 5 votes vote down vote up
public static ImmutableList<ColumnProperty> from(AutoValueExtension.Context context) {
    ImmutableList.Builder<ColumnProperty> values = ImmutableList.builder();
    for (Map.Entry<String, ExecutableElement> entry : context.properties().entrySet()) {
        values.add(new ColumnProperty(entry.getKey(), entry.getValue()));
    }
    return values.build();
}
 
Example #5
Source File: AutoValueProcessor.java    From auto with Apache License 2.0 5 votes vote down vote up
@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)));
}
 
Example #6
Source File: AutoValueProcessor.java    From auto with Apache License 2.0 5 votes vote down vote up
@Override
public Set<String> getSupportedOptions() {
  ImmutableSet.Builder<String> builder = ImmutableSet.builder();
  AutoValueExtension.IncrementalExtensionType incrementalType =
      extensions.stream()
          .map(e -> e.incrementalType(processingEnv))
          .min(Comparator.naturalOrder())
          .orElse(AutoValueExtension.IncrementalExtensionType.ISOLATING);
  builder.add(OMIT_IDENTIFIERS_OPTION).addAll(optionsFor(incrementalType));
  for (AutoValueExtension extension : extensions) {
    builder.addAll(extension.getSupportedOptions());
  }
  return builder.build();
}
 
Example #7
Source File: AutoValueProcessor.java    From auto with Apache License 2.0 5 votes vote down vote up
private static ImmutableSet<String> optionsFor(
    AutoValueExtension.IncrementalExtensionType incrementalType) {
  switch (incrementalType) {
    case ISOLATING:
      return ImmutableSet.of(IncrementalAnnotationProcessorType.ISOLATING.getProcessorOption());
    case AGGREGATING:
      return ImmutableSet.of(IncrementalAnnotationProcessorType.AGGREGATING.getProcessorOption());
    case UNKNOWN:
      return ImmutableSet.of();
  }
  throw new AssertionError(incrementalType);
}
 
Example #8
Source File: IncrementalExtensionTest.java    From auto with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #9
Source File: IncrementalExtensionTest.java    From auto with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #10
Source File: ExtensionTest.java    From auto with Apache License 2.0 5 votes vote down vote up
@Test
public void testCantConsumeTwice() {
  class ConsumeDizzle extends NonFinalExtension {
    @Override
    public Set<String> consumeProperties(Context context) {
      return ImmutableSet.of("dizzle");
    }
  }
  class AlsoConsumeDizzle extends ConsumeDizzle {}
  AutoValueExtension ext1 = new ConsumeDizzle();
  AutoValueExtension ext2 = new AlsoConsumeDizzle();
  Truth.assertThat(ext1).isNotEqualTo(ext2);
  JavaFileObject impl =
      JavaFileObjects.forSourceLines(
          "foo.bar.Baz",
          "package foo.bar;",
          "import com.google.auto.value.AutoValue;",
          "@AutoValue public abstract class Baz {",
          "  abstract String foo();",
          "  abstract String dizzle();",
          "}");
  Compilation compilation =
      javac()
          .withProcessors(new AutoValueProcessor(ImmutableList.of(ext1, ext2)))
          .compile(impl);
  assertThat(compilation)
      .hadErrorContaining("wants to consume a method that was already consumed")
      .inFile(impl)
      .onLineContaining("String dizzle()");
}
 
Example #11
Source File: ExtensionTest.java    From auto with Apache License 2.0 5 votes vote down vote up
private void doTestBadJarDoesntBlowUp(File badJar) throws IOException {
  FileOutputStream fileOutputStream = new FileOutputStream(badJar);
  JarOutputStream jarOutputStream = new JarOutputStream(fileOutputStream);
  byte[] bogusLine = "bogus line\n".getBytes("UTF-8");
  ZipEntry zipEntry = new ZipEntry("META-INF/services/" + AutoValueExtension.class.getName());
  zipEntry.setSize(bogusLine.length);
  jarOutputStream.putNextEntry(zipEntry);
  jarOutputStream.write(bogusLine);
  jarOutputStream.close();
  ClassLoader badJarLoader = new URLClassLoader(new URL[] {badJar.toURI().toURL()});
  JavaFileObject javaFileObject =
      JavaFileObjects.forSourceLines(
          "foo.bar.Baz",
          "package foo.bar;",
          "",
          "import com.google.auto.value.AutoValue;",
          "",
          "@AutoValue",
          "public abstract class Baz {",
          "}");
  Compilation compilation =
      javac()
          .withProcessors(new AutoValueProcessor(badJarLoader))
          .compile(javaFileObject);
  assertThat(compilation).succeeded();
  assertThat(compilation).hadWarningContaining(
      "This may be due to a corrupt jar file in the compiler's classpath.\n  "
          + ServiceConfigurationError.class.getName());
  assertThat(compilation).generatedSourceFile("foo.bar.AutoValue_Baz");
}
 
Example #12
Source File: AutoValueProcessor.java    From auto with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
public AutoValueProcessor(Iterable<? extends AutoValueExtension> extensions) {
  super(AUTO_VALUE_NAME);
  this.extensions = ImmutableList.copyOf(extensions);
  this.loaderForExtensions = null;
}
 
Example #13
Source File: AutoValueProcessor.java    From auto with Apache License 2.0 4 votes vote down vote up
private ImmutableSet<ExecutableElement> methodsConsumedByExtensions(
    TypeElement type,
    ImmutableList<AutoValueExtension> applicableExtensions,
    ExtensionContext context,
    ImmutableSet<ExecutableElement> abstractMethods,
    ImmutableMap<String, ExecutableElement> properties) {
  Set<ExecutableElement> consumed = new HashSet<>();
  for (AutoValueExtension extension : applicableExtensions) {
    Set<ExecutableElement> consumedHere = new HashSet<>();
    for (String consumedProperty : extension.consumeProperties(context)) {
      ExecutableElement propertyMethod = properties.get(consumedProperty);
      if (propertyMethod == null) {
        errorReporter()
            .reportError(
                type,
                "Extension %s wants to consume a property that does not exist: %s",
                extensionName(extension),
                consumedProperty);
      } else {
        consumedHere.add(propertyMethod);
      }
    }
    for (ExecutableElement consumedMethod : extension.consumeMethods(context)) {
      if (!abstractMethods.contains(consumedMethod)) {
        errorReporter()
            .reportError(
                type,
                "Extension %s wants to consume a method that is not one of the abstract methods"
                    + " in this class: %s",
                extensionName(extension),
                consumedMethod);
      } else {
        consumedHere.add(consumedMethod);
      }
    }
    for (ExecutableElement repeat : intersection(consumed, consumedHere)) {
      errorReporter()
          .reportError(
              repeat,
              "Extension %s wants to consume a method that was already consumed by another"
                  + " extension",
              extensionName(extension));
    }
    consumed.addAll(consumedHere);
  }
  return ImmutableSet.copyOf(consumed);
}
 
Example #14
Source File: AutoValueProcessor.java    From auto with Apache License 2.0 4 votes vote down vote up
private String extensionName(AutoValueExtension extension) {
  return extension.getClass().getName();
}