| Xtend operation construction - IJvmModelInferrer [message #722974] |
Wed, 07 September 2011 06:52  |
Balazs Molnar Messages: 28 Registered: July 2009 |
Junior Member |
|
|
When I create a new type from within my IJvmModelInferrer extension using the following:
val jvmClass = typesFactory.createJvmGenericType
jvmClass.makePublic
... etc
I also would like to add a new operation which would have a return type of the new type. How can I set the return type of the new operation to the JvmTypeReference of the newly created jvmClass ?
thank you,
Balázs
|
|
|
|
|
|
|
|
|
|
|
|
| Re: Xtend operation construction - IJvmModelInferrer [message #723364 is a reply to message #723363] |
Thu, 08 September 2011 08:20   |
Christian Dietrich Messages: 4396 Registered: July 2009 |
Senior Member |
|
|
Here is what it did
/*******************************************************************************
* Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.example.domainmodel.jvmmodel
import org.eclipse.xtext.example.domainmodel.domainmodel.*
import org.eclipse.xtext.example.domainmodel.*
import org.eclipse.xtext.xbase.jvmmodel.*
import org.eclipse.emf.ecore.*
import org.eclipse.xtext.common.types.*
import org.eclipse.xtext.common.types.util.*
import static org.eclipse.xtext.common.types.*
import java.util.*
import com.google.inject.Inject
import static org.eclipse.xtext.EcoreUtil2.*
import org.eclipse.xtext.common.types.access.IJvmTypeProvider$Factory
class DomainmodelJvmModelInferrer implements IJvmModelInferrer {
@Inject TypesFactory typesFactory
@Inject extension IJvmModelAssociator jvmModelAssociator
@Inject extension JvmVisibilityExtension jvmVisibilityExtension
@Inject extension DomainmodelExtensions domainmodelExtensions
@Inject
IJvmTypeProvider$Factory typeProviderFactory
override List<JvmDeclaredType> inferJvmModel(EObject sourceObject) {
sourceObject.disassociate
transform( sourceObject ).toList
}
def dispatch Iterable<JvmDeclaredType> transform(DomainModel model) {
model.elements.map(e | transform(e)).flatten
}
def dispatch Iterable<JvmDeclaredType> transform(PackageDeclaration packageDecl) {
packageDecl.elements.map(e | transform(e)).flatten
}
def dispatch Iterable<JvmDeclaredType> transform(Entity entity) {
val jvmClass = typesFactory.createJvmGenericType
jvmClass.simpleName = entity.name
jvmClass.packageName = entity.packageName
entity.associatePrimary(jvmClass)
jvmClass.makePublic
if (entity.superType != null)
jvmClass.superTypes += cloneWithProxies(entity.superType)
for(f : entity.features) {
transform(f, jvmClass)
}
val jvmField = typesFactory.createJvmField
val stringTypeReference = typesFactory.createJvmParameterizedTypeReference
stringTypeReference.type = typeProviderFactory.findOrCreateTypeProvider(entity.eResource.resourceSet)
.findTypeByName("java.lang.String")
jvmField.type = cloneWithProxies(stringTypeReference)
jvmField.simpleName = "test"
jvmField.makePublic
jvmClass.members += jvmField
entity.associatePrimary(jvmField)
val emptyConstructor = typesFactory.createJvmConstructor
emptyConstructor.makePublic
emptyConstructor.simpleName = jvmClass.simpleName
emptyConstructor.declaringType = jvmClass
jvmClass.members += emptyConstructor
entity.associatePrimary(emptyConstructor)
newArrayList(jvmClass as JvmDeclaredType)
}
def dispatch Iterable<JvmDeclaredType> transform(Import importDecl) {
emptyList
}
def dispatch void transform(Property property, JvmGenericType type) {
val jvmField = typesFactory.createJvmField
jvmField.simpleName = property.name
jvmField.type = cloneWithProxies(property.type)
jvmField.makePrivate
type.members += jvmField
property.associatePrimary(jvmField)
val jvmGetter = typesFactory.createJvmOperation
jvmGetter.simpleName = "get" + property.name.toFirstUpper
jvmGetter.returnType = cloneWithProxies(property.type)
jvmGetter.makePublic
type.members += jvmGetter
property.associatePrimary(jvmGetter)
val jvmSetter = typesFactory.createJvmOperation
jvmSetter.simpleName = "set" + property.name.toFirstUpper
val parameter = typesFactory.createJvmFormalParameter
parameter.name = property.name.toFirstUpper
parameter.parameterType = cloneWithProxies(property.type)
jvmSetter.makePublic
jvmSetter.parameters += parameter
type.members += jvmSetter
property.associatePrimary(jvmSetter)
}
def dispatch void transform(Operation operation, JvmGenericType type) {
val jvmOperation = typesFactory.createJvmOperation
jvmOperation.simpleName = operation.name
jvmOperation.returnType = cloneWithProxies(operation.type)
jvmOperation.parameters.addAll(operation.params.map(p|cloneWithProxies(p)))
jvmOperation.makePublic
type.members += jvmOperation
operation.associatePrimary(jvmOperation)
}
}
public class DomainmodelQualifiedNameProvider extends DefaultDeclarativeQualifiedNameProvider {
@Inject
private IQualifiedNameConverter converter;
QualifiedName qualifiedName(JvmGenericType type) {
QualifiedName res = converter.toQualifiedName(type.getQualifiedName());
return res;
}
QualifiedName qualifiedName(JvmConstructor type) {
QualifiedName res = converter.toQualifiedName(type.getQualifiedName());
return res;
}
}
~Christian
[Updated on: Thu, 08 September 2011 08:21] Report message to a moderator
|
|
|
|
| Re: Xtend operation construction - IJvmModelInferrer [message #723428 is a reply to message #723397] |
Thu, 08 September 2011 10:23  |
Balazs Molnar Messages: 28 Registered: July 2009 |
Junior Member |
|
|
Hello,
It turned out that I had to change my validator from the default to the one that extends XbaseJavaValidator as in the domain example.
The errors disappeared, although it is now because the NamesAreUniqueValidator invoked from the basic validator no longer runs on the model.
regards,
Balázs
|
|
|
Powered by
FUDForum. Page generated in 0.01981 seconds