How do I "get" a Scala case object from Java?

mipadi picture mipadi · Apr 1, 2010 · Viewed 13.6k times · Source

I created a hierarchy of case objects in Scala that looks like the following:

package my.awesome.package

sealed abstract class PresetShapeType(val displayName: String)

case object AccelerationSensor extends PresetShapeType("Acceleration Sensor")
case object DisplacementSensor extends PresetShapeType("Displacement Sensor")
case object ForceSensor        extends PresetShapeType("Force Sensor")
case object PressureSensor     extends PresetShapeType("Pressure Sensor")
case object StrainSensor       extends PresetShapeType("Strain Sensor")

I also have a piece of Java code in which I'd like to access PressureSensor, but the following does not work:

package my.awesome.package.subpackage;

import my.awesome.package.PressureSensor;

// Do some stuff, then...

DVShape newshape = DVShapeFactory.createPresetShape(PressureSensor, new Point3f(0,0,0));

So, how do I reference the PressureSensor case object from Java? I decompiled the byte code for both the PressureSensor and PressureSensor$ classes, which yielded the following:

Compiled from "DVShapeFactory.scala"
public final class org.nees.rpi.vis.PressureSensor extends java.lang.Object{
    public static final java.lang.Object productElement(int);
    public static final int productArity();
    public static final java.lang.String productPrefix();
    public static final int $tag();
    public static final java.lang.String displayName();
}

Compiled from "DVShapeFactory.scala"
public final class org.nees.rpi.vis.PressureSensor$ extends org.nees.rpi.vis.PresetShapeType implements scala.ScalaObject,scala.Product,java.io.Serializable{
    public static final org.nees.rpi.vis.PressureSensor$ MODULE$;
    public static {};
    public org.nees.rpi.vis.PressureSensor$();
    public java.lang.Object readResolve();
    public java.lang.Object productElement(int);
    public int productArity();
    public java.lang.String productPrefix();
    public final java.lang.String toString();
    public int $tag();
}

But that didn't yield any great insight.

Answer

Seth Tisue picture Seth Tisue · Apr 1, 2010

from Java, say:

my.awesome.package.PressureSensor$.MODULE$