|
What this is
Other links
The source code/*
* ConstantPool.java
*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
* Microsystems, Inc. All Rights Reserved.
*
* Contributor(s): Thomas Ball
*
* Version: $Revision: 1.12 $
*/
package org.netbeans.modules.classfile;
import java.io.*;
import java.util.*;
/**
* Class representing a Java class file constant pool.
*
* @author Thomas Ball
*/
public final class ConstantPool {
private static final int CONSTANT_POOL_START = 1;
// Constant Type enums (JVM spec table 4.3)
static final int CONSTANT_Utf8 = 1;
static final int CONSTANT_Integer = 3;
static final int CONSTANT_Float = 4;
static final int CONSTANT_Long = 5;
static final int CONSTANT_Double = 6;
static final int CONSTANT_Class = 7;
static final int CONSTANT_String = 8;
static final int CONSTANT_FieldRef = 9;
static final int CONSTANT_MethodRef = 10;
static final int CONSTANT_InterfaceMethodRef = 11;
static final int CONSTANT_NameAndType = 12;
CPEntry[] cpEntries;
int constantPoolCount = 0;
/**
* Create a ConstantPool object from a stream of bytes.
*
* @param size number of entries in this constant pool.
* @param bytes a stream of bytes defining the constant pool.
*/
/* package-private */ ConstantPool(int size, InputStream bytes)
throws IOException {
if (size < 0)
throw new IllegalArgumentException("size cannot be negative");
if (bytes == null)
throw new IllegalArgumentException("byte stream not specified");
constantPoolCount = size;
cpEntries = new CPEntry[constantPoolCount];
load(bytes);
}
/**
* Create a new ConstantPool object with no entries.
* NOTE: not supported until classfile writing is.
*/
/*public*/ ConstantPool() {
constantPoolCount = CONSTANT_POOL_START;
cpEntries = new CPEntry[constantPoolCount];
}
/**
* Get the CPEntry at a specific constant pool index.
*
* @param index the constant pool index for the entry
*/
public final CPEntry get(int index) {
if (index <= 0 || index >= cpEntries.length)
throw new IndexOutOfBoundsException(Integer.toString(index));
return cpEntries[index];
}
/**
* Get the CPClassInfo at a specific index.
*
* @param index the constant pool index for the entry
*/
public final CPClassInfo getClass(int index) {
if (index <= 0)
throw new IndexOutOfBoundsException(Integer.toString(index));
return (CPClassInfo)get(index);
}
/* Return an array of all constants of a specified class type.
*
* @param type the constant pool type to return.
*/
public final Collection getAllConstants(Class classType) {
return Collections.unmodifiableCollection(
getAllConstantsImpl(classType));
}
private Collection getAllConstantsImpl(Class classType) {
int n = cpEntries.length;
Collection c = new ArrayList(n);
for (int i = CONSTANT_POOL_START; i < n; i++) {
if (cpEntries[i] != null &&
cpEntries[i].getClass().equals(classType)) {
c.add(cpEntries[i]);
}
}
return c;
}
/* Return the collection of all unique class references in pool.
*
* @return a Set of ClassNames specifying the referenced classnames.
*
* @deprecated use
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.