users@jaxb.java.net

Re: hasEnum & Class.forName in CodeModel

From: <HeruMartinus.Salim_at_infineon.com>
Date: Tue, 21 Nov 2006 13:45:45 +0100

Ok,

Here is the code fragment:

public abstract class Port
{

        //This enum type containing all the Classes
        //subclassing the abstract class Port
    public enum Type {

        BUS_SLAVE_PORT,
        BUS_MASTER_PORT,
        SIGNAL_PORT;

    }

}

public class IPComponent
    extends Block
{

    protected List<Port> port;

    public Iterator<Port> getPort() {
        if (port == null) {
            port = new ArrayList<Port>();
        }
        return this.port.iterator();
    }

    public Port addPort() { //Expected to be public Port
addPort(Port.Type type)
        if (port == null) {
            port = new ArrayList<Port>();
        }
                /*
                Here is the algorithm to convert from enum type
(Port.Type) to respecting class
                eg. Port.SIGNAL_PORT means creating an instance of class
SignalPort (subclass of Port)
                */

        Port value = new Port(); //So here in case Type is
Port.SIGNAL_PORT, -> Port value = new SignalPort();
        this.port.add(value);
        return value;
    }


}

> -----Original Message-----
> From: Aleksei Valikov [mailto:valikov_at_gmx.net]
> Sent: Tuesday, November 21, 2006 1:30 PM
> To: users_at_jaxb.dev.java.net
> Subject: Re: hasEnum & Class.forName in CodeModel
>
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>