KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.IConstantPool;
14 import org.eclipse.jdt.core.util.IConstantPoolConstant;
15 import org.eclipse.jdt.core.util.IConstantPoolEntry;
16
17 /**
18  * Default implementation of IConstantPool.
19  */

20 public class ConstantPool extends ClassFileStruct implements IConstantPool {
21     
22     private int constantPoolCount;
23     private int[] constantPoolOffset;
24     private byte[] classFileBytes;
25     
26     ConstantPool(byte[] reference, int[] constantPoolOffset) {
27         this.constantPoolCount = constantPoolOffset.length;
28         this.constantPoolOffset = constantPoolOffset;
29         this.classFileBytes = reference;
30     }
31
32     /**
33      * @see IConstantPool#decodeEntry(int)
34      */

35     public IConstantPoolEntry decodeEntry(int index) {
36         ConstantPoolEntry constantPoolEntry = new ConstantPoolEntry();
37         constantPoolEntry.reset();
38         int kind = getEntryKind(index);
39         constantPoolEntry.setKind(kind);
40         switch(kind) {
41             case IConstantPoolConstant.CONSTANT_Class :
42                 constantPoolEntry.setClassInfoNameIndex(u2At(this.classFileBytes, 1, this.constantPoolOffset[index]));
43                 constantPoolEntry.setClassInfoName(getUtf8ValueAt(constantPoolEntry.getClassInfoNameIndex()));
44                 break;
45             case IConstantPoolConstant.CONSTANT_Double :
46                 constantPoolEntry.setDoubleValue(doubleAt(classFileBytes, 1, this.constantPoolOffset[index]));
47                 break;
48             case IConstantPoolConstant.CONSTANT_Fieldref :
49                 constantPoolEntry.setClassIndex(u2At(this.classFileBytes, 1, this.constantPoolOffset[index]));
50                 int declaringClassIndex = u2At(this.classFileBytes, 1, this.constantPoolOffset[constantPoolEntry.getClassIndex()]);
51                 constantPoolEntry.setClassName(getUtf8ValueAt(declaringClassIndex));
52                 constantPoolEntry.setNameAndTypeIndex(u2At(this.classFileBytes, 3, this.constantPoolOffset[index]));
53                 int fieldNameIndex = u2At(this.classFileBytes, 1, this.constantPoolOffset[constantPoolEntry.getNameAndTypeIndex()]);
54                 int fieldDescriptorIndex = u2At(this.classFileBytes, 3, this.constantPoolOffset[constantPoolEntry.getNameAndTypeIndex()]);
55                 constantPoolEntry.setFieldName(getUtf8ValueAt(fieldNameIndex));
56                 constantPoolEntry.setFieldDescriptor(getUtf8ValueAt(fieldDescriptorIndex));
57                 break;
58             case IConstantPoolConstant.CONSTANT_Methodref :
59             case IConstantPoolConstant.CONSTANT_InterfaceMethodref :
60                 constantPoolEntry.setClassIndex(u2At(this.classFileBytes, 1, this.constantPoolOffset[index]));
61                 declaringClassIndex = u2At(this.classFileBytes, 1, this.constantPoolOffset[constantPoolEntry.getClassIndex()]);
62                 constantPoolEntry.setClassName(getUtf8ValueAt(declaringClassIndex));
63                 constantPoolEntry.setNameAndTypeIndex(u2At(this.classFileBytes, 3, this.constantPoolOffset[index]));
64                 int methodNameIndex = u2At(this.classFileBytes, 1, this.constantPoolOffset[constantPoolEntry.getNameAndTypeIndex()]);
65                 int methodDescriptorIndex = u2At(this.classFileBytes, 3, this.constantPoolOffset[constantPoolEntry.getNameAndTypeIndex()]);
66                 constantPoolEntry.setMethodName(getUtf8ValueAt(methodNameIndex));
67                 constantPoolEntry.setMethodDescriptor(getUtf8ValueAt(methodDescriptorIndex));
68                 break;
69             case IConstantPoolConstant.CONSTANT_Float :
70                 constantPoolEntry.setFloatValue(floatAt(classFileBytes, 1, this.constantPoolOffset[index]));
71                 break;
72             case IConstantPoolConstant.CONSTANT_Integer :
73                 constantPoolEntry.setIntegerValue(i4At(classFileBytes, 1, this.constantPoolOffset[index]));
74                 break;
75             case IConstantPoolConstant.CONSTANT_Long :
76                 constantPoolEntry.setLongValue(i8At(classFileBytes, 1, this.constantPoolOffset[index]));
77                 break;
78             case IConstantPoolConstant.CONSTANT_NameAndType :
79                 constantPoolEntry.setNameAndTypeNameIndex(u2At(this.classFileBytes, 1, this.constantPoolOffset[index]));
80                 constantPoolEntry.setNameAndTypeDescriptorIndex(u2At(this.classFileBytes, 3, this.constantPoolOffset[index]));
81                 break;
82             case IConstantPoolConstant.CONSTANT_String :
83                 constantPoolEntry.setStringIndex(u2At(this.classFileBytes, 1, this.constantPoolOffset[index]));
84                 constantPoolEntry.setStringValue(getUtf8ValueAt(constantPoolEntry.getStringIndex()));
85                 break;
86             case IConstantPoolConstant.CONSTANT_Utf8 :
87                 constantPoolEntry.setUtf8Length(u2At(this.classFileBytes, 1, this.constantPoolOffset[index]));
88                 constantPoolEntry.setUtf8Value(getUtf8ValueAt(index));
89         }
90         return constantPoolEntry;
91     }
92
93     /**
94      * @see IConstantPool#getConstantPoolCount()
95      */

96     public int getConstantPoolCount() {
97         return this.constantPoolCount;
98     }
99
100     /**
101      * @see IConstantPool#getEntryKind(int)
102      */

103     public int getEntryKind(int index) {
104         return this.u1At(this.classFileBytes, 0, this.constantPoolOffset[index]);
105     }
106
107     private char[] getUtf8ValueAt(int utf8Index) {
108         int utf8Offset = this.constantPoolOffset[utf8Index];
109         return utf8At(classFileBytes, 0, utf8Offset + 3, u2At(classFileBytes, 0, utf8Offset + 1));
110     }
111 }
112
Popular Tags