I have following example Code:
class Test{
#Deprecated
Test(String par){
println(par)
}
Test(int par){
println(par)
}
#Deprecated
void myFunc(){
println("Hello")
}
}
Test test = new Test("Griasdi")
test.myFunc()
Test test2 = new Test(1)
I want the Test(String par) constructor and the myFunc Method be be marked as deprecated, But IntelliJ only marks the Method as Deprecated but not the constructor, see here on the Picture:
Does anybody know why the constructor is not marked as deprecated? Is this a Groovy thing or an IntelliJ issue?
Is this a Groovy thing or an IntelliJ issue?
It is an IntelliJ thing. Groovy isn't creating and annotating the editor in the IDE.
Related
I am writing a test for a Spring-Boot project written in Kotlin 1.5. This piece of code does fail at test runtime with an InvalidUseOfMatchersException and I struggle to figure out why:
#SpringBootTest
#AutoConfigureMockMvc
internal class ControllerTest {
#Autowired
lateinit var mockMvc: MockMvc
#MockBean
lateinit var mockedAuthFilter: AuthFilter
#BeforeEach
fun setup() {
assertNotNull(mockedAuthFilter)
`when`(
mockedAuthFilter.shouldProceed(
any(HttpServletRequest::class.java),
any(AuthConfig::class.java)
)
).thenReturn(true)
}
#Test
fun `This call should return without an error`() {
mockMvc.perform(get("/api/entities")).andDo(print()).andExpect(status().isOk)
}
}
All I can find in the web for this error is that you tried an argument matcher on a basic type - but that is not the case here. Have you faced this problem and how did you solve it?
From my understanding the failure comes from a masked nullpointer exception caused by the shouldProceed method itself which doesn't allow a call with nulled arguments, which kind of happens internally in Mockito during the setup of the mocked instance after any() returns.
The solution was to not use Mockito with Kotlin, but the MockK test utility instead.
I have Test class which has written purely with kotlin in the library project.
class Test{
#Deprecated(message = "Use other function")
fun testFunction(id: String): Test {
this.testId = id
return this
}
}
I've deprecated testFunction() with Deprecated annotation. Btw Deprecated class is under the kotlin package. When i test this deprecated function in kotlin project works as expected(ide shows deprecated warning and strikethrough)
Example: Test(). testFunction("test")
But in the java project it doesn't show warning or strikethrough to function bye ide. When I open the declaration of deprecated function it's like below
#Deprecated(
message = "Use other function"
)
#NotNull
public final Test testFunction(#NotNull String var1) {
Intrinsics.checkParameterIsNotNull(var1, "id");
this.testId = var1;
return this;
}
any help would be appreciated
In Kotlin, the functions, properties, and classes marked with kotlin.Deprecated annotation also get the Deprecated attribute in the resulting JVM bytecode. This allows Java compiler to see them as deprecated and emit the corresponding warning.
Take for example this function that is deprecated in Kotlin:
https://github.com/JetBrains/kotlin/blob/v1.3.50/libraries/stdlib/common/src/generated/_Ranges.kt#L171-L175
If you try to call it from java as
kotlin.ranges.RangesKt.intRangeContains(null, 1.0);
javac compiler will report the following warning:
Warning:(55, 31) java: intRangeContains(kotlin.ranges.ClosedRange<java.lang.Integer>,double) in kotlin.ranges.RangesKt___RangesKt has been deprecated
and both IDEA and Android Studio will mark it as deprecated as well:
Is it possible to create a custom generated constructor in Intellij IDEA.
I would like to use the setter methods by default to initialize the global class variables.
Instead of :
public Person(long svnr, LocalDate geb, Geschlecht geschlecht) {
this.svnr = svnr;
this.geb = geb;
this.geschlecht = geschlecht;
}
like this:
public Person(long svnr, LocalDate geb, Geschlecht geschlecht) {
setSvnr(svnr);
setGeb(geb);
setGeschlecht(geschlecht);
}
Unfortunately there is no ready tool to generate constructors with setters in IntelliJ IDEA, please follow the issue created for this feature request:
https://youtrack.jetbrains.com/issue/IDEABKL-465
How do I get IntelliJ to auto-generate a lambda expression as the argument being passed?
What I want:
I have seen the Question How to autocomplete lambdas in IntelliJ IDEA?, but that does not seem to produce my desired result.
As Tagir Valeev already mentioned in his comment, IntelliJ will auto-generate lambda code, but only if your ClickListener is a (functional) interface.
E.g.:
#FunctionalInterface
interface ClickListener {
void listen();
}
class Clicker {
void addClickListener(ClickListener listener){
// ...
}
}
Then, Intellij suggests upon hitting Ctrl+Shift+Space
Note: As to the #FunctionalInterface annotation, Jav Doc says
This annotation is not a requirement for the compiler to recognize an interface as a functional interface
I use the lib jackson-module-kotlin to parse string of json into object.
My issue is when I parse a string into an enum , and when I launch with intellij, I have this stack trace:
Caused by: kotlin.reflect.jvm.internal.KotlinReflectionInternalError:
Reflection on built-in Kotlin types is not yet fully supported. No
metadata found for public final val name: kotlin.String defined in
kotlin.Enum[DeserializedPropertyDescriptor#212b316a]
I don't have this issue when I launch with maven.
I use kotlin 1.1.51, with intellij kotlin plugin 1.2.0-release-IJ2017.3-1, I target a JVM 1.8, and i use jackson-module-kotlin version 2.8.7
what should I do?
enum class CType { DEAL, FILE }
data class Code(val code: String, val type: CType)
fun testDeserialization() {
val mapper = jacksonObjectMapper()
// following line throws an exception:
mapper.readValue("""{"code":"A","type":"DEAL"}""", Code::class.java)
}
The only way I got it working is by adding additional #JvmStatic annotation. I had mapper.registerModule(new KotlinModule()); and all, nothing worked but this:
package nc.features.algo.model
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonValue
enum class LHStatus (
#get:JsonValue val code: Int
) {
LH_POS_OVU_WAITING(1),
LH_NEG_OVU_WAITING(2),
;
companion object {
#JsonCreator
#JvmStatic
fun deser(code: Int?): LHStatus? {
if (code == null) return null
for (i in values()) {
if (i.code == code) return i
}
return null
}
}
}
You have to do a few things.
Update Jackson dependencies to the latest version (right now, 2.9.4).
Update Kotlin version to a version equal or greater than 1.3.0.
Be sure to add the following dependencies to your build.gradle:
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:$jackson_version"
... then you call registerKotlinModule() on your Jackson ObjectMapper and the code of your enum should be just like this:
enum class CType(#get:JsonValue val value: String) {
DEAL("deal"),
FILE("file");
companion object {
#JsonCreator
fun fromString(value: String): CType? {
for (type in CType.values()) {
if (type.name.equals(value, true)) {
return gender
}
}
return null
}
}
}
Intellij is most likely using the kotlin compiler version 1.2.0 (from the plugin) and it doesn't seem to support reflection properly.
I suggest you do one of the following:
Upgrade your kotlin version in maven and the intellij kotlin plugin to newer versions (e.g. 1.2.30). If you do that, you also have to update jackson-module-kotlin to >= 1.9, since there is an incompatibility with kotlin 1.2 (see here).
Set the kotlin compiler version to 1.1 in Intellij Idea settings.
It is generally a good idea to use the same version of kotlin in Intellij Idea and maven/gradle.
You need to use the Kotlin module for Jackson that is compatible with Kotlin 1.2.x, this includes minimally these three versions of the module:
2.9.4.1 (works with any 2.9.x of Jackson, but best to use most recent)
2.8.11.1 (for Jackson 2.8.x)
2.7.9.1 (for Jackson 2.7.x)
Otherwise, you will run into a problem with library mismatches.
The jackson-module-kotlin homepage lists these as the current versions, but they are likely to change and you can check the various Maven repository search engines to see which library versions are available and which dependencies they have on Kotlin to find matching versions.
Also note you can import the extensions for the ObjectMapper class and use reified types, so instead of:
val something = mapper.readValue("""{"code":"A","type":"DEAL"}""", Code::class.java)
you would have:
val something: Code = mapper.readValue("""{"code":"A","type":"DEAL"}""")
or alternatively:
val something = mapper.readValue<Code>("""{"code":"A","type":"DEAL"}""")
It is usually bad to use the erased type (i.e. Whatever::class.java) since this does not work for anything with generic type parameters, and using reified types also works nicely when deserializing into collections.