tags:

views:

50

answers:

2

I'm making a JVM language. This language has modules (namespaces) and the ability to import java libraries. In the import section the user can import things like "java.io." or "java.concurrent." etc. How do I know that a "File" reference in the source is in the package java.io and not somewhere else?

+1  A: 

Because Java is statically typed the lookup need to be be unambiguous. When you import entire packages (or namespaces) your compiler will need to handle name clashes in those namespaces if the types are used in the source. So you'll need to scan the list of available types to check.

That is why you sometimes see source code with the full class name expanded. e.g. java.util.Date when in import both java.util and java.sql Otherwise the compiler doesn't know which one to use.

Scala has some interesting features about importing from Java packages. You can look at those for inspiration.

leonm
"So you'll need to scan the list of available types to check." that's exactly what I want to know, how do I scan the available types? I don't know any Package.scan() in java. Thanks for the scala reference, i'll take a look
islon
A: 

If you want to know which classes are in a package you need to list all the class files in the directory for that package in the class path.

Peter Lawrey