Extending a Java class


#1

I am experimenting with Kotlin to see how it would work as a programming language for NetKernel.

To create an accessor endpoint using Kotlin, I need to extend the Java class org.netkernel.module.standard.endpoint.StandardAccessorImpl

I am having trouble getting started and need some guidance.

In Java, I would do this:

package net.databliss.netkernel.layer0.endpoints;

import org.netkernel.layer0.nkf.INKFRequestContext;
import org.netkernel.module.standard.endpoint.StandardAccessorImpl;

public class JavaAccessor extends StandardAccessorImpl
  {

  public JavaAccessor()
  {
  declareThreadSafe();
  }

  public void onSource(INKFRequestContext context)
  {
  context.createResponseFrom(“This is a response”);
  }

  }


I have attemped this Kotlin code:
package net.databliss.netkernel.layer0.endpoints

import org.netkernel.module.standard.endpoint.StandardAccessorImpl

class MyAccessor : StandardAccessorImpl
  {

  }

But I get an error message saying that "Constructor invocation should be explicitly specified".

How would I create a Kotlin class that replicates the Java example?

– Randy


#2

If i'm not mistaken you should write something like:

class MyAccessor : StandardAccessorImpl() {

}

look here http://confluence.jetbrains.net/display/Kotlin/Classes+and+Inheritance for more info


#3

public class JavaAccessor : StandardAccessorImpl() // Explicit inokation of the super constructor   {

  { // Anonymous initializer in the class body
  declareThreadSafe();
  }

  public fun onSource(context: INKFRequestContext?) // The parameter is nullable by default. Annotate the base class if you want to get rif of this
  {
  context.createResponseFrom(“This is a response”);
  }

  }


#4

Thank you!

With your guidance I now have the following compiling (and making sense to me):

package net.databliss.netkernel.layer0.endpoints import org.netkernel.module.standard.endpoint.StandardAccessorImpl import org.netkernel.layer0.nkf.INKFRequestContext class TestEndpoint : StandardAccessorImpl()   {   {   declareThreadSafe()   }   override public fun onSource(context: INKFRequestContext?)   {   context?.createResponseFrom("This is a response")   } }

It is clear that there would be value rewriting parts of NetKernel that would be adjunct to extension point written by users to handle issues like Null, etc. I will investigate this once I get this example working in NetKernel (the follow-up question is a subject of my next post - to keep the conversation threads focused).

Randy


#5

For nulls, you can simply use annotations (in the code or external): http://blog.jetbrains.com/kotlin/using-external-annotations/


#6

Andrey,

Thank you. Worked perfectly!

– Randy