class LibrarySourceLocator {

    public String findSources(String className) {
        String result = null;
        String sourceFilePath = className.replace('.', '/') + ".java";
        try {
            // TODO: consider using javax.annotation.processing.Filer.getResource and javax.tools.StandardLocation.SOURCE_PATH

            // classpath
            InputStream in = getClass().getClassLoader().getResourceAsStream(sourceFilePath);
            if (in != null) {
                result = toString(in);
            }

            // JDK
            if (result == null) {
                File jdkSourcesFile = new File(javaHome.getParentFile(), "src.zip");
                if (jdkSourcesFile.isFile()) {
                    result = findZipFileEntry(jdkSourcesFile, sourceFilePath);
                }
            }

            // JDK 6 on OS X
            if (result == null) {
                File jdkSourcesFile = new File(javaHome, "src.jar");
                if (jdkSourcesFile.isFile()) {
                    result = findZipFileEntry(jdkSourcesFile, "src/" + sourceFilePath);
                }
            }

            // local Maven repository
            if (result == null) {
                File classesJar = findJarContaining(className);
                if (classesJar != null) {
                    File sourcesJar = new File(classesJar.getPath().replaceAll("\\.jar$", "-sources.jar"));
                    if (sourcesJar.isFile()) {
                        result = findZipFileEntry(sourcesJar, sourceFilePath);
                    }
                }
            }
        } catch (IOException e) {
            System.err.println("Error: failed to read sources of " + className);
            e.printStackTrace();
        }
        return result;
    }

}