KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > util > AnnotationComponentValue


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.util;
12
13 import org.eclipse.jdt.core.util.ClassFormatException;
14 import org.eclipse.jdt.core.util.IAnnotation;
15 import org.eclipse.jdt.core.util.IAnnotationComponentValue;
16 import org.eclipse.jdt.core.util.IConstantPool;
17 import org.eclipse.jdt.core.util.IConstantPoolConstant;
18 import org.eclipse.jdt.core.util.IConstantPoolEntry;
19
20 /**
21  * Default implementation of IAnnotationComponent
22  */

23 public class AnnotationComponentValue extends ClassFileStruct implements IAnnotationComponentValue {
24     private static final IAnnotationComponentValue[] NO_VALUES = new AnnotationComponentValue[0];
25     
26     private IAnnotationComponentValue[] annotationComponentValues;
27     private IAnnotation annotationValue;
28     private IConstantPoolEntry classInfo;
29     private int classFileInfoIndex;
30     private IConstantPoolEntry constantValue;
31     private int constantValueIndex;
32     private int enumConstantTypeNameIndex;
33     private int enumConstantNameIndex;
34     private char[] enumConstantTypeName;
35     private char[] enumConstantName;
36
37     private int readOffset;
38     private int tag;
39     private int valuesNumber;
40     
41     public AnnotationComponentValue(
42             byte[] classFileBytes,
43             IConstantPool constantPool,
44             int offset) throws ClassFormatException {
45         this.classFileInfoIndex = -1;
46         this.constantValueIndex = -1;
47         this.enumConstantTypeNameIndex = -1;
48         this.enumConstantNameIndex = -1;
49         final int t = u1At(classFileBytes, 0, offset);
50         this.tag = t;
51         this.readOffset = 1;
52         switch(t) {
53             case 'B' :
54             case 'C' :
55             case 'D' :
56             case 'F' :
57             case 'I' :
58             case 'J' :
59             case 'S' :
60             case 'Z' :
61             case 's' :
62                 final int constantIndex = this.u2At(classFileBytes, this.readOffset, offset);
63                 this.constantValueIndex = constantIndex;
64                 if (constantIndex != 0) {
65                     IConstantPoolEntry constantPoolEntry = constantPool.decodeEntry(constantIndex);
66                     switch(constantPoolEntry.getKind()) {
67                         case IConstantPoolConstant.CONSTANT_Long :
68                         case IConstantPoolConstant.CONSTANT_Float :
69                         case IConstantPoolConstant.CONSTANT_Double :
70                         case IConstantPoolConstant.CONSTANT_Integer :
71                         case IConstantPoolConstant.CONSTANT_Utf8 :
72                             break;
73                         default :
74                             throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
75                     }
76                     this.constantValue = constantPoolEntry;
77                 }
78                 this.readOffset += 2;
79                 break;
80             case 'e' :
81                 int index = this.u2At(classFileBytes, this.readOffset, offset);
82                 this.enumConstantTypeNameIndex = index;
83                 if (index != 0) {
84                     IConstantPoolEntry constantPoolEntry = constantPool.decodeEntry(index);
85                     if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
86                         throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
87                     }
88                     this.enumConstantTypeName = constantPoolEntry.getUtf8Value();
89                 }
90                 this.readOffset += 2;
91                 index = this.u2At(classFileBytes, this.readOffset, offset);
92                 this.enumConstantNameIndex = index;
93                 if (index != 0) {
94                     IConstantPoolEntry constantPoolEntry = constantPool.decodeEntry(index);
95                     if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
96                         throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
97                     }
98                     this.enumConstantName = constantPoolEntry.getUtf8Value();
99                 }
100                 this.readOffset += 2;
101                 break;
102             case 'c' :
103                 final int classFileIndex = this.u2At(classFileBytes, this.readOffset, offset);
104                 this.classFileInfoIndex = classFileIndex;
105                 if (classFileIndex != 0) {
106                     IConstantPoolEntry constantPoolEntry = constantPool.decodeEntry(classFileIndex);
107                     if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
108                         throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
109                     }
110                     this.classInfo = constantPoolEntry;
111                 }
112                 this.readOffset += 2;
113                 break;
114             case '@' :
115                 Annotation annotation = new Annotation(classFileBytes, constantPool, this.readOffset + offset);
116                 this.annotationValue = annotation;
117                 this.readOffset += annotation.sizeInBytes();
118                 break;
119             case '[' :
120                 final int numberOfValues = this.u2At(classFileBytes, this.readOffset, offset);
121                 this.valuesNumber = numberOfValues;
122                 if (numberOfValues != 0) {
123                     this.readOffset += 2;
124                     this.annotationComponentValues = new IAnnotationComponentValue[numberOfValues];
125                     for (int i = 0; i < numberOfValues; i++) {
126                         AnnotationComponentValue value = new AnnotationComponentValue(classFileBytes, constantPool, offset + readOffset);
127                         this.annotationComponentValues[i] = value;
128                         this.readOffset += value.sizeInBytes();
129                     }
130                 } else {
131                     this.annotationComponentValues = NO_VALUES;
132                 }
133                 break;
134         }
135     }
136     /* (non-Javadoc)
137      * @see org.eclipse.jdt.core.util.IAnnotationComponentValue#getAnnotationComponentValues()
138      */

139     public IAnnotationComponentValue[] getAnnotationComponentValues() {
140         return this.annotationComponentValues;
141     }
142     /* (non-Javadoc)
143      * @see org.eclipse.jdt.core.util.IAnnotationComponentValue#getAnnotationValue()
144      */

145     public IAnnotation getAnnotationValue() {
146         return this.annotationValue;
147     }
148     /* (non-Javadoc)
149      * @see org.eclipse.jdt.core.util.IAnnotationComponentValue#getClassInfo()
150      */

151     public IConstantPoolEntry getClassInfo() {
152         return this.classInfo;
153     }
154     /* (non-Javadoc)
155      * @see org.eclipse.jdt.core.util.IAnnotationComponentValue#getClassInfoIndex()
156      */

157     public int getClassInfoIndex() {
158         return this.classFileInfoIndex;
159     }
160     /* (non-Javadoc)
161      * @see org.eclipse.jdt.core.util.IAnnotationComponentValue#getConstantValue()
162      */

163     public IConstantPoolEntry getConstantValue() {
164         return this.constantValue;
165     }
166     /* (non-Javadoc)
167      * @see org.eclipse.jdt.core.util.IAnnotationComponentValue#getConstantValueIndex()
168      */

169     public int getConstantValueIndex() {
170         return this.constantValueIndex;
171     }
172     /* (non-Javadoc)
173      * @see org.eclipse.jdt.core.util.IAnnotationComponentValue#getEnumConstantName()
174      */

175     public char[] getEnumConstantName() {
176         return this.enumConstantName;
177     }
178     /* (non-Javadoc)
179      * @see org.eclipse.jdt.core.util.IAnnotationComponentValue#getEnumConstantNameIndex()
180      */

181     public int getEnumConstantNameIndex() {
182         return this.enumConstantNameIndex;
183     }
184     /* (non-Javadoc)
185      * @see org.eclipse.jdt.core.util.IAnnotationComponentValue#getEnumConstantTypeName()
186      */

187     public char[] getEnumConstantTypeName() {
188         return this.enumConstantTypeName;
189     }
190     /* (non-Javadoc)
191      * @see org.eclipse.jdt.core.util.IAnnotationComponentValue#getEnumConstantTypeNameIndex()
192      */

193     public int getEnumConstantTypeNameIndex() {
194         return enumConstantTypeNameIndex;
195     }
196     /* (non-Javadoc)
197      * @see org.eclipse.jdt.core.util.IAnnotationComponentValue#getTag()
198      */

199     public int getTag() {
200         return this.tag;
201     }
202     /* (non-Javadoc)
203      * @see org.eclipse.jdt.core.util.IAnnotationComponentValue#getValuesNumber()
204      */

205     public int getValuesNumber() {
206         return this.valuesNumber;
207     }
208     
209     int sizeInBytes() {
210         return this.readOffset;
211     }
212 }
213
Popular Tags