Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/jdeprscan/LoadProc.java
41161 views
/*1* Copyright (c) 2016, 2018, 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.lang.annotation.IncompleteAnnotationException;28import java.util.ArrayList;29import java.util.List;30import java.util.Set;31import java.util.stream.Collectors;3233import javax.annotation.processing.AbstractProcessor;34import javax.annotation.processing.Messager;35import javax.annotation.processing.ProcessingEnvironment;36import javax.annotation.processing.RoundEnvironment;37import javax.annotation.processing.SupportedAnnotationTypes;38import javax.annotation.processing.SupportedSourceVersion;3940import javax.lang.model.SourceVersion;41import javax.lang.model.element.Element;42import javax.lang.model.element.ElementKind;43import javax.lang.model.element.ExecutableElement;44import javax.lang.model.element.TypeElement;45import javax.lang.model.type.ArrayType;46import javax.lang.model.type.DeclaredType;47import javax.lang.model.type.ExecutableType;48import javax.lang.model.type.TypeMirror;49import javax.lang.model.util.Elements;5051import javax.tools.Diagnostic;5253/**54* Annotation processor for the Deprecation Scanner tool.55* Examines APIs for deprecated elements and records information56*57*/58@SupportedAnnotationTypes("java.lang.Deprecated")59public class LoadProc extends AbstractProcessor {60Elements elements;61Messager messager;62final List<DeprData> deprList = new ArrayList<>();6364public LoadProc() {65}6667@Override68public void init(ProcessingEnvironment pe) {69super.init(pe);70elements = pe.getElementUtils();71messager = pe.getMessager();72}7374@Override75public SourceVersion getSupportedSourceVersion() {76return SourceVersion.latest();77}7879@Override80public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {81if (roundEnv.processingOver()) {82return false;83}8485// Assume annotations contains only @Deprecated.86// Note: no way to get deprecated packages, since87// @Deprecated is ignored in package-info.java files.8889Set<? extends Element> set = roundEnv.getElementsAnnotatedWith(Deprecated.class);90for (Element e : set) {91ElementKind kind = e.getKind();92Deprecated depr = e.getAnnotation(Deprecated.class);93switch (kind) {94case CLASS:95case INTERFACE:96case ENUM:97case ANNOTATION_TYPE:98addType(kind, (TypeElement)e, depr);99break;100case CONSTRUCTOR:101case ENUM_CONSTANT:102case FIELD:103case METHOD:104Element encl = e.getEnclosingElement();105ElementKind enclKind = encl.getKind();106switch (enclKind) {107case CLASS:108case INTERFACE:109case ENUM:110case ANNOTATION_TYPE:111String detail = getDetail(e);112addMember(kind, (TypeElement)encl, detail, depr);113break;114default:115messager.printMessage(Diagnostic.Kind.WARNING,116"element " + e +117" within unknown enclosing element " + encl +118" of kind " + enclKind, e);119break;120}121break;122default:123messager.printMessage(Diagnostic.Kind.WARNING,124"unknown element " + e +125" of kind " + kind +126" within " + e.getEnclosingElement(), e);127break;128}129}130return true;131}132133public List<DeprData> getDeprecations() {134return deprList;135}136137String getDetail(Element e) {138if (e.getKind().isField()) {139return e.getSimpleName().toString();140} else {141// method or constructor142ExecutableElement ee = (ExecutableElement) e;143String ret;144ret = desc(ee.getReturnType());145List<? extends TypeMirror> parameterTypes = ((ExecutableType)ee.asType()).getParameterTypes();146String parms = parameterTypes.stream()147.map(this::desc)148.collect(Collectors.joining());149return ee.getSimpleName().toString() + "(" + parms + ")" + ret;150}151}152153String desc(TypeMirror tm) {154switch (tm.getKind()) {155case BOOLEAN:156return "Z";157case BYTE:158return "B";159case SHORT:160return "S";161case CHAR:162return "C";163case INT:164return "I";165case LONG:166return "J";167case FLOAT:168return "F";169case DOUBLE:170return "D";171case VOID:172return "V";173case DECLARED:174String s =175((TypeElement)((DeclaredType)tm).asElement()).getQualifiedName().toString();176s = s.replace('.', '/');177return "L" + s + ";";178case ARRAY:179return "[" + desc(((ArrayType)tm).getComponentType());180default:181return tm.getKind().toString();182}183}184185void addType(ElementKind kind, TypeElement type, Deprecated dep) {186addData(kind, type, "", dep);187}188189void addMember(ElementKind kind, TypeElement type, String nameSig, Deprecated dep) {190addData(kind, type, nameSig, dep);191}192193void addData(ElementKind kind, TypeElement type, String nameSig, Deprecated dep) {194String typeName = elements.getBinaryName(type).toString().replace('.', '/');195196String since = "";197try {198since = dep.since();199} catch (IncompleteAnnotationException ignore) { }200201boolean forRemoval = false;202try {203forRemoval = dep.forRemoval();204} catch (IncompleteAnnotationException ignore) { }205206deprList.add(new DeprData(kind, type, typeName, nameSig, since, forRemoval));207}208}209210211