Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtend operation construction - IJvmModelInferrer(Xtend operation construction - IJvmModelInferrer)
Xtend operation construction - IJvmModelInferrer [message #722974] Wed, 07 September 2011 10:52 Go to next message
Balazs Molnar is currently offline Balazs MolnarFriend
Messages: 28
Registered: July 2009
Junior Member
When I create a new type from within my IJvmModelInferrer extension using the following:

val jvmClass = typesFactory.createJvmGenericType
jvmClass.makePublic
... etc

I also would like to add a new operation which would have a return type of the new type. How can I set the return type of the new operation to the JvmTypeReference of the newly created jvmClass ?

thank you,
Balázs
Re: Xtend operation construction - IJvmModelInferrer [message #722976 is a reply to message #722974] Wed, 07 September 2011 11:00 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi,

have a look at the domainmodel example.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtend operation construction - IJvmModelInferrer [message #723082 is a reply to message #722976] Wed, 07 September 2011 15:13 Go to previous messageGo to next message
Balazs Molnar is currently offline Balazs MolnarFriend
Messages: 28
Registered: July 2009
Junior Member
Thanks, I did that and it was there...
I am now trying to create an empty constructor like this:
val emptyConstructor = typesFactory.createJvmConstructor
emptyConstructor.makePublic
emptyConstructor.simpleName = jvmClass.simpleName
emptyConstructor.declaringType = jvmClass
jvmClass.members += emptyConstructor
The above results in "Couldn't resolve reference to JvmConstructor ..." when trying to use it from an XBase expression
(The type itself is usable (can be used as an operation argument for example)).
What is the right method to implement the constructor ?

Thank you,
Balázs
Re: Xtend operation construction - IJvmModelInferrer [message #723098 is a reply to message #723082] Wed, 07 September 2011 15:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi,

cannot reproduce your problem using the domainmodel example

at least when doing this: http://www.eclipse.org/forums/index.php/mv/msg/222363/699568/#msg_699568

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 07 September 2011 16:13]

Report message to a moderator

Re: Xtend operation construction - IJvmModelInferrer [message #723234 is a reply to message #722974] Thu, 08 September 2011 06:35 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
You could use
org.eclipse.xtext.common.types.util.TypeReferences.createTypeRef(JvmType, JvmTypeReference...)
or use the TypesFactory to create a JvmParameterizedTypeReference and
set the type on it.

Am 07.09.11 12:52, schrieb Balazs Molnar:
> When I create a new type from within my IJvmModelInferrer extension
> using the following:
>
> val jvmClass = typesFactory.createJvmGenericType
> jvmClass.makePublic
> .. etc
>
> I also would like to add a new operation which would have a return type
> of the new type. How can I set the return type of the new operation to
> the JvmTypeReference of the newly created jvmClass ?
>
> thank you,
> Balázs


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


---
Get professional support from the Xtext committers at www.typefox.io
Re: Xtend operation construction - IJvmModelInferrer [message #723274 is a reply to message #723234] Thu, 08 September 2011 08:39 Go to previous messageGo to next message
Balazs Molnar is currently offline Balazs MolnarFriend
Messages: 28
Registered: July 2009
Junior Member
- the method creation returning the newly created type works fine
- on the constructor: I have modified my QualifiedNameProvider, which now lets me use my constructor. But when using my model I get this message thrown:

Duplicate JvmAnnotationTarget '<unnamed>'

any ideas what I should check ?

thanks a lot,
Balázs
Re: Xtend operation construction - IJvmModelInferrer [message #723277 is a reply to message #723274] Thu, 08 September 2011 08:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi,

seems that you created more than one lement without a name.
what about using the debugger to find out what happens?

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Thu, 08 September 2011 08:42]

Report message to a moderator

Re: Xtend operation construction - IJvmModelInferrer [message #723341 is a reply to message #723277] Thu, 08 September 2011 11:31 Go to previous messageGo to next message
Balazs Molnar is currently offline Balazs MolnarFriend
Messages: 28
Registered: July 2009
Junior Member
Hello,

It looks like the type and the constructor share the same name if I use the method described above to customize the DefaultDeclarativeQualifiedNameProvider:
QualifiedName qualifiedName(JvmConstructor  type) {
		QualifiedName qn =converter.toQualifiedName(type.getQualifiedName()); 
		return qn;
	}

I must overlook something if it works for others, the JvmConstructorImplCustom
public String getQualifiedName(char innerClassDelimiter) {
		JvmDeclaredType declaringType = getDeclaringType();
		if (declaringType != null)
			return declaringType.getQualifiedName(innerClassDelimiter);
		return getSimpleName();
	}

returns the declaring type's qualified name which is the same as the said type's qualified name. How can these not conflict ? Should I change my DefaultDeclarativeQualifiedNameProvider customization or there is something else done wrong ?

thanks,
Balázs
Re: Xtend operation construction - IJvmModelInferrer [message #723351 is a reply to message #723341] Thu, 08 September 2011 11:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
HI,

what shall i do to reproduce this: using the domain model example i do not get the problem

I am using Xtext 2.0.0 maybe this is a new check in 2.0.1

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Thu, 08 September 2011 12:02]

Report message to a moderator

Re: Xtend operation construction - IJvmModelInferrer [message #723363 is a reply to message #723351] Thu, 08 September 2011 12:19 Go to previous messageGo to next message
Balazs Molnar is currently offline Balazs MolnarFriend
Messages: 28
Registered: July 2009
Junior Member
Hello,

I am using this version: Xtext SDK 2.0.0.v201106070531.
The problem can easily be something I introduced in my own project. I will check the domain example.

Balázs
Re: Xtend operation construction - IJvmModelInferrer [message #723364 is a reply to message #723363] Thu, 08 September 2011 12:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Here is what it did

/*******************************************************************************
 * Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.xtext.example.domainmodel.jvmmodel

import org.eclipse.xtext.example.domainmodel.domainmodel.*
import org.eclipse.xtext.example.domainmodel.*
import org.eclipse.xtext.xbase.jvmmodel.*
import org.eclipse.emf.ecore.*
import org.eclipse.xtext.common.types.*
import org.eclipse.xtext.common.types.util.*
import static org.eclipse.xtext.common.types.*
import java.util.*
import com.google.inject.Inject
import static org.eclipse.xtext.EcoreUtil2.*
import org.eclipse.xtext.common.types.access.IJvmTypeProvider$Factory

class DomainmodelJvmModelInferrer implements IJvmModelInferrer {

	@Inject TypesFactory typesFactory
	
	@Inject extension IJvmModelAssociator jvmModelAssociator
		
	@Inject extension JvmVisibilityExtension jvmVisibilityExtension
	
	@Inject extension DomainmodelExtensions domainmodelExtensions
	
	@Inject
    IJvmTypeProvider$Factory typeProviderFactory

	override List<JvmDeclaredType> inferJvmModel(EObject sourceObject) {
		sourceObject.disassociate
		transform( sourceObject ).toList
	}
	
	def dispatch Iterable<JvmDeclaredType> transform(DomainModel model) {
		model.elements.map(e | transform(e)).flatten
	}
	 
	def dispatch Iterable<JvmDeclaredType> transform(PackageDeclaration packageDecl) {
		packageDecl.elements.map(e | transform(e)).flatten
	}

	def dispatch Iterable<JvmDeclaredType> transform(Entity entity) {
		val jvmClass = typesFactory.createJvmGenericType 
		jvmClass.simpleName = entity.name
		jvmClass.packageName = entity.packageName
		entity.associatePrimary(jvmClass)
		jvmClass.makePublic
		if (entity.superType != null)
			jvmClass.superTypes += cloneWithProxies(entity.superType)
		for(f : entity.features) {
			transform(f, jvmClass)
		}
		val jvmField = typesFactory.createJvmField
        val stringTypeReference = typesFactory.createJvmParameterizedTypeReference
        stringTypeReference.type = typeProviderFactory.findOrCreateTypeProvider(entity.eResource.resourceSet)
        .findTypeByName("java.lang.String")
        jvmField.type = cloneWithProxies(stringTypeReference)
        jvmField.simpleName = "test"
		jvmField.makePublic
		jvmClass.members += jvmField
		entity.associatePrimary(jvmField)
		
		val emptyConstructor = typesFactory.createJvmConstructor
    emptyConstructor.makePublic
    emptyConstructor.simpleName = jvmClass.simpleName
    emptyConstructor.declaringType = jvmClass
    jvmClass.members += emptyConstructor
		entity.associatePrimary(emptyConstructor)
		
		newArrayList(jvmClass as JvmDeclaredType) 	 
	}
	
	def dispatch Iterable<JvmDeclaredType> transform(Import importDecl) {
		emptyList
	}
	
	def dispatch void transform(Property property, JvmGenericType type) {
		val jvmField = typesFactory.createJvmField
		jvmField.simpleName = property.name
		jvmField.type = cloneWithProxies(property.type)
		jvmField.makePrivate
		type.members += jvmField
		property.associatePrimary(jvmField)
		
		val jvmGetter = typesFactory.createJvmOperation
		jvmGetter.simpleName = "get" + property.name.toFirstUpper
		jvmGetter.returnType = cloneWithProxies(property.type)
		jvmGetter.makePublic
		type.members += jvmGetter
		property.associatePrimary(jvmGetter)
		
		val jvmSetter = typesFactory.createJvmOperation
		jvmSetter.simpleName = "set" + property.name.toFirstUpper
		val parameter = typesFactory.createJvmFormalParameter
		parameter.name = property.name.toFirstUpper
		parameter.parameterType = cloneWithProxies(property.type)
		jvmSetter.makePublic
		jvmSetter.parameters += parameter
		type.members += jvmSetter
		property.associatePrimary(jvmSetter)
	}
	
	def dispatch void transform(Operation operation, JvmGenericType type) {
		val jvmOperation = typesFactory.createJvmOperation
		jvmOperation.simpleName = operation.name
		jvmOperation.returnType = cloneWithProxies(operation.type)
		jvmOperation.parameters.addAll(operation.params.map(p|cloneWithProxies(p))) 
		jvmOperation.makePublic
		type.members += jvmOperation
		operation.associatePrimary(jvmOperation)
	}
	 
}



public class DomainmodelQualifiedNameProvider extends DefaultDeclarativeQualifiedNameProvider {

	@Inject
	private IQualifiedNameConverter converter;
	
	QualifiedName qualifiedName(JvmGenericType type) {
		QualifiedName res = converter.toQualifiedName(type.getQualifiedName());
			return res;
	}
	
	QualifiedName qualifiedName(JvmConstructor type) {
			QualifiedName res = converter.toQualifiedName(type.getQualifiedName());
			return res;
	}

}


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Thu, 08 September 2011 12:21]

Report message to a moderator

Re: Xtend operation construction - IJvmModelInferrer [message #723397 is a reply to message #723364] Thu, 08 September 2011 13:18 Go to previous messageGo to next message
Balazs Molnar is currently offline Balazs MolnarFriend
Messages: 28
Registered: July 2009
Junior Member
Hello,

It works fine in the domainmodel example for me too. It is only my own project where something is messed up. sorry.

Balázs
Re: Xtend operation construction - IJvmModelInferrer [message #723428 is a reply to message #723397] Thu, 08 September 2011 14:23 Go to previous message
Balazs Molnar is currently offline Balazs MolnarFriend
Messages: 28
Registered: July 2009
Junior Member
Hello,

It turned out that I had to change my validator from the default to the one that extends XbaseJavaValidator as in the domain example.
The errors disappeared, although it is now because the NamesAreUniqueValidator invoked from the basic validator no longer runs on the model.

regards,
Balázs
Previous Topic:Content Assist Multiple pages
Next Topic:Scoping - recommendation/example?
Goto Forum:
  


Current Time: Thu Sep 26 07:16:56 GMT 2024

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

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

Back to the top