Scala: Compation Trait object not showing in Java

The companion object for the attribute in Scala does not have visibility issues in Scala:

trait ProtocolPacket extends Serializable { def toByteArray: Array[Byte] } object ProtocolPacket { def getStreamType( streamBytes: Array[Byte] ) = { // ... } } 

However, on the Java side (for example, gets higher in the bank), ProtocolPacket.getStreamType not displayed. In fact, the source (IDEA decompiled) does not have the getStreamType method defined for ProtocolPacket

EDIT:

I found similar images to SO regarding Companion$MODULE$ , but was fooled by IDEA :), as shown below:

enter image description here

The above compiles and runs fine (the shell itself and / or IDEA) if someone falls into the trap.

+6
source share
2 answers

Looking at the javap output, you will find:

 $ javap ProtocolPacket public interface ProtocolPacket extends scala.Serializable{ public abstract byte[] toByteArray(); } 

and related object:

 $ javap ProtocolPacket$ public final class ProtocolPacket$ extends java.lang.Object implements scala.ScalaObject,scala.Serializable{ public static final ProtocolPacket$ MODULE$; public static {}; public void getStreamType(byte[]); public java.lang.Object readResolve(); } 

this makes me believe in Java, which you can write about:

 ProtocolPacket$.MODULE$.getStreamType(/**/) 
+5
source

I think this is ProtocolPacket$.MODULE$.getStreamType() in Java, but I have not double checked.

See also What do you call a singleton method from Scala from Java? .

+2
source

Source: https://habr.com/ru/post/909297/


All Articles