Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Nested class and visibility of members
Nested class and visibility of members [message #1840212] Thu, 08 April 2021 18:10 Go to next message
Arakul Tikea is currently offline Arakul TikeaFriend
Messages: 5
Registered: April 2021
Junior Member
I want to get nested classes in my generated java file and I did something like below in my grammar but it's not working. Please tell me where is the issue or any other approach of achieving it.

Entity:
'entity' name=ValidID ('extends' superType=JvmTypeReference)? '{'
features+=Feature*
enitities+=Entity*
'}';

Also, I want to change the visibility for each of my fields and inner classes in the java file generated from the model. I tried handling it using jvmvisibility in xtend using below grammar but everything is setting to default only. Please help.

Property:
visibility=('PRIVATE')?('PROTECTED')?('PUBLIC')? name=ValidID ':' type=JvmTypeReference ;
Re: Nested class and visibility of members [message #1840219 is a reply to message #1840212] Fri, 09 April 2021 05:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
can you please provide more information? how does your inferrer look like?
do you infer inner classes there?
about the visibility: the grammar makes zero sense.
also: what do you want to achieve here? unclear if you want to do it in grammar or in inferrer?!?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Nested class and visibility of members [message #1840910 is a reply to message #1840219] Wed, 28 April 2021 16:16 Go to previous messageGo to next message
Arakul Tikea is currently offline Arakul TikeaFriend
Messages: 5
Registered: April 2021
Junior Member
I am trying to change the visibility of each variable that I declare i.e. the visibility of each of the field members in the generated code should be as it was mentioned in the model. Below is my Grammar :

grammar org.example.domainmodel.Domainmodel with
org.eclipse.xtext.xbase.Xbase

generate domainmodel "http://www.example.org/domainmodel/Domainmodel"

Domainmodel:
elements+=AbstractElement*;

PackageDeclaration:
'package' name=QualifiedName '{'
elements+=AbstractElement*
'}';

AbstractElement:
Import | PackageDeclaration | Entity ;


Import:
'import' (importedNamespace=QualifiedName) ';'?;

/*This is class rule */
Entity:
'entity' name=ValidID ('extends' superType=JvmTypeReference)? '{'
entities+=Entity*
features+=Feature*
'}';

Feature:
Property /*| InnerClasses */| Operation ;

Property:
visibility =('DEFAULT')?('PRIVATE')?('PROTECTED')? name=ValidID ':' type=JvmTypeReference ;

Operation:
'op' name=ValidID
'('(params+=FullJvmFormalParameter
(',' params+=FullJvmFormalParameter)*)?')'
':' type=JvmTypeReference
body=XBlockExpression;

Below is my Inferrer class :

class DomainmodelJvmModelInferrer extends AbstractModelInferrer {

/**
* a builder API to programmatically create Jvm elements
* in readable way.
*/
@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider

def dispatch void infer(Entity element,
IJvmDeclaredTypeAcceptor acceptor,
boolean isPrelinkingPhase) {
acceptor.accept(element.toClass( element.fullyQualifiedName )) [
documentation = element.documentation
println("documentation: "+documentation)
println("element.superType: "+element.superType)

if (element.superType !== null)
superTypes += element.superType.cloneWithProxies
println("superTypes: "+superTypes)

for (feature : element.features) {
switch feature {
Property : {
members += feature.toField(feature.name, feature.type)[visibility=JvmVisibility.getByName(feature.visibility)]
}

Operation : {
members += feature.toMethod(feature.name, feature.type) [
documentation = feature.documentation
for (p : feature.params) {
parameters += p.toParameter(p.name, p.parameterType)
}
body = feature.body
] } } } ] } }

The visibility of each variable is just changing to Default in generated code and it prints PUBLIC when sysout. What could be the issue please?
Re: Nested class and visibility of members [message #1840911 is a reply to message #1840910] Wed, 28 April 2021 16:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
---

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

[Updated on: Wed, 28 April 2021 16:50]

Report message to a moderator

Re: Nested class and visibility of members [message #1840913 is a reply to message #1840911] Wed, 28 April 2021 16:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
it acutally is a grammar issue as i stated before

Quote:

about the visibility: the grammar makes zero sense.


do you mean

Property:
(visibility =('DEFAULT'|'PRIVATE'|'PROTECTED'))? name=ValidID ':' type=JvmTypeReference ;

=> wonder how your print looks like


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

[Updated on: Wed, 28 April 2021 16:50]

Report message to a moderator

Re: Nested class and visibility of members [message #1840914 is a reply to message #1840913] Wed, 28 April 2021 17:08 Go to previous messageGo to next message
Arakul Tikea is currently offline Arakul TikeaFriend
Messages: 5
Registered: April 2021
Junior Member
Ah..Thankssss a lot!..I am a beginner with xtext and kind of stuck with this.
Also, one more requirement I have - I need to generate imports too as like in java code we have after package declaration but I dont have any method in Inferrer to parse it for e.g like toField or toMethod. Could you please tell me any alternative you may know. The grammar and code remains the same as above.
Re: Nested class and visibility of members [message #1840918 is a reply to message #1840914] Wed, 28 April 2021 19:07 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi, can you be more specific ?
The inferrer together with JvmModelGenerator does imports automatically
Thus i wonder what you exactly do in the inferrer


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Nested class and visibility of members [message #1840919 is a reply to message #1840918] Wed, 28 April 2021 19:23 Go to previous messageGo to next message
Arakul Tikea is currently offline Arakul TikeaFriend
Messages: 5
Registered: April 2021
Junior Member
Below is my model. I want to generate those import lines as exactly it is written in my java file. The grammar and inferrer class is same as above. Please let me know if anything required is missing. Is there a problem in Grammar for Import part?

package model {
import java.util.concurrent;
import vv.cc.ll;

PUBLIC entity CA {
entity InExternalCommunication {
PRIVATE abc: String
PROTECTED def: String
DEFAULT gh: float
}
PRIVATE kl: String
}
}

Also, one last query- what if I need the variables to be of entity type (class type) too, not only string, float etc. I tried the below part in grammar but feel something else is required:

Property:
(visibility =('DEFAULT'|'PRIVATE'|'PROTECTED'))? name=ValidID ':' type=JvmTypeReference|Entity;
Re: Nested class and visibility of members [message #1840920 is a reply to message #1840919] Wed, 28 April 2021 19:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
I still don't understand the import problem
You show them in the model not the generated code


For the type references please check the domain model example you can create with the new example wizard


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Nested class and visibility of members [message #1840921 is a reply to message #1840920] Wed, 28 April 2021 19:41 Go to previous messageGo to next message
Arakul Tikea is currently offline Arakul TikeaFriend
Messages: 5
Registered: April 2021
Junior Member
I meant, Could we get the imports in generated code? like a normal java file. Currently, the below is getting generated after the above model is being saved:

package model;
// -----wanted the imports to be generated here-----

@SuppressWarnings("all")
public class CA {
class InExternalCommunication {
private String abc;

protected String def;

float gh;
}

private String kl;
}
Re: Nested class and visibility of members [message #1840922 is a reply to message #1840921] Wed, 28 April 2021 19:48 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
But why do you want to generate imports when you don't use them

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:How could I create a new Xtext project from existing ecore model then there is no brackets in xtext?
Next Topic:xtext-services and iframe
Goto Forum:
  


Current Time: Thu Apr 25 07:27:45 GMT 2024

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

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

Back to the top