langtools/test/jdk/javadoc/tool/sampleapi/lib/sampleapi/SampleApi.java
author shurailine
Tue, 21 Feb 2017 15:38:07 -0800
changeset 44015 27aa35eb727c
parent 38838 3d6ea76b35d6
permissions -rw-r--r--
8151220: Extend sample API to use modules. Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33919
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
     1
/*
37641
6949903ba85a 8151777: Add "@index" tag to the sampleapi generator
ksrini
parents: 35426
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
33919
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
     4
 *
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
     8
 *
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    13
 * accompanied this code).
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    14
 *
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    18
 *
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    21
 * questions.
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    22
 */
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    23
package sampleapi;
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    24
44015
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    25
import com.sun.source.util.JavacTask;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    26
import com.sun.tools.javac.api.JavacTaskImpl;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    27
import com.sun.tools.javac.api.JavacTool;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    28
import com.sun.tools.javac.util.Context;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    29
import java.io.IOException;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    30
import java.io.InputStream;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    31
import java.io.UncheckedIOException;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    32
import java.nio.file.Files;
33919
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    33
import java.nio.file.Path;
44015
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    34
import java.nio.file.Paths;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    35
import java.util.ArrayList;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    36
import java.util.List;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    37
import java.util.Optional;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    38
import static java.util.stream.Collectors.toList;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    39
import javax.xml.parsers.DocumentBuilder;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    40
import javax.xml.parsers.DocumentBuilderFactory;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    41
import javax.xml.parsers.ParserConfigurationException;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    42
import org.w3c.dom.Document;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    43
import org.w3c.dom.Element;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    44
import org.w3c.dom.NodeList;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    45
import org.xml.sax.SAXException;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    46
import sampleapi.generator.ModuleGenerator;
33919
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    47
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    48
import sampleapi.generator.PackageGenerator;
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    49
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    50
public class SampleApi {
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    51
44015
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    52
    private final Context ctx;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    53
    private final List<ModuleGenerator> modules = new ArrayList<>();
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    54
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    55
    public SampleApi() {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    56
        JavacTool jt = JavacTool.create();
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    57
        JavacTask task = jt.getTask(null, null, null, null, null, null);
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    58
        ctx = ((JavacTaskImpl) task).getContext();
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    59
    }
33919
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    60
44015
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    61
    public static SampleApi load(Path resDir)
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    62
            throws ParserConfigurationException, IOException, SAXException {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    63
        SampleApi result = new SampleApi();
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    64
        System.out.println("Loading resources from " + resDir);
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    65
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    66
        DocumentBuilder builder = factory.newDocumentBuilder();
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    67
        Files.list(resDir)
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    68
                .peek(f -> System.out.println(f.getFileName()))
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    69
                .filter(f -> f.getFileName().toString().endsWith(".xml"))
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    70
                .peek(f -> System.out.println(f.getFileName()))
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    71
                .forEach(resFile -> {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    72
                    try (InputStream is = Files.newInputStream(resFile)) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    73
                        Document document = builder.parse(is);
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    74
                        NodeList moduleElements = document.getElementsByTagName("module");
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    75
                        for (int i = 0; i < moduleElements.getLength(); i++) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    76
                            result.modules.add(ModuleGenerator
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    77
                                    .load((Element) moduleElements.item(i)));
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    78
                        }
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    79
                    } catch (IOException ex) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    80
                        throw new UncheckedIOException(ex);
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    81
                    } catch (SAXException ex) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    82
                        throw new RuntimeException(ex);
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    83
                    }
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    84
                });
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    85
        return result;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    86
    }
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    87
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    88
    public Context getContext() {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    89
        return ctx;
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    90
    }
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    91
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    92
    public List<ModuleGenerator> getModules() {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    93
        return modules;
33919
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    94
    }
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
    95
44015
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    96
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    97
    public void generate(Path outDir) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    98
        //resolveIDs(modules);
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
    99
        modules.forEach(m -> {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   100
            try {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   101
                m.generate(outDir, this);
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   102
            } catch (IOException ex) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   103
                throw new UncheckedIOException(ex);
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   104
            }
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   105
        });
33919
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
   106
    }
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
   107
44015
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   108
    public void generate(String dir)
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   109
            throws ParserConfigurationException, IOException, SAXException {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   110
        generate(Paths.get(dir));
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   111
    }
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   112
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   113
    public ModuleGenerator moduleById(String id) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   114
        String real_id = getId(id);
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   115
        return modules.stream()
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   116
                            .filter(m -> m.id.equals(real_id))
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   117
                            .findAny().orElseThrow(() -> new IllegalStateException("No module with id: " + real_id));
33919
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
   118
    }
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
   119
44015
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   120
    public PackageGenerator packageById(String id) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   121
        String real_id = getId(id);
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   122
        return modules.stream()
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   123
                .flatMap(m -> m.packages.stream())
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   124
                .filter(p -> p.id.equals(real_id)).findAny()
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   125
                .orElseThrow(() -> new IllegalStateException("No package with id: " + real_id));
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   126
    }
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   127
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   128
    public String classById(String id) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   129
        String real_id = getId(id);
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   130
        return modules.stream()
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   131
                .flatMap(m -> m.packages.stream())
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   132
                .peek(p -> System.out.println(p.packageName + " " + p.idBases.size()))
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   133
                .flatMap(p -> p.idBases.entrySet().stream()
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   134
                    .filter(e -> e.getKey().equals(real_id))
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   135
                    .map(e -> p.packageName + "." + e.getValue().name.toString())
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   136
                    .peek(System.out::println))
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   137
                .findAny().orElseThrow(() -> new IllegalStateException("No class with id: " + id));
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   138
    }
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   139
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   140
    public boolean isId(String name) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   141
        return name.startsWith("$");
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   142
    }
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   143
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   144
    public boolean isIdEqual(String name, String id) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   145
        return isId(name) && getId(name).equals(id);
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   146
    }
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   147
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   148
    public String getId(String name) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   149
        if(!isId(name)) {
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   150
            throw new IllegalStateException("Not an id: " + name);
33919
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
   151
        }
44015
27aa35eb727c 8151220: Extend sample API to use modules.
shurailine
parents: 38838
diff changeset
   152
        return name.substring(1);
33919
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
   153
    }
e9fccc09cfc6 8130880: Create sampleapi regression test
ksrini
parents:
diff changeset
   154
}