Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generic Collection
Generic Collection [message #541554] Mon, 21 June 2010 10:27 Go to next message
jeremie is currently offline jeremie
Messages: 233
Registered: April 2010
Senior Member
Hi,

In my ecore model, I create a Type Collection<T>

in My class VariableAccessImpl, I have these methods :
public EClassifier getType() {
		return (isCollection()) ? AdapterDSLFactory.eINSTANCE.createCollection().eClass() : getVariable().getEType();
	}

	public boolean isCollection() {
		return getVariable().isCollection();
	}


The problem, is the generic type of Collection. At this moment I don"t know the generic type, it is dynamic.
The generic type of Collection is getVariable().getEType() :

So I write this :
public EClassifier getType() {
		return (isCollection()) ? AdapterDSLFactory.eINSTANCE.<getVariable().getEType()>createCollection().eClass() : getVariable().getEType();
	}


but java don't like ".<getVariable().getEType()>createCollection()"

How can I resolve this problem?

Thanks,

Jérémie
Re: Generic Collection [message #541573 is a reply to message #541554] Mon, 21 June 2010 11:12 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 24538
Registered: July 2009
Senior Member
Jérémie,

No matter what type you specify, it doesn't affect the result value, so
why not use AdapterDSLFactory.eINSTANCE.<Object>createCollection().eClass()


jeremie wrote:
> Hi,
>
> In my ecore model, I create a Type Collection<T>
>
> in My class VariableAccessImpl, I have these methods :
>
> public EClassifier getType() {
> return (isCollection()) ?
> AdapterDSLFactory.eINSTANCE.createCollection().eClass() :
> getVariable().getEType();
> }
>
> public boolean isCollection() {
> return getVariable().isCollection();
> }
>
>
> The problem, is the generic type of Collection. At this moment I don"t
> know the generic type, it is dynamic.
> The generic type of Collection is getVariable().getEType() :
>
> So I write this :
>
> public EClassifier getType() {
> return (isCollection()) ?
> AdapterDSLFactory.eINSTANCE.<getVariable().getEType()>createCollection().eClass()
> : getVariable().getEType();
> }
>
>
> but java don't like ".<getVariable().getEType()>createCollection()"
>
> How can I resolve this problem?
>
> Thanks,
>
> Jérémie
>
Re: Generic Collection [message #541580 is a reply to message #541573] Mon, 21 June 2010 11:40 Go to previous messageGo to next message
jeremie is currently offline jeremie
Messages: 233
Registered: April 2010
Senior Member
hmm because I think it will be useful for me to know what kinds of object contain the Collection.
Re: Generic Collection [message #541585 is a reply to message #541580] Mon, 21 June 2010 12:10 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 24538
Registered: July 2009
Senior Member
Jeremie,

The return type of your method is EClassifier; you'd need it to be
something like EGenericType if you want to capture more information.
Keep in mind too that like Java, EMF's generic type system is based on
erasure, so at runtime value.getClass() and value.eClass() return the
same thing regardless of whether value is actually of type List<Foo> or
List<Bar>.


jeremie wrote:
> hmm because I think it will be useful for me to know what kinds of
> object contain the Collection.
Re: Generic Collection [message #541596 is a reply to message #541585] Mon, 21 June 2010 13:29 Go to previous messageGo to next message
jeremie is currently offline jeremie
Messages: 233
Registered: April 2010
Senior Member
I try to do something like this,
	public EClassifier getType() {
		EClassifier eType = getVariable().getEType();
		EGenericType eGenericType = EcoreFactory.eINSTANCE.createEGenericType();
		eGenericType.setEClassifier(eType);
		 ETypeParameter eTypeParameter = EcoreFactory.eINSTANCE.createETypeParameter();
		 eTypeParameter.setName(eType.getName());
		Collection<eTypeParameter> collection = AdapterDSLFactory.eINSTANCE.<eTypeParameter>createCollection();
		Collection<eGenericType> collection2 = AdapterDSLFactory.eINSTANCE.<eGenericType>createCollection();
//		ETypeParameter eTypeParameter = collection.eClass().getETypeParameters().get(0); 
//		eTypeParameter = (ETypeParameter) getVariable().getEType();
		return (isCollection()) ? collection.eClass() : getVariable().getEType();
	}


But it still doesn't work.

I am not sure it is possible why this way.
Re: Generic Collection [message #541602 is a reply to message #541596] Mon, 21 June 2010 14:07 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 24538
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------070402060805060203090904
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Jeremie,

Comments below.


jeremie wrote:
> I try to do something like this,
>
> public EClassifier getType() {
> EClassifier eType = getVariable().getEType();
> EGenericType eGenericType =
> EcoreFactory.eINSTANCE.createEGenericType();
> eGenericType.setEClassifier(eType);
> ETypeParameter eTypeParameter =
> EcoreFactory.eINSTANCE.createETypeParameter();
> eTypeParameter.setName(eType.getName());
> Collection<eTypeParameter> collection =
> AdapterDSLFactory.eINSTANCE.<eTypeParameter>createCollection();
No matter how hard you try, you'll never be able to pass the value of a
variable as the static type. It *must *be the static name of a class.
> Collection<eGenericType> collection2 =
> AdapterDSLFactory.eINSTANCE.<eGenericType>createCollection();
> // ETypeParameter eTypeParameter =
> collection.eClass().getETypeParameters().get(0); //
> eTypeParameter = (ETypeParameter) getVariable().getEType();
> return (isCollection()) ? collection.eClass() :
> getVariable().getEType();
And as I also said, no matter how hard you try, the collection will
never reveal its type arguments.
> }
>
>
> But it still doesn't work.
It never will. You're going down dead end.
>
> I am not sure it is possible why this way.
>
Only if you change the return to be EGenericType will you get
somewhere. It's not entirely clear what you're trying to accomplish...

--------------070402060805060203090904
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Jeremie,<br>
<br>
Comments below.<br>
<br>
<br>
jeremie wrote:
<blockquote cite="mid:hvo7id$er4$1@build.eclipse.org" type="cite">I try
to do something like this,
<br>
<br>
    public EClassifier getType() {
<br>
        EClassifier eType = getVariable().getEType();
<br>
        EGenericType eGenericType =
EcoreFactory.eINSTANCE.createEGenericType();
<br>
        eGenericType.setEClassifier(eType);
<br>
         ETypeParameter eTypeParameter =
EcoreFactory.eINSTANCE.createETypeParameter();
<br>
         eTypeParameter.setName(eType.getName());
<br>
        Collection&lt;eTypeParameter&gt; collection =
AdapterDSLFactory.eINSTANCE.&lt;eTypeParameter&gt;cr eateCollection();
<br>
</blockquote>
No matter how hard you try, you'll never be able to pass the value of a
variable as the static type.  It <b>must </b>be the static name of a
class.<br>
<blockquote cite="mid:hvo7id$er4$1@build.eclipse.org" type="cite">       
Collection&lt;eGenericType&gt; collection2 =
AdapterDSLFactory.eINSTANCE.&lt;eGenericType&gt;crea teCollection();
<br>
//        ETypeParameter eTypeParameter =
collection.eClass().getETypeParameters().get(0); //       
eTypeParameter = (ETypeParameter) getVariable().getEType();
<br>
        return (isCollection()) ? collection.eClass() :
getVariable().getEType();
<br>
</blockquote>
And as I also said, no matter how hard you try, the collection will
never reveal its type arguments.<br>
<blockquote cite="mid:hvo7id$er4$1@build.eclipse.org" type="cite">    }
<br>
<br>
<br>
But it still doesn't work.
<br>
</blockquote>
It never will. You're going down dead end.<br>
<blockquote cite="mid:hvo7id$er4$1@build.eclipse.org" type="cite"><br>
I am not sure it is possible why this way.
<br>
<br>
</blockquote>
Only if you change the return to be EGenericType will you get
somewhere.  It's not entirely clear what you're trying to accomplish...<br>
</body>
</html>

--------------070402060805060203090904--
Re: Generic Collection [message #541704 is a reply to message #541602] Tue, 22 June 2010 05:17 Go to previous messageGo to next message
jeremie is currently offline jeremie
Messages: 233
Registered: April 2010
Senior Member
I will try to explain better the problem.

for exemple I have this Expression :

node.inports

where inport is a Collection of "IPort"

but in EMF there are no type Collection,
The only Way to know that it is a collection is like this :

node.inports,getUpperBound() == -1

And I need to do some special operation if it is a collection

That's why I create the type Collection in my model.
In this type I add some EOperation.

The problem is that I don't have the information of what kind of EClassifier are in the Collection.

I try to use the genericity, So I create Collection<T>

but <T> is static but it mus be dynamic.

because An expression have to methode :

	public EClassifier getType() {
		Collection collection = AdapterDSLFactory.eINSTANCE.createCollection();;
		return (isCollection()) ? collection.eClass() : getElement().getEType();
	}

	public boolean isCollection() {
		return this.getElement().getUpperBound() == -1;
	}


if the Expression is a Collection, this collection contain eLements of type getElement().getEType().

I don't know how the Collection Type can have this information

I hope that the problem is more clear now.

Don't hesitate to ask questions

Jérémie




Re: Generic Collection [message #541812 is a reply to message #541704] Tue, 22 June 2010 11:15 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 24538
Registered: July 2009
Senior Member
Jeremie,

Comments below.

jeremie wrote:
> I will try to explain better the problem.
>
> for exemple I have this Expression :
>
> node.inports
>
> where inport is a Collection of "IPort"
>
> but in EMF there are no type Collection,
> The only Way to know that it is a collection is like this :
>
> node.inports,getUpperBound() == -1
>
> And I need to do some special operation if it is a collection
>
> That's why I create the type Collection in my model.
> In this type I add some EOperation.
>
> The problem is that I don't have the information of what kind of
> EClassifier are in the Collection.
>
> I try to use the genericity, So I create Collection<T>
>
> but <T> is static but it mus be dynamic.
That's what I've been trying to explain. Generics in Java and EMF is a
static concept. There is no runtime information available to
distinguish whether an instance of Collection is Collection<T>,
Collection<?>, or Collection<Object.
>
> because An expression have to methode :
>
>
> public EClassifier getType() {
> Collection collection =
> AdapterDSLFactory.eINSTANCE.createCollection();;
> return (isCollection()) ? collection.eClass() :
> getElement().getEType();
> }
>
> public boolean isCollection() {
> return this.getElement().getUpperBound() == -1;
> }
>
>
> if the Expression is a Collection, this collection contain eLements of
> type getElement().getEType().
>
> I don't know how the Collection Type can have this information
Ask yourself the same question but the Java version of it... How can
java.lang.Class have this information? It can't.
>
> I hope that the problem is more clear now.
It's been clear for a while you're trying to do something that's not
possible.
>
> Don't hesitate to ask questions
>
> Jérémie
>
>
>
>
>
Re: Generic Collection [message #541832 is a reply to message #541812] Tue, 22 June 2010 12:31 Go to previous message
jeremie is currently offline jeremie
Messages: 233
Registered: April 2010
Senior Member
oki,

that's not possible...
that's what I feared.


I will try to resolve this problem by a different way.

Thanks a lot for your help

Jérémie
Previous Topic:b3 status update
Next Topic:"Superfluous" building with external compiler
Goto Forum:
  


Current Time: Mon May 20 15:14:48 EDT 2013

Powered by FUDForum. Page generated in 0.01718 seconds