1 /* 2 * Copyright 2004 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 */ 25 26 package com.sun.tools.apt.mirror.declaration; 27 28 29 import java.util.Collection; 30 import java.util.ArrayList; 31 32 import com.sun.mirror.declaration.*; 33 import com.sun.mirror.util.SourcePosition; 34 import com.sun.tools.apt.mirror.AptEnv; 35 import com.sun.tools.javac.code.Attribute; 36 import com.sun.tools.javac.code.Symbol.*; 37 import com.sun.tools.javac.code.TypeTags; 38 39 40 /** 41 * Implementation of AnnotationValue 42 */ 43 @SuppressWarnings("deprecation") 44 public class AnnotationValueImpl implements AnnotationValue { 45 46 protected final AptEnv env; 47 protected final Attribute attr; 48 protected final AnnotationMirrorImpl annotation; 49 50 AnnotationValueImpl(AptEnv env, Attribute attr, AnnotationMirrorImpl annotation) { 51 this.env = env; 52 this.attr = attr; 53 this.annotation = annotation; 54 } 55 56 57 /** 58 * {@inheritDoc} 59 */ 60 public String toString() { 61 StringBuilder sb = new StringBuilder(); 62 Constants.Formatter fmtr = Constants.getFormatter(sb); 63 64 fmtr.append(getValue()); 65 return fmtr.toString(); 66 } 67 68 /** 69 * {@inheritDoc} 70 */ 71 public Object getValue() { 72 ValueVisitor vv = new ValueVisitor(); 73 attr.accept(vv); 74 return vv.value; 75 } 76 77 78 public SourcePosition getPosition() { 79 // Imprecise implementation; just return position of enclosing 80 // annotation. 81 return (annotation == null) ? null : annotation.getPosition(); 82 } 83 84 private class ValueVisitor implements Attribute.Visitor { 85 86 public Object value; 87 88 public void visitConstant(Attribute.Constant c) { 89 value = Constants.decodeConstant(c.value, c.type); 90 } 91 92 public void visitClass(Attribute.Class c) { 93 value = env.typeMaker.getType( 94 env.jctypes.erasure(c.type)); 95 } 96 97 public void visitEnum(Attribute.Enum e) { 98 value = env.declMaker.getFieldDeclaration(e.value); 99 } 100 101 public void visitCompound(Attribute.Compound c) { 102 value = new AnnotationMirrorImpl(env, c, 103 (annotation == null) ? 104 null : 105 annotation.getDeclaration()); 106 } 107 108 public void visitArray(Attribute.Array a) { 109 ArrayList<AnnotationValue> vals = 110 new ArrayList<AnnotationValue>(a.values.length); 111 for (Attribute elem : a.values) { 112 vals.add(new AnnotationValueImpl(env, elem, annotation)); 113 } 114 value = vals; 115 } 116 117 public void visitError(Attribute.Error e) { 118 value = "<error>"; // javac will already have logged an error msg 119 } 120 } 121 }