JvmModelInferrer refer to a java class not in the model [message #759366] |
Mon, 28 November 2011 08:44  |
Eclipse User |
|
|
|
Hello,
I want to generate java code from a model using XBlockExpression from XBase. Here is a part of the grammar :
Parseur :
('acceptRow' acceptRow = XBlockExpression)?
('acceptCol' acceptCol = XBlockExpression)?
I want to generate a method from the XBlockExpression without having to declare method signature in my DSL.
In the DSL, I would have :
acceptRow {! row.isEmpty()}
that would generate :
public boolean acceptRow(com.myruntimelib.Row row) {
return ! row.isEmpty();
}
I am stuck in the JvmModelInferrer, I can't find a way to declare a method taking a com.myruntimelib.Row parameter or returning a boolean value.
I try :
members += element.toMethod("acceptRow", null) [
it.parameters += it.toParameter("row", element.toClass("com.myruntimelib.Row") )
it.setBody ['''
...
element.toClass("com.myruntimelib.Row") returns a JvmGenericType but a JvmTypeReference is expected.
How can I transform a JvmGenericType to a JvmTypeReference ?
Thanks a lot,
Denis
|
|
|
|
|
Re: JvmModelInferrer refer to a java class not in the model [message #1005771 is a reply to message #759385] |
Mon, 28 January 2013 11:43   |
Eclipse User |
|
|
|
I keep incurring in strange behavior with TypeReferences injected instances...
I'm trying to add Junit4 annotations to a couple of methods inside my inferrer and, as a test, I tried two "semantically equivalent" paths but with different results.
In my inferrer I have:
class TestInferrer {
@Inject extension JvmTypesBuilder
@Inject TypesFactory typesFactory
@Inject TypeReferences typeRefs
private JvmUtils jvmUtils
/**
* Infers TestSpec to Junit4 test classes
* @param testSpec The TestSpec object containing the test specifcation for an Entity
* @param acceptor the IAcceptor used to infer the model
*/
def infer (TestSpec testSpec, IAcceptor<JvmDeclaredType> acceptor) {
jvmUtils = JvmUtils::instance
acceptor.accept (testSpec.toClass(getNameForTestClass(testSpec.entity))[
it.documentation = "Test class for Entity " + testSpec.entity.name
[...]
/*
* setUp / tearDown implementation
*/
if (testSpec.setupBody != null)
members += testSpec.toMethod("setUp", testSpec.newTypeRef("void"))[
it.body = testSpec.setupBody
var anno = jvmUtils.getAnnotationReference(typeof(org.junit.Before), testSpec.entity)
if (anno != null)
it.annotations += anno
]
[...]
/*
* Map every Test to a test method
*/
for (test: testSpec.testCases){
members += test.toMethod(test.name, test.newTypeRef("void")) [
var annotation = typesFactory.createJvmAnnotationReference
annotation.annotation = typeRefs.findDeclaredType(typeof(org.junit.Test), testSpec.entity) as JvmAnnotationType
it.annotations += annotation
it.body = test.body
]
}
])
}
}
while the code inside JvmUtil to get an annotation reference is:
class JvmUtils {
@Inject TypeReferences typeRefs
def getAnnotationReference (Class<?> annotationClass, EObject context){
if (!annoRefs.containsKey(annotationClass.name)){
if (typeRefs == null)
typeRefs = new TypeReferences()
var temp = TypesFactory::eINSTANCE.createJvmAnnotationReference
temp.annotation = typeRefs.findDeclaredType(annotationClass, context) as JvmAnnotationType
annoRefs.put(annotationClass.name, temp)
}
annoRefs.get(annotationClass.name)
}
Using the utility I wrote inside JvmUtils I keep getting NullPointerExceptions because typeRefs is null and manually instantiating it results in the TypeFactory inside typeRefs to be null.
The code inside the TestInferrer class getting a reference for the "@Test" annotation works flawlessly, instead.
How come the two "semantically equivalent" code paths behave differently? Am I doing something wrong with Guice's injection?
|
|
|
Re: JvmModelInferrer refer to a java class not in the model [message #1005807 is a reply to message #1005771] |
Mon, 28 January 2013 14:49   |
Eclipse User |
|
|
|
But JvmUtils is not injected, that's why you get null pointer
exceptions, as far as I understand...
On 01/28/2013 05:43 PM, Alan Alberghini wrote:
> I keep incurring in strange behavior with TypeReferences injected
> instances...
> I'm trying to add Junit4 annotations to a couple of methods inside my
> inferrer and, as a test, I tried two "semantically equivalent" paths but
> with different results.
> In my inferrer I have:
>
> class TestInferrer {
>
> @Inject extension JvmTypesBuilder
>
> @Inject TypesFactory typesFactory
> @Inject TypeReferences typeRefs
>
> private JvmUtils jvmUtils
>
> /**
> * Infers TestSpec to Junit4 test classes
> * @param testSpec The TestSpec object containing the test
> specifcation for an Entity
> * @param acceptor the IAcceptor used to infer the model
> */
>
> def infer (TestSpec testSpec, IAcceptor<JvmDeclaredType> acceptor) {
> jvmUtils = JvmUtils::instance
> acceptor.accept
> (testSpec.toClass(getNameForTestClass(testSpec.entity))[
>
> it.documentation = "Test class for Entity " +
> testSpec.entity.name
>
> [...]
> /*
> * setUp / tearDown implementation
> */
> if (testSpec.setupBody != null)
> members += testSpec.toMethod("setUp",
> testSpec.newTypeRef("void"))[
> it.body = testSpec.setupBody
> var anno =
> jvmUtils.getAnnotationReference(typeof(org.junit.Before), testSpec.entity)
> if (anno != null)
> it.annotations += anno ]
>
> [...]
> /*
> * Map every Test to a test method
> */
>
> for (test: testSpec.testCases){
> members += test.toMethod(test.name,
> test.newTypeRef("void")) [
> var annotation =
> typesFactory.createJvmAnnotationReference
> annotation.annotation =
> typeRefs.findDeclaredType(typeof(org.junit.Test), testSpec.entity) as
> JvmAnnotationType
> it.annotations += annotation
> it.body = test.body
> ]
> }
>
> ])
> }
> }
>
> while the code inside JvmUtil to get an annotation reference is:
>
> class JvmUtils {
>
> @Inject TypeReferences typeRefs
>
> def getAnnotationReference (Class<?> annotationClass, EObject context){
> if (!annoRefs.containsKey(annotationClass.name)){
> if (typeRefs == null)
> typeRefs = new TypeReferences()
> var temp = TypesFactory::eINSTANCE.createJvmAnnotationReference
> temp.annotation = typeRefs.findDeclaredType(annotationClass,
> context) as JvmAnnotationType
> annoRefs.put(annotationClass.name, temp)
> }
> annoRefs.get(annotationClass.name)
> }
>
> Using the utility I wrote inside JvmUtils I keep getting
> NullPointerExceptions because typeRefs is null and manually
> instantiating it results in the TypeFactory inside typeRefs to be null.
> The code inside the TestInferrer class getting a reference for the
> "@Test" annotation works flawlessly, instead.
> How come the two "semantically equivalent" code paths behave
> differently? Am I doing something wrong with Guice's injection?
--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.09054 seconds