1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javadoc/AnnotationValueImpl.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,172 @@ 1.4 +/* 1.5 + * Copyright 2003-2004 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.javadoc; 1.30 + 1.31 + 1.32 +import com.sun.javadoc.*; 1.33 + 1.34 +import com.sun.tools.javac.code.Attribute; 1.35 +import com.sun.tools.javac.code.Symbol.*; 1.36 +import com.sun.tools.javac.code.Type; 1.37 +import com.sun.tools.javac.code.TypeTags; 1.38 + 1.39 + 1.40 +/** 1.41 + * Represents a value of an annotation type element. 1.42 + * 1.43 + * @author Scott Seligman 1.44 + * @since 1.5 1.45 + */ 1.46 + 1.47 +public class AnnotationValueImpl implements AnnotationValue { 1.48 + 1.49 + private final DocEnv env; 1.50 + private final Attribute attr; 1.51 + 1.52 + 1.53 + AnnotationValueImpl(DocEnv env, Attribute attr) { 1.54 + this.env = env; 1.55 + this.attr = attr; 1.56 + } 1.57 + 1.58 + /** 1.59 + * Returns the value. 1.60 + * The type of the returned object is one of the following: 1.61 + * <ul><li> a wrapper class for a primitive type 1.62 + * <li> <code>String</code> 1.63 + * <li> <code>Type</code> (representing a class literal) 1.64 + * <li> <code>FieldDoc</code> (representing an enum constant) 1.65 + * <li> <code>AnnotationDesc</code> 1.66 + * <li> <code>AnnotationValue[]</code> 1.67 + * </ul> 1.68 + */ 1.69 + public Object value() { 1.70 + ValueVisitor vv = new ValueVisitor(); 1.71 + attr.accept(vv); 1.72 + return vv.value; 1.73 + } 1.74 + 1.75 + private class ValueVisitor implements Attribute.Visitor { 1.76 + public Object value; 1.77 + 1.78 + public void visitConstant(Attribute.Constant c) { 1.79 + if (c.type.tag == TypeTags.BOOLEAN) { 1.80 + // javac represents false and true as integers 0 and 1 1.81 + value = Boolean.valueOf( 1.82 + ((Integer)c.value).intValue() != 0); 1.83 + } else { 1.84 + value = c.value; 1.85 + } 1.86 + } 1.87 + 1.88 + public void visitClass(Attribute.Class c) { 1.89 + value = TypeMaker.getType(env, 1.90 + env.types.erasure(c.type)); 1.91 + } 1.92 + 1.93 + public void visitEnum(Attribute.Enum e) { 1.94 + value = env.getFieldDoc(e.value); 1.95 + } 1.96 + 1.97 + public void visitCompound(Attribute.Compound c) { 1.98 + value = new AnnotationDescImpl(env, c); 1.99 + } 1.100 + 1.101 + public void visitArray(Attribute.Array a) { 1.102 + AnnotationValue vals[] = new AnnotationValue[a.values.length]; 1.103 + for (int i = 0; i < vals.length; i++) { 1.104 + vals[i] = new AnnotationValueImpl(env, a.values[i]); 1.105 + } 1.106 + value = vals; 1.107 + } 1.108 + 1.109 + public void visitError(Attribute.Error e) { 1.110 + value = "<error>"; 1.111 + } 1.112 + } 1.113 + 1.114 + /** 1.115 + * Returns a string representation of the value. 1.116 + * 1.117 + * @return the text of a Java language annotation value expression 1.118 + * whose value is the value of this annotation type element. 1.119 + */ 1.120 + public String toString() { 1.121 + ToStringVisitor tv = new ToStringVisitor(); 1.122 + attr.accept(tv); 1.123 + return tv.toString(); 1.124 + } 1.125 + 1.126 + private class ToStringVisitor implements Attribute.Visitor { 1.127 + private final StringBuffer sb = new StringBuffer(); 1.128 + 1.129 + public String toString() { 1.130 + return sb.toString(); 1.131 + } 1.132 + 1.133 + public void visitConstant(Attribute.Constant c) { 1.134 + if (c.type.tag == TypeTags.BOOLEAN) { 1.135 + // javac represents false and true as integers 0 and 1 1.136 + sb.append(((Integer)c.value).intValue() != 0); 1.137 + } else { 1.138 + sb.append(FieldDocImpl.constantValueExpression(c.value)); 1.139 + } 1.140 + } 1.141 + 1.142 + public void visitClass(Attribute.Class c) { 1.143 + sb.append(c); 1.144 + } 1.145 + 1.146 + public void visitEnum(Attribute.Enum e) { 1.147 + sb.append(e); 1.148 + } 1.149 + 1.150 + public void visitCompound(Attribute.Compound c) { 1.151 + sb.append(new AnnotationDescImpl(env, c)); 1.152 + } 1.153 + 1.154 + public void visitArray(Attribute.Array a) { 1.155 + // Omit braces from singleton. 1.156 + if (a.values.length != 1) sb.append('{'); 1.157 + 1.158 + boolean first = true; 1.159 + for (Attribute elem : a.values) { 1.160 + if (first) { 1.161 + first = false; 1.162 + } else { 1.163 + sb.append(", "); 1.164 + } 1.165 + elem.accept(this); 1.166 + } 1.167 + // Omit braces from singleton. 1.168 + if (a.values.length != 1) sb.append('}'); 1.169 + } 1.170 + 1.171 + public void visitError(Attribute.Error e) { 1.172 + sb.append("<error>"); 1.173 + } 1.174 + } 1.175 +}