java8 的 javadocs 查找使用 -param- 作为参数分隔,在 intellij 中它使用 (param),因此在 java 8 文档中每次查找都会失败。有办法解决这个问题吗?
最佳答案
这似乎是一个错误(即没有修复)。我找不到关于它的错误报告,所以我打开了一个:IDEA-118970 Java 8 Javadoc for methods not found due to URL syntax change希望它在 13.0.2 中得到修复。
与此同时,作为一种解决方法,我编写了一个程序来解析 Java api
目录(例如 jdkRoot/docs/api
)并转换新的 Java 8 语法锚定到先前的语法。运行后,只需将您的 JDK 8 定义配置为使用应用程序创建的修改后的目录。这是我很快就敲出来的东西,所以我可能漏掉了什么。但一些快速测试表明它工作正常。
该应用需要 Java 7+ 和 jsoup HTML 解析器库。
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.DecimalFormat;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
//Using System out since it is a one off utility and to keep dependencies simple
@SuppressWarnings({"CallToPrintStackTrace", "UseOfSystemOutOrSystemErr"})
public class JavaDocParserAndConvert
{
private static Path API_DIR;
private static Path OUT_DIR;
private static final Pattern dashRegex = Pattern.compile("-");
public static void main(String... args)
{
try
{
if (args.length != 1)
{
System.out.println("Usage: JavaDocParserAndConvert <path-to-java-api-directory");
System.out.println(" Example: JavaDocParserAndConvert C:\\java\\jdk8ea\\b121-x64\\docs\\api");
System.exit(-1);
}
API_DIR = Paths.get(args[0]);
if (Files.notExists(API_DIR))
{
System.err.println("Specified API Directory does not exist.");
System.exit(-1);
}
OUT_DIR = API_DIR.getParent().resolve("api-modified");
System.out.println("OUT_DIR = " + OUT_DIR);
if (Files.notExists(OUT_DIR))
{
Files.createDirectories(OUT_DIR);
}
JavaDocParserAndConvert converter = new JavaDocParserAndConvert();
converter.execute();
}
catch (Exception ex)
{
System.err.println("FATAL EXCEPTION: " + ex.toString());
ex.printStackTrace();
}
System.exit(0);
}
private void execute() throws Exception
{
HtmlFileVisitor fileVisitor = new HtmlFileVisitor();
Files.walkFileTree(API_DIR, fileVisitor);
fileVisitor.executorService.shutdown();
fileVisitor.executorService.awaitTermination(6, TimeUnit.HOURS);
System.out.println("Completed");
}
class HtmlFileVisitor extends SimpleFileVisitor<Path>
{
private final ExecutorService executorService = Executors.newFixedThreadPool(10);
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
{
if (file.getFileName().toString().endsWith(".html"))
{
FileProcessor processor = new FileProcessor(file);
executorService.submit(processor);
}
return FileVisitResult.CONTINUE;
}
}
static class FileProcessor implements Runnable
{
static final DecimalFormat formatter = new DecimalFormat("#,###");
static final AtomicInteger fileCount = new AtomicInteger(0);
final Path file;
FileProcessor(Path file)
{
this.file = file;
}
@Override
public void run()
{
try
{
final int fileCountValue = fileCount.incrementAndGet();
if ((fileCountValue % 100) == 0)
{
System.out.printf("File Count: %7s%n", formatter.format(fileCountValue));
}
processFile(file);
}
catch (Exception e)
{
System.err.println("An Exception occurred processing file " + file + " Details: " + e.toString());
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void processFile(Path file) throws IOException
{
final Document document = Jsoup.parse(file.toFile(), "UTF-8");
final Elements anchorElements = document.getElementsByTag("a");
for (Element anchorElement : anchorElements)
{
convertAttr(anchorElement);
}
final Path subPath = file.subpath(API_DIR.getNameCount(), file.getNameCount());
final Path outputFile = OUT_DIR.resolve(subPath);
final Path dir = outputFile.getParent();
if (Files.notExists(dir))
{
Files.createDirectories(dir);
}
Files.deleteIfExists(outputFile);
Files.createFile(outputFile);
try (final BufferedWriter writer = Files.newBufferedWriter(outputFile, Charset.forName("UTF-8")))
{
writer.write(document.toString());
}
}
private void convertAttr(Element anchor)
{
convertAttr(anchor, "name");
convertAttr(anchor, "href");
}
private void convertAttr(Element anchor, String attrName)
{
final String attrValue = anchor.attr(attrName);
if (!attrValue.isEmpty())
{
final String updatedNameAttr = convertAttrValue(attrValue);
anchor.attr(attrName, updatedNameAttr);
}
}
private String convertAttrValue(String attrValue)
{
if (attrValue.endsWith("-"))
{
String updated = attrValue;
updated = dashRegex.matcher(updated).replaceFirst("(");
updated = updated.substring(0, updated.length() - 1) + ')';
updated = dashRegex.matcher(updated).replaceAll(", ");
return updated;
}
else
{
return attrValue;
}
}
}
}
关于java - Javadocs 8 的 Lokups,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20828254/