001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core.util;
011:
012: import org.eclipse.jdt.core.util.ClassFormatException;
013: import org.eclipse.jdt.core.util.IAnnotation;
014: import org.eclipse.jdt.core.util.IAnnotationComponent;
015: import org.eclipse.jdt.core.util.IConstantPool;
016: import org.eclipse.jdt.core.util.IConstantPoolConstant;
017: import org.eclipse.jdt.core.util.IConstantPoolEntry;
018:
019: /**
020: * Default implementation of IAnnotation
021: */
022: public class Annotation extends ClassFileStruct implements IAnnotation {
023:
024: private static final IAnnotationComponent[] NO_ENTRIES = new IAnnotationComponent[0];
025:
026: private int typeIndex;
027: private char[] typeName;
028: private int componentsNumber;
029: private IAnnotationComponent[] components;
030: private int readOffset;
031:
032: /**
033: * Constructor for Annotation.
034: *
035: * @param classFileBytes
036: * @param constantPool
037: * @param offset
038: * @throws ClassFormatException
039: */
040: public Annotation(byte[] classFileBytes,
041: IConstantPool constantPool, int offset)
042: throws ClassFormatException {
043:
044: final int index = u2At(classFileBytes, 0, offset);
045: this .typeIndex = index;
046: if (index != 0) {
047: IConstantPoolEntry constantPoolEntry = constantPool
048: .decodeEntry(index);
049: if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
050: throw new ClassFormatException(
051: ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
052: }
053: this .typeName = constantPoolEntry.getUtf8Value();
054: } else {
055: throw new ClassFormatException(
056: ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
057: }
058: final int length = u2At(classFileBytes, 2, offset);
059: this .componentsNumber = length;
060: this .readOffset = 4;
061: if (length != 0) {
062: this .components = new IAnnotationComponent[length];
063: for (int i = 0; i < length; i++) {
064: AnnotationComponent component = new AnnotationComponent(
065: classFileBytes, constantPool, offset
066: + readOffset);
067: this .components[i] = component;
068: this .readOffset += component.sizeInBytes();
069: }
070: } else {
071: this .components = NO_ENTRIES;
072: }
073: }
074:
075: /* (non-Javadoc)
076: * @see org.eclipse.jdt.core.util.IAnnotation#getTypeIndex()
077: */
078: public int getTypeIndex() {
079: return this .typeIndex;
080: }
081:
082: /* (non-Javadoc)
083: * @see org.eclipse.jdt.core.util.IAnnotation#getComponentsNumber()
084: */
085: public int getComponentsNumber() {
086: return this .componentsNumber;
087: }
088:
089: /* (non-Javadoc)
090: * @see org.eclipse.jdt.core.util.IAnnotation#getComponents()
091: */
092: public IAnnotationComponent[] getComponents() {
093: return this .components;
094: }
095:
096: int sizeInBytes() {
097: return this .readOffset;
098: }
099:
100: /* (non-Javadoc)
101: * @see org.eclipse.jdt.core.util.IAnnotation#getTypeName()
102: */
103: public char[] getTypeName() {
104: return this.typeName;
105: }
106: }
|