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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 104
5e89c4ca637c
permissions
-rw-r--r--

Initial load

     1 /*
     2  * Copyright 2003-2005 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javac.code;
    28 import java.util.LinkedHashMap;
    29 import java.util.Map;
    30 import javax.lang.model.element.AnnotationMirror;
    31 import javax.lang.model.element.AnnotationValue;
    32 import javax.lang.model.element.AnnotationValueVisitor;
    33 import javax.lang.model.element.ExecutableElement;
    34 import javax.lang.model.type.DeclaredType;
    35 import com.sun.tools.javac.code.Symbol.*;
    36 import com.sun.tools.javac.util.*;
    38 import static com.sun.tools.javac.code.TypeTags.*;
    40 /** An annotation value.
    41  *
    42  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    43  *  you write code that depends on this, you do so at your own risk.
    44  *  This code and its internal interfaces are subject to change or
    45  *  deletion without notice.</b>
    46  */
    47 public abstract class Attribute implements AnnotationValue {
    49     /** The type of the annotation element. */
    50     public Type type;
    52     public Attribute(Type type) {
    53         this.type = type;
    54     }
    56     public abstract void accept(Visitor v);
    58     public Object getValue() {
    59         throw new UnsupportedOperationException();
    60     }
    62     public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
    63         throw new UnsupportedOperationException();
    64     }
    67     /** The value for an annotation element of primitive type or String. */
    68     public static class Constant extends Attribute {
    69         public final Object value;
    70         public void accept(Visitor v) { v.visitConstant(this); }
    71         public Constant(Type type, Object value) {
    72             super(type);
    73             this.value = value;
    74         }
    75         public String toString() {
    76             return Constants.format(value, type);
    77         }
    78         public Object getValue() {
    79             return Constants.decode(value, type);
    80         }
    81         public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
    82             if (value instanceof String)
    83                 return v.visitString((String) value, p);
    84             if (value instanceof Integer) {
    85                 int i = (Integer) value;
    86                 switch (type.tag) {
    87                 case BOOLEAN:   return v.visitBoolean(i != 0, p);
    88                 case CHAR:      return v.visitChar((char) i, p);
    89                 case BYTE:      return v.visitByte((byte) i, p);
    90                 case SHORT:     return v.visitShort((short) i, p);
    91                 case INT:       return v.visitInt(i, p);
    92                 }
    93             }
    94             switch (type.tag) {
    95             case LONG:          return v.visitLong((Long) value, p);
    96             case FLOAT:         return v.visitFloat((Float) value, p);
    97             case DOUBLE:        return v.visitDouble((Double) value, p);
    98             }
    99             throw new AssertionError("Bad annotation element value: " + value);
   100         }
   101     }
   103     /** The value for an annotation element of type java.lang.Class,
   104      *  represented as a ClassSymbol.
   105      */
   106     public static class Class extends Attribute {
   107         public final Type type;
   108         public void accept(Visitor v) { v.visitClass(this); }
   109         public Class(Types types, Type type) {
   110             super(makeClassType(types, type));
   111             this.type = type;
   112         }
   113         static Type makeClassType(Types types, Type type) {
   114             Type arg = type.isPrimitive()
   115                 ? types.boxedClass(type).type
   116                 : types.erasure(type);
   117             return new Type.ClassType(types.syms.classType.getEnclosingType(),
   118                                       List.of(arg),
   119                                       types.syms.classType.tsym);
   120         }
   121         public String toString() {
   122             return type + ".class";
   123         }
   124         public Type getValue() {
   125             return type;
   126         }
   127         public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
   128             return v.visitType(type, p);
   129         }
   130     }
   132     /** A compound annotation element value, the type of which is an
   133      *  attribute interface.
   134      */
   135     public static class Compound extends Attribute implements AnnotationMirror {
   136         /** The attributes values, as pairs.  Each pair contains a
   137          *  reference to the accessing method in the attribute interface
   138          *  and the value to be returned when that method is called to
   139          *  access this attribute.
   140          */
   141         public final List<Pair<MethodSymbol,Attribute>> values;
   142         public Compound(Type type,
   143                         List<Pair<MethodSymbol,Attribute>> values) {
   144             super(type);
   145             this.values = values;
   146         }
   147         public void accept(Visitor v) { v.visitCompound(this); }
   149         /**
   150          * Returns a string representation of this annotation.
   151          * String is of one of the forms:
   152          *     @com.example.foo(name1=val1, name2=val2)
   153          *     @com.example.foo(val)
   154          *     @com.example.foo
   155          * Omit parens for marker annotations, and omit "value=" when allowed.
   156          */
   157         public String toString() {
   158             StringBuilder buf = new StringBuilder();
   159             buf.append("@");
   160             buf.append(type);
   161             int len = values.length();
   162             if (len > 0) {
   163                 buf.append('(');
   164                 boolean first = true;
   165                 for (Pair<MethodSymbol, Attribute> value : values) {
   166                     if (!first) buf.append(", ");
   167                     first = false;
   169                     Name name = value.fst.name;
   170                     if (len > 1 || name != name.table.value) {
   171                         buf.append(name);
   172                         buf.append('=');
   173                     }
   174                     buf.append(value.snd);
   175                 }
   176                 buf.append(')');
   177             }
   178             return buf.toString();
   179         }
   181         public Attribute member(Name member) {
   182             for (Pair<MethodSymbol,Attribute> pair : values)
   183                 if (pair.fst.name == member) return pair.snd;
   184             return null;
   185         }
   187         public Attribute.Compound getValue() {
   188             return this;
   189         }
   191         public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
   192             return v.visitAnnotation(this, p);
   193         }
   195         public DeclaredType getAnnotationType() {
   196             return (DeclaredType) type;
   197         }
   199         public Map<MethodSymbol, Attribute> getElementValues() {
   200             Map<MethodSymbol, Attribute> valmap =
   201                 new LinkedHashMap<MethodSymbol, Attribute>();
   202             for (Pair<MethodSymbol, Attribute> value : values)
   203                 valmap.put(value.fst, value.snd);
   204             return valmap;
   205         }
   206     }
   208     /** The value for an annotation element of an array type.
   209      */
   210     public static class Array extends Attribute {
   211         public final Attribute[] values;
   212         public Array(Type type, Attribute[] values) {
   213             super(type);
   214             this.values = values;
   215         }
   216         public void accept(Visitor v) { v.visitArray(this); }
   217         public String toString() {
   218             StringBuilder buf = new StringBuilder();
   219             buf.append('{');
   220             boolean first = true;
   221             for (Attribute value : values) {
   222                 if (!first)
   223                     buf.append(", ");
   224                 first = false;
   225                 buf.append(value);
   226             }
   227             buf.append('}');
   228             return buf.toString();
   229         }
   230         public List<Attribute> getValue() {
   231             return List.from(values);
   232         }
   233         public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
   234             return v.visitArray(getValue(), p);
   235         }
   236     }
   238     /** The value for an annotation element of an enum type.
   239      */
   240     public static class Enum extends Attribute {
   241         public VarSymbol value;
   242         public Enum(Type type, VarSymbol value) {
   243             super(type);
   244             assert value != null;
   245             this.value = value;
   246         }
   247         public void accept(Visitor v) { v.visitEnum(this); }
   248         public String toString() {
   249             return value.enclClass() + "." + value;     // qualified name
   250         }
   251         public VarSymbol getValue() {
   252             return value;
   253         }
   254         public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
   255             return v.visitEnumConstant(value, p);
   256         }
   257     }
   259     public static class Error extends Attribute {
   260         public Error(Type type) {
   261             super(type);
   262         }
   263         public void accept(Visitor v) { v.visitError(this); }
   264         public String toString() {
   265             return "<error>";
   266         }
   267         public String getValue() {
   268             return toString();
   269         }
   270         public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
   271             return v.visitString(toString(), p);
   272         }
   273     }
   275     /** A visitor type for dynamic dispatch on the kind of attribute value. */
   276     public static interface Visitor {
   277         void visitConstant(Attribute.Constant value);
   278         void visitClass(Attribute.Class clazz);
   279         void visitCompound(Attribute.Compound compound);
   280         void visitArray(Attribute.Array array);
   281         void visitEnum(Attribute.Enum e);
   282         void visitError(Attribute.Error e);
   283     }
   284 }

mercurial