Invalid 'import is never used' warning for annotations [message #1222605] |
Fri, 20 December 2013 22:00 |
Michel de Blok Messages: 8 Registered: August 2013 |
Junior Member |
|
|
Hi all,
I'm using XText 2.5, extending the XbaseWithAnnotations grammar, and I have a list of annotations on my elements using:
annotations+=XAnnotation*
This works fine, and XBase auto-imports the annotations nicely, but the DSL editor always shows the warning "The import 'xxx' is never used." for the imported annotation in my model file. And if I organize the imports, the annotation import is removed and my model contains an error (couldn't resolve reference to the annotation).
How can I tell XBase that the import is actually used and should not be removed? And why isn't this recognized automatically?
I do map the XAnnotation elements to a JvmAnnotationReference in my JvmModelInferrer with:
annotations.translateAnnotationsTo(it)
The organize imports function works properly for other imported JvmTypes, like classes that are imported and extended in my model, but only for annotations it seems that XBase never 'sees' that the annotations are being used.
Does anyone else have this problem and solved it?
Regards,
MdB
|
|
|
Re: Invalid 'import is never used' warning for annotations [message #1222622 is a reply to message #1222605] |
Fri, 20 December 2013 23:03 |
|
Hi,
i acutually cannot reproduce that.
can you share a reproduceable grammar?
here is mine
grammar org.xtext.example.mydsl2.MyDsl with org.eclipse.xtext.xbase.annotations.XbaseWithAnnotations
generate myDsl "http://www.xtext.org/example/mydsl2/MyDsl"
Model:
i=XImportSection
greetings+=Greeting*;
Greeting:
annos+=XAnnotation*
'Hello' name=ID '!';
class MyDslJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
def dispatch void infer(Model element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(element.toClass("my.company.greeting.MyGreetings"))
.initializeLater([
for (greeting : element.greetings) {
val m = greeting.toMethod("hello" + greeting.name, greeting.newTypeRef(typeof(String))) [
body = [
append('''return "Hello «greeting.name»";''')
]
]
members += m
greeting.annos.translateAnnotationsTo(m)
}
])
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Fri, 20 December 2013 23:04] Report message to a moderator
|
|
|
Re: Invalid 'import is never used' warning for annotations [message #1222845 is a reply to message #1222622] |
Sat, 21 December 2013 15:39 |
Michel de Blok Messages: 8 Registered: August 2013 |
Junior Member |
|
|
Hi,
your sample works fine for me too, no unused import issues there.
However, if I use the following grammar:
grammar org.xtext.example.mydsl2.MyDsl with org.eclipse.xtext.xbase.annotations.XbaseWithAnnotations
generate myDsl "http://www.xtext.org/example/mydsl2/MyDsl"
Model:
importSection=XImportSection?
packages+=PackageDeclaration*
;
PackageDeclaration: {PackageDeclaration} (
'package' name=QualifiedName '{'
elements+=AnnotatedElement*
'}'
)
;
AnnotatedElement:
annotations+=XAnnotation*
target=AbstractElement
;
AbstractElement:
ElementA | ElementB
;
ElementA:
'elementA' name=ValidID
;
ElementB:
'elementB' name=ValidID
;
And the following inferrer:
class MyDslJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider
def dispatch void infer(AbstractElement element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(element.toClass(element.fullyQualifiedName)).initializeLater(
[
(element.eContainer as AnnotatedElement).annotations.translateAnnotationsTo(it)
])
}
}
With the following model:
import javax.persistence.Entity
import javax.jws.WebService
package my.domain {
@WebService
elementA TestA
@Entity
elementB TestB
}
Then both imports have the "unused import" warning, although they are used by both the model and the generated Java code.
And if I do an "organize imports" on this model, then the @WebService annotation import is removed and the @Entity annotation import is not removed (?)
[Updated on: Sat, 21 December 2013 15:58] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02689 seconds