Prashant Bhardwaj
Prashant Bhardwaj

Reputation: 1213

How to pass Class Type of some Inner class from Java source to Scala?

I have some java code which defines two classes as follows

public class Foo{
      /*Some code here*/

      private final Bar b = new Bar(); //Object of inner class.

      final class Bar{

      /*Some code here*/
      }

      @Override
      public Class<? extends SomeClass> getSerializedClass(){
        return Bar.class;
      }

    }

Now I want pass the type of inner class Bar to some other code in Scala.

trait Trait1 {

  def func1[B](path: String, overwrite:Boolean= false, value:Int ) = {

    val converter = new OutputConverter[A, Bar, B] (K,V)
    Sink[A, B](path, converter, overwrite)


}

case class Sink[K, B](path: String,
                      outputConverter: OutputConverter[A, Bar, B],
                      overwrite: Boolean = false) extends DataSink[A, Bar, B] with SinkSource {
   /**Some code here*/

}

The difficulty is that I cannot change the Java code. Can anyone please tell me a method to it?

Upvotes: 0

Views: 79

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170723

If you want to use it as a type (like Foo.Bar in Java): Foo#Bar (e.g. OutputConverter[A, Foo#Bar, B]).

If you want a Class[_] object (like Foo.Bar.class in Java): classOf[Foo#Bar].

EDIT: I didn't notice that Bar isn't public. If your Scala code is in the same package as Java code, you should be able to access it in the above ways; if it isn't, you can't access Bar (except by reflection) and shouldn't be able too.

See http://www.iulidragos.org/?p=166 for some more details.

Upvotes: 1

Related Questions