Code Monkey home page Code Monkey logo

Comments (9)

gk5885 avatar gk5885 commented on September 22, 2024

IMO, your feature request should be with your serialization framework for different construction strategies, not with AutoValue.

from auto.

talpr avatar talpr commented on September 22, 2024

I agree that Kryo should be able to construct object even without a no-arg constructor (and it might be able to with custom serializers), but it was just an example - I'm sure there are other libraries that use reflection to access a class's constructors, and in those cases AutoValue classes can't be used.

Now, for "regular" methods there's no problem since you can define them in the base (abstract) class, but you can't do that for constructors, so I do think there's value in somehow specifying that AutoValue should generate them.

from auto.

eamonnmcmanus avatar eamonnmcmanus commented on September 22, 2024

I just checked, and as I expected Kryo instantiates the object being deserialized through its private no-arg constructor and then sets its fields through reflection. It's possible to do this for the final fields that AutoValue generates, since Field.setAccessible allows you to override finalness (which bothers me but there it is). However I would not like to have to reason about the concurrent behaviour of objects built that way (do other threads see the new field values? when?). And, there's no way to validate the field values as you typically would in the static create method of the abstract class. So that's potentially a security hole.

The question of serialization is an important one and I've given it some thought. We already have some special-case code for GWT serialization. Could that be generalized in a way that would allow other serialization frameworks to function? For example, allowing users to generate other classes by supplying further templates for the Apache Velocity engine that AutoValue uses internally? I'm not satisfied that there's a solution whose benefit outweighs its complexity, though.

From looking at the Kryo documentation it does seem that there are mechanisms that could be used in generated code (https://github.com/EsotericSoftware/kryo#kryoserializable) if there were a way to teach AutoValue how.

from auto.

alfonsomunozpomer avatar alfonsomunozpomer commented on September 22, 2024

Seeing that this issue is still open, I just wanted to contribute by saying that you can create seralizers for your AutoValue classes. An example follows:

public class OntologyTermSerializer extends Serializer<OntologyTerm>  {
    public OntologyTermSerializer() {
        super();
    }

    @Override
    public void write(Kryo kryo, Output output, OntologyTerm ontologyTerm) {
        kryo.writeObject(output, ontologyTerm.id());
        kryo.writeObjectOrNull(output, ontologyTerm.source(), String.class);
    }

    @Override
    public OntologyTerm read(Kryo kryo, Input input, Class<OntologyTerm> aClass) {
        return OntologyTerm.create(kryo.readObject(input, String.class), kryo.readObjectOrNull(input, String.class));
    }

    public static void registerSerializers(final Kryo kryo) {
        final OntologyTermSerializer serializer = new OntologyTermSerializer();
        kryo.register(OntologyTerm.create("").getClass(), serializer);
    }
}

Where OntologyTerm is:

@AutoValue
public abstract class OntologyTerm {

    public abstract String id();

    @Nullable
    public abstract String source();

    public static OntologyTerm create(String id) {
        return create(id, null);
    }

    public static OntologyTerm create(String id, String source) {
        return new AutoValue_OntologyTerm(id, source);
    }
    ...
}

from auto.

eamonnmcmanus avatar eamonnmcmanus commented on September 22, 2024

Thanks, that's a very helpful tip for people running into this problem!

from auto.

alfonsomunozpomer avatar alfonsomunozpomer commented on September 22, 2024

No problem. The “magic” and where I stumbled a bit until I figured it out is the line:

kryo.register(OntologyTerm.create("").getClass(), serializer);

That links the actual AutoValue class to the abstract class.

from auto.

cgruber avatar cgruber commented on September 22, 2024

I was looking at the auto project overall, and thinking about a
documentation refresh, and this seems like a good candidate for a "tips and
tricks" page for auto-value.

On Thu, 28 May 2015 at 10:17 Alfonso Muñoz-Pomer Fuentes <
[email protected]> wrote:

No problem. The “magic” and where I stumbled a bit until I figured it out
is the line:

kryo.register(OntologyTerm.create("").getClass(), serializer);

That links the actual AutoValue class to the abstract class.


Reply to this email directly or view it on GitHub
#122 (comment).

from auto.

tuna-einstein avatar tuna-einstein commented on September 22, 2024

An auto generated builder can be used. Because, the generated builder class will have zero argument constructor with setter and getter methods. For example,

@AutoValue
public abstract class MyInfo {
    public MyInfo() {}
    public abstract String getUserName();
    public abstract String getPassword();
    public static Builder builder() {
        return new AutoValue_MyInfo.Builder();
    }

    public static Class<? extends Builder> builderClass() {
        return AutoValue_MyInfo.Builder.class;
    }

    @AutoValue.Builder
    public abstract static class Builder {
        public abstract Builder setUserName(String value);
        public abstract Builder setPassword(String value); 

        public abstract String getUserName();
        public abstract String getPassword();

        public abstract MyInfo build();
        public abstract Builder toBuilder();
    }
}

from auto.

pabloem avatar pabloem commented on September 22, 2024

I'd just like to throw my hat in and comment that I ran into the same issue using Avro for serialization.

from auto.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.