0

I have a module written in Scala that I need to integrate into a Java web application. Everything works great, except the fact that methods and fields that are

private[somepackage] 

in the Scala classes appear to be accessible from the Java code from outside that package. Is there any way to hide those?

EDIT: example illustrating what is happening

package my.scala.module

class SomeClass {
  private[scala] val myValue = "this should be hidden"
}


package com.something.service;
import my.scala.module.SomeClass;

public class MyService {
    private static SomeClass someInstance = new SomeClass();

    public static void main(String[] args){
        System.out.println(someInstance.myValue());
    }
}

Running main will cause "this should be hidden" to print

3
  • I think a small working example to show the exact error is required. I haven't run into this issue yet. Commented Oct 19, 2016 at 23:27
  • posted one as an edit Commented Oct 19, 2016 at 23:28
  • ibm.com/developerworks/library/j-scala07298 shows an example of your issue. Commented Oct 19, 2016 at 23:47

1 Answer 1

2

There is no way to encode this constraint in JVM bytecode. It is enforced by the Scala compiler, but neither the JVM nor Java know anything about it.

There are some Scala features which can be encoded in JVM bytecode, and some which can't.

In particular, there are some constraints which cannot be encoded in JVM bytecode, e.g. sealed or private[foo], or val. Which means that if you get your hands on the compiled JVM bytecode of a Scala source file, then you can do stuff that you can't do from Scala by interacting with the code through a language that is not Scala.

This is not specific to the JVM backend, you have similar, and even more pronounced problems with Scala.js, since the compilation target here (ECMAScript) offers even less ways of expressing constraints than JVM bytecode does.

But really, this is just a general problem: I can take a language as safe and pure as Haskell, compile it to native code, and if I get my hands on the compiled binary, all safety will be lost. In fact, most Haskell compilers perform (almost) complete type erasure, so there are literally no types, and no type constraints left after compilation.

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

1 Comment

See ibm.com/developerworks/library/j-scala07298 for an example of this issue.

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.