Skip to main content



      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 01:44 Go to next message
Eclipse UserFriend
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 03:08 Go to previous messageGo to next message
Eclipse UserFriend
Hi, just leave out the (). Regards Christian
Re: xpand extension with xpand doesnt work as expected? [message #757667 is a reply to message #757661] Mon, 21 November 2011 03:38 Go to previous messageGo to next message
Eclipse UserFriend
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 03:42 Go to previous messageGo to next message
Eclipse UserFriend
Hi, can you share two complete reproduceable Xtend classes
Re: xpand extension with xpand doesnt work as expected? [message #757672 is a reply to message #757669] Mon, 21 November 2011 03:48 Go to previous messageGo to next message
Eclipse UserFriend
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 03:58 Go to previous messageGo to next message
Eclipse UserFriend
@Inject extension EntityExtensions !!!!
Re: xpand extension with xpand doesnt work as expected? [message #757678 is a reply to message #757677] Mon, 21 November 2011 04:02 Go to previous messageGo to next message
Eclipse UserFriend
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 04:10 Go to previous messageGo to next message
Eclipse UserFriend
how does you extension file now look like?
Re: xpand extension with xpand doesnt work as expected? [message #757685 is a reply to message #757684] Mon, 21 November 2011 04:13 Go to previous messageGo to next message
Eclipse UserFriend
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 04:15 Go to previous messageGo to next message
Eclipse UserFriend
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 02:28 Go to previous messageGo to next message
Eclipse UserFriend
Import static extension is for static (and thus) java methods. You
have to use @Inject extension. Regards Christian
Re: xpand extension with xpand doesnt work as expected? [message #757892 is a reply to message #757653] Mon, 21 November 2011 02:37 Go to previous messageGo to next message
Eclipse UserFriend
E.tablename without braces doesn't work?
Re: xpand extension with xpand doesnt work as expected? [message #757893 is a reply to message #757678] Mon, 21 November 2011 04:07 Go to previous messageGo to next message
Eclipse UserFriend
How does the file look like. I hope you did not put the !!!! There
Re: xpand extension with xpand doesnt work as expected? [message #758201 is a reply to message #757678] Tue, 22 November 2011 05:19 Go to previous message
Eclipse UserFriend
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: Mon Jun 30 20:35:45 EDT 2025

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

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

Back to the top