KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.IAnnotationComponent;
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 IAnnotation
22  */

23 public class Annotation extends ClassFileStruct implements IAnnotation {
24
25     private static final IAnnotationComponent[] NO_ENTRIES = new IAnnotationComponent[0];
26     
27     private int typeIndex;
28     private char[] typeName;
29     private int componentsNumber;
30     private IAnnotationComponent[] components;
31     private int readOffset;
32     
33     /**
34      * Constructor for Annotation.
35      *
36      * @param classFileBytes
37      * @param constantPool
38      * @param offset
39      * @throws ClassFormatException
40      */

41     public Annotation(
42             byte[] classFileBytes,
43             IConstantPool constantPool,
44             int offset) throws ClassFormatException {
45         
46         final int index = u2At(classFileBytes, 0, offset);
47         this.typeIndex = index;
48         if (index != 0) {
49             IConstantPoolEntry constantPoolEntry = constantPool.decodeEntry(index);
50             if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
51                 throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
52             }
53             this.typeName = constantPoolEntry.getUtf8Value();
54         } else {
55             throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
56         }
57         final int length = u2At(classFileBytes, 2, offset);
58         this.componentsNumber = length;
59         this.readOffset = 4;
60         if (length != 0) {
61             this.components = new IAnnotationComponent[length];
62             for (int i = 0; i < length; i++) {
63                 AnnotationComponent component = new AnnotationComponent(classFileBytes, constantPool, offset + readOffset);
64                 this.components[i] = component;
65                 this.readOffset += component.sizeInBytes();
66             }
67         } else {
68             this.components = NO_ENTRIES;
69         }
70     }
71     
72     /* (non-Javadoc)
73      * @see org.eclipse.jdt.core.util.IAnnotation#getTypeIndex()
74      */

75     public int getTypeIndex() {
76         return this.typeIndex;
77     }
78     /* (non-Javadoc)
79      * @see org.eclipse.jdt.core.util.IAnnotation#getComponentsNumber()
80      */

81     public int getComponentsNumber() {
82         return this.componentsNumber;
83     }
84     /* (non-Javadoc)
85      * @see org.eclipse.jdt.core.util.IAnnotation#getComponents()
86      */

87     public IAnnotationComponent[] getComponents() {
88         return this.components;
89     }
90     
91     int sizeInBytes() {
92         return this.readOffset;
93     }
94     /* (non-Javadoc)
95      * @see org.eclipse.jdt.core.util.IAnnotation#getTypeName()
96      */

97     public char[] getTypeName() {
98         return this.typeName;
99     }
100 }
101
Popular Tags