Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a method with Java protected access in Scala?

I have a Java class that I have ported to Scala, but which still has some Java subclasses implementing abstract functionality.

The original Java had

public abstract class Base {
    ...
    protected abstract Foo getFoo();
}

which was extended

public class Fred extends Base {
    ...
    protected Foo getFoo() {
        return foo;
    }
}

Now when I port Base to Scala

class Base {
    ...
    protected def getFoo(): Foo
}

The compiler complains that

Cannot reduce the visibility of the inherited method ...


Now I understand that because its rules are subtly different, Scala tends to enforce visibility in the compiler rather than the bytecode, so that getFoo is marked public in the javap'd class. But

protected[packagename] def getFoo()

still does not generate a pukka (Java) protected method, although I think that the visibility should be the same.

Is there any way to persuade the Scala compiler to emit a method that Java will consider protected?

like image 219
Duncan McGregor Avatar asked Sep 10 '25 23:09

Duncan McGregor


1 Answers

If you can sandwich Scala between two Java implementations, you could create a Java superclass that only has the desired abstract protected methods, at least until you convert all subclasses to Scala (if this is actually code under your control). But I do hope there's a better way...

EDIT: Another option is to use some kind of bytecode rewriter, like the annotation processing tool.

like image 175
Jordão Avatar answered Sep 13 '25 05:09

Jordão