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