rharter / auto-value-gson

AutoValue Extension to add Gson De/Serializer support
Apache License 2.0
607 stars 103 forks source link

"Unable to get public no-arg constructor" simply when adding auto-value-gson dependency #251

Closed DieterVDW closed 4 years ago

DieterVDW commented 4 years ago

This is my first time using auto-value-gson and it looks perfect for what I want to do, but unfortunately I can't get my code to work.

I refactored until I got a testcase that looks identical to the code in the readme and the classes in the example directory, however I still can't get it to work.

This is my testcase:

import com.google.auto.value.AutoValue;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.ryanharter.auto.value.gson.AutoValueGsonBuilder;

@AutoValue
public abstract class Foo {

  public abstract String value();

  @AutoValueGsonBuilder
  public static Builder builder() {
    return new AutoValue_Foo.Builder();
  }

  @AutoValue.Builder
  public abstract static class Builder {

    public abstract Builder value(String value);

    public abstract Foo build();
  }

  public static TypeAdapter<Foo> typeAdapter(Gson gson) {
    return new AutoValue_Foo.GsonTypeAdapter(gson);
  }
}

When trying to compile this, I get the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project model: Compilation failure
[ERROR] Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: com.ryanharter.auto.value.gson.factory.AutoValueGsonAdapterFactoryProcessor Unable to get public no-arg constructor

Some specs:

java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T20:41:47+02:00)
      <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.6</version>
      </dependency>
      <dependency>
        <groupId>com.ryanharter.auto.value</groupId>
        <artifactId>auto-value-gson</artifactId>
        <version>1.3.0</version>
      </dependency>

I'm probably doing something wrong, but it's not obvious to me. Any tips?

DieterVDW commented 4 years ago

Ironically, I also get this error when I try it on the following class (only file in the module):

public class Foo { }

This is my pom (apart from this there is only the artifact details in the pom, nothing else):

  <dependencies>
    <dependency>
      <groupId>com.google.auto.value</groupId>
      <artifactId>auto-value</artifactId>
      <version>1.7</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.6</version>
    </dependency>
    <dependency>
      <groupId>com.ryanharter.auto.value</groupId>
      <artifactId>auto-value-gson</artifactId>
      <version>1.3.0</version>
    </dependency>
  </dependencies>

  <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.1</version>
          <configuration>
            <source>11</source>
            <target>11</target>
<!--
            <annotationProcessorPaths>
                <path>
                    <groupId>com.google.auto.value</groupId>
                    <artifactId>auto-value</artifactId>
                    <version>1.7</version>
                </path>
            </annotationProcessorPaths>
-->
          </configuration>
        </plugin>
      </plugins>
  </build>

When I uncomment the annotationProcessorsPath section I get the following error (using the Foo class code from the first post):

[ERROR] /Users/dieterpersonal/Google Drive/AudioHarvest/Code/AudioHarvest/model/src/main/java/com/ascentinel/audioharvest/model/Foo.java:[27,29] cannot find symbol
  symbol:   class GsonTypeAdapter
  location: class com.ascentinel.audioharvest.model.AutoValue_Foo

Totally clueless here, this seems like the absolute minimal testcase? Any ideas what could be wrong with my build?

DieterVDW commented 4 years ago

In fact simply adding the dependency to any module makes the module fail with this error...

DieterVDW commented 4 years ago

Much ado about nothing! If I had read the part about the gradle dependencies in my enthousiasm, I could have found I had to actually add the annotation processors!

It works absolutely brilliantly now.