Plugin tries to cast a PsiJavaFileImpl into a XmlFile

Answered

Hi guys. I'm working on a plugin for the OFBiz ERP. Currently, i'm working on the references from a java file to a Service definition wich is in a XML file.

And i get this error :

2021-07-30 18:26:07,065 [ 153128]  ERROR - aemon.impl.PassExecutorService - class com.intellij.psi.impl.source.PsiJavaFileImpl cannot be cast to class com.intellij.psi.xml.XmlFile (com.intellij.psi.impl.source.PsiJavaFileImpl is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader @7930fb8a; com.intellij.psi.xml.XmlFile is in unnamed module of loader com.intellij.util.lang.UrlClassLoader @6d8a00e3) 
java.lang.ClassCastException: class com.intellij.psi.impl.source.PsiJavaFileImpl cannot be cast to class com.intellij.psi.xml.XmlFile (com.intellij.psi.impl.source.PsiJavaFileImpl is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader @7930fb8a; com.intellij.psi.xml.XmlFile is in unnamed module of loader com.intellij.util.lang.UrlClassLoader @6d8a00e3)
at org.apache.ofbiz.dom.editor.context.OfbizControllerContext.isInContext(OfbizControllerContext.java:24)
at com.intellij.codeInsight.template.impl.TemplateManagerImpl.getDirectlyApplicableContextTypes(TemplateManagerImpl.java:515)
at com.intellij.codeInsight.template.impl.TemplateManagerImpl.getApplicableContextTypes(TemplateManagerImpl.java:649)
at com.intellij.codeInsight.template.impl.TemplateManagerImpl.isApplicable(TemplateManagerImpl.java:564)
at com.intellij.codeInsight.template.impl.TemplateManagerImpl.isApplicable(TemplateManagerImpl.java:560)
at com.intellij.codeInsight.daemon.impl.quickfix.IterateOverIterableIntention.isAvailable(IterateOverIterableIntention.java:61)
at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.availableFor(ShowIntentionActionsHandler.java:157)
at com.intellij.codeInsight.daemon.impl.ShowIntentionsPass.lambda$getActionsToShow$1(ShowIntentionsPass.java:322)
at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.chooseBetweenHostAndInjected(ShowIntentionActionsHandler.java:190)
at com.intellij.codeInsight.daemon.impl.ShowIntentionsPass.getActionsToShow(ShowIntentionsPass.java:321)
at com.intellij.codeInsight.daemon.impl.ShowIntentionsPass.doCollectInformation(ShowIntentionsPass.java:226)
at com.intellij.codeHighlighting.TextEditorHighlightingPass.collectInformation(TextEditorHighlightingPass.java:54)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.lambda$doRun$1(PassExecutorService.java:399)
at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1110)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.lambda$doRun$2(PassExecutorService.java:392)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:629)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:581)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:60)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.doRun(PassExecutorService.java:391)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.lambda$run$0(PassExecutorService.java:367)
at com.intellij.openapi.application.impl.ReadMostlyRWLock.executeByImpatientReader(ReadMostlyRWLock.java:170)
at com.intellij.openapi.application.impl.ApplicationImpl.executeByImpatientReader(ApplicationImpl.java:182)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.run(PassExecutorService.java:365)
at com.intellij.concurrency.JobLauncherImpl$VoidForkJoinTask$1.exec(JobLauncherImpl.java:181)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
2021-07-30 18:26:07,067 [ 153130] ERROR - aemon.impl.PassExecutorService - IntelliJ IDEA 2020.2.4 Build #IC-202.8194.7
2021-07-30 18:26:07,067 [ 153130] ERROR - aemon.impl.PassExecutorService - JDK: 11.0.9; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o.
2021-07-30 18:26:07,067 [ 153130] ERROR - aemon.impl.PassExecutorService - OS: Linux
2021-07-30 18:26:07,067 [ 153130] ERROR - aemon.impl.PassExecutorService - Plugin to blame: OFBiz IDEA Integration version: 0.10.1
2021-07-30 18:26:07,068 [ 153131] ERROR - aemon.impl.PassExecutorService - Last Action: GotoDeclaration

Does it seems familiar to someone ? As far as i understand, there is an unsuccessful cast, bit i don't know why the cast is tried in a first place.

 

Is there some code i can share ? The project is quite large, i don't know witch part to share to get any help.
Thanks beforehand

0
4 comments

Please link sources of

org.apache.ofbiz.dom.editor.context.OfbizControllerContext 
0

Sorry for late answer, we've started fresh on a new structure, but the problem remains.
We dit it in groovy this time, and i got the same cast error with component file.

// the error is thrown in the line that casts component
PsiDirectory getComponentDir(String name){
List<DomFileElement> componentFiles = DomService.getInstance()
.getFileElements(ComponentFile.class, project, GlobalSearchScope.allScope(project))
for(DomFileElement component : componentFiles){
component = (ComponentFile) component
if( component.getName().getValue().equalsIgnoreCase(name)){
return component.getFile().getContainingDirectory()
}
}
return null
}

Here is the file description :

package org.apache.ofbiz.dom
import com.intellij.util.xml.DomElement
import com.intellij.util.xml.DomFileDescription

class ComponentFileDescription <S extends DomElement> extends DomFileDescription<ComponentFile> {
private static final String rootTagName = "ofbiz-component"

ComponentFileDescription() { super(ComponentFile.class, rootTagName) }
}

and here is the dom description :

package org.apache.ofbiz.dom
import com.intellij.util.xml.*

interface ComponentFile extends DomFileElement {
@Attribute("name")
@Stubbed
GenericAttributeValue<String> getName();

@SubTagList("entity-resource")
List<EntityModelFile> getEntityResources();

@SubTagList("service-resource")
List<ServiceDefFile> getServiceResources();
}

And here is the error :

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'File XmlFile:ofbiz-component.xml' with class 'com.intellij.util.xml.impl.DomFileElementImpl' to class 'org.apache.ofbiz.dom.ComponentFile'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:415)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:329)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:243)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:615)
at org.apache.ofbiz.project.ProjectServiceImpl.getComponentDir(ProjectServiceImpl.groovy:52)
at org.apache.ofbiz.project.ProjectServiceInterface$getComponentDir.call(Unknown Source)
at org.apache.ofbiz.reference.common.ComponentAwareFileReferenceSet$2.multiResolve(ComponentAwareFileReferenceSet.groovy:115)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.getContexts(FileReference.java:106)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.lambda$innerResolve$0(FileReference.java:131)
at com.intellij.openapi.util.RecursionManager$1.computePreventingRecursion(RecursionManager.java:111)
at com.intellij.openapi.util.RecursionGuard.doPreventingRecursion(RecursionGuard.java:43)
at com.intellij.openapi.util.RecursionManager.doPreventingRecursion(RecursionManager.java:68)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.innerResolve(FileReference.java:131)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference$MyResolver.resolve(FileReference.java:594)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference$MyResolver.resolve(FileReference.java:589)
at com.intellij.psi.impl.source.resolve.ResolveCache.lambda$resolveWithCaching$2(ResolveCache.java:184)
at com.intellij.openapi.util.Computable.get(Computable.java:18)
at com.intellij.psi.impl.source.resolve.ResolveCache.lambda$loggingResolver$4(ResolveCache.java:260)
at com.intellij.openapi.util.Computable.get(Computable.java:18)
at com.intellij.psi.impl.source.resolve.ResolveCache.resolve(ResolveCache.java:238)
at com.intellij.psi.impl.source.resolve.ResolveCache.resolveWithCaching(ResolveCache.java:184)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.multiResolve(FileReference.java:122)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.getContexts(FileReference.java:106)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.lambda$innerResolve$0(FileReference.java:131)
at com.intellij.openapi.util.RecursionManager$1.computePreventingRecursion(RecursionManager.java:111)
at com.intellij.openapi.util.RecursionGuard.doPreventingRecursion(RecursionGuard.java:43)
at com.intellij.openapi.util.RecursionManager.doPreventingRecursion(RecursionManager.java:68)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.innerResolve(FileReference.java:131)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference$MyResolver.resolve(FileReference.java:594)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference$MyResolver.resolve(FileReference.java:589)
at com.intellij.psi.impl.source.resolve.ResolveCache.lambda$resolveWithCaching$2(ResolveCache.java:184)
at com.intellij.openapi.util.Computable.get(Computable.java:18)
at com.intellij.psi.impl.source.resolve.ResolveCache.lambda$loggingResolver$4(ResolveCache.java:260)
at com.intellij.openapi.util.Computable.get(Computable.java:18)
at com.intellij.psi.impl.source.resolve.ResolveCache.resolve(ResolveCache.java:238)
at com.intellij.psi.impl.source.resolve.ResolveCache.resolveWithCaching(ResolveCache.java:184)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.multiResolve(FileReference.java:122)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.getContexts(FileReference.java:106)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.lambda$innerResolve$0(FileReference.java:131)
at com.intellij.openapi.util.RecursionManager$1.computePreventingRecursion(RecursionManager.java:111)
at com.intellij.openapi.util.RecursionGuard.doPreventingRecursion(RecursionGuard.java:43)
at com.intellij.openapi.util.RecursionManager.doPreventingRecursion(RecursionManager.java:68)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.innerResolve(FileReference.java:131)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference$MyResolver.resolve(FileReference.java:594)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference$MyResolver.resolve(FileReference.java:589)
at com.intellij.psi.impl.source.resolve.ResolveCache.lambda$resolveWithCaching$2(ResolveCache.java:184)
at com.intellij.openapi.util.Computable.get(Computable.java:18)
at com.intellij.psi.impl.source.resolve.ResolveCache.lambda$loggingResolver$4(ResolveCache.java:260)
at com.intellij.openapi.util.Computable.get(Computable.java:18)
at com.intellij.psi.impl.source.resolve.ResolveCache.resolve(ResolveCache.java:238)
at com.intellij.psi.impl.source.resolve.ResolveCache.resolveWithCaching(ResolveCache.java:184)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.multiResolve(FileReference.java:122)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.resolve(FileReference.java:353)
at com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference.resolve(FileReference.java:47)
at com.intellij.codeInspection.xml.DeprecatedClassUsageInspection.checkReferences(DeprecatedClassUsageInspection.java:46)
at com.intellij.codeInspection.xml.DeprecatedClassUsageInspection$1.visitXmlAttributeValue(DeprecatedClassUsageInspection.java:37)
at com.intellij.psi.impl.source.xml.XmlAttributeValueImpl.accept(XmlAttributeValueImpl.java:43)
at com.intellij.codeInspection.InspectionEngine.acceptElements(InspectionEngine.java:64)
at com.intellij.codeInspection.InspectionEngine.createVisitorAndAcceptElements(InspectionEngine.java:55)
at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.runToolOnElements(LocalInspectionsPass.java:326)
at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.lambda$visitPriorityElementsAndInit$4(LocalInspectionsPass.java:285)
at com.intellij.util.AstLoadingFilter.forceAllowTreeLoading(AstLoadingFilter.java:159)
at com.intellij.util.AstLoadingFilter.forceAllowTreeLoading(AstLoadingFilter.java:151)
at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.lambda$visitPriorityElementsAndInit$5(LocalInspectionsPass.java:283)
at com.intellij.util.AstLoadingFilter.disallowTreeLoading(AstLoadingFilter.java:130)
at com.intellij.util.AstLoadingFilter.disallowTreeLoading(AstLoadingFilter.java:119)
at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.lambda$visitPriorityElementsAndInit$6(LocalInspectionsPass.java:283)
at com.intellij.concurrency.ApplierCompleter.execAndForkSubTasks(ApplierCompleter.java:136)
at com.intellij.concurrency.ApplierCompleter.execAndForkSubTasks(ApplierCompleter.java:149)
at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1078)
at com.intellij.concurrency.ApplierCompleter.lambda$wrapInReadActionAndIndicator$1(ApplierCompleter.java:92)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:705)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:647)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:63)
at com.intellij.concurrency.ApplierCompleter.wrapInReadActionAndIndicator(ApplierCompleter.java:104)
at com.intellij.concurrency.ApplierCompleter.lambda$compute$0(ApplierCompleter.java:83)
at com.intellij.openapi.application.impl.ReadMostlyRWLock.executeByImpatientReader(ReadMostlyRWLock.java:174)
at com.intellij.openapi.application.impl.ApplicationImpl.executeByImpatientReader(ApplicationImpl.java:183)
at com.intellij.concurrency.ApplierCompleter.compute(ApplierCompleter.java:83)
at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:746)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)

it seems that the type returned by the DomManager is DomFileElementImpl, but that's not what i want. How do i convert it ?

 

0

interface ComponentFile extends DomFileElement

 

it should extend DomElement as it is the root DomElement

0

It worked, thanks a lot !

0

Please sign in to leave a comment.