src/share/classes/com/sun/tools/javac/code/Attribute.java

changeset 1
9a66ca7c79fa
child 104
5e89c4ca637c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Attribute.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,284 @@
     1.4 +/*
     1.5 + * Copyright 2003-2005 Sun Microsystems, Inc.  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.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.code;
    1.30 +
    1.31 +import java.util.LinkedHashMap;
    1.32 +import java.util.Map;
    1.33 +import javax.lang.model.element.AnnotationMirror;
    1.34 +import javax.lang.model.element.AnnotationValue;
    1.35 +import javax.lang.model.element.AnnotationValueVisitor;
    1.36 +import javax.lang.model.element.ExecutableElement;
    1.37 +import javax.lang.model.type.DeclaredType;
    1.38 +import com.sun.tools.javac.code.Symbol.*;
    1.39 +import com.sun.tools.javac.util.*;
    1.40 +
    1.41 +import static com.sun.tools.javac.code.TypeTags.*;
    1.42 +
    1.43 +/** An annotation value.
    1.44 + *
    1.45 + *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.46 + *  you write code that depends on this, you do so at your own risk.
    1.47 + *  This code and its internal interfaces are subject to change or
    1.48 + *  deletion without notice.</b>
    1.49 + */
    1.50 +public abstract class Attribute implements AnnotationValue {
    1.51 +
    1.52 +    /** The type of the annotation element. */
    1.53 +    public Type type;
    1.54 +
    1.55 +    public Attribute(Type type) {
    1.56 +        this.type = type;
    1.57 +    }
    1.58 +
    1.59 +    public abstract void accept(Visitor v);
    1.60 +
    1.61 +    public Object getValue() {
    1.62 +        throw new UnsupportedOperationException();
    1.63 +    }
    1.64 +
    1.65 +    public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
    1.66 +        throw new UnsupportedOperationException();
    1.67 +    }
    1.68 +
    1.69 +
    1.70 +    /** The value for an annotation element of primitive type or String. */
    1.71 +    public static class Constant extends Attribute {
    1.72 +        public final Object value;
    1.73 +        public void accept(Visitor v) { v.visitConstant(this); }
    1.74 +        public Constant(Type type, Object value) {
    1.75 +            super(type);
    1.76 +            this.value = value;
    1.77 +        }
    1.78 +        public String toString() {
    1.79 +            return Constants.format(value, type);
    1.80 +        }
    1.81 +        public Object getValue() {
    1.82 +            return Constants.decode(value, type);
    1.83 +        }
    1.84 +        public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
    1.85 +            if (value instanceof String)
    1.86 +                return v.visitString((String) value, p);
    1.87 +            if (value instanceof Integer) {
    1.88 +                int i = (Integer) value;
    1.89 +                switch (type.tag) {
    1.90 +                case BOOLEAN:   return v.visitBoolean(i != 0, p);
    1.91 +                case CHAR:      return v.visitChar((char) i, p);
    1.92 +                case BYTE:      return v.visitByte((byte) i, p);
    1.93 +                case SHORT:     return v.visitShort((short) i, p);
    1.94 +                case INT:       return v.visitInt(i, p);
    1.95 +                }
    1.96 +            }
    1.97 +            switch (type.tag) {
    1.98 +            case LONG:          return v.visitLong((Long) value, p);
    1.99 +            case FLOAT:         return v.visitFloat((Float) value, p);
   1.100 +            case DOUBLE:        return v.visitDouble((Double) value, p);
   1.101 +            }
   1.102 +            throw new AssertionError("Bad annotation element value: " + value);
   1.103 +        }
   1.104 +    }
   1.105 +
   1.106 +    /** The value for an annotation element of type java.lang.Class,
   1.107 +     *  represented as a ClassSymbol.
   1.108 +     */
   1.109 +    public static class Class extends Attribute {
   1.110 +        public final Type type;
   1.111 +        public void accept(Visitor v) { v.visitClass(this); }
   1.112 +        public Class(Types types, Type type) {
   1.113 +            super(makeClassType(types, type));
   1.114 +            this.type = type;
   1.115 +        }
   1.116 +        static Type makeClassType(Types types, Type type) {
   1.117 +            Type arg = type.isPrimitive()
   1.118 +                ? types.boxedClass(type).type
   1.119 +                : types.erasure(type);
   1.120 +            return new Type.ClassType(types.syms.classType.getEnclosingType(),
   1.121 +                                      List.of(arg),
   1.122 +                                      types.syms.classType.tsym);
   1.123 +        }
   1.124 +        public String toString() {
   1.125 +            return type + ".class";
   1.126 +        }
   1.127 +        public Type getValue() {
   1.128 +            return type;
   1.129 +        }
   1.130 +        public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
   1.131 +            return v.visitType(type, p);
   1.132 +        }
   1.133 +    }
   1.134 +
   1.135 +    /** A compound annotation element value, the type of which is an
   1.136 +     *  attribute interface.
   1.137 +     */
   1.138 +    public static class Compound extends Attribute implements AnnotationMirror {
   1.139 +        /** The attributes values, as pairs.  Each pair contains a
   1.140 +         *  reference to the accessing method in the attribute interface
   1.141 +         *  and the value to be returned when that method is called to
   1.142 +         *  access this attribute.
   1.143 +         */
   1.144 +        public final List<Pair<MethodSymbol,Attribute>> values;
   1.145 +        public Compound(Type type,
   1.146 +                        List<Pair<MethodSymbol,Attribute>> values) {
   1.147 +            super(type);
   1.148 +            this.values = values;
   1.149 +        }
   1.150 +        public void accept(Visitor v) { v.visitCompound(this); }
   1.151 +
   1.152 +        /**
   1.153 +         * Returns a string representation of this annotation.
   1.154 +         * String is of one of the forms:
   1.155 +         *     @com.example.foo(name1=val1, name2=val2)
   1.156 +         *     @com.example.foo(val)
   1.157 +         *     @com.example.foo
   1.158 +         * Omit parens for marker annotations, and omit "value=" when allowed.
   1.159 +         */
   1.160 +        public String toString() {
   1.161 +            StringBuilder buf = new StringBuilder();
   1.162 +            buf.append("@");
   1.163 +            buf.append(type);
   1.164 +            int len = values.length();
   1.165 +            if (len > 0) {
   1.166 +                buf.append('(');
   1.167 +                boolean first = true;
   1.168 +                for (Pair<MethodSymbol, Attribute> value : values) {
   1.169 +                    if (!first) buf.append(", ");
   1.170 +                    first = false;
   1.171 +
   1.172 +                    Name name = value.fst.name;
   1.173 +                    if (len > 1 || name != name.table.value) {
   1.174 +                        buf.append(name);
   1.175 +                        buf.append('=');
   1.176 +                    }
   1.177 +                    buf.append(value.snd);
   1.178 +                }
   1.179 +                buf.append(')');
   1.180 +            }
   1.181 +            return buf.toString();
   1.182 +        }
   1.183 +
   1.184 +        public Attribute member(Name member) {
   1.185 +            for (Pair<MethodSymbol,Attribute> pair : values)
   1.186 +                if (pair.fst.name == member) return pair.snd;
   1.187 +            return null;
   1.188 +        }
   1.189 +
   1.190 +        public Attribute.Compound getValue() {
   1.191 +            return this;
   1.192 +        }
   1.193 +
   1.194 +        public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
   1.195 +            return v.visitAnnotation(this, p);
   1.196 +        }
   1.197 +
   1.198 +        public DeclaredType getAnnotationType() {
   1.199 +            return (DeclaredType) type;
   1.200 +        }
   1.201 +
   1.202 +        public Map<MethodSymbol, Attribute> getElementValues() {
   1.203 +            Map<MethodSymbol, Attribute> valmap =
   1.204 +                new LinkedHashMap<MethodSymbol, Attribute>();
   1.205 +            for (Pair<MethodSymbol, Attribute> value : values)
   1.206 +                valmap.put(value.fst, value.snd);
   1.207 +            return valmap;
   1.208 +        }
   1.209 +    }
   1.210 +
   1.211 +    /** The value for an annotation element of an array type.
   1.212 +     */
   1.213 +    public static class Array extends Attribute {
   1.214 +        public final Attribute[] values;
   1.215 +        public Array(Type type, Attribute[] values) {
   1.216 +            super(type);
   1.217 +            this.values = values;
   1.218 +        }
   1.219 +        public void accept(Visitor v) { v.visitArray(this); }
   1.220 +        public String toString() {
   1.221 +            StringBuilder buf = new StringBuilder();
   1.222 +            buf.append('{');
   1.223 +            boolean first = true;
   1.224 +            for (Attribute value : values) {
   1.225 +                if (!first)
   1.226 +                    buf.append(", ");
   1.227 +                first = false;
   1.228 +                buf.append(value);
   1.229 +            }
   1.230 +            buf.append('}');
   1.231 +            return buf.toString();
   1.232 +        }
   1.233 +        public List<Attribute> getValue() {
   1.234 +            return List.from(values);
   1.235 +        }
   1.236 +        public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
   1.237 +            return v.visitArray(getValue(), p);
   1.238 +        }
   1.239 +    }
   1.240 +
   1.241 +    /** The value for an annotation element of an enum type.
   1.242 +     */
   1.243 +    public static class Enum extends Attribute {
   1.244 +        public VarSymbol value;
   1.245 +        public Enum(Type type, VarSymbol value) {
   1.246 +            super(type);
   1.247 +            assert value != null;
   1.248 +            this.value = value;
   1.249 +        }
   1.250 +        public void accept(Visitor v) { v.visitEnum(this); }
   1.251 +        public String toString() {
   1.252 +            return value.enclClass() + "." + value;     // qualified name
   1.253 +        }
   1.254 +        public VarSymbol getValue() {
   1.255 +            return value;
   1.256 +        }
   1.257 +        public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
   1.258 +            return v.visitEnumConstant(value, p);
   1.259 +        }
   1.260 +    }
   1.261 +
   1.262 +    public static class Error extends Attribute {
   1.263 +        public Error(Type type) {
   1.264 +            super(type);
   1.265 +        }
   1.266 +        public void accept(Visitor v) { v.visitError(this); }
   1.267 +        public String toString() {
   1.268 +            return "<error>";
   1.269 +        }
   1.270 +        public String getValue() {
   1.271 +            return toString();
   1.272 +        }
   1.273 +        public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
   1.274 +            return v.visitString(toString(), p);
   1.275 +        }
   1.276 +    }
   1.277 +
   1.278 +    /** A visitor type for dynamic dispatch on the kind of attribute value. */
   1.279 +    public static interface Visitor {
   1.280 +        void visitConstant(Attribute.Constant value);
   1.281 +        void visitClass(Attribute.Class clazz);
   1.282 +        void visitCompound(Attribute.Compound compound);
   1.283 +        void visitArray(Attribute.Array array);
   1.284 +        void visitEnum(Attribute.Enum e);
   1.285 +        void visitError(Attribute.Error e);
   1.286 +    }
   1.287 +}

mercurial