LightElement/LightMethodBuilder returning null from getContainingFile causing IllegalArgumentException in InjectedLanguageManager

Answered

I'm trying to write a plugin that provides support for a DSL that ends up generating java source, but without doing the generation through LightElement(s) like LightPsiClassBuilder and LightMethodBuilder. Currently it seems to be causing exceptions which might be messing with the type resolution of the types used in the light classes and methods. I looked into the intellij code and it appears that LightElement.getContainingFile only returns null, and that causes InjectedLanguageManagerImpl.isInjectedFragment to throw an IllegalArgumentException exception.

 

How should I deal with this? Always provide my own LightMethod based class rather than using LightMethodBuilder? Doesn't that make LightMethodBuilder useless?

 

Also, how should this kind of language support be done ideally? I have a bunch of DSL that declare interfaces and want to provide simulated declarations for them if the classes haven't been previously generated by an external build system.

 

Thanks

 

Stacktrace is below.

 

```2019-08-23 08:03:33,996 [ 70949] ERROR - aemon.impl.PassExecutorService - Argument for @NotNull parameter 'file' of com/intellij/psi/impl/source/tree/injected/InjectedLanguageManagerImpl.isInjectedFragment must not be null
java.lang.IllegalArgumentException: Argument for @NotNull parameter 'file' of com/intellij/psi/impl/source/tree/injected/InjectedLanguageManagerImpl.isInjectedFragment must not be null
at com.intellij.psi.impl.source.tree.injected.InjectedLanguageManagerImpl.$$$reportNull$$$0(InjectedLanguageManagerImpl.java)
at com.intellij.psi.impl.source.tree.injected.InjectedLanguageManagerImpl.isInjectedFragment(InjectedLanguageManagerImpl.java)
at com.intellij.codeInspection.dataFlow.HardcodedContracts.getHardcodedContracts(HardcodedContracts.java:161)
at com.intellij.codeInsight.DefaultInferredAnnotationProvider.getHardcodedContractAnnotation(DefaultInferredAnnotationProvider.java:102)
at com.intellij.codeInsight.DefaultInferredAnnotationProvider.findInferredAnnotation(DefaultInferredAnnotationProvider.java:59)
at com.intellij.codeInsight.InferredAnnotationsManagerImpl.findInferredAnnotation(InferredAnnotationsManagerImpl.java:26)
at com.intellij.codeInsight.AnnotationUtil.lambda$null$0(AnnotationUtil.java:108)
at com.intellij.util.containers.ConcurrentFactoryMap$2.create(ConcurrentFactoryMap.java:181)
at com.intellij.util.containers.ConcurrentFactoryMap.get(ConcurrentFactoryMap.java:45)
at com.intellij.codeInsight.AnnotationUtil.findNonCodeAnnotation(AnnotationUtil.java:119)
at com.intellij.codeInsight.AnnotationUtil.findAnnotation(AnnotationUtil.java:74)
at com.intellij.codeInsight.AnnotationUtil.findAnnotation(AnnotationUtil.java:63)
at com.intellij.codeInsight.AnnotationUtil.findAnnotation(AnnotationUtil.java:58)
at com.intellij.codeInsight.AnnotationUtil.findAnnotationInHierarchy(AnnotationUtil.java:170)
at com.intellij.codeInspection.dataFlow.JavaMethodContractUtil.findContractAnnotation(JavaMethodContractUtil.java:184)
at com.intellij.codeInspection.dataFlow.JavaMethodContractUtil.lambda$getContractInfo$1(JavaMethodContractUtil.java:139)
at com.intellij.psi.util.CachedValuesManager.lambda$getCachedValue$0(CachedValuesManager.java:140)
at com.intellij.psi.impl.PsiCachedValueImpl.doCompute(PsiCachedValueImpl.java:49)
at com.intellij.util.CachedValueBase.getValueWithLock(CachedValueBase.java:222)
at com.intellij.psi.impl.PsiCachedValue.getValueWithLock(PsiCachedValue.java:60)
at com.intellij.psi.impl.PsiCachedValueImpl.getValue(PsiCachedValueImpl.java:38)
at com.intellij.util.CachedValuesManagerImpl.getCachedValue(CachedValuesManagerImpl.java:88)
at com.intellij.psi.util.CachedValuesManager.getCachedValue(CachedValuesManager.java:139)
at com.intellij.psi.util.CachedValuesManager.getCachedValue(CachedValuesManager.java:126)
at com.intellij.codeInspection.dataFlow.JavaMethodContractUtil.getContractInfo(JavaMethodContractUtil.java:138)
at com.intellij.codeInspection.dataFlow.JavaMethodContractUtil.isPure(JavaMethodContractUtil.java:194)
at com.siyeh.ig.bugs.IgnoreResultOfCallInspection$IgnoreResultOfCallVisitor.isPureMethod(IgnoreResultOfCallInspection.java:278)
at com.siyeh.ig.bugs.IgnoreResultOfCallInspection$IgnoreResultOfCallVisitor.visitCalledExpression(IgnoreResultOfCallInspection.java:204)
at com.siyeh.ig.bugs.IgnoreResultOfCallInspection$IgnoreResultOfCallVisitor.visitMethodCallExpression(IgnoreResultOfCallInspection.java:181)
at com.intellij.psi.impl.source.tree.java.PsiMethodCallExpressionImpl.accept(PsiMethodCallExpressionImpl.java:127)
at com.intellij.codeInspection.InspectionEngine.acceptElements(InspectionEngine.java:75)
at com.intellij.codeInspection.InspectionEngine.createVisitorAndAcceptElements(InspectionEngine.java:63)
at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.runToolOnElements(LocalInspectionsPass.java:292)
at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.lambda$null$5(LocalInspectionsPass.java:260)
at com.intellij.util.AstLoadingFilter.disallowTreeLoading(AstLoadingFilter.java:126)
at com.intellij.util.AstLoadingFilter.disallowTreeLoading(AstLoadingFilter.java:115)
at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.lambda$visitPriorityElementsAndInit$6(LocalInspectionsPass.java:257)
at com.intellij.concurrency.ApplierCompleter.execAndForkSubTasks(ApplierCompleter.java:133)
at com.intellij.concurrency.ApplierCompleter.tryToExecAllList(ApplierCompleter.java:223)
at com.intellij.concurrency.ApplierCompleter.execAndForkSubTasks(ApplierCompleter.java:151)
at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1168)
at com.intellij.concurrency.ApplierCompleter.lambda$wrapInReadActionAndIndicator$1(ApplierCompleter.java:105)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:582)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:532)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:87)
at com.intellij.concurrency.ApplierCompleter.wrapInReadActionAndIndicator(ApplierCompleter.java:116)
at com.intellij.concurrency.ApplierCompleter.lambda$compute$0(ApplierCompleter.java:96)
at com.intellij.openapi.application.impl.ReadMostlyRWLock.executeByImpatientReader(ReadMostlyRWLock.java:147)
at com.intellij.openapi.application.impl.ApplicationImpl.executeByImpatientReader(ApplicationImpl.java:222)
at com.intellij.concurrency.ApplierCompleter.compute(ApplierCompleter.java:96)
at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)```

6 comments
Comment actions Permalink

I'm not sure I understand why you try to achieve with Light* stuff, but maybe com.intellij.psi.augment.PsiAugmentProvider will be easier for DSL "fake"?

0
Comment actions Permalink

In previous experiments it appeared that a PsiAugmentProvider didn't do what I wanted, but perhaps it will. I'll look at it in a bit.

 

My current code uses a PsiElementFinder and ShortNamesCache to provide the ide with the fakes.

 

How should I be providing synthetic or fake elements to the IDE? Is the Light* api not the right thing to use? My code has been loosely inspired by the Android and Kotlin plugins.

0
Comment actions Permalink

So you're providing a complete "fake" class filled with fake methods? Or what exactly is your DSL about?

It seems the best solution seems to implement your own light class, see org.jetbrains.android.augment.AndroidLightClassBase as inspiration

0
Comment actions Permalink

The DSL specifies interfaces with methods and properties. And the code expects certain generated APIs to exist, like a default implementation and some extra static methods on the interface. So I do end up needing to provide complete fake classes with methods.

 

I do have my own Light classes at the moment. Originally based on AndroidLightClassBase, but I've since switched to subclassing LightPsiClassBuilder and LightMethodBuilder instead.

 

Which is the correct plugin api to be using for this? PsiAugmentProvider or PsiElementFinder? Or both? I'd like to allow the IDE to pick up actual generated files after the external build generates them, but prior to that I need to provide fakes.

0
Comment actions Permalink

IMHO using custom light classes via PsiElementFinder  would be best choice here.

0
Comment actions Permalink

I ended up significantly modifying the parser and language definition to integrate a lot tighter into the java subsystem. It was a bit of a pain in the rear, but in the end it works rather well.

I'm now having an issue with another part of my plugin using PsiElementFinder to provide light classes. The ide is requesting and getting my custom classes, but then marks them red with the hover text "Cannot resolve symbol 'x'".

Basically theres a (generated) class that is only accessed statically, has a few inner (generated) classes, and is often accessed not using the full qualified name from the same package. Normally this should work fine, but for some reason the intellij highlighting decides it can't find the class. While debugging its like the path it takes through multiResolveImpl/OurGenericsResolver never bothers to ask plugins or look up symbols in the current package. It only goes as far as the current file and stops. Though a following step creating the quick fix for the "error" does query my ShortNamesCache and finds the class just fine.

It's really strange to me. Not sure exactly what I should be doing to get this to work properly.

TL;DR; Need to provide a public static final class that is visible to imports and in files in the same package without errors. Current attempt using PsiElementFinder and ShortNamesCache isn't doing the business.

0

Please sign in to leave a comment.