This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Access modifiers in bytecode

2 replies
Stefan Wehr
Joined: 2011-01-24,
User offline. Last seen 42 years 45 weeks ago.

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?

Lex
Joined: 2010-02-28,
User offline. Last seen 42 years 45 weeks ago.
Re: Access modifiers in bytecode

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?
>

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
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:

http://www.scala-lang.org/node/8215

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland