Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Inheritance from non-grammar java class(How to provide a superType for a class from "external" java class)
Inheritance from non-grammar java class [message #1791714] Wed, 04 July 2018 12:46 Go to next message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
Within our project we have the scenario that your generated classes shall extend a "provided" Java class.

More specific the class that should be the root of the generated code (lets call it: "BaseClass") is shipped as a jar. The jar is already available on the classpath and can be referenced within the xtext project.

Since this is part of a bigger project within a company I can't provide a complete example.

My first goal is to understand if I'm heading towards a solution or if I need to use another approach.

As an example for the XBase-Grammar:
Entity:
	'entity' name=ValidID '{'
	(features+=Feature)*
	'}';


Within the Inferrer my first attempt was:

def dispatch void infer(Entity element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
	acceptor.accept(element.toClass(element.name)) [
	packageName = element.packageName
	superTypes += typeRef(BaseClass)
	]
}


This leads to a Nullpointer Exception. I know that I need to bind/provide the "BaseClass" but my problem is that I don't understand how and where.
There are many examples explaining how to handle cross-reference and handling grammar-related references but I couldn't find a more detailed example for reference resolution from "external" resources like jars.

I admit that I most likely missed a basic point from the Xtext docs about "JDT-Based Container Manager". At the moment I'm not even sure what Provider would be the correct one.

The ultimate goal is to get a generated class like this:

EntityXY extends BaseClass {
	//happyVariables
}


In advance, thats for your support!
Re: Inheritance from non-grammar java class [message #1791715 is a reply to message #1791714] Wed, 04 July 2018 12:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
is baseclass on the classpath of the mydsl/dmodel/yourdsl file?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Inheritance from non-grammar java class [message #1791717 is a reply to message #1791715] Wed, 04 July 2018 13:00 Go to previous messageGo to next message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
It is a Xtext Maven project. The DSL project has the jar on the classpath and I can reference it in normal Java classes in that project. (the same where the .xtext is defined)
The "BaseClass" is also imported within the xtend class of the inferrer.

[Updated on: Wed, 04 July 2018 13:02]

Report message to a moderator

Re: Inheritance from non-grammar java class [message #1791720 is a reply to message #1791717] Wed, 04 July 2018 13:07 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
no, i mean the path of the model file.
if you have done that it should work with no more changes
(to be sure try to use "x.y.z.BaseClass".typeRef instead of typeRef(BaseClass)

if that does not help please provide a reproducible example


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Inheritance from non-grammar java class [message #1791728 is a reply to message #1791720] Wed, 04 July 2018 13:44 Go to previous messageGo to next message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
Just to be sure that I don't missunderstand your response. Are you talking about an import statement in the model or the configuration of the manifest (to set up the classpath)?

The jar is only included in the build.properties with "jars.extra.classpath" and as an export-package within the manifest.
Re: Inheritance from non-grammar java class [message #1791729 is a reply to message #1791728] Wed, 04 July 2018 13:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
index.php/fa/33308/0/

here is an example



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Inheritance from non-grammar java class [message #1791735 is a reply to message #1791729] Wed, 04 July 2018 14:48 Go to previous messageGo to next message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
That makes a lot of sense. The generated class should get/know the import too. :D

How do I "hook" into the import section regarding the Inferrer?

Within the grammar it seems easy with "XImportSection" but within the context of the inferrer I couldn't find an method/extention to access the import part of the generated class.
Re: Inheritance from non-grammar java class [message #1791736 is a reply to message #1791735] Wed, 04 July 2018 14:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you dont. that happens automatically.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Inheritance from non-grammar java class [message #1791744 is a reply to message #1791736] Wed, 04 July 2018 15:57 Go to previous messageGo to next message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
Well. I found one of my mistakes. The jar is not referenced in the "resulting" project.

I found your suggestion: https://www.eclipse.org/forums/index.php?t=msg&th=509841&goto=1092236&#msg_1092236
how to add the jar to the manifest. But i'm not sure if I set the "build" tab correctly. At the moment only the extra classpath entries is specified.

Re: Inheritance from non-grammar java class [message #1791749 is a reply to message #1791744] Wed, 04 July 2018 16:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Sorry without detailed information I can only guess

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Inheritance from non-grammar java class [message #1791794 is a reply to message #1791749] Thu, 05 July 2018 11:55 Go to previous messageGo to next message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
I followed your suggestion (from the quoted link)

Christian Dietrich wrote on Thu, 22 August 2013 13:56
Hi,

it would be preferred if the exist as osgi bundles. if not

- create a lib folder in the project
- drop the libs there.
- open the manifest file.
- add the libs on the runtime tab/classpath section
- add the lib folder on the build tab of the editor.


I removed all my previous changes to the manifest regarding the base.jar

I added the libs on the classpath section in the runtime tab.
+ add the libs folder to the runtime information on the build tab.

After I build it with clean package (works) and run the Eclipse Application it throws this Exception:

java.lang.NullPointerException
	at **package/patch/to/class*.IdWithKeyElementInferrerExtension.lambda$1(IdWithKeyElementInferrerExtension.java:98)
	at org.eclipse.xtext.xbase.jvmmodel.JvmModelAssociator$1.run(JvmModelAssociator.java:397)
	at org.eclipse.xtext.xbase.jvmmodel.JvmModelAssociator.installDerivedState(JvmModelAssociator.java:407)
	at org.eclipse.xtext.resource.DerivedStateAwareResource.installDerivedState(DerivedStateAwareResource.java:242)
	at org.eclipse.xtext.xbase.resource.BatchLinkableResource.getContents(BatchLinkableResource.java:148)
	at org.eclipse.xtext.xbase.typesystem.internal.LogicalContainerAwareBatchTypeResolver.getEntryPoints(LogicalContainerAwareBatchTypeResolver.java:44)
	at org.eclipse.xtext.xbase.typesystem.internal.DefaultBatchTypeResolver.getTypeResolver(DefaultBatchTypeResolver.java:84)
	at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver$1.get(CachingBatchTypeResolver.java:49)
	at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver$1.get(CachingBatchTypeResolver.java:46)
	at org.eclipse.xtext.util.OnChangeEvictingCache.get(OnChangeEvictingCache.java:77)
	at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver.doResolveTypes(CachingBatchTypeResolver.java:46)
	at org.eclipse.xtext.xbase.typesystem.internal.AbstractBatchTypeResolver.resolveTypes(AbstractBatchTypeResolver.java:69)
	at org.eclipse.xtext.xbase.resource.BatchLinkingService.resolveBatched(BatchLinkingService.java:71)
	at org.eclipse.xtext.xbase.resource.BatchLinkableResource.resolveLazyCrossReferences(BatchLinkableResource.java:165)
	at org.eclipse.xtext.EcoreUtil2.resolveLazyCrossReferences(EcoreUtil2.java:500)
	at org.eclipse.xtext.ui.editor.reconciler.XtextDocumentReconcileStrategy.postParse(XtextDocumentReconcileStrategy.java:175)
	at org.eclipse.xtext.ui.editor.reconciler.XtextDocumentReconcileStrategy.doReconcile(XtextDocumentReconcileStrategy.java:153)
	at org.eclipse.xtext.ui.editor.reconciler.XtextDocumentReconcileStrategy.reconcile(XtextDocumentReconcileStrategy.java:67)
	at org.eclipse.xtext.ui.editor.reconciler.XtextReconciler.doRun(XtextReconciler.java:449)
	at org.eclipse.xtext.ui.editor.reconciler.XtextReconciler.access$3(XtextReconciler.java:429)
	at org.eclipse.xtext.ui.editor.reconciler.XtextReconciler$1.process(XtextReconciler.java:363)
	at org.eclipse.xtext.ui.editor.reconciler.XtextReconciler$1.process(XtextReconciler.java:1)
	at org.eclipse.xtext.util.concurrent.IUnitOfWork$Void.exec(IUnitOfWork.java:37)
	at org.eclipse.xtext.resource.OutdatedStateManager.exec(OutdatedStateManager.java:91)
	at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.modify(XtextDocument.java:428)
	at org.eclipse.xtext.ui.editor.model.XtextDocument.internalModify(XtextDocument.java:162)
	at org.eclipse.xtext.ui.editor.reconciler.XtextReconciler.run(XtextReconciler.java:360)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)



The IdWithKey Extention Error line is this:

superTypes += **package/path/to/class**.IdWithKey.typeRef

[Updated on: Thu, 05 July 2018 12:00]

Report message to a moderator

Re: Inheritance from non-grammar java class [message #1791795 is a reply to message #1791794] Thu, 05 July 2018 12:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i need a reproducible example ....

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Inheritance from non-grammar java class [message #1791816 is a reply to message #1791795] Thu, 05 July 2018 14:47 Go to previous messageGo to next message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
Sure. I hoped it was a obvious mistake.

I cleaned the project before I zipped it to reduce the size.
Re: Inheritance from non-grammar java class [message #1791824 is a reply to message #1791816] Thu, 05 July 2018 15:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
where is your test project?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Inheritance from non-grammar java class [message #1791826 is a reply to message #1791824] Thu, 05 July 2018 16:13 Go to previous messageGo to next message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
And the Test Project:
Re: Inheritance from non-grammar java class [message #1791827 is a reply to message #1791816] Thu, 05 July 2018 16:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
why do you have 3 inferrer classes: JvmTypeReferenceBuilder is not meant to be injected in a non main inferrer class.
otherwise it will not be initialized correctly.

=>

def dispatch void infer(IdWithKeyElement element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
createExtendable(element, acceptor, isPreIndexingPhase, _typeReferenceBuilder)
}



@Extension public JvmTypeReferenceBuilder _typeReferenceBuilder;
@Inject extension JvmTypesBuilder
@Inject extension GeneratorHelperExtension

def void createExtendable(IdWithKeyElement element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase, JvmTypeReferenceBuilder _typeReferenceBuilder) {
this._typeReferenceBuilder = _typeReferenceBuilder;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Inheritance from non-grammar java class [message #1791850 is a reply to message #1791827] Fri, 06 July 2018 08:36 Go to previous messageGo to next message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
It is older code from an coworker that I "inherited". Because I didn't expect that it could cause trouble, I kept it.

Since I expect the project to gather a lot of code: What is your suggestion to structure the code? I would expect that there is a concept or idea how to split up big classes.
Re: Inheritance from non-grammar java class [message #1791851 is a reply to message #1791850] Fri, 06 July 2018 08:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
no there is not. the concept is: you need to know what you are doing.
thus if you want to split use the workaround i posted above


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

[Updated on: Fri, 06 July 2018 08:40]

Report message to a moderator

Re: Inheritance from non-grammar java class [message #1791855 is a reply to message #1791851] Fri, 06 July 2018 08:50 Go to previous messageGo to next message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
Sry, I missed that part.
Re: Inheritance from non-grammar java class [message #1791873 is a reply to message #1791855] Fri, 06 July 2018 11:18 Go to previous message
Karsten Wilken is currently offline Karsten WilkenFriend
Messages: 59
Registered: August 2016
Member
The Nullpointer is fixed now.

After I added the jar manually to the test project it works as expected.

Thank you for your help and patience, Christian!

Previous Topic:About CrossReferenceSerialization in Xtext
Next Topic:Problems using AbstractLanguageServerTest
Goto Forum:
  


Current Time: Fri Apr 19 08:00:08 GMT 2024

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

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

Back to the top