1 /* 2 * Copyright (c) 2011, 2015, 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.sjavac.comp; 27 28 import static javax.lang.model.element.Modifier.PRIVATE; 29 30 import java.util.List; 31 import java.util.stream.Collectors; 32 33 import javax.lang.model.element.Element; 34 import javax.lang.model.element.ExecutableElement; 35 import javax.lang.model.element.TypeElement; 36 import javax.lang.model.element.TypeParameterElement; 37 import javax.lang.model.element.VariableElement; 38 import javax.lang.model.type.TypeMirror; 39 import javax.lang.model.util.ElementScanner14; 40 41 import com.sun.tools.javac.code.Symbol.ClassSymbol; 42 import com.sun.tools.javac.util.DefinedBy; 43 import com.sun.tools.javac.util.DefinedBy.Api; 44 import com.sun.tools.sjavac.pubapi.PubApi; 45 import com.sun.tools.sjavac.pubapi.PubApiTypeParam; 46 import com.sun.tools.sjavac.pubapi.PubMethod; 47 import com.sun.tools.sjavac.pubapi.PubType; 48 import com.sun.tools.sjavac.pubapi.PubVar; 49 import com.sun.tools.sjavac.pubapi.TypeDesc; 50 51 /** Utility class that constructs a textual representation 52 * of the public api of a class. 53 * 54 * <p><b>This is NOT part of any supported API. 55 * If you write code that depends on this, you do so at your own risk. 56 * This code and its internal interfaces are subject to change or 57 * deletion without notice.</b> 58 */ 59 @SuppressWarnings("preview") 60 public class PubapiVisitor extends ElementScanner14<Void, Void> { 61 62 private PubApi collectedApi = new PubApi(); 63 64 private boolean isNonPrivate(Element e) { 65 return !e.getModifiers().contains(PRIVATE); 66 } 67 68 @Override @DefinedBy(Api.LANGUAGE_MODEL) 69 public Void visitType(TypeElement e, Void p) { 70 if (isNonPrivate(e)) { 71 PubApi prevApi = collectedApi; 72 collectedApi = new PubApi(); 73 super.visitType(e, p); 74 if (!isAnonymous(e)) { 75 String name = ((ClassSymbol) e).flatname.toString(); 76 PubType t = new PubType(e.getModifiers(), 77 name, 78 //e.getQualifiedName().toString(), 79 collectedApi); 80 prevApi.types.put(t.fqName, t); 81 } 82 collectedApi = prevApi; 83 } 84 return null; 85 } 86 87 private boolean isAnonymous(TypeElement e) { 88 return e.getQualifiedName().length() == 0; 89 } 90 91 private static String encodeChar(int c) { 92 return String.format("\\u%04x", c); 93 } 94 95 @Override @DefinedBy(Api.LANGUAGE_MODEL) 96 public Void visitVariable(VariableElement e, Void p) { 97 if (isNonPrivate(e)) { 98 Object constVal = e.getConstantValue(); 99 String constValStr = null; 100 // TODO: This doesn't seem to be entirely accurate. What if I change 101 // from, say, 0 to 0L? (And the field is public final static so that 102 // it could get inlined.) 103 if (constVal != null) { 104 if (e.asType().toString().equals("char")) { 105 // What type is 'value'? Is it already a char? 106 char c = constVal.toString().charAt(0); 107 constValStr = "'" + encodeChar(c) + "'"; 108 } else { 109 constValStr = constVal.toString() 110 .chars() 111 .mapToObj(PubapiVisitor::encodeChar) 112 .collect(Collectors.joining("", "\"", "\"")); 113 } 114 } 115 116 PubVar v = new PubVar(e.getModifiers(), 117 TypeDesc.fromType(e.asType()), 118 e.toString(), 119 constValStr); 120 collectedApi.variables.put(v.identifier, v); 121 } 122 123 // Safe to not recurse here, because the only thing 124 // to visit here is the constructor of a variable declaration. 125 // If it happens to contain an anonymous inner class (which it might) 126 // then this class is never visible outside of the package anyway, so 127 // we are allowed to ignore it here. 128 return null; 129 } 130 131 @Override @DefinedBy(Api.LANGUAGE_MODEL) 132 public Void visitExecutable(ExecutableElement e, Void p) { 133 if (isNonPrivate(e)) { 134 PubMethod m = new PubMethod(e.getModifiers(), 135 getTypeParameters(e.getTypeParameters()), 136 TypeDesc.fromType(e.getReturnType()), 137 e.getSimpleName().toString(), 138 getTypeDescs(getParamTypes(e)), 139 getTypeDescs(e.getThrownTypes())); 140 collectedApi.methods.put(m.asSignatureString(), m); 141 } 142 return null; 143 } 144 145 private List<PubApiTypeParam> getTypeParameters(List<? extends TypeParameterElement> elements) { 146 return elements.stream() 147 .map(e -> new PubApiTypeParam(e.getSimpleName().toString(), getTypeDescs(e.getBounds()))) 148 .collect(Collectors.toList()); 149 } 150 151 private List<TypeMirror> getParamTypes(ExecutableElement e) { 152 return e.getParameters() 153 .stream() 154 .map(VariableElement::asType) 155 .collect(Collectors.toList()); 156 } 157 158 private List<TypeDesc> getTypeDescs(List<? extends TypeMirror> list) { 159 return list.stream() 160 .map(TypeDesc::fromType) 161 .collect(Collectors.toList()); 162 } 163 164 public PubApi getCollectedPubApi() { 165 return collectedApi; 166 } 167 }