yatatsu / AutoBundle

AutoBundle generates boilerplate code for field binding with android.os.Bundle
Apache License 2.0
135 stars 13 forks source link

Define method annotation spec with `addAnnotation()` #29

Closed appiritsaishii closed 5 years ago

appiritsaishii commented 6 years ago

I want to change method spec defining.

AutoBundleWriter generates some methods with annotation. Unfortunately my project depends on newer JavaPoet version (1.11.0) generates invalid code:

public final class LibraryFragmentAutoBundle {
  public static LibraryFragmentAutoBundle. @NonNull Builder builder() {
    return new LibraryFragmentAutoBundle.Builder();
  }
  ...

JavaPoet contains braking changes for AutoBundle but I want to use it. For fixing this, it might be better to change method spec defining.

AutoBundleWriter defines some methods as .returns(fooClass.annotated(ANNOTATION_NONNULL)) now. It seems that method signature means "returns annotated Foo class type":

private static MethodSpec createCallBuilderMethod(AutoBundleBindingClass target) {
    ClassName builderClass = getBuilderClassName(target);
    MethodSpec.Builder builder =
            MethodSpec.methodBuilder("builder")
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
            .returns(builderClass.annotated(ANNOTATION_NONNULL)) // <- method's returning signature ("annotated type")
            .addCode("return new $T(", builderClass);
...

So, separate annotation from returning:

private static MethodSpec createCallBuilderMethod(AutoBundleBindingClass target) {
    ClassName builderClass = getBuilderClassName(target);
    MethodSpec.Builder builder =
            MethodSpec.methodBuilder("builder")
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
            .addAnnotation(ANNOTATION_NONNULL) // <- method's annotation
            .returns(builderClass)             // <- method's returning signature
            .addCode("return new $T(", builderClass);
...

then

public final class LibraryFragmentAutoBundle {
  @NonNull
  public static LibraryFragmentAutoBundle.Builder builder() {
    return new LibraryFragmentAutoBundle.Builder();
  }
...
yatatsu commented 5 years ago

Sorry for late reply. I will see it soon.

yatatsu commented 5 years ago

Thanks for fixing the issue. I'll merge it.