Javadocs的Lokups 8

时间:2013-12-29 17:50:36

标签: java intellij-idea java-8

java8的javadocs查找使用-param-用于参数分离,在intellij中它使用(param),因此每次查找都在java 8 docs中失败。有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这似乎是一个错误(即没有修复)。我无法找到有关它的错误报告,所以我打开了一个: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;
            }
        }
    }
}
相关问题