|
|
|
|
|
|
|
|
|
|
Re: Xtend operation construction - IJvmModelInferrer [message #723364 is a reply to message #723363] |
Thu, 08 September 2011 12:20 |
|
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
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Thu, 08 September 2011 12:21] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05363 seconds