~ubuntu-branches/ubuntu/natty/aspectj/natty

« back to all changes in this revision

Viewing changes to org.aspectj/modules/bcel-builder/src/org/aspectj/apache/bcel/classfile/annotation/Annotation.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-10-04 16:37:23 UTC
  • mfrom: (1.1.3 upstream) (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20091004163723-ck4y7j7fhjxskkie
Tags: 1.6.6+dfsg-1
* New upstream release.
  - Update 02_use_gjdoc.diff patch
* Update my email address

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* *******************************************************************
2
 
 * Copyright (c) 2004 IBM Corporation
3
 
 * 
4
 
 * All rights reserved. 
5
 
 * This program and the accompanying materials are made available 
6
 
 * under the terms of the Eclipse Public License v1.0 
7
 
 * which accompanies this distribution and is available at 
8
 
 * http://www.eclipse.org/legal/epl-v10.html 
9
 
 *  
10
 
 * Contributors: 
11
 
 *    Andy Clement     initial implementation 
12
 
 * ******************************************************************/
13
 
package org.aspectj.apache.bcel.classfile.annotation;
14
 
 
15
 
import java.io.DataInputStream;
16
 
import java.io.DataOutputStream;
17
 
import java.io.IOException;
18
 
import java.util.ArrayList;
19
 
import java.util.Iterator;
20
 
import java.util.List;
21
 
 
22
 
import org.aspectj.apache.bcel.Constants;
23
 
import org.aspectj.apache.bcel.classfile.ConstantPool;
24
 
import org.aspectj.apache.bcel.classfile.ConstantUtf8;
25
 
import org.aspectj.apache.bcel.classfile.Utility;
26
 
 
27
 
/**
28
 
 * An annotation is an immutable object (AnnotationGen is the mutable variant) - it basically contains a list
29
 
 * of name-value pairs.
30
 
 */
31
 
public class Annotation {
32
 
        private int typeIndex;
33
 
        private List /* ElementNameValuePair */ evs = new ArrayList();
34
 
        private ConstantPool cpool;
35
 
        private boolean isRuntimeVisible;
36
 
        
37
 
        public String toString() {
38
 
                StringBuffer sb = new StringBuffer();
39
 
                sb.append("ANNOTATION ["+getTypeSignature()+"] ["+
40
 
                                (isRuntimeVisible?"runtimeVisible":"runtimeInvisible")+"] [");
41
 
                for (Iterator iter = evs.iterator(); iter.hasNext();) {
42
 
                        ElementNameValuePair element = (ElementNameValuePair) iter.next();
43
 
                        sb.append(element.toString());
44
 
                        if (iter.hasNext()) sb.append(",");
45
 
                }
46
 
                sb.append("]");
47
 
                return sb.toString();
48
 
        }
49
 
        
50
 
        private Annotation(ConstantPool cpool) {
51
 
                this.cpool = cpool;
52
 
        }
53
 
        
54
 
        public Annotation(int index,ConstantPool cpool,boolean visible) {
55
 
                this.cpool = cpool;
56
 
                this.typeIndex = index;
57
 
                this.isRuntimeVisible = visible;
58
 
        }
59
 
        
60
 
        protected static Annotation read(DataInputStream dis,ConstantPool cpool,boolean isRuntimeVisible) throws IOException {
61
 
                Annotation a = new Annotation(cpool);
62
 
                a.typeIndex = dis.readUnsignedShort();
63
 
                int elemValuePairCount = dis.readUnsignedShort();
64
 
                for (int i=0;i<elemValuePairCount;i++) {
65
 
                        int nidx = dis.readUnsignedShort();             
66
 
                        a.addElementNameValuePair(
67
 
                                        new ElementNameValuePair(nidx,ElementValue.readElementValue(dis,cpool),cpool));
68
 
                }
69
 
                a.isRuntimeVisible(isRuntimeVisible);
70
 
                return a;
71
 
        }
72
 
        
73
 
        protected void dump(DataOutputStream dos) throws IOException {
74
 
                dos.writeShort(typeIndex);      // u2 index of type name in cpool
75
 
                dos.writeShort(evs.size()); // u2 element_value pair count
76
 
                for (int i = 0 ; i<evs.size();i++) {
77
 
                        ElementNameValuePair envp = (ElementNameValuePair) evs.get(i);
78
 
                        envp.dump(dos);
79
 
                }
80
 
        }
81
 
        
82
 
        public void addElementNameValuePair(ElementNameValuePair evp) {
83
 
                evs.add(evp);
84
 
        }
85
 
        
86
 
        
87
 
        public int getTypeIndex() {
88
 
                return typeIndex;
89
 
        }
90
 
        
91
 
        public String getTypeSignature() {
92
 
          ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(typeIndex,Constants.CONSTANT_Utf8);
93
 
          return c.getBytes();
94
 
        }
95
 
        
96
 
        public String getTypeName() {
97
 
                ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(typeIndex,Constants.CONSTANT_Utf8);
98
 
                return Utility.signatureToString(c.getBytes());
99
 
        }
100
 
        
101
 
        /**
102
 
         * Returns list of ElementNameValuePair objects
103
 
         */
104
 
        public List getValues() {
105
 
                return evs;
106
 
        }
107
 
 
108
 
        protected void isRuntimeVisible(boolean b) {
109
 
                isRuntimeVisible = b;
110
 
        }
111
 
        
112
 
        public boolean isRuntimeVisible() {
113
 
                return isRuntimeVisible;
114
 
        }
115
 
 
116
 
        public String toShortString() {
117
 
                StringBuffer result = new StringBuffer();
118
 
                result.append("@");
119
 
                result.append(getTypeName());
120
 
                if (getValues().size()>0) {
121
 
                        result.append("(");
122
 
                        for (Iterator iter = getValues().iterator(); iter.hasNext();) {
123
 
                                ElementNameValuePair element = (ElementNameValuePair) iter.next();
124
 
                                result.append(element.toShortString());
125
 
                        }
126
 
                        result.append(")");
127
 
                }
128
 
                return result.toString();
129
 
        }
130
 
}