Comments (8)
Sounds like a reasonable idea. Does Moshi have an API for requesting adapters by type and annotation?
from auto-value-moshi.
from auto-value-moshi.
So there's two ways I'd see this working:
- Pull annotations off the methods at runtime. Requires having a reference to the actual
Method
at runtime. Delves into reflection territory and proguard issues - Copy annotations directly into the generated code. I don't actually know what would go into this, but there is some precedent in AutoValue itself for doing this.
I'm going to explore the second option a bit as I think that would be the ideal case.
from auto-value-moshi.
Hmm, looks like there's no way around this without doing some reflection unless we want to create a ton of boilerplate anonymous Annotation classes to simulate instances. AutoValue is cute by basically recording a stringified version of the annotations and writing them in directly, but that unfortunately doesn't help us.
Some ideas
- Reflectively get the method during adapter construction (
MyClass.getMethod("a").getAnnotations()
). Do it lazily and all reflection - Be smarter by checking during compile time that a given annotation on a method with the
@JsonQualifier
annotation exists (e.g. don't inline unnecessarily, which I think would be most) - Avoid obfuscation issues (but not reflection entirely) by annotating all with
@Json(name = "whatevs")
, then look them up in one loop through declaredMethods() and match them? - Find some other way of making methods look-up-able in a way that doesn't suck
from auto-value-moshi.
Alternative idea - borrow AutoValue's trick, but inline the generated annotations onto generated getters or fields for the adapters. Then we at least have a deterministic model for pulling their information.
Getters (uglier, but guaranteed to work with annotations since they must be able to target methods)
public static final class MoshiJsonAdapter extends JsonAdapter<WithJsonQualifierAnnotationsObject> {
private final JsonAdapter<String> aAdapter;
private final JsonAdapter<List<String>> bAdapter;
public MoshiJsonAdapter(Moshi moshi) {
this.aAdapter = moshi.adapter(String.class,
new LinkedHashSet<Annotation>(getClass().getDeclaredMethod("aAnnotationsHolder")
.getAnnotations()));
this.bAdapter = adapter(moshi, "b");
}
@ReverseList void aAnnotationsHolder() {
}
}
Or setters (might be iffy if the target annotation doesn't work with fields for some reason, but no empty placeholder methods!)
public static final class MoshiJsonAdapter extends JsonAdapter<WithJsonQualifierAnnotationsObject> {
@ReverseList private final JsonAdapter<String> aAdapter;
private final JsonAdapter<List<String>> bAdapter;
public MoshiJsonAdapter(Moshi moshi) {
this.aAdapter = moshi.adapter(String.class,
new LinkedHashSet<Annotation>(getClass().getDeclaredField("aAnnotationsHolder")
.getAnnotations()));
this.bAdapter = adapter(moshi, "b");
}
}
Both could potentially be smarter by accessing the known index directly (e.g. getFields()[1]
), but I'm not sure how safe this is with different compilers/runtimes.
from auto-value-moshi.
I'm leaning toward this:
- inline annotations onto fields. Caveat is that your qualifiers must work on fields.
- intelligently only inline detected annotations that are annotated with
@JsonQualifier
. If there's nothing, then we don't generate any special annotation handling - if we detect the existence of
@Keep
on the classpath, bolt those on to the fields too to protect them in proguard. Otherwise the only rule I can think of is to protect fields in anything calledMoshiJsonAdapter
:/
@swankjesse would be curious to hear your thoughts on all this too
from auto-value-moshi.
WAIT this already appears to exist 😨 #24
SoooOoOoOo I guess the question now becomes - is it worth optimizing further per the above?
(excuse me while I get this egg off my face)
from auto-value-moshi.
I think we can do this the way we do in moshi code gen - require field site target support and bolt them on to the adapter property.
We would need to borrow these rules if we can't just rely on moshi's embedded rules directly https://github.com/square/moshi/blob/master/moshi/src/main/resources/META-INF/proguard/moshi.pro#L31-L61
from auto-value-moshi.
Related Issues (20)
- MoshiJsonAdapter is not generated if #jsonAdapter(Moshi) is not public HOT 2
- Unresolved reference for AutoValue MoshiJsonAdapter HOT 1
- NoSuchMethodError: com.google.auto.common.MoreTypes.equivalence()
- Unable to build when implement the generic class HOT 4
- Support of default values using builders HOT 7
- autovalue moshi builder support does not work for optionals HOT 5
- Fails to generate adapter factory for inner class
- Generated Factory does not handle generated adapters HOT 1
- Use JsonAdapter nonNull() for nonnull checks
- Generic base types cause compile errors HOT 1
- Collection-valued property exposed as immutable causes compile error HOT 4
- please publish auto-value-moshi-runtime: HOT 2
- incorrect adapter created when using annotation for variable serialization HOT 2
- Gradle runtime dependency name incorrect in README.md HOT 1
- please upload new snapshot version to maven. HOT 8
- Parcelable auto value entities are being generated incorrectly with the 1.0.0 version HOT 9
- runtime artifact accidentally published as annotations HOT 6
- java poet 1.12.0 HOT 5
- Nullable fields not serialized HOT 1
- Failed to resolve: com.ryanharter.auto.value:auto-value-moshi-runtime:1.0.0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from auto-value-moshi.