0

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?

3
  • What do you mean by ` I want pass the Class type of inner class Bar to some other code in Scala.`? I am sorry I dont understand the question Commented Nov 21, 2014 at 9:41
  • 2
    Bar is declared protected in the Java code. Passing it outside to another class, in Java or some other language is cause for concern. You may be best served to copy what you need out of Bar and pass that. Commented Nov 21, 2014 at 9:48
  • @Jatin : I want to pass type information of inner class to one generic method in Scala code. I apologize if I was not clear enough earliar. Commented Nov 21, 2014 at 10:04

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Don't you think he should make the Bar class static for this to work? I'm curious because I'm not sure.
@EpicPandaForce No, he shouldn't.
Cannot change Java code as that code is part of a library.
@PrashantBhardwaj Then the library author is specifically disallowing you access to Bar by not making it public. As the answer says, you can bypass it by e.g. putting your code into the same package, but this isn't a good idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.