TraverseProc.java revision 3792:d516975e8110
1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.jdeprscan;
27
28import java.util.ArrayList;
29import java.util.Collection;
30import java.util.Comparator;
31import java.util.HashMap;
32import java.util.HashSet;
33import java.util.List;
34import java.util.Map;
35import java.util.Set;
36
37import javax.annotation.processing.AbstractProcessor;
38import javax.annotation.processing.Messager;
39import javax.annotation.processing.ProcessingEnvironment;
40import javax.annotation.processing.RoundEnvironment;
41import javax.annotation.processing.SupportedAnnotationTypes;
42import javax.annotation.processing.SupportedSourceVersion;
43
44import javax.lang.model.element.ModuleElement;
45import javax.lang.model.element.TypeElement;
46import javax.lang.model.util.Elements;
47
48import static javax.lang.model.SourceVersion.RELEASE_9;
49import javax.lang.model.element.Element;
50import javax.lang.model.element.ElementKind;
51import javax.lang.model.element.Modifier;
52import javax.lang.model.element.PackageElement;
53import javax.tools.Diagnostic;
54
55@SupportedSourceVersion(RELEASE_9)
56@SupportedAnnotationTypes("*")
57public class TraverseProc extends AbstractProcessor {
58    Elements elements;
59    Messager messager;
60    final List<String> moduleRoots;
61    Map<PackageElement, List<TypeElement>> publicTypes;
62
63    TraverseProc(List<String> roots) {
64        moduleRoots = roots;
65    }
66
67    @Override
68    public void init(ProcessingEnvironment pe) {
69        super.init(pe);
70        elements = pe.getElementUtils();
71        messager = pe.getMessager();
72    }
73
74    @Override
75    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
76        if (roundEnv.processingOver()) {
77            return false;
78        }
79
80        Set<ModuleElement> modules = new HashSet<>();
81        for (String mname : moduleRoots) {
82            ModuleElement me = elements.getModuleElement(mname);
83            if (me == null) {
84                messager.printMessage(Diagnostic.Kind.ERROR,
85                                      String.format("module %s not found%n", mname));
86            } else {
87                modules.addAll(findModules(me));
88            }
89        }
90
91        Set<PackageElement> packages = findPackages(modules);
92
93        publicTypes = findPublicTypes(packages);
94
95        return true;
96    }
97
98    void printPublicTypes() {
99        printPublicTypes(publicTypes);
100    }
101
102    public Map<PackageElement, List<TypeElement>> getPublicTypes() {
103        return publicTypes;
104    }
105
106    void printPublicTypes(Map<PackageElement, List<TypeElement>> types) {
107        System.out.println("All public types:");
108        types.entrySet().stream()
109             .sorted(Comparator.comparing(e -> e.getKey().toString()))
110             .forEach(e -> {
111                 System.out.println("  " + e.getKey());
112                 e.getValue().stream()
113                         .sorted(Comparator.comparing(TypeElement::toString))
114                         .forEach(t -> System.out.println("    " + t));
115             });
116        System.out.println();
117        System.out.flush();
118    }
119
120    Set<ModuleElement> findModules(ModuleElement root) {
121        return findModules0(root, new HashSet<>(), 0);
122    }
123
124    Set<ModuleElement> findModules0(ModuleElement m, Set<ModuleElement> set, int nesting) {
125        set.add(m);
126        for (ModuleElement.Directive dir : m.getDirectives()) {
127            if (dir.getKind() == ModuleElement.DirectiveKind.REQUIRES) {
128                ModuleElement.RequiresDirective req = (ModuleElement.RequiresDirective)dir;
129                findModules0(req.getDependency(), set, nesting + 1);
130            }
131        }
132        return set;
133    }
134
135    Set<PackageElement> findPackages(Collection<ModuleElement> mods) {
136        Set<PackageElement> set = new HashSet<>();
137        for (ModuleElement m : mods) {
138            for (ModuleElement.Directive dir : m.getDirectives()) {
139                if (dir.getKind() == ModuleElement.DirectiveKind.EXPORTS) { //XXX
140                    ModuleElement.ExportsDirective exp = (ModuleElement.ExportsDirective)dir;
141                    if (exp.getTargetModules() == null) {
142                        set.add(exp.getPackage());
143                    }
144                }
145            }
146        }
147        return set;
148    }
149
150    Map<PackageElement, List<TypeElement>> findPublicTypes(Collection<PackageElement> pkgs) {
151        Map<PackageElement, List<TypeElement>> map = new HashMap<>();
152        for (PackageElement pkg : pkgs) {
153            List<TypeElement> enclosed = new ArrayList<>();
154            for (Element e : pkg.getEnclosedElements()) {
155                addPublicTypes(enclosed, e);
156            }
157            map.put(pkg, enclosed);
158        }
159        return map;
160    }
161
162    void addPublicTypes(List<TypeElement> list, Element e) {
163        ElementKind kind = e.getKind();
164        if ((kind.isClass() || kind.isInterface())
165                && e.getModifiers().contains(Modifier.PUBLIC)) {
166            list.add((TypeElement)e);
167            for (Element enc : e.getEnclosedElements()) {
168                addPublicTypes(list, enc);
169            }
170        }
171    }
172}
173