Can't access protected constructor?

511 views
Skip to first unread message

Haoyi Li

unread,
Feb 12, 2013, 4:28:41 PM2/12/13
to scala-user
I'm getting the following error trying to convert the examples at [http://www.javablogging.com/dynamic-in-memory-compilation/] to scala. In particular, i converted:

public class ClassFileManager extends ForwardingJavaFileManager {
    public ClassFileManager(StandardJavaFileManager standardManager) {
        super(standardManager);
    }
    ...
}
became

class ClassFileManager(fileManager: StandardJavaFileManager) 
extends ForwardingJavaFileManager(fileManager) {
But then I get

scala: constructor ForwardingJavaFileManager in class ForwardingJavaFileManager cannot be accessed in package svm
 Access to protected constructor ForwardingJavaFileManager not permitted because
 enclosing package svm is not a subclass of 
 class ForwardingJavaFileManager in package tools where target is defined
class ClassFileManager(fileManager: StandardJavaFileManager)
                      ^

Which kind of makes sense, but is also rather odd. For some reason the protected constructor is being called in the scope of the external package (svm) and not in the scope of the class (ClassFileManager). Why is this the case? And is there any idiomatic way of doing this? 

It works in Java, but looking at how many lines of code I saved writing the rest of this in Scala, I'd prefer to keep it in Scala if at all possible.

Som Snytt

unread,
Feb 12, 2013, 8:10:12 PM2/12/13
to Haoyi Li, scala-user
On Tue, Feb 12, 2013 at 1:28 PM, Haoyi Li <haoy...@gmail.com> wrote:

[snip]
class ClassFileManager(fileManager: StandardJavaFileManager) 
extends ForwardingJavaFileManager(fileManager) {
But then I get

scala: constructor ForwardingJavaFileManager in class ForwardingJavaFileManager cannot be accessed in package svm
 

It is used at: src/compiler/scala/tools/util/Javap.scala


    // manages named arrays of bytes, which might have failed to load
    class JavapFileManager(val managed: Seq[Input])(delegate: JavaFileManager = defaultFileManager)
      extends ForwardingJavaFileManager[JavaFileManager](delegate) {
 

Haoyi Li

unread,
Feb 12, 2013, 10:51:07 PM2/12/13
to Som Snytt, scala-user
Strange. When I replaced

extends ForwardingJavaFileManager(fileManager)

with

extends ForwardingJavaFileManager[StandardJavaFileManager](fileManager)

which is what is used in the scala library, the error disappeared, and it compiled. Yay!

The immediate problem is solved. But is this a bug? It seems weird that adding a type parameter (that should have been inferred anyway) should affect the access/visibility of the constructor in any way, but yet here it does.

-Haoyi
Reply all
Reply to author
Forward
0 new messages