Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/jdeprscan/DeprDB.java
41161 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.jdeprscan;2627import java.io.BufferedReader;28import java.io.IOException;29import java.nio.file.Files;30import java.nio.file.Paths;31import java.util.ArrayList;32import java.util.Arrays;33import java.util.Formatter;34import java.util.HashMap;35import java.util.List;36import java.util.Locale;37import java.util.Map;38import java.util.Set;3940import javax.lang.model.element.ElementKind;4142/**43* A database of deprecations (APIs declared to be deprecated),44* loaded from a JDK or from a class library.45*/46public class DeprDB {47/**48* Deprecated types.49* A map from deprecated type names to DeprData values.50* Types include classes, interfaces, enums, and annotation types.51*/52final Map<String, DeprData> types = new HashMap<>();5354/**55* Deprecated methods. Key is type name, value is map from method56* signatures in the form "methodname(parms)ret" to DeprData value.57*/58final Map<String, Map<String, DeprData>> methods = new HashMap<>();5960/**61* Deprecated fields. Key is type name, value is map from field name62* to forRemoval value.63*/64final Map<String, Map<String, DeprData>> fields = new HashMap<>();6566/**67* Set of valid ElementKind strings.68*/69static final Set<String> validElementKinds =70Set.of(Arrays.stream(ElementKind.values())71.map(ElementKind::toString)72.toArray(String[]::new));737475private DeprDB() { }7677public static List<DeprData> loadFromFile(String filename) throws IOException {78List<DeprData> list = new ArrayList<>();7980exit:81try (final BufferedReader br = Files.newBufferedReader(Paths.get(filename))) {82String line = br.readLine();83if (line == null || !line.equals("#jdepr1")) {84System.out.printf("ERROR: invalid first line %s%n", line);85break exit;86}87while ((line = br.readLine()) != null) {88if (line.startsWith("#")) {89continue;90}91List<String> tokens = CSV.split(line);92if (tokens.size() != 5) {93System.out.printf("ERROR: %s%n", line);94continue;95}96// kind,typeName,descOrName,since,forRemoval97String kindStr = tokens.get(0);98String type = tokens.get(1);99String detail = tokens.get(2);100String since = tokens.get(3);101boolean forRemoval = Boolean.parseBoolean(tokens.get(4));102ElementKind kind;103104if (validElementKinds.contains(kindStr)) {105kind = ElementKind.valueOf(kindStr);106} else {107System.out.printf("ERROR: invalid element kind %s%n", kindStr);108continue;109}110111DeprData data = new DeprData(kind, /*TypeElement*/null, type, detail, since, forRemoval);112list.add(data);113}114}115return list;116}117118public static DeprDB loadFromList(List<DeprData> deprList) {119DeprDB db = new DeprDB();120121for (DeprData dd : deprList) {122switch (dd.kind) {123case CLASS:124case INTERFACE:125case ENUM:126case ANNOTATION_TYPE:127db.types.put(dd.typeName, dd);128break;129case METHOD:130case CONSTRUCTOR:131db.methods.computeIfAbsent(dd.typeName, k -> new HashMap<>())132.put(dd.nameSig, dd);133break;134case ENUM_CONSTANT:135case FIELD:136db.fields.computeIfAbsent(dd.typeName, k -> new HashMap<>())137.put(dd.nameSig, dd);138break;139}140}141142return db;143}144145@Override146public String toString() {147StringBuilder sb = new StringBuilder();148Formatter f = new Formatter(sb, Locale.US);149f.format("=== Types ===%n");150f.format("%s%n", types.toString());151f.format("=== Methods ===%n");152f.format("%s%n", methods.toString());153f.format("=== Fields ===%n");154f.format("%s%n", fields.toString());155return sb.toString();156}157158public DeprData getTypeDeprecated(String typeName) {159return types.get(typeName);160}161162public DeprData getMethodDeprecated(String typeName, String methodName, String type) {163Map<String, DeprData> m = methods.get(typeName);164if (m == null) {165return null;166}167return m.get(methodName + type);168}169170public DeprData getFieldDeprecated(String typeName, String fieldName) {171Map<String, DeprData> f = fields.get(typeName);172if (f == null) {173return null;174}175return f.get(fieldName);176}177}178179180