Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » xpand extension with xpand doesnt work as expected?
xpand extension with xpand doesnt work as expected? [message #757653] Mon, 21 November 2011 06:44 Go to next message
Tilo Thiele is currently offline Tilo ThieleFriend
Messages: 14
Registered: March 2010
Junior Member
Following the docs I created a file EntityExtensions.xtend:

package com.emsgmbh.appgen.generator
import com.emsgmbh.appgen.persistence.persistence.Entity
public class EntityExtensions {
	def tablename(Entity e) {
		return "abc";
	}
}


Then I added to my Generator.xtend:

...
import static extension com.emsgmbh.appgen.generator.EntityExtensions.*
...


but the new method tablename() couldn't be seen for an Entity object. Looking at the generated Java code showed me that the method tablename(Entity e) is not a static method, which is a plausible reason.

After my first approach failed, I tried to inject - in my generator I wrote:

...
import com.google.inject.Inject

class PersistenceOracleGenerator implements IGenerator2  {
	
	@Inject EntityExtensions


which ended in a syntax error, as he expected an alias after the extension class.
After changing to
	@Inject EntityExtensions eext


I was able to write
	... Entity e ...

	eext.tablename(e)

but not
	... Entity e ...

	e.tablename()

what I expected. Isn't it possible to write such an xtend extension in xtend?
Re: xpand extension with xpand doesnt work as expected? [message #757661 is a reply to message #757653] Mon, 21 November 2011 08:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13423
Registered: July 2009
Senior Member
Hi, just leave out the (). Regards Christian

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xpand extension with xpand doesnt work as expected? [message #757667 is a reply to message #757661] Mon, 21 November 2011 08:38 Go to previous messageGo to next message
Tilo Thiele is currently offline Tilo ThieleFriend
Messages: 14
Registered: March 2010
Junior Member
Christian,

ups - I didn't mention - I tried this already a couple of times. Content assist doesn't offer the extension method(s) at all. Using them ends up with a syntax error. The problem is somewhere else. I can't see it.

btw - I use the latest itemis distro with Xtext 2.1.0

/tilo
Re: xpand extension with xpand doesnt work as expected? [message #757669 is a reply to message #757667] Mon, 21 November 2011 08:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13423
Registered: July 2009
Senior Member
Hi, can you share two complete reproduceable Xtend classes

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xpand extension with xpand doesnt work as expected? [message #757672 is a reply to message #757669] Mon, 21 November 2011 08:48 Go to previous messageGo to next message
Tilo Thiele is currently offline Tilo ThieleFriend
Messages: 14
Registered: March 2010
Junior Member
EntityExtensions.xtend:
package com.emsgmbh.appgen.generator

import com.emsgmbh.appgen.persistence.persistence.Entity
import com.emsgmbh.appgen.persistence.persistence.Feature
import com.emsgmbh.appgen.persistence.persistence.PackageDeclaration

class EntityExtensions {
	def tablename(Entity e) {
		// TODO implement
		return "abc";
	}
	
	def getPrefix(PackageDeclaration p) {
		// TODO implement
		return "";
	}
	
	def boolean isHasOnePrimaryKey(Entity e) {
		var h = 0;
		for(Feature f: e.getFeatures()) {
//			TODO implement
		}
		return h==1;
	}

}


PersistenceOracleGenerator:
package com.emsgmbh.appgen.generator

import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import static extension org.eclipse.xtext.xtend2.lib.ResourceExtensions.*
import com.emsgmbh.appgen.persistence.persistence.Entity
import static extension com.emsgmbh.appgen.generator.EntityExtensions.*

import com.google.inject.Inject

class PersistenceOracleGenerator implements IGenerator2  {
	
	@Inject EntityExtensions eext

	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
			fsa.generateFile('create-tables-oracle.sql', resource.compile)
	}
	
	override void doGenerate(java.util.List<EObject> objects, IFileSystemAccess fsa) {
			fsa.generateFile('create-tables-oracle.sql', objects.compile)
	}
	
	def String compile(java.util.List<EObject> objects) {
		var rv = ""
		for(e : objects) {
			rv = rv+e.compile
		}
		return rv
	}
	
	def String compile(Resource r) {
		var rv = ""
		for(e : r.allContentsIterable.filter(typeof(Entity))) {
			rv = rv+e.compile
		}
		return rv
	}
	
	def dispatch compile(EObject obj) '''
		-- cannot create table for object «obj.eClass»
	'''

	def dispatch compile(Entity en) '''
		create table «eext.tablename(en)» (
		);
	'''

}
Re: xpand extension with xpand doesnt work as expected? [message #757677 is a reply to message #757672] Mon, 21 November 2011 08:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13423
Registered: July 2009
Senior Member
@Inject extension EntityExtensions !!!!

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xpand extension with xpand doesnt work as expected? [message #757678 is a reply to message #757677] Mon, 21 November 2011 09:02 Go to previous messageGo to next message
Tilo Thiele is currently offline Tilo ThieleFriend
Messages: 14
Registered: March 2010
Junior Member
Christian Dietrich wrote on Mon, 21 November 2011 03:58
@Inject extension EntityExtensions !!!!


This ends up in an syntax error:

Description	Resource	Path	Location	Type
no viable alternative at input 'override'	PersistenceOracleGenerator.xtend	/com.emsgmbh.appgen.persistence/src/com/emsgmbh/appgen/generator	line: 16 /com.emsgmbh.appgen.persistence/src/com/emsgmbh/appgen/generator/PersistenceOracleGenerator.xtend	Xtext Check (fast)

Re: xpand extension with xpand doesnt work as expected? [message #757684 is a reply to message #757678] Mon, 21 November 2011 09:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13423
Registered: July 2009
Senior Member
how does you extension file now look like?

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xpand extension with xpand doesnt work as expected? [message #757685 is a reply to message #757684] Mon, 21 November 2011 09:13 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
I'm note sure whether you're actually using Xtend2.1: the errors seem to indicate Xtend2.0 is active.

Re: xpand extension with xpand doesnt work as expected? [message #757688 is a reply to message #757684] Mon, 21 November 2011 09:15 Go to previous messageGo to next message
Tilo Thiele is currently offline Tilo ThieleFriend
Messages: 14
Registered: March 2010
Junior Member
outch - I forgot the keyword extension. My blunder.

@Inject extension EntityExtensions

made the difference - of cause. Now it works fine. Thanks for the pain reliever.

Re: xpand extension with xpand doesnt work as expected? [message #757891 is a reply to message #757653] Mon, 21 November 2011 07:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13423
Registered: July 2009
Senior Member
Import static extension is for static (and thus) java methods. You
have to use @Inject extension. Regards Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xpand extension with xpand doesnt work as expected? [message #757892 is a reply to message #757653] Mon, 21 November 2011 07:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13423
Registered: July 2009
Senior Member
E.tablename without braces doesn't work?

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xpand extension with xpand doesnt work as expected? [message #757893 is a reply to message #757678] Mon, 21 November 2011 09:07 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13423
Registered: July 2009
Senior Member
How does the file look like. I hope you did not put the !!!! There

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xpand extension with xpand doesnt work as expected? [message #758201 is a reply to message #757678] Tue, 22 November 2011 10:19 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3107
Registered: July 2009
Senior Member
Please make sure that you use the latest version (2.1.1) of Xtend.

Regards,
Sebastian
--
Need professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

On Mon, 21 Nov 2011 10:02:32 +0100, Tilo Thiele
<forums-noreply@eclipse.org> wrote:

> Christian Dietrich wrote on Mon, 21 November 2011 03:58
>> @Inject extension EntityExtensions !!!!
>
>
> This ends up in an syntax error:
>
>
> Description Resource Path Location Type
> no viable alternative at input
> 'override' PersistenceOracleGenerator.xtend /com.emsgmbh.appgen.persistence/src/com/emsgmbh/appgen/generator line:
> 16
> /com.emsgmbh.appgen.persistence/src/com/emsgmbh/appgen/generator/PersistenceOracleGenerator.xtend Xtext
> Check (fast)
>
>
Previous Topic:Generating with xtend and MWE2
Next Topic:firstChild attribute.
Goto Forum:
  


Current Time: Sun Apr 05 08:19:57 GMT 2020

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

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

Back to the top