001    /*
002     * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.apt.mirror.declaration;
027    
028    
029    import java.util.Collection;
030    import java.util.ArrayList;
031    
032    import com.sun.mirror.declaration.*;
033    import com.sun.mirror.util.SourcePosition;
034    import com.sun.tools.apt.mirror.AptEnv;
035    import com.sun.tools.javac.code.Attribute;
036    import com.sun.tools.javac.code.Symbol.*;
037    import com.sun.tools.javac.code.TypeTags;
038    
039    
040    /**
041     * Implementation of AnnotationValue
042     */
043    
044    public class AnnotationValueImpl implements AnnotationValue {
045    
046        protected final AptEnv env;
047        protected final Attribute attr;
048        protected final AnnotationMirrorImpl annotation;
049    
050        AnnotationValueImpl(AptEnv env, Attribute attr, AnnotationMirrorImpl annotation) {
051            this.env = env;
052            this.attr = attr;
053            this.annotation = annotation;
054        }
055    
056    
057        /**
058         * {@inheritDoc}
059         */
060        public String toString() {
061            StringBuilder sb = new StringBuilder();
062            Constants.Formatter fmtr = Constants.getFormatter(sb);
063    
064            fmtr.append(getValue());
065            return fmtr.toString();
066        }
067    
068        /**
069         * {@inheritDoc}
070         */
071        public Object getValue() {
072            ValueVisitor vv = new ValueVisitor();
073            attr.accept(vv);
074            return vv.value;
075        }
076    
077    
078        public SourcePosition getPosition() {
079            // Imprecise implementation; just return position of enclosing
080            // annotation.
081            return (annotation == null) ? null : annotation.getPosition();
082        }
083    
084        private class ValueVisitor implements Attribute.Visitor {
085    
086            public Object value;
087    
088            public void visitConstant(Attribute.Constant c) {
089                value = Constants.decodeConstant(c.value, c.type);
090            }
091    
092            public void visitClass(Attribute.Class c) {
093                value = env.typeMaker.getType(
094                            env.jctypes.erasure(c.type));
095            }
096    
097            public void visitEnum(Attribute.Enum e) {
098                value = env.declMaker.getFieldDeclaration(e.value);
099            }
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    }