src/share/classes/com/sun/tools/sjavac/comp/PubapiVisitor.java

changeset 1570
f91144b7da75
parent 1504
22e417cdddee
child 2227
998b10c43157
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/sjavac/comp/PubapiVisitor.java	Mon Feb 04 18:08:53 2013 -0500
     1.3 @@ -0,0 +1,157 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.sjavac.comp;
    1.30 +
    1.31 +import java.util.Iterator;
    1.32 +import java.util.List;
    1.33 +import javax.lang.model.element.Modifier;
    1.34 +import javax.lang.model.element.ExecutableElement;
    1.35 +import javax.lang.model.element.TypeElement;
    1.36 +import javax.lang.model.element.VariableElement;
    1.37 +import javax.lang.model.type.TypeMirror;
    1.38 +import javax.lang.model.util.ElementScanner6;
    1.39 +
    1.40 +/** Utility class that constructs a textual representation
    1.41 + * of the public api of a class.
    1.42 + *
    1.43 + * <p><b>This is NOT part of any supported API.
    1.44 + * If you write code that depends on this, you do so at your own
    1.45 + * risk.  This code and its internal interfaces are subject to change
    1.46 + * or deletion without notice.</b></p>
    1.47 + */
    1.48 +public class PubapiVisitor extends ElementScanner6<Void, Void> {
    1.49 +
    1.50 +    StringBuffer sb;
    1.51 +    // Important that it is 1! Part of protocol over wire, silly yes.
    1.52 +    // Fix please.
    1.53 +    int indent = 1;
    1.54 +
    1.55 +    public PubapiVisitor(StringBuffer sb) {
    1.56 +        this.sb = sb;
    1.57 +    }
    1.58 +
    1.59 +    String depth(int l) {
    1.60 +        return "                                              ".substring(0, l);
    1.61 +    }
    1.62 +
    1.63 +    @Override
    1.64 +    public Void visitType(TypeElement e, Void p) {
    1.65 +        if (e.getModifiers().contains(Modifier.PUBLIC)
    1.66 +            || e.getModifiers().contains(Modifier.PROTECTED))
    1.67 +        {
    1.68 +            sb.append(depth(indent) + "TYPE " + e.getQualifiedName() + "\n");
    1.69 +            indent += 2;
    1.70 +            Void v = super.visitType(e, p);
    1.71 +            indent -= 2;
    1.72 +            return v;
    1.73 +        }
    1.74 +        return null;
    1.75 +    }
    1.76 +
    1.77 +    @Override
    1.78 +    public Void visitVariable(VariableElement e, Void p) {
    1.79 +        if (e.getModifiers().contains(Modifier.PUBLIC)
    1.80 +            || e.getModifiers().contains(Modifier.PROTECTED)) {
    1.81 +            sb.append(depth(indent)).append("VAR ")
    1.82 +                    .append(makeVariableString(e)).append("\n");
    1.83 +        }
    1.84 +        // Safe to not recurse here, because the only thing
    1.85 +        // to visit here is the constructor of a variable declaration.
    1.86 +        // If it happens to contain an anonymous inner class (which it might)
    1.87 +        // then this class is never visible outside of the package anyway, so
    1.88 +        // we are allowed to ignore it here.
    1.89 +        return null;
    1.90 +    }
    1.91 +
    1.92 +    @Override
    1.93 +    public Void visitExecutable(ExecutableElement e, Void p) {
    1.94 +        if (e.getModifiers().contains(Modifier.PUBLIC)
    1.95 +            || e.getModifiers().contains(Modifier.PROTECTED)) {
    1.96 +            sb.append(depth(indent)).append("METHOD ")
    1.97 +                    .append(makeMethodString(e)).append("\n");
    1.98 +        }
    1.99 +        return null;
   1.100 +    }
   1.101 +
   1.102 +    /**
   1.103 +     * Creates a String representation of a method element with everything
   1.104 +     * necessary to track all public aspects of it in an API.
   1.105 +     * @param e Element to create String for.
   1.106 +     * @return String representation of element.
   1.107 +     */
   1.108 +    protected String makeMethodString(ExecutableElement e) {
   1.109 +        StringBuilder result = new StringBuilder();
   1.110 +        for (Modifier modifier : e.getModifiers()) {
   1.111 +            result.append(modifier.toString());
   1.112 +            result.append(" ");
   1.113 +        }
   1.114 +        result.append(e.getReturnType().toString());
   1.115 +        result.append(" ");
   1.116 +        result.append(e.toString());
   1.117 +
   1.118 +        List<? extends TypeMirror> thrownTypes = e.getThrownTypes();
   1.119 +        if (!thrownTypes.isEmpty()) {
   1.120 +            result.append(" throws ");
   1.121 +            for (Iterator<? extends TypeMirror> iterator = thrownTypes
   1.122 +                    .iterator(); iterator.hasNext();) {
   1.123 +                TypeMirror typeMirror = iterator.next();
   1.124 +                result.append(typeMirror.toString());
   1.125 +                if (iterator.hasNext()) {
   1.126 +                    result.append(", ");
   1.127 +                }
   1.128 +            }
   1.129 +        }
   1.130 +        return result.toString();
   1.131 +    }
   1.132 +
   1.133 +    /**
   1.134 +     * Creates a String representation of a variable element with everything
   1.135 +     * necessary to track all public aspects of it in an API.
   1.136 +     * @param e Element to create String for.
   1.137 +     * @return String representation of element.
   1.138 +     */
   1.139 +    protected String makeVariableString(VariableElement e) {
   1.140 +        StringBuilder result = new StringBuilder();
   1.141 +        for (Modifier modifier : e.getModifiers()) {
   1.142 +            result.append(modifier.toString());
   1.143 +            result.append(" ");
   1.144 +        }
   1.145 +        result.append(e.asType().toString());
   1.146 +        result.append(" ");
   1.147 +        result.append(e.toString());
   1.148 +        Object value = e.getConstantValue();
   1.149 +        if (value != null) {
   1.150 +            result.append(" = ");
   1.151 +            if (e.asType().toString().equals("char")) {
   1.152 +                int v = (int)value.toString().charAt(0);
   1.153 +                result.append("'\\u"+Integer.toString(v,16)+"'");
   1.154 +            } else {
   1.155 +                result.append(value.toString());
   1.156 +            }
   1.157 +        }
   1.158 +        return result.toString();
   1.159 +    }
   1.160 +}

mercurial