Scala object-nested classes in Eclipse Java

288 views
Skip to first unread message

Andrius Velykis

unread,
Apr 19, 2012, 4:25:48 AM4/19/12
to scala...@googlegroups.com
I am having trouble with Scala-Java interop when using Eclipse IDE. I have a Scala structure where a class is nested within nested objects. When trying to use the compiled classes in Java code, I cannot reference the inner class. Has anyone else encountered similar problem or are there any workarounds?

An example to reproduce the problem is below. I am interested in the Inner class of the following Scala code:

package mypackage;

object Outer {
  object Middle {
    class Inner {
      def msg() {
        println("Running");
      }
    }
  }
}


To use the class Inner in Java code, I use the following code:

mypackage.Outer$Middle$Inner inner = new mypackage.Outer$Middle$Inner();
inner.msg();

However, Eclipse (Indigo) compiler complains with the following message at the first line:
The nested type mypackage.Outer$Middle$Inner cannot be referenced using its binary name

Interestingly, the Java code above works when used with NetBeans 6.9.1.


Is this an issue with Eclipse compiler when using Scala classes? Or am I referencing the class incorrectly?

I have also posted the question on StackOverflow earlier, thinking this may be an Eclipse issue, but have not got a definite answer there..

Thank you,
~Andrius Velykis

Sandro Gržičić

unread,
Apr 19, 2012, 6:21:08 AM4/19/12
to scala...@googlegroups.com
Hi, just want to say note that ScalaIDE related questions are best answered on the scala-ide-user group. ;)

It seems this is an Eclipse+ScalaIDE related problem, not so much a Scala problem. :)

Andrius Velykis

unread,
Apr 19, 2012, 8:14:45 AM4/19/12
to scala...@googlegroups.com
Hello,

This is not explicitly ScalaIDE question - I could use scalac to compile and then jar into the library. The question is about Scala-Java interop when using Eclipse IDE, and I was wondering what is the right way of referencing such class, and how to do that in Eclipse.

Thank you,
~Andrius Velykis

Som Snytt

unread,
Apr 19, 2012, 3:03:52 PM4/19/12
to Andrius Velykis, scala...@googlegroups.com
On Thu, Apr 19, 2012 at 5:14 AM, Andrius Velykis <and...@velykis.lt> wrote:

This is not explicitly ScalaIDE question - I could use scalac to compile and then jar into the library. The question is about Scala-Java interop when using Eclipse IDE, and I was wondering what is the right way of referencing such class, and how to do that in Eclipse.

 
I can confirm the issue, which is that Eclipse balks on OuterObject$MiddleObject$Inner because OuterObject and MiddleObject don't have an InnerClass relationship.  (I'm guessing a bit about why Eclipse balks.)

Here's a workaround of sorts:

You can create a class called Outer$Middle$Inner in your Eclipse project, which will only warn on the name.  Give it a stub interface so there are no compilation errors.

Then in your run configuration, you can "add external jar" to "User entries" (to add your scala jar) and then move it "up" ahead of the "default classpath" that includes the stub.

(Alternatively, you can edit .classpath manually, put your scala binary before java src. The editor will still complain about the classname, but that error won't stop you from running.)

Is that worth the trouble?  On a real project, the stub class would go in a special stub-src dir so it doesn't get packaged in product by some horrible accident.

Grzegorz Kossakowski

unread,
Apr 19, 2012, 8:20:13 PM4/19/12
to Som Snytt, Andrius Velykis, scala...@googlegroups.com
On 19 April 2012 19:03, Som Snytt <som....@gmail.com> wrote:
On Thu, Apr 19, 2012 at 5:14 AM, Andrius Velykis <and...@velykis.lt> wrote:

This is not explicitly ScalaIDE question - I could use scalac to compile and then jar into the library. The question is about Scala-Java interop when using Eclipse IDE, and I was wondering what is the right way of referencing such class, and how to do that in Eclipse.

 
I can confirm the issue, which is that Eclipse balks on OuterObject$MiddleObject$Inner because OuterObject and MiddleObject don't have an InnerClass relationship.  (I'm guessing a bit about why Eclipse balks.)

Actually, they do. scalac should generate signatures for those objects.

-- 
Grzegorz

Grzegorz Kossakowski

unread,
Apr 19, 2012, 8:21:41 PM4/19/12
to Andrius Velykis, scala...@googlegroups.com
On 19 April 2012 08:25, Andrius Velykis <and...@velykis.lt> wrote:
To use the class Inner in Java code, I use the following code:

mypackage.Outer$Middle$Inner inner = new mypackage.Outer$Middle$Inner();
inner.msg();

However, Eclipse (Indigo) compiler complains with the following message at the first line:
The nested type mypackage.Outer$Middle$Inner cannot be referenced using its binary name

Interestingly, the Java code above works when used with NetBeans 6.9.1.


Is this an issue with Eclipse compiler when using Scala classes? Or am I referencing the class incorrectly?

What Scala version are you using? Also, did you try:

new mypackage.Outer$.Middle$.Inner();

--
Grzegorz Kossakowski

Andrius Velykis

unread,
Apr 21, 2012, 3:09:20 PM4/21/12
to scala...@googlegroups.com
Hello,

> What Scala version are you using? Also, did you try:
> new mypackage.Outer$.Middle$.Inner();

I am using Scala 2.9.1 (tried both Scala-IDE and command-line official Scala distribution) with Eclipse Indigo. The suggested approach does not help - I am getting the following error from Eclipse: mypackage.Outer$.Middle$ cannot be resolved to a type.

Note that the problem is not in the initialisation - I cannot reference the class. E.g. neither of the following work in Eclipse: 
mypackage.Outer$Middle$Inner inner = null;
mypackage.Outer$.Middle$.Inner inner = null;
mypackage.Outer.Middle$.Inner inner = null;
mypackage.Outer.Middle.Inner inner = null;

Thanks,
~Andrius Velykis

On Friday, 20 April 2012 01:21:41 UTC+1, Grzegorz Kossakowski wrote:

Andrius Velykis

unread,
Apr 29, 2012, 2:32:12 PM4/29/12
to scala...@googlegroups.com
Hello,

On my question at StackOverflow (http://stackoverflow.com/questions/10207149/scala-object-nested-classes-in-eclipse-java) I got a reply from Iulian Dragos that deep nesting information gets lost in bytecode translation. Such structures should be avoided if Java interop is important.

Posting this to wrap up the thread if someone else searches for a similar answer here.

Thanks,
~Andrius
Reply all
Reply to author
Forward
0 new messages