Fri, 12 Jul 2013 13:11:12 -0700
8020278: NPE in javadoc
Reviewed-by: mcimadamore, vromero
ohrstrom@1504 | 1 | /* |
ohrstrom@1504 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
ohrstrom@1504 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohrstrom@1504 | 4 | * |
ohrstrom@1504 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohrstrom@1504 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohrstrom@1504 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohrstrom@1504 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohrstrom@1504 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohrstrom@1504 | 10 | * |
ohrstrom@1504 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohrstrom@1504 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohrstrom@1504 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohrstrom@1504 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohrstrom@1504 | 15 | * accompanied this code). |
ohrstrom@1504 | 16 | * |
ohrstrom@1504 | 17 | * You should have received a copy of the GNU General Public License version |
ohrstrom@1504 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohrstrom@1504 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohrstrom@1504 | 20 | * |
ohrstrom@1504 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohrstrom@1504 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohrstrom@1504 | 23 | * questions. |
ohrstrom@1504 | 24 | */ |
ohrstrom@1504 | 25 | |
ohrstrom@1504 | 26 | package com.sun.tools.sjavac.comp; |
ohrstrom@1504 | 27 | |
ohrstrom@1504 | 28 | import java.util.Iterator; |
ohrstrom@1504 | 29 | import java.util.List; |
ohrstrom@1504 | 30 | import javax.lang.model.element.Modifier; |
ohrstrom@1504 | 31 | import javax.lang.model.element.ExecutableElement; |
ohrstrom@1504 | 32 | import javax.lang.model.element.TypeElement; |
ohrstrom@1504 | 33 | import javax.lang.model.element.VariableElement; |
ohrstrom@1504 | 34 | import javax.lang.model.type.TypeMirror; |
ohrstrom@1504 | 35 | import javax.lang.model.util.ElementScanner6; |
ohrstrom@1504 | 36 | |
ohrstrom@1504 | 37 | /** Utility class that constructs a textual representation |
ohrstrom@1504 | 38 | * of the public api of a class. |
ohrstrom@1504 | 39 | * |
ohrstrom@1504 | 40 | * <p><b>This is NOT part of any supported API. |
ohrstrom@1504 | 41 | * If you write code that depends on this, you do so at your own |
ohrstrom@1504 | 42 | * risk. This code and its internal interfaces are subject to change |
ohrstrom@1504 | 43 | * or deletion without notice.</b></p> |
ohrstrom@1504 | 44 | */ |
ohrstrom@1504 | 45 | public class PubapiVisitor extends ElementScanner6<Void, Void> { |
ohrstrom@1504 | 46 | |
ohrstrom@1504 | 47 | StringBuffer sb; |
ohrstrom@1504 | 48 | // Important that it is 1! Part of protocol over wire, silly yes. |
ohrstrom@1504 | 49 | // Fix please. |
ohrstrom@1504 | 50 | int indent = 1; |
ohrstrom@1504 | 51 | |
ohrstrom@1504 | 52 | public PubapiVisitor(StringBuffer sb) { |
ohrstrom@1504 | 53 | this.sb = sb; |
ohrstrom@1504 | 54 | } |
ohrstrom@1504 | 55 | |
ohrstrom@1504 | 56 | String depth(int l) { |
ohrstrom@1504 | 57 | return " ".substring(0, l); |
ohrstrom@1504 | 58 | } |
ohrstrom@1504 | 59 | |
ohrstrom@1504 | 60 | @Override |
ohrstrom@1504 | 61 | public Void visitType(TypeElement e, Void p) { |
ohrstrom@1504 | 62 | if (e.getModifiers().contains(Modifier.PUBLIC) |
ohrstrom@1504 | 63 | || e.getModifiers().contains(Modifier.PROTECTED)) |
ohrstrom@1504 | 64 | { |
ohrstrom@1504 | 65 | sb.append(depth(indent) + "TYPE " + e.getQualifiedName() + "\n"); |
ohrstrom@1504 | 66 | indent += 2; |
ohrstrom@1504 | 67 | Void v = super.visitType(e, p); |
ohrstrom@1504 | 68 | indent -= 2; |
ohrstrom@1504 | 69 | return v; |
ohrstrom@1504 | 70 | } |
ohrstrom@1504 | 71 | return null; |
ohrstrom@1504 | 72 | } |
ohrstrom@1504 | 73 | |
ohrstrom@1504 | 74 | @Override |
ohrstrom@1504 | 75 | public Void visitVariable(VariableElement e, Void p) { |
ohrstrom@1504 | 76 | if (e.getModifiers().contains(Modifier.PUBLIC) |
ohrstrom@1504 | 77 | || e.getModifiers().contains(Modifier.PROTECTED)) { |
ohrstrom@1504 | 78 | sb.append(depth(indent)).append("VAR ") |
ohrstrom@1504 | 79 | .append(makeVariableString(e)).append("\n"); |
ohrstrom@1504 | 80 | } |
ohrstrom@1504 | 81 | // Safe to not recurse here, because the only thing |
ohrstrom@1504 | 82 | // to visit here is the constructor of a variable declaration. |
ohrstrom@1504 | 83 | // If it happens to contain an anonymous inner class (which it might) |
ohrstrom@1504 | 84 | // then this class is never visible outside of the package anyway, so |
ohrstrom@1504 | 85 | // we are allowed to ignore it here. |
ohrstrom@1504 | 86 | return null; |
ohrstrom@1504 | 87 | } |
ohrstrom@1504 | 88 | |
ohrstrom@1504 | 89 | @Override |
ohrstrom@1504 | 90 | public Void visitExecutable(ExecutableElement e, Void p) { |
ohrstrom@1504 | 91 | if (e.getModifiers().contains(Modifier.PUBLIC) |
ohrstrom@1504 | 92 | || e.getModifiers().contains(Modifier.PROTECTED)) { |
ohrstrom@1504 | 93 | sb.append(depth(indent)).append("METHOD ") |
ohrstrom@1504 | 94 | .append(makeMethodString(e)).append("\n"); |
ohrstrom@1504 | 95 | } |
ohrstrom@1504 | 96 | return null; |
ohrstrom@1504 | 97 | } |
ohrstrom@1504 | 98 | |
ohrstrom@1504 | 99 | /** |
ohrstrom@1504 | 100 | * Creates a String representation of a method element with everything |
ohrstrom@1504 | 101 | * necessary to track all public aspects of it in an API. |
ohrstrom@1504 | 102 | * @param e Element to create String for. |
ohrstrom@1504 | 103 | * @return String representation of element. |
ohrstrom@1504 | 104 | */ |
ohrstrom@1504 | 105 | protected String makeMethodString(ExecutableElement e) { |
ohrstrom@1504 | 106 | StringBuilder result = new StringBuilder(); |
ohrstrom@1504 | 107 | for (Modifier modifier : e.getModifiers()) { |
ohrstrom@1504 | 108 | result.append(modifier.toString()); |
ohrstrom@1504 | 109 | result.append(" "); |
ohrstrom@1504 | 110 | } |
ohrstrom@1504 | 111 | result.append(e.getReturnType().toString()); |
ohrstrom@1504 | 112 | result.append(" "); |
ohrstrom@1504 | 113 | result.append(e.toString()); |
ohrstrom@1504 | 114 | |
ohrstrom@1504 | 115 | List<? extends TypeMirror> thrownTypes = e.getThrownTypes(); |
ohrstrom@1504 | 116 | if (!thrownTypes.isEmpty()) { |
ohrstrom@1504 | 117 | result.append(" throws "); |
ohrstrom@1504 | 118 | for (Iterator<? extends TypeMirror> iterator = thrownTypes |
ohrstrom@1504 | 119 | .iterator(); iterator.hasNext();) { |
ohrstrom@1504 | 120 | TypeMirror typeMirror = iterator.next(); |
ohrstrom@1504 | 121 | result.append(typeMirror.toString()); |
ohrstrom@1504 | 122 | if (iterator.hasNext()) { |
ohrstrom@1504 | 123 | result.append(", "); |
ohrstrom@1504 | 124 | } |
ohrstrom@1504 | 125 | } |
ohrstrom@1504 | 126 | } |
ohrstrom@1504 | 127 | return result.toString(); |
ohrstrom@1504 | 128 | } |
ohrstrom@1504 | 129 | |
ohrstrom@1504 | 130 | /** |
ohrstrom@1504 | 131 | * Creates a String representation of a variable element with everything |
ohrstrom@1504 | 132 | * necessary to track all public aspects of it in an API. |
ohrstrom@1504 | 133 | * @param e Element to create String for. |
ohrstrom@1504 | 134 | * @return String representation of element. |
ohrstrom@1504 | 135 | */ |
ohrstrom@1504 | 136 | protected String makeVariableString(VariableElement e) { |
ohrstrom@1504 | 137 | StringBuilder result = new StringBuilder(); |
ohrstrom@1504 | 138 | for (Modifier modifier : e.getModifiers()) { |
ohrstrom@1504 | 139 | result.append(modifier.toString()); |
ohrstrom@1504 | 140 | result.append(" "); |
ohrstrom@1504 | 141 | } |
ohrstrom@1504 | 142 | result.append(e.asType().toString()); |
ohrstrom@1504 | 143 | result.append(" "); |
ohrstrom@1504 | 144 | result.append(e.toString()); |
ohrstrom@1504 | 145 | Object value = e.getConstantValue(); |
ohrstrom@1504 | 146 | if (value != null) { |
ohrstrom@1504 | 147 | result.append(" = "); |
ohrstrom@1504 | 148 | if (e.asType().toString().equals("char")) { |
ohrstrom@1504 | 149 | int v = (int)value.toString().charAt(0); |
ohrstrom@1504 | 150 | result.append("'\\u"+Integer.toString(v,16)+"'"); |
ohrstrom@1504 | 151 | } else { |
ohrstrom@1504 | 152 | result.append(value.toString()); |
ohrstrom@1504 | 153 | } |
ohrstrom@1504 | 154 | } |
ohrstrom@1504 | 155 | return result.toString(); |
ohrstrom@1504 | 156 | } |
ohrstrom@1504 | 157 | } |