How do I “get” a Scala case object from Java?

2019-01-23 02:01发布

问题:

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.

回答1:

from Java, say:

my.awesome.package.PressureSensor$.MODULE$


回答2:

PressureSensor$.MODULE$ should give you the instance of the case object.



回答3:

This is still a hack, but in my opinion a bit more readable in Java. Just add a method to explicitly return the reference to the singleton instance (it shows up as a static method on the class):

sealed abstract class PresetShapeType(val displayName: String)

case object AccelerationSensor extends PresetShapeType("Acceleration Sensor") { def instance = this }
case object DisplacementSensor extends PresetShapeType("Displacement Sensor") { def instance = this }
case object ForceSensor extends PresetShapeType("Force Sensor") { def instance = this }
case object PressureSensor extends PresetShapeType("Pressure Sensor") { def instance = this }
case object StrainSensor extends PresetShapeType("Strain Sensor") { def instance = this }

And then in Java:

import my.awesome.package.PressureSensor;
DVShape newshape = DVShapeFactory.createPresetShape(PressureSensor.instance(), new Point3f(0,0,0));