开发者

How do I resolve a class/field in a JVM compiler?

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜