Converter creates wrong value for GenericDomValue
Hi,
i'm writing a plugin for my company, which handles a special kind of XML-file. Inside this XML, there are some lines like:
<template model="ViewModelName">path to a JSP</template>
where ViewModelName is a name of a viewmodel and path to a JSP a path to a JSP-template.
Given this, i've written an interface to handle these both informations:
internal interface Template : DomElement {
@Convert(JSPFileConverter::class)
@TagValue
@Required
fun getValue(): GenericDomValue<PsiFile?>
@Convert(ModelConverter::class)
fun getModel(): GenericAttributeValue<Collection<PsiClass>>
}
Both converters will be called. The ModelConverter returns a collection of PsiClass and this works well. When reading from getModel(), I'll get the GenericAttributeValue and so on.
The problem is when reading from getValue().
val value = element.getValue()
The JSPFileConverter returns a PsiFile. This works and should be translated to a GenericDomValue<PsiFile?>, similar to the code above. But then, I'll get a ClassCasException:
java.lang.ClassCastException: com.intellij.psi.impl.source.jsp.JspFileImpl cannot be cast to com.intellij.util.xml.GenericDomValue
This means, getValue() is declared as returning a GenericDomValue<PsiFile?> but in reality it returns the calculated PsiFile.
I need this GenericDomValue for positioning an error in an inspection:
internal class MissingJSPValidation : BasicDomElementsInspection<Config>() {
override fun checkDomElement(element: DomElement, holder: DomElementAnnotationHolder, helper: DomHighlightingHelper) {
if (element is Template) {
val value = element.getValue()
val fileName = value.stringValue
if (fileName == null) {
holder.createProblem(value, message("no.file.set"))
} else if (fileName.isNotEmpty() && element.jspFile() == null) {
holder.createProblem(value, message("file.doesnt.exist", fileValue))
}
}
}
}
What is the difference?
Please sign in to leave a comment.
You don't need this GenericDomValue, @TagValue means that the current ("template") tag's value will converted into JSP file and returned from the method. Just make it return JspFile. In the annotator, you can pass Template directly.
Hi Peter,
thanks for your help. This works, but doesn't solve my whole problem. For the text, I need a DomElement to mark it as an error, if the file is missing:
holder.createProblem(...)That was the reason for me to use the GenericDomValue.With this, I have both: the JspFile and the DomElement.
You don't need GenericDomValue: you already have Template which is a DomElement.
Yes and no. :-)
I'd like to mark the text in case of an error. With the Template, the tagname is underlined, which displays the wrong position for the error.
I see. Have you tried to extend Template from GenericDomElement?
That was the trick. :-)
Thank you.