|
|
|
Re: Deriving from XBase [message #1690248 is a reply to message #1690233] |
Thu, 26 March 2015 06:04 |
|
for your first problem please be a bit more specific. Having a look what is done with other literals should help you to introduce new ones.
for the second:
inferr this part:
public void someMethod() {
if(....) {
someMethodIfPart();
}
}
private void someMethodIfPart() {
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Re: Deriving from XBase [message #1690395 is a reply to message #1690372] |
Fri, 27 March 2015 06:35 |
|
Hi,
have a look at the jvmmodelinferrer (use the domain model example as the example it is meant to be)
unfortunately this depends on the rest of the grammar but a simple implementation could look like
(asuming you inferr the context of the notification to a class - based on the domain model example)
Feature:
Property | Operation |Notification;
package org.eclipse.xtext.example.domainmodel.jvmmodel
import com.google.inject.Inject
import org.eclipse.xtext.example.domainmodel.domainmodel.Entity
import org.eclipse.xtext.example.domainmodel.domainmodel.Notification
import org.eclipse.xtext.example.domainmodel.domainmodel.Operation
import org.eclipse.xtext.example.domainmodel.domainmodel.Property
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1
class DomainmodelJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider
def dispatch infer(Entity entity, extension IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
accept(entity.toClass( entity.fullyQualifiedName )) [
documentation = entity.documentation
if (entity.superType != null)
superTypes += entity.superType.cloneWithProxies
// let's add a default constructor
members += entity.toConstructor []
// and one which can be called with a lambda for initialization.
val procedureType = typeRef(Procedure1, typeRef(it)) /* Procedure<MyEntity> */
members += entity.toConstructor [
parameters += entity.toParameter("initializer", procedureType)
// here we implement the body using black box Java code.
body = '''
initializer.apply(this);
'''
]
// now let's go over the features
for ( f : entity.features ) {
switch f {
Notification: {
val notification = f
members += notification.toMethod("notification_"+notification.name, Void.TYPE.typeRef()) [
body = '''
if (_notification_«notification.name»_internal_predicate()) {
_notification_«notification.name»_internal();
}
'''
]
members += notification.toMethod("_notification_"+notification.name+"_internal", Void.TYPE.typeRef()) [
body = notification.code
]
members += notification.toMethod("_notification_"+notification.name+"_internal_predicate", Boolean.TYPE.typeRef()) [
body = notification.predicate
]
}
// for properties we create a field, a getter and a setter
Property : {
val field = f.toField(f.name, f.type)
members += field
members += f.toGetter(f.name, f.type)
members += f.toSetter(f.name, f.type)
}
// operations are mapped to methods
Operation : {
members += f.toMethod(f.name, f.type ?: inferredType) [
documentation = f.documentation
for (p : f.params) {
parameters += p.toParameter(p.name, p.parameterType)
}
// here the body is implemented using a user expression.
// Note that by doing this we set the expression into the context of this method,
// The parameters, 'this' and all the members of this method will be visible for the expression.
body = f.body
]
}
}
}
// finally we want to have a nice toString methods.
members += entity.toToStringMethod(it)
]
}
}
resulting for an example model
entity X {
notification y ~ true : {println(true)}
}
in
public class X {
public void notification_y() {
if (_notification_y_internal_predicate()) {
_notification_y_internal();
}
}
public void _notification_y_internal() {
InputOutput.<Boolean>println(Boolean.valueOf(true));
}
public boolean _notification_y_internal_predicate() {
return true;
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
Re: Deriving from XBase [message #1691061 is a reply to message #1691057] |
Thu, 02 April 2015 04:52 |
|
Hi,
first of all: what you are doing is "very advanced" stuff.
then: these are two totally different usecases:
Your Notification is not a custom XExpression, it is a high level construct.
The solution for high level constructs is to map it to basic highlevel java constructs. doing this you get scoping / typesystem for free. otherwise it would be a hell of work to adapt this as well (e.g. XbaseBatchScopeProvider)
for XExpressions you adapt XbaseCompiler and TypeComputer
if you keep that in mind it should always work
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Thu, 02 April 2015 04:56] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04177 seconds