users@codemodel.java.net

Re: Is it possible to obtain a JDefinedClass from a java.lang.Class

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Mon, 1 Aug 2011 16:32:38 +0200

Hi,

> First of all, sorry for my poor english level...
>
> I have one technical question : is it possible to obtain a JDefinedClass
> from an existing java.lang.Class ?

No, only a JClass.

> This from the perspective of annotating an existing Bean copy without
> introspecting the existing Bean.
>
> The code snippet I attempt to do may be like this :
>
> JCodeModel cm = new JCodeModel();
>
> // My existing Bean
> JClass baseClass = cm.ref(MyBean.class);
>
> // Make a JDefinedClass of a MyBean copy (with methods and attributs already
> defined).
> JDefinedClass copiedClass = cm._getClass(baseClass,
> "org.pkg.test.newClassName");

Well, you could introspect the MyBean.class via reflection and
generate a new JDefinedClass which would delegate all the public
methods to the MyBean class instance. This would work.

> // Add the annotation to the myMethod() method.
> copiedClass.getMethod("myMethod", null).annotate(MyAnnotation.class);

Bye,
/lexi