1/*
2 * Copyright (c) 1998, 2014, 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.doclets.internal.toolkit.util;
27
28import java.util.*;
29
30import com.sun.javadoc.*;
31import com.sun.tools.doclets.internal.toolkit.Configuration;
32
33/**
34 * Build list of all the deprecated packages, classes, constructors, fields and methods.
35 *
36 *  <p><b>This is NOT part of any supported API.
37 *  If you write code that depends on this, you do so at your own risk.
38 *  This code and its internal interfaces are subject to change or
39 *  deletion without notice.</b>
40 *
41 * @author Atul M Dambalkar
42 */
43@Deprecated
44public class DeprecatedAPIListBuilder {
45
46    public static final int NUM_TYPES = 12;
47
48    public static final int PACKAGE = 0;
49    public static final int INTERFACE = 1;
50    public static final int CLASS = 2;
51    public static final int ENUM = 3;
52    public static final int EXCEPTION = 4;
53    public static final int ERROR = 5;
54    public static final int ANNOTATION_TYPE = 6;
55    public static final int FIELD = 7;
56    public static final int METHOD = 8;
57    public static final int CONSTRUCTOR = 9;
58    public static final int ENUM_CONSTANT = 10;
59    public static final int ANNOTATION_TYPE_MEMBER = 11;
60
61    /**
62     * List of deprecated type Lists.
63     */
64    private List<List<Doc>> deprecatedLists;
65    private final Configuration configuration;
66    private final Utils utils;
67
68    /**
69     * Constructor.
70     *
71     * @param configuration the current configuration of the doclet
72     */
73    public DeprecatedAPIListBuilder(Configuration configuration) {
74        this.configuration = configuration;
75        this.utils = configuration.utils;
76        deprecatedLists = new ArrayList<>();
77        for (int i = 0; i < NUM_TYPES; i++) {
78            deprecatedLists.add(i, new ArrayList<Doc>());
79        }
80        buildDeprecatedAPIInfo();
81    }
82
83    /**
84     * Build the sorted list of all the deprecated APIs in this run.
85     * Build separate lists for deprecated packages, classes, constructors,
86     * methods and fields.
87     *
88     * @param configuration the current configuration of the doclet.
89     */
90    private void buildDeprecatedAPIInfo() {
91        Set<PackageDoc> packages = configuration.packages;
92        for (PackageDoc pkg : packages) {
93            if (utils.isDeprecated(pkg)) {
94                getList(PACKAGE).add(pkg);
95            }
96        }
97        for (ClassDoc cd : configuration.root.classes()) {
98            if (utils.isDeprecated(cd)) {
99                if (cd.isOrdinaryClass()) {
100                    getList(CLASS).add(cd);
101                } else if (cd.isInterface()) {
102                    getList(INTERFACE).add(cd);
103                } else if (cd.isException()) {
104                    getList(EXCEPTION).add(cd);
105                } else if (cd.isEnum()) {
106                    getList(ENUM).add(cd);
107                } else if (cd.isError()) {
108                    getList(ERROR).add(cd);
109                } else if (cd.isAnnotationType()) {
110                    getList(ANNOTATION_TYPE).add(cd);
111                }
112            }
113            composeDeprecatedList(getList(FIELD), cd.fields());
114            composeDeprecatedList(getList(METHOD), cd.methods());
115            composeDeprecatedList(getList(CONSTRUCTOR), cd.constructors());
116            if (cd.isEnum()) {
117                composeDeprecatedList(getList(ENUM_CONSTANT), cd.enumConstants());
118            }
119            if (cd.isAnnotationType()) {
120                composeDeprecatedList(getList(ANNOTATION_TYPE_MEMBER),
121                                      ((AnnotationTypeDoc) cd).elements());
122            }
123        }
124        sortDeprecatedLists();
125    }
126
127    /**
128     * Add the members into a single list of deprecated members.
129     *
130     * @param list List of all the particular deprecated members, e.g. methods.
131     * @param members members to be added in the list.
132     */
133    private void composeDeprecatedList(List<Doc> list, MemberDoc[] members) {
134        for (MemberDoc member : members) {
135            if (utils.isDeprecated(member)) {
136                list.add(member);
137            }
138        }
139    }
140
141    /**
142     * Sort the deprecated lists for class kinds, fields, methods and
143     * constructors.
144     */
145    private void sortDeprecatedLists() {
146        for (int i = 0; i < NUM_TYPES; i++) {
147            Collections.sort(getList(i));
148        }
149    }
150
151    /**
152     * Return the list of deprecated Doc objects of a given type.
153     *
154     * @param type the constant representing the type of list being returned.
155     */
156    public List<Doc> getList(int type) {
157        return deprecatedLists.get(type);
158    }
159
160    /**
161     * Return true if the list of a given type has size greater than 0.
162     *
163     * @param type the type of list being checked.
164     */
165    public boolean hasDocumentation(int type) {
166        return (deprecatedLists.get(type)).size() > 0;
167    }
168}
169