Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More complex generic example #23

Open
dyorgio opened this issue Oct 13, 2014 · 2 comments
Open

More complex generic example #23

dyorgio opened this issue Oct 13, 2014 · 2 comments

Comments

@dyorgio
Copy link

dyorgio commented Oct 13, 2014

Documentation has only one example about using generics.

I couldn't find on internet how extend a class that defines more than one generic parameter
using javassist.

I have a class that I want to extend at runtime using javassist, but generic parameter need to exists at runtime for reflection (hibernate needs it).

So, I´m not posting this issue to get a response for my problem.

The issue is true, have no documentation and examples with use of generics in the real world (extends, implements, < T extends MyType < X < V > > , X, V >).

@chibash
Copy link
Member

chibash commented Oct 14, 2014

Multiple generic parameters are not so different from a single one.
Just build a class file as if all the parameters are of type java.lang.Object.
Then attach appropriate generic signatures to the class and the methods.

@dyorgio
Copy link
Author

dyorgio commented Oct 14, 2014

Hi @chibash , thank you for reply 😃

Sometimes what is easy to us is hard to another people that don´t use javassist every day.

I solve my problem, trying a lot....many, many, many times...

I will put here, so if you or another dev of javassist put it on a example/tutorial file it will be cool:

Problem: retain ParametrizedType generic info at runtime for hibernate read it from AttributeConverter interface.

Solution: Use

ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get("your.base.generic.enum.converter.Type");
cc.setName("someRandomName");

ClassSignature cs = new ClassSignature(null, null,
                                  // Set interface and its generic params
                                  new ClassType[]{new ClassType(AttributeConverter.class.getName(),
                                    new TypeArgument[]{
                                      new TypeArgument(new ClassType("user.enum.Type")),
                                        new TypeArgument(new ClassType("database.Type"))
                                    })});

cc.setGenericSignature(cs.encode());

// Constructor that call base class constructor with argument
CtConstructor ctConstructor = new CtConstructor(new CtClass[]{}, cc);
ctConstructor.setBody("this(" + "user.enum.Type" + ".class);");
cc.addConstructor(ctConstructor);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants