LibrarySourceLocator { public String findSources(String className) { String result = null; String sourceFilePath = className.replace('.', '/') + ".java"; try { InputStream in = getClass().getClassLoader().getResourceAsStream(sourceFilePath); if (in != null) { result = toString(in); } if (result == null) { File jdkSourcesFile = new File(javaHome.getParentFile(), "src.zip"); if (jdkSourcesFile.isFile()) { result = findZipFileEntry(jdkSourcesFile, sourceFilePath); } } if (result == null) { File jdkSourcesFile = new File(javaHome, "src.jar"); if (jdkSourcesFile.isFile()) { result = findZipFileEntry(jdkSourcesFile, "src/" + sourceFilePath); } } 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; }  String findSources(String className);  }
@Test public void finds_sources_from_JDK_src_zip() { String sources = locator.findSources(java.lang.String.class.getName()); assertThat(sources, containsString("class String")); }
@Test public void cannot_find_sources_of_unpublished_JDK_classes() { String sources = locator.findSources(sun.misc.Unsafe.class.getName()); assertThat(sources, is(nullValue())); }
@Test public void finds_sources_from_Maven_sources_jar() { String sources = locator.findSources(com.google.common.io.ByteStreams.class.getName()); assertThat(sources, containsString("class ByteStreams")); }
@Test public void finds_sources_from_classpath() { String sources = locator.findSources(org.mockito.asm.Opcodes.class.getName()); assertThat(sources, containsString("interface Opcodes")); }