users@jaxb.java.net

Re: hasEnum & Class.forName in CodeModel

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Tue, 21 Nov 2006 13:30:10 +0100

Hi.

> Is there any Method to check whether a JType (object of type
> JClass/JDefinedClass) has an enum or not, an to get the constants of the
> enum?

classes() iterates over inner classes.
getClassType() is the class type, see ClassType.

Enum constants are currently not available publicly, please log an issue on
codemodel.dev.java.net project. Currently you can get it from the private field
enumConstantsByName via reflection (ugly :( ).

> And are there any tricks to create a JClass from String expression? Just
> something similar to Class.forName. I've searched the API, but didn't
> find one.

codeModel.ref(...).

> I'm trying to initialize an Object referred by the EnumConstant.
>
> For Ex:
> Class A is abstract, and has subclass of class B & C

Inner class you mean.

> So class A Definition looks like this.
> Class A
> {
> enum Type{
> B,
> C
> }
> }
>
> And there is a class which has a field of type A.
>
> Class Root
> {
> A aField;
> }
>
> I want to add a method to initialize this aField with the expected
> sublcass.
>
> So it should look like this
>
> Void addA( A.Type type)
> {
> String typeStr = type.getString();
> Class clazz = Class.forName(typeStr);
> aField = new clazz();
> }
>
> Is there any way to accomplish this???

Not 100% sure what you try to do. Please post the "target" code fragment, I
can't get what you mean.

Bye.
/lexi