How to list `cannot resolve symbol` warnings in groovy code? (IntelliJ) - intellij-idea

I want to list all cannot resolve symbol warnings in my groovy code. I set severity level of Groovy->Probable bugs->Access to unresolved expression in my inspection profile to Warning. IntelliJ does highlight the cannot resolve symbol warnings in edit view but it does not list the problems in the list of problems after I run Analyze->Inspect Code....
I'm using IntelliJ IDEA 15.0.2.
Running inspection on the following piece of groovy code responds with a message No suspicious code found though fooo() is highlighted.
class Example {
def foo() {
fooo() // highlighted as `Cannot resolve symbol 'fooo'`
}
}

You are looking for static compilation behavior. Please use #CompileStatic for it
import groovy.transform.CompileStatic
#CompileStatic
class Example {
def foo() {
fooo() // highlighted as `Cannot resolve symbol 'fooo'`: shows as error in IJ14
}
}

Related

Why is the kotlin-gradle-plugin failing to create a PSIFile from CodeInsightTestFixture.configureByText?

I created an IntelliJ plugin using the template https://github.com/JetBrains/intellij-platform-plugin-template. The template comes with a test that runs on an XML file. I want to create a similar test for a Kotlin file. Here's the template test file plus my added test (test2):
package org.jetbrains.plugins.template
import com.intellij.ide.highlighter.XmlFileType
import com.intellij.psi.xml.XmlFile
import com.intellij.testFramework.TestDataPath
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import com.intellij.util.PsiErrorElementUtil
#TestDataPath("\$CONTENT_ROOT/src/test/testData")
class MyPluginTest : BasePlatformTestCase() {
fun testXMLFile() {
val psiFile = myFixture.configureByText(XmlFileType.INSTANCE, "<foo>bar</foo>")
val xmlFile = assertInstanceOf(psiFile, XmlFile::class.java)
assertFalse(PsiErrorElementUtil.hasErrors(project, xmlFile.virtualFile))
assertNotNull(xmlFile.rootTag)
xmlFile.rootTag?.let {
assertEquals("foo", it.name)
assertEquals("bar", it.value.text)
}
}
override fun getTestDataPath() = "src/test/testData/rename"
fun testRename() {
myFixture.testRename("foo.xml", "foo_after.xml", "a2")
}
// Here's my test
fun test2() {
val fileText: String = """
package com.loganmay.test
data class MyClass(val myString: String)
""".trimIndent()
val psiFile = myFixture.configureByText("a.kt", fileText)
val xmlFile = assertInstanceOf(psiFile, XmlFile::class.java)
}
}
Without changing the build.gradle file, that test fails with:
Expected instance of: com.intellij.psi.xml.XmlFile actual: com.intellij.psi.impl.source.PsiPlainTextFileImpl
I want it to parse the text as a PsiFile that's also a KtFile. From various sources, I've been led to believe that the fixture is parsing it as a plain text file because the test project doesn't have access to the Kotlin compiler. So, I added:
dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
}
to the build.gradle. Then, when I run the test, configureByText throws an exception with a big trace, the root exception of which is:
Caused by: java.lang.Throwable: 'filetype.archive.display.name' is not found in java.util.PropertyResourceBundle#4ecbb519(messages.CoreBundle)
... 53 more
org.jetbrains.plugins.template.MyPluginTest > test2 FAILED
com.intellij.diagnostic.PluginException at ComponentManagerImpl.kt:511
Caused by: java.util.MissingResourceException at Registry.java:164
Does anyone have any insight into what the issue is or know how to resolve it?
Notes:
I also tried importing the kotlin compiler and casting psiFile as KtFile, which produced the same error, an idea I got from here
This project has a test like this that may be working
This post and this post recommend adding the kotlin gradle plugin, which I did
This question seems similar
Yann Cebron replied on the jetbrains help forum with an answer for Java, which also worked for Kotlin.
The solution is to add a dependency to the IntelliJ gradle plugin. The template comes with these lines in the build.gradle:
intellij {
pluginName.set(properties("pluginName"))
version.set(properties("platformVersion"))
type.set(properties("platformType"))
// Plugin Dependencies. Uses `platformPlugins` property from the gradle.properties file.
plugins.set(properties("platformPlugins").split(',').map(String::trim).filter(String::isNotEmpty))
}
So, didn't need to do anything there. In my gradle.properties, I added
platformPlugins = com.intellij.java, org.jetbrains.kotlin
To my plugin.xml, I added:
<depends>com.intellij.modules.java</depends>
<depends>org.jetbrains.kotlin</depends>
I was able to remove
dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
}
from the build.gradle which I mentioned above.
Now, the test works for Java and Kotlin files.

Stencil: Sudden build error without message after adding `#Method` to component

I cannot give too much information here (because there really isn't), but only this:
All of the sudden, after adding a #Method function to a stencil component:
#Method()
async setMenuItems(items: Element[]): Promise<void> {
// code here
}
the component stopped compiling with the following - really unhelpful - error:
[ ERROR ] ./src/components/menu-content/menu-content.tsx:63:44
build error
L62: #Method()
L63: async setMenuItems(elements: Element[]): Promise<void> {
L64: const unsupportedChildren = elements.filter(e => !this.isSupportedChild(e)).map(e => e.tagName);
[12:37.1] build failed in 7.02 s
Things to notice
the return type Promise<void> inside the error-message is highlighted red
there are other #Methods that do work within this component (even with the same return type).
the "broken" #Method is structurally equal to those that do work.
TypeScript compiler does not complain about anything
Only stencil compiler fails
I already tried...
to google for this issue - did not find any hints to this problem
to remove the async and add return Promise.resolve()
to rename the method (I mean.. why not)
to move the method to another place in class
to remove the whole method (compiles fine x( )
to remove the #Method decorator (compiled, but of course my method is removed from API)
to delete node_modules folder and reinstall
I remember that I already had this error once, and apparently I somehow fixed it (or not, idk). But if I did, I cannot remember how.
Does anyone have an idea how to debug this - or even better fix?
I figured it out. A more complete version of my component is:
import { Element, ... } from '#stencil/core';
class MenuContent {
#Element() element: MenuContentElement;
#Method()
setMenuItems(elements: Element[]): Promise<void> {
// ------------------^^^^^^^
// Element is meant to be the built-in browser interface for Element
// but stencil thinks that this is the imported Element from '#stencil/core'!
}
}
The exact problem here is, that the stencil-compiler seems to assume that the elements parameter is of type Element that is imported from #stencil/core which is obviously wrong and leads to this strange unhelpful error.
Possible Solutions
1. Use an alias type for the built-in Element type
// types.ts
export type DomElement = Element;
// menu-content.tsx
import { DomElement } from './types';
...
async setMenuItems(elements: DomElement[]): Promise<void> { ... }
2. Switch to HTMLElement
Note: This is only legit, when you don't need to support other Element-types such as SVGElements for example!
async setMenuItems(elements: HTMLElement[]): Promise<void> { ... }
3. Use alias in import statement
Please note: When using #stencil eslint rules, they will complain about your renamed import and say that "own class members are not allowed to be public".
I created a ticket for it here: https://github.com/ionic-team/stencil-eslint/issues/28
import { Element as ElementDecorator} from '#stencil/core';
class MenuContent {
// eslint will complain about that:
// "Own class properties cannot be public."
#ElementDecorator() element: HTMLMenuContentElement;
}
I experienced this same issue not with the Element type but with the Event type, so it appears to be rooted deeper - also about a year after you reported this issue it seems to still be a problem with Stencil

Selenium testng.xml file compilation issue

When i am trying to compile testng.xml file it is giving the following error
Error:(15, 27) java: cannot find symbol
symbol: method dependsOnMethod()
location: #interface org.testng.annotations.Test
you have to provide method in double quotes
#Test(dependsOnMethods="loginTest")
or
#Test (dependsOnMethods = { "loginTest" })
You have written 'dependsOnMethod' instead of 'dependsOnMethods'. Please check

How to get rid of the compile time error (despite using the import) for assertThrows? (Java, Eclipse Oxygen)

I am testing out the following code snippet that I found here.
Eclipse Oxygen Version: Oxygen.2 Release (4.7.2) - if that matters
import org.junit.jupiter.api.Assertions;
....
#Test
void exceptionTesting() {
Executable closureContainingCodeToTest = () -> {throw new IllegalArgumentException("a message");};
Assertions.assertThrows(IllegalArgumentException.class, closureContainingCodeToTest, "a message");
}
However, the code doesn't compile.
I am getting the error below:
The method assertThrows(Class, Executable, String) in the type Assertions is not applicable for the arguments (Class, Executable, String) DbHandlerTest.java line 96 Java Problem
Of course my goal is not just to test the above snippet but to write a test for my code. Please help.
I figured out the problem ...
Thanks somuras for the right question.
Following import was wrong
import org.junit.jupiter.api.Executable;
It should have been this:
import org.junit.jupiter.api.function.Executable;

#HystrixProperty cannot be resolved to a type

I have a method marked with #HystrixCommand that has a fallback method defined. I'm trying to add a hystrix property to it so that in case of a timeout it degrades gracefully into a fallback method.
But when I add the #HystrixProperty it shows an error in the STS IDE (3.8.2 Release) saying #HystrixProperty cannot be resolved to a type.
Here is what I'm trying to do:
#HystrixCommand(fallbackMethod="fallbackPerformOperation",
commandProperties={#HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="5000")})
public Future<Object> performOperation(String requestString) throws InterruptedException {
return new AsyncResult<Object>() {
#Override
public Object invoke() {.......
}}}
and this is the error being shown in the IDE:
I'm unable to figure out what the problem is.
Do I need to clear the STS Cache? If so how do I do it?
Thank You.
With in the IDE it is not obvious to suggest the import HystrixProperty class, thus you need to manually import this
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
Then the error should be gone