1

I've created a set of classes that represent RESTful resources, and other helper things that actually do the HTTP requests to retrieve and build objects. My classes look like this :

class MyResource{
    Attribute id = new Attribute(this, long);
    Attribute name = new Attribute(this, String);
    /* etc */
}

Now it happens that I would like to use POJO classes in order to plug to a framework that likes to deal with POJOs.

I would like to have proxies that would look like this:

class MyResourceProxy{
    private MyResource realResource;

    public MyResourceProxy(MyResource o){realResource = o;}

    public long getId(){
        return realResource.id.get();
    }

    public void setId(long value){
        realResource.id.set(value);
    }

    public String getName(){
        return realResource.name.get();
    }

    public void setName(String value){
        realResource.name.set(value);
    }        
}

I don't want to have to maintain code for those proxy classes, but only the "resource-type" master classes.

I looked into introspection and found a hint on how to generate the said proxy code on demand. The question is : is it possible to generate the code at compile-time, and then have it compiled along with the library? Maybe I've taken the wrong turn and I'm doing something uninteresting, though ;)

What do you think? Thanks!

2
  • If the client code is that dynamic, how is the user of your client code supposed to deal with that? Commented Mar 24, 2011 at 17:54
  • Well the client code user would know what classes/methods to use, but I want to make sure the proxy classes are always up-to-date. Commented Mar 25, 2011 at 10:47

2 Answers 2

2

It depends on what you build system is, if you mean javac, then I would say no, but if you use ant or maven then you can.

There are lots of examples for code generators.

In your case I would use reflection on the compiled MyResource class. I would consider using Velocity to help template the class. It may be overkill in your case, but as you generate more code it may be useful.

Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried using dependency injection to generate your classes on instantiation?

Basic example for DI

1 Comment

I would have like something lighter, but thanks for the hint.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.