0

I am writing a compiler for Java, and would like to implicitly import java.lang package to all source files (This is what most Java compilers that I know of do).

To do that, I need to somehow obtain either a set of all classes (ie.,Set<Class<?>>) or a set of all qualified names (ie., Set<String>) of all classes in the package.

I've looked at the reflections API from this page, but the structure of my project (and other factors) make it a less-than-suitable candidate.

So I'm wondering whether there is any other way to obtain such classes (other than hard-coding the thing, of course!)? The fact that java.lang is the most popular package in Java kind of suggests that there should be some way(s).

Community
  • 1
  • 1
  • 2
    *"make it a less - than -suitable candidate"* Why, specifically? – Andrew Thompson Feb 13 '13 at 04:58
  • Note that your compiler will also need to implicitly import the default (no name) package and the package the source file is in. – Paul Bellora Feb 13 '13 at 05:00
  • Well, to start off, the requirement is that the compiler has to be able to compile itself, and my compiler is not handling generic very well (let alone reflections and all that). But suppose we ignored that for a moment, in practice, when I added the jar downloaded from the site along with its dependencies (Guava and slf4j) to the build file, the project still doesn't run correctly, and I keep getting `java.lang.NoClassDefFoundError: com.google.common.base.Predicates` – user1801813 Feb 13 '13 at 05:07
  • @AndrewThompson (See above, sorry I forgot to tag) – user1801813 Feb 13 '13 at 05:13
  • @PaulBellora That part is already handled properly. What I need here is to add a little 'feature', that is, to have `java.lang.*` implicitly imported – user1801813 Feb 13 '13 at 05:14
  • @user1801813 Okay, interesting. – Paul Bellora Feb 13 '13 at 05:17

1 Answers1

0

try

    String path = Object.class.getResource("Object.class").getPath().replace("!/java/lang/Object.class", "");
    ZipFile z = new ZipFile(new File(new URI(path)));
    Enumeration<? extends ZipEntry> e =z.entries();
    Set<String> s = new HashSet<String>();
    while(e.hasMoreElements()) {
        String n = e.nextElement().getName();
        if (n.matches("java/lang/\\w+\\.class")) {
            s.add(n);
        }
    }
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275