Skip to content

Annotation Processor to generate List of generated Gson's TypeAdapterFactory(s) for Immutables.org

Notifications You must be signed in to change notification settings

techery/immutables-gson-adapters-provider

Repository files navigation

Immutables.org Gson Adapters Provider

Annotation Processor to aggregate all generated Gson's TypeAdapterFactory(s) by Immutables.org

What is it for?

Immutables.org is handy for models and Gson type adapters generation, the problems is you have to register all auto-generated adapters manually – that's where the processor comes in: it creates simple class to provide Collection of TypeAdapterFacotory.

Usage & Installation

GsonBuilder gsonBuilder = new GsonBuilder();
for (TypeAdapterFactory immutablesFactory : new ImmutablesGsonAdaptersProvider().getAdapters()) {
  gsonBuilder.registerTypeAdapterFactory(immutablesFactory);
}
Gson gson = gsonBuilder.create();

Build Status

repositories {
    maven { url "https://jitpack.io" }
}

compileJava { // optionally
    options.compilerArgs += '-Aorg.immutables.gson.adapter.package=org.immutables.gson.adapter.util'
    options.compilerArgs += '-Aorg.immutables.gson.adapter.provider_name=ImmutablesGsonAdaptersProvider'
}

dependencies {
    apt 'com.github.techery:immutables-gson-adapters-provider:$version'
}

Caveats

Processor uses only 1 round of annotation processing and while you don't generate interfaces for immutables (to generate models upon) it should be okey. The most solid solution would involve some gradle plugin to operate with classes generated by every possible round of processing, but is another story.

Also, Immutables.org generates META-INF/services file so possibly could be used by ServiceLoader but it's not a reliable solution for android and could cause performance penalty.

Credits

Thanks to Immutables.org for their constantly improving library and @devanatolii for basic processor implementation.

License

Copyright (c) 2017 Techery

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.

About

Annotation Processor to generate List of generated Gson's TypeAdapterFactory(s) for Immutables.org

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages