- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Access modifiers in bytecode
Mon, 2011-01-24, 12:27
Hi all,
I'm using Scala to implement large parts of a Java library. Now I
would like to make the implementation classes "package private" in the
Java sense; that is, certain Scala classes should have the "package
private" modifier in their bytecode representation. Unfortunately, I
failed to achieve this: scalac always makes the compiled classes
"public" in the bytecode, as verified by looking at the output of
javap.
Here's an example:
// scala source code
package p {
private class C {}
}
// output of javap
Compiled from "C.scala"
public class p.C extends java.lang.Object implements scala.ScalaObject{
public p.C();
}
I also tried replacing "private" with "private[p]", "private[this]"
and "protected", with no success: in the generated bytecode, class C
is still public.
What's wrong here?
Mon, 2011-01-24, 14:37
#2
Re: Access modifiers in bytecode
On Mon, Jan 24, 2011 at 12:27:47PM +0100, Stefan Wehr wrote:
> I'm using Scala to implement large parts of a Java library. Now I
> would like to make the implementation classes "package private" in the
> Java sense; that is, certain Scala classes should have the "package
> private" modifier in their bytecode representation. Unfortunately, I
> failed to achieve this: scalac always makes the compiled classes
> "public" in the bytecode, as verified by looking at the output of
> javap.
That is correct. It never emits the protected flag for nonsynthetic
members.
> What's wrong here?
See this thread:
I had no luck getting package private working from scala. The
workaround I've used was to make a skeleton java class with package
private members, then extend it from scala.
On Mon, Jan 24, 2011 at 6:27 AM, Stefan Wehr wrote:
> Hi all,
>
> I'm using Scala to implement large parts of a Java library. Now I
> would like to make the implementation classes "package private" in the
> Java sense; that is, certain Scala classes should have the "package
> private" modifier in their bytecode representation. Unfortunately, I
> failed to achieve this: scalac always makes the compiled classes
> "public" in the bytecode, as verified by looking at the output of
> javap.
>
> Here's an example:
>
> // scala source code
> package p {
> private class C {}
> }
>
> // output of javap
> Compiled from "C.scala"
> public class p.C extends java.lang.Object implements scala.ScalaObject{
> public p.C();
> }
>
> I also tried replacing "private" with "private[p]", "private[this]"
> and "protected", with no success: in the generated bytecode, class C
> is still public.
>
> What's wrong here?
>