Annotation Processor to aggregate all generated Gson
's TypeAdapterFactory
(s) by Immutables.org
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
.
GsonBuilder gsonBuilder = new GsonBuilder();
for (TypeAdapterFactory immutablesFactory : new ImmutablesGsonAdaptersProvider().getAdapters()) {
gsonBuilder.registerTypeAdapterFactory(immutablesFactory);
}
Gson gson = gsonBuilder.create();
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'
}
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.
Thanks to Immutables.org for their constantly improving library and @devanatolii for basic processor implementation.
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.