Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to access EList template type at runtime
How to access EList template type at runtime [message #417052] Mon, 25 February 2008 11:42 Go to next message
Eclipse UserFriend
Originally posted by: john.obyrne.gmail.com

Hi,

I would like at runtime to be able to do
if (element instanceof EList<MyTemplate>)
// Do something
else if (element instanceof EList<MyOtherTemplate>)
// Do something else
....

I know it's not possible to know at runtime the template type (even using
reflection) in java. In the previous code generated engine we used, we had a
method on our Collection which was:

T getManagedType() {
return T.class;
}

which allowed us to know the Template type at runtime.

Is there a possibility in EMF to get the template type at runtime? (using
EMF self describing capability maybe)

Thanks,
John
Re: How to access EList template type at runtime [message #417055 is a reply to message #417052] Mon, 25 February 2008 12:10 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
John,

The lists returned by multi-valued references always implements
EStructuralFeature.Setting which provides access to the
EStructuralFeature managed by that list, which in turn provides access
to the feature's eType, i.e., the EClassifier. For a generated model,
that EClassifier's instance class would be what you're after.


John O'Byrne wrote:
> Hi,
>
> I would like at runtime to be able to do
> if (element instanceof EList<MyTemplate>)
> // Do something
> else if (element instanceof EList<MyOtherTemplate>)
> // Do something else
> ....
>
> I know it's not possible to know at runtime the template type (even using
> reflection) in java. In the previous code generated engine we used, we had a
> method on our Collection which was:
>
> T getManagedType() {
> return T.class;
> }
>
> which allowed us to know the Template type at runtime.
>
> Is there a possibility in EMF to get the template type at runtime? (using
> EMF self describing capability maybe)
>
> Thanks,
> John
>
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to access EList template type at runtime [message #417063 is a reply to message #417055] Mon, 25 February 2008 17:25 Go to previous message
Eclipse UserFriend
Originally posted by: john.obyrne.gmail.com

Thanks, it works perfectly :)

I use the following code:

public static boolean isCollectionOf(EList<?> list, Class<?> type) {
if (list instanceof Setting) {
Setting setting = (Setting) list;
if
(type.isAssignableFrom(setting.getEStructuralFeature().getEG enericType().getEClassifier().getInstanceClass()))
return true;
}
return false;
}

"Ed Merks" <merks@ca.ibm.com> wrote in message
news:fpub8h$l56$1@build.eclipse.org...
> John,
>
> The lists returned by multi-valued references always implements
> EStructuralFeature.Setting which provides access to the EStructuralFeature
> managed by that list, which in turn provides access to the feature's
> eType, i.e., the EClassifier. For a generated model, that EClassifier's
> instance class would be what you're after.
>
>
> John O'Byrne wrote:
>> Hi,
>>
>> I would like at runtime to be able to do
>> if (element instanceof EList<MyTemplate>)
>> // Do something
>> else if (element instanceof EList<MyOtherTemplate>)
>> // Do something else
>> ....
>>
>> I know it's not possible to know at runtime the template type (even using
>> reflection) in java. In the previous code generated engine we used, we
>> had a method on our Collection which was:
>>
>> T getManagedType() {
>> return T.class;
>> }
>>
>> which allowed us to know the Template type at runtime.
>>
>> Is there a possibility in EMF to get the template type at runtime? (using
>> EMF self describing capability maybe)
>>
>> Thanks,
>> John
>>
>>
Previous Topic:Using Model Listeners / Adapters in AbstractPropertySections
Next Topic:Proper way to reflectively add/remove value from multi-valued feature?
Goto Forum:
  


Current Time: Fri Apr 19 15:58:17 GMT 2024

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

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

Back to the top