Need help with Kotlin syntax to make a call to a Java method defined with generics


#1

This is the Java method signature I am trying to call from Kotlin:

<T> T source(String aURI, Class<T> aRepresentationClass) throws NKFException;

In Java I can do this:

IHDSNode node = context.source(“res:/myResource”, IHDSNode.class);

I tried this in Kotlin:

  val node  = context.source("active:something",IHDSNode.class )

but I get an error message:

“Expression 'class ) ’ cannot be a selector  (occur after a dot)”

which references the “.class” part of the method call.

What syntax do I need to construct this method call?

Thank you! – Randy


#2

To get class use this

val clz: Class<String> = javaClass<String>()

#3

This is helpful. Thank you.

I’m still stuck, and maybe this is a bug, an IDE issue, or still something I do not understand.

When I create the following in IntelliJ (latest 12 release and latest Kotlin plugin (m3.1) the text “javaClass” is in red and the fly-over error message says “Unresolved reference: javaClass”. Sounds like I’m missing an import, but this is supposed to be a “core” feature of Kotlin.

Here’s my code copied from IntelliJ:

package net.databliss.netkernel.layer0.endpoints import org.netkernel.module.standard.endpoint.StandardAccessorImpl import org.netkernel.layer0.nkf.INKFRequestContext import org.netkernel.layer0.representation.IHDSNode class TestEndpoint : StandardAccessorImpl()   {   {   declareThreadSafe()   }   override public fun onSource(context: INKFRequestContext) {      val request = context.createRequest("res:/resources/tests/testlist.xml")   request?.setRepresentationClass(javaClass<IHDSNode>())   val node : IHDSNode  = context.issueRequest(request) as IHDSNode   val aNode = node.getFirstNode("//information")   context.createResponseFrom(aNode)   } }


#4

Does the compilation fail if you hit Ctrl+F9 (Cmd+F9 on Mac)?


#5

Yes. It fails.

Information:Using kotlinHome=/Users/randolph.kahle/Library/Application Support/IntelliJIdea12/Kotlin/kotlinc Information:Invoking in-process compiler org.jetbrains.jet.cli.jvm.K2JVMCompiler with arguments [-module, /Users/randolph.kahle/development/source/netkernel/package/bb-overlay/out/production/urn.net.databliss.netkernel.layer0/script.kts, -output, /Users/randolph.kahle/development/source/netkernel/package/bb-overlay/out/production/urn.net.databliss.netkernel.layer0, -tags, -verbose, -version, -noStdlib, -noJdkAnnotations, -noJdk] Information:Kotlin Compiler version 0.4.68 Information:Compilation completed with 1 error and 1 warning in 1 sec Information:1 error Information:1 warning /Users/randolph.kahle/development/source/netkernel/module/urn.net.databliss.netkernel.layer0/src/main/java/net/databliss/netkernel/layer0/endpoints/TestEndpoint.kt   Warning:Warning:line (14)The corresponding parameter in the supertype 'StandardAccessorImpl' is named 'aContext'. This may cause problems when calling this function with named arguments.   Error:Error:line (18)Unresolved reference: javaClass


#6

Does this code work for you?

``

import java.util.concurrent.*

fun main(args: Array<String>) {
  println(javaClass<BlockingDeque<*>>())
}


#7

I hope putting it into a package does not conflict...

This code:

package net.databliss.kotlin

import java.util.concurrent.*

fun main(args: Array<String>) {
  println(javaClass<BlockingDeque<*>>())
}

Has “println” and “javaClass” highlighted in red and says it cannot resolve.

Randy


#8

My guess is you have no Kotlin library set up for your project. This should be taken care of by a notification bar sliding from the top of your editor. Did you see one?


#9

Andrey,

Thank you for following up on this.

Your comment makes sense to me. I do not see any slider.

I removed the Kotlin plugin and then re-downloaded it. Same result.

What is strange is that the compiler and plugin were working fine and then I updated both Leda and (I believe) the plugin for M3.1 and then it stopped working.

I’m on OS X Mountain Lion - latest version…

Do you think I should remove Leda and the plugin and and try to start fresh?

Thank you – Randy


#10

Andrey,

I did remove Leda and the plug in. I cleared all cache areas, etc. in OS X. I downloaded the latest Leda and then installed the Kotlin plugin.

Unfortunately, the results are the same.

Regards,

Randy


#11

Could you archive your project and post it here or send it to me at andrey.breslav at jetbrains.com? Thanks


#12

Andrey,

I sent it via email earlier this morning. Please let me know if you do not receive it.


#13

Replied by email with some instructions. Long story short: you didn't configure your dependencies in pom.xml, and the IDE got confused. Maven set-up instructions: http://confluence.jetbrains.net/display/Kotlin/Kotlin+Build+Tools#KotlinBuildTools-Maven In your case it suffices to delete pom.xml