Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EAttribute to Java datatype
EAttribute to Java datatype [message #1273063] Wed, 19 March 2014 12:34 Go to next message
Sudharshan Ravi is currently offline Sudharshan RaviFriend
Messages: 12
Registered: March 2014
Junior Member
Hi

If i have an EAttribute that got converted from xsd:boolean for example what is the easiest way to determine the corresponding Java type of this attribute without using a long list of switch case ?

I tried
EAttribute.getEType().getInstanceClass().getClass()

To find out the type but this returns a generic Class<? extends type> not the actual "type"
Re: EAttribute to Java datatype [message #1273089 is a reply to message #1273063] Wed, 19 March 2014 13:30 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi,

The Java type of the attribute is just getEType().getInstanceClass().
Asking for the class of that gives you java.lang.Class, which is the
class of every run-time class.

HTH,

Christian


On 2014-03-19 12:34:05 +0000, Sudharshan Ravi said:

> Hi
>
> If i have an EAttribute that got converted from xsd:boolean for example
> what is the easiest way to determine the corresponding Java type of
> this attribute without using a long list of switch case ?
>
> I tried EAttribute.getEType().getInstanceClass().getClass() To find out
> the type but this returns a generic Class<? extends type> not the
> actual "type"
Re: EAttribute to Java datatype [message #1273093 is a reply to message #1273063] Wed, 19 March 2014 13:42 Go to previous messageGo to next message
Hussein MHANNA is currently offline Hussein MHANNAFriend
Messages: 45
Registered: February 2014
Location: LAVAL
Member
Hi,

To get the java type, you could use EAttribute.getEAttributeType().getInstanceClass().

ex: to test if the attribute type is a boolean

EAttribute.getEAttributeType().getInstanceClass().equals(Boolean.class)


If the datatype is predefined in ecore (like EString, EBoolean, ....), then you can use the EcorePackage.Literals ex :
getEAttributeType().equals(EcorePackage.Literals.EBoolean)



Kind Regards,

Hussein


ALL4TEC
Re: EAttribute to Java datatype [message #1273111 is a reply to message #1273093] Wed, 19 March 2014 14:30 Go to previous messageGo to next message
Sudharshan Ravi is currently offline Sudharshan RaviFriend
Messages: 12
Registered: March 2014
Junior Member
Hi

Quote:
Hi,

The Java type of the attribute is just getEType().getInstanceClass().
Asking for the class of that gives you java.lang.Class, which is the
class of every run-time class.

HTH,

Christian


But we want the specific Java class which was used for the mapping, not the class of every run-time class.

Quote:
To get the java type, you could use EAttribute.getEAttributeType().getInstanceClass().

ex: to test if the attribute type is a boolean

EAttribute.getEAttributeType().getInstanceClass().equals(Boolean.class)


If the datatype is predefined in ecore (like EString, EBoolean, ....), then you can use the EcorePackage.Literals ex :
getEAttributeType().equals(EcorePackage.Literals.EBoolean)


This is the output of the type of the attribute

System.out.println("attr.getEAttributeType().getInstanceClass() : " + attr.getEAttributeType().getInstanceClass());
System.out.println("attr.getEType().getInstanceClass() : " + attr.getEType().getInstanceClass());


attr.getEAttributeType().getInstanceClass() : boolean
attr.getEType().getInstanceClass() : boolean



System.out.println("attr.getEAttributeType().getInstanceClass().equals(Boolean.class) : " + attr.getEAttributeType().getInstanceClass().equals(Boolean.class));

attr.getEAttributeType().getInstanceClass().equals(Boolean.class) : false


Re: EAttribute to Java datatype [message #1273115 is a reply to message #1273111] Wed, 19 March 2014 14:38 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi,

Yes, that's why you need the getEType().getInstanceClass() only. I was
explaining why asking further for the getClass() was giving you the
wrong result.

For your particular attribute, this returns boolean.class, not
Boolean.class, probably because the attribute's type is
EcorePackage.Literals.EBOOLEAN (which represents Java's boolean), not
EcorePackage.Literals.EBOOLEAN_OBJECT (which represents Java's
java.lang.Boolean).

Cheers,

Christian


On 2014-03-19 14:30:43 +0000, Sudharshan Ravi said:

> Hi
>
> Quote:
>> Hi,
>>
>> The Java type of the attribute is just getEType().getInstanceClass().
>> Asking for the class of that gives you java.lang.Class, which is the
>> class of every run-time class.
>>
>> HTH,
>>
>> Christian
>
>
> But we want the specific Java class which was used for the mapping, not
> the class of every run-time class.
>
> Quote:
>> To get the java type, you could use
>> EAttribute.getEAttributeType().getInstanceClass().
>>
>> ex: to test if the attribute type is a boolean
>>
>> EAttribute.getEAttributeType().getInstanceClass().equals(Boolean.class)
>>
>>
>> If the datatype is predefined in ecore (like EString, EBoolean, ....),
>> then you can use the EcorePackage.Literals ex :
>> getEAttributeType().equals(EcorePackage.Literals.EBoolean)
>
>
> This is the output of the type of the attribute
>
> System.out.println("attr.getEAttributeType().getInstanceClass() : " +
> attr.getEAttributeType().getInstanceClass());
> System.out.println("attr.getEType().getInstanceClass() : " +
> attr.getEType().getInstanceClass());
>
>
> attr.getEAttributeType().getInstanceClass() : boolean
> attr.getEType().getInstanceClass() : boolean
>
>
>
> System.out.println("attr.getEAttributeType().getInstanceClass().equals(Boolean.class)
> : " +
> attr.getEAttributeType().getInstanceClass().equals(Boolean.class));
>
> attr.getEAttributeType().getInstanceClass().equals(Boolean.class) : false
Re: EAttribute to Java datatype [message #1273116 is a reply to message #1273111] Wed, 19 March 2014 14:39 Go to previous messageGo to next message
Felix Dorner is currently offline Felix DornerFriend
Messages: 295
Registered: March 2012
Senior Member
On 19/03/2014 15:30, Sudharshan Ravi wrote:

> attr.getEAttributeType().getInstanceClass() : boolean
> attr.getEType().getInstanceClass() : boolean

The two will always return the same thing.

>
> System.out.println("attr.getEAttributeType().getInstanceClass().equals(Boolean.class)
> : " + attr.getEAttributeType().getInstanceClass().equals(Boolean.class));
>
> attr.getEAttributeType().getInstanceClass().equals(Boolean.class) : false
>

How about you use boolean.class instead of Boolean.class? It's not
really clear what you want though :)
Re: EAttribute to Java datatype [message #1273130 is a reply to message #1273116] Wed, 19 March 2014 15:07 Go to previous messageGo to next message
Hussein MHANNA is currently offline Hussein MHANNAFriend
Messages: 45
Registered: February 2014
Location: LAVAL
Member
I think with this getEAttributeType().equals(EcorePackage.Literals.EBoolean), it will work in your case (EBoolean is the equivalent of the boolean java primitive type and EBOOLEAN_OBJECT is the equivalent of the Boolean object java.lang.Boolean).

Hussein


ALL4TEC
Re: EAttribute to Java datatype [message #1273140 is a reply to message #1273130] Wed, 19 March 2014 15:26 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Hessein,

It could also be XMLTypePackage.Literals.BOOLEAN (and will be if the
model is derived from an XSD) so one should be careful about the purpose
of the test, which isn't clear. Of course == can be used for testing
EObjects equality...

On 19/03/2014 8:07 AM, Hussein MHANNA wrote:
> I think with this
> getEAttributeType().equals(EcorePackage.Literals.EBoolean), it will
> work in your case (EBoolean is the equivalent of the boolean java
> primitive type and EBOOLEAN_OBJECT is the equivalent of the Boolean
> object java.lang.Boolean).
>
> Hussein


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Binary resource compatibility
Next Topic:DragAndDropCommand move objects between different owners
Goto Forum:
  


Current Time: Fri Apr 26 23:34:43 GMT 2024

Powered by FUDForum. Page generated in 0.03307 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top