com.vladium.jcd.cls
Class ConstantCollection

java.lang.Object
  extended bycom.vladium.jcd.cls.ConstantCollection
All Implemented Interfaces:
java.lang.Cloneable, IClassFormatOutput, IConstantCollection

final class ConstantCollection
extends java.lang.Object
implements IConstantCollection

Author:
(C) 2001, Vladimir Roubtsov

Nested Class Summary
private static class ConstantCollection.ConstantIterator
           
 
Nested classes inherited from class com.vladium.jcd.cls.IConstantCollection
IConstantCollection.IConstantComparator, IConstantCollection.IConstantIterator
 
Field Summary
private  ObjectIntMap m_CONSTANT_Utf8_index
           
private  java.util.List m_constants
           
private  int m_size
           
 
Constructor Summary
(package private) ConstantCollection(int capacity)
           
 
Method Summary
 void accept(IClassDefVisitor visitor, java.lang.Object ctx)
           
 int add(CONSTANT_info constant)
          Appends 'constant' to the end of the collection.
 java.lang.Object clone()
          Performs a deep copy.
 int find(int type, IConstantCollection.IConstantComparator comparator)
          Searches the pool for a matching constant of given type with equality semantics expressed by 'comparator'.
 int findCONSTANT_Utf8(java.lang.String value)
          Convenience method that can lookup CONSTANT_Utf8 entries in O(1) time on average.
 CONSTANT_info get(int index)
          Returns a CONSTANT_info at a given pool index.
private  ObjectIntMap getCONSTANT_Utf8_index()
           
 IConstantCollection.IConstantIterator iterator()
          Returns a fail-fast iterator over all valid entries in the pool.
 CONSTANT_info set(int index, CONSTANT_info constant)
          Replaces an existing constant pool entry.
 int size()
          Returns the number of CONSTANT_info entries in this collection.
 void writeInClassFormat(UDataOutputStream out)
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_constants

private java.util.List m_constants

m_size

private int m_size

m_CONSTANT_Utf8_index

private transient ObjectIntMap m_CONSTANT_Utf8_index
Constructor Detail

ConstantCollection

ConstantCollection(int capacity)
Method Detail

get

public CONSTANT_info get(int index)
Description copied from interface: IConstantCollection
Returns a CONSTANT_info at a given pool index. Note that 'index' is 1-based [the way an index would be embedded in bytecode instructions]. Note that because CONSTANT_Long and CONSTANT_Double entries occupy two consequitive index slots certain index values inside the valid range can be invalid; use IConstantCollection.iterator() to iterate only over valid entries in a transparent fashion.

Specified by:
get in interface IConstantCollection
Parameters:
index - constant pool index [must be in [1, size()] range]
Returns:
CONSTANT_info constant pool entry at this index [never null]

iterator

public IConstantCollection.IConstantIterator iterator()
Description copied from interface: IConstantCollection
Returns a fail-fast iterator over all valid entries in the pool. The resulting object would be invalidated by simultaneous mutation to the underlying collection pool.

Specified by:
iterator in interface IConstantCollection
Returns:
IConstantIterator iterator over all entries in the collection [never null]

find

public int find(int type,
                IConstantCollection.IConstantComparator comparator)
Description copied from interface: IConstantCollection
Searches the pool for a matching constant of given type with equality semantics expressed by 'comparator'. This method guarantees that when comparator.equals(c) is called c.type() is 'type'. The cost is O(pool size). When multiple matches exist, the location of the first one found will be returned (chosen in some indeterministic way).

Specified by:
find in interface IConstantCollection
Parameters:
type - type of constants to filter by [not validated]
comparator - [may not be null]
Returns:
index of the first found entry [-1 if not found]

findCONSTANT_Utf8

public int findCONSTANT_Utf8(java.lang.String value)
Description copied from interface: IConstantCollection
Convenience method that can lookup CONSTANT_Utf8 entries in O(1) time on average. Note that .class format does not guarantee that all such entries are not duplicated in the pool. When multiple matches exist, the location of the first one found will be returned (chosen in some indeterministic way).

Specified by:
findCONSTANT_Utf8 in interface IConstantCollection
Parameters:
value - string value on which to match [may not be null]
Returns:
index of the first found entry [-1 if not found]

size

public int size()
Description copied from interface: IConstantCollection
Returns the number of CONSTANT_info entries in this collection.

Specified by:
size in interface IConstantCollection
Returns:
the number of constants in this pool [can be 0]

clone

public java.lang.Object clone()
Performs a deep copy.

Specified by:
clone in interface IConstantCollection

writeInClassFormat

public void writeInClassFormat(UDataOutputStream out)
                        throws java.io.IOException
Specified by:
writeInClassFormat in interface IClassFormatOutput
Throws:
java.io.IOException

accept

public void accept(IClassDefVisitor visitor,
                   java.lang.Object ctx)
Specified by:
accept in interface IConstantCollection

set

public CONSTANT_info set(int index,
                         CONSTANT_info constant)
Description copied from interface: IConstantCollection
Replaces an existing constant pool entry. A replacement can be made only for a constant of the same width as the constant currently occupying the slot.

Specified by:
set in interface IConstantCollection
Parameters:
index - constant pool index [must be in [1, size()] range]
constant - new entry to set [may not be null; input unchecked]
Returns:
CONSTANT_info previous contents at this pool index [never null]

add

public int add(CONSTANT_info constant)
Description copied from interface: IConstantCollection
Appends 'constant' to the end of the collection. No duplicate checks are made.

Specified by:
add in interface IConstantCollection
Parameters:
constant - new constant [may not be null; input unchecked]
Returns:
the pool index of the newly added entry [always positive]

getCONSTANT_Utf8_index

private ObjectIntMap getCONSTANT_Utf8_index()