Package Torello.Java.Additional
Class ConstantPool
- java.lang.Object
-
- Torello.Java.Additional.ConstantPool
-
public class ConstantPool extends java.lang.Object
A Data-Class for representing a Java'.class'
File's Constant-Pool.
The following types are the types provided by an instance ofConstantPool
. The type listed in the'values'
column is the type stored in the primary table Lookup-Table. For the more complex constant types, there is an additional Data-Type which may be constructed or created to represent the various constants as a Java-Class. These additionally provided types are Nested-Classes or "Inner-Classes" of this class (ClassConstantPool
).
They are useful for the constants which function more as "Records", and are actually "Table Pointer-Pairs" that point to a slew of entries from a Constant-Pool Lookup-Table.
Tag-Kind 'values'
TypeDereferenced-Type Available 0 UNUSED 1: TAG_UTF_8
java.lang.String
None Provided 2 UNUSED 3: TAG_INTEGER
java.lang.Integer
None Provided 4: TAG_FLOAT
java.lang.Float
None Provided 5: TAG_LONG
java.lang.Long
None Provided 6: TAG_DOUBLE
java.lang.Double
None Provided 7: TAG_CLASS
java.lang.Integer
(Table-Pointer)None Provided 8: TAG_STRING
java.lang.Integer
(Table-Pointer)None Provided 9: TAG_FIELD_REF
Ret2<Integer, Integer>
(Pointer-Pair)ConstantPool.Reference
10: TAG_METHOD_REF
Ret2<Integer, Integer>
(Pointer-Pair)ConstantPool.Reference
11: TAG_INTERFACE_METHOD_REF
Ret2<Integer, Integer>
(Pointer-Pair)ConstantPool.Reference
12: TAG_NAME_AND_TYPE
Ret2<Integer, Integer>
(Pointer-Pair)None Provided 13 UNUSED 14 UNUSED 15: TAG_METHOD_HANDLE
Ret2<Byte, Integer>
(Method 'Kind' & Table-Pointer)ConstantPool.MethodHandle
16: TAG_METHOD_TYPE
java.lang.Integer
(Table-Pointer)None Provided 17: TAG_DYNAMIC
Ret2<Integer, Integer>
(Pointer-Pair)ConstantPool.Dynamic
18: TAG_INVOKE_DYNAMIC
Ret2<Integer, Integer>
(Pointer-Pair)ConstantPool.Dynamic
19: TAG_MODULE
java.lang.Integer
(Table-Pointer)None Provided 20: TAG_PACKAGE
java.lang.Integer
(Table-Pointer)None Provided
Hi-Lited Source-Code:This File's Source Code:
- View Here: Torello/Java/Additional/ConstantPool.java
- Open New Browser-Tab: Torello/Java/Additional/ConstantPool.java
File Size: 45,891 Bytes Line Count: 1,112 '\n' Characters Found
Static-Data Builder:
- View Here: ../data-files/ConstantPoolData.java
- Open New Browser-Tab: ../data-files/ConstantPoolData.java
File Size: 4,853 Bytes Line Count: 111 '\n' Characters Found
Constant-Pool Parser:
- View Here: ReadConstant.java
- Open New Browser-Tab: ReadConstant.java
File Size: 6,742 Bytes Line Count: 156 '\n' Characters Found
-
-
Nested Class Summary
Nested Classes Modifier and Type Class static class
ConstantPool.Dynamic
static class
ConstantPool.MethodHandle
static class
ConstantPool.Reference
-
Field Summary
Instance Fields: Constant-Pool Table-Size Modifier and Type Field int
tableSize
int
tableSizeBytes
Instance Fields: All Table Entries: Constant-Kind, Value & byte[]-Array Index, as Parallel-Lists Modifier and Type Field ReadOnlyList<Integer>
indices
ReadOnlyList<Byte>
tags
ReadOnlyList<Object>
values
Instance Fields: Constant-Pool Table-Indices, Grouped By 'Type-Kind' Modifier and Type Field ReadOnlyList<ReadOnlyList<Integer>>
indicesGroupBy
Instance Fields: Constant-Pool Values, Pointers De-Referenced into Objects Modifier and Type Field ReadOnlyList<Object>
dereferencedValues
Static, Final Fields: The Names and Sizes of each 'Type-Kind' Modifier and Type Field static ReadOnlyList<String>
tagNames
static ReadOnlyList<Byte>
tagWidthBytes
Static, Final Fields: The JDK / Java Assigned Tag-Number of each 'Type-Kind' Modifier and Type Field static byte
TAG_CLASS
static byte
TAG_DOUBLE
static byte
TAG_DYNAMIC
static byte
TAG_FIELD_REF
static byte
TAG_FLOAT
static byte
TAG_INTEGER
static byte
TAG_INTERFACE_METHOD_REF
static byte
TAG_INVOKE_DYNAMIC
static byte
TAG_LONG
static byte
TAG_METHOD_HANDLE
static byte
TAG_METHOD_REF
static byte
TAG_METHOD_TYPE
static byte
TAG_MODULE
static byte
TAG_NAME_AND_TYPE
static byte
TAG_PACKAGE
static byte
TAG_STRING
static byte
TAG_UTF_8
-
Constructor Summary
Constructors Constructor ConstantPool(byte[] bArr)
-
Method Summary
Simple Table-Introspection Methods Modifier and Type Method Stream<String>
allFieldNames()
Stream<ConstantPool.Reference>
allFields()
Stream<String>
allMethodNames()
Stream<ConstantPool.Reference>
allMethods()
Methods: class java.lang.Object Modifier and Type Method boolean
equals(Object other)
int
hashCode()
String
toString()
-
-
-
Field Detail
-
tableSize
public final int tableSize
This is identical totags.size()
- which is necessarily the same asvalues.size(), indices.size()
and evendereferencedValues.size()
.
All four instance lists in this class are "Parallel Lists". This means that the data contained by an element in any one of the lists is data that is pertinent to a piece of data in all three of the other lists - as long as that datum is located at the exact same list index of its other list.
Notes on the Lists-
Note that though both Java Array's and List's have an index that starts at zero, the
values which is stored at zero-position in each of the following lists:
tags
,values
,indices
&dereferencedValues
is not considered one of the Constant-Pool Constants.
- Java's Constant-Pool has a few quirks, one of which is that when a Table-Constant has a
size of 8-Bytes - which is referring to, specifically,
Long
andDouble
constants - the Constant-Number which would otherwise immediately follow these 8-Byte values are skipped!. By the term "Constant Number", we are just referring to the List-Index for these four Parallel Lists.
To restate the above point, any Table-Constant List-Position which is immediately after aLong
-Constant or aDouble
-Constant will always be null. Please review the Code-Snippet below, and pay close attention to when the contents of one of this class' Parallel-Lists contain null.
ConstantPool cp = new ConstantPool(bArr); // If the following loop prints anything, it can only print 'null' for (Integer i : cp.indicesGroupBy.get(ConstantPool.TAG_LONG)) if ((i + 1) < cp.tableSize) System.out.println(cp.values.get(i + 1));
- Code:
- Exact Field Declaration Expression:
public final int tableSize;
-
Note that though both Java Array's and List's have an index that starts at zero, the
values which is stored at zero-position in each of the following lists:
-
tableSizeBytes
public final int tableSizeBytes
The value stored in thispublic final
constant shall be precisely equal to the Byte-Number, within the inputbyte[]
-Array, where theAccess-Flags
Unsigned-Integer starts minus 10.
To explain, the Java Constant-Pool is the first Table-Structure inside of any valid Java'.class'
file. The Constant-Pool begins onbyte
#10 of thebyte[]
-Array representing a'.class'
File. Bytes #8 and #9 contain the size of the table, which have been stored in the array to reveal precisely how many constants should be read from this Constants-Table.
In any standard'.class'
File, a Two-Byte Unsigned Integer that contains the Bit-Mask for the list of Access-Flags immediately follows the Constant-Pool Table. So, when it is said that:- The value stored in
'tableSizeBytes'
is the same as the location of the first Unsigned-Byte of theAccess-Flags
Bit-Mask, it is because that Bit-Mask is the very next item after the Constant-Pool within the original'.class'
File'sbyte[]
-Array.
- The reason
'10'
would actually need to be added to'tableSizeBytes'
to retrieve that Array-Index, is due to the fact that within the confines of any Java'.class'
-File represented as abyte[]
-Array, the Table / Pool itself does not begin until the 10th byte of the file!
final byte[] bArr = FileRW.readBinary("MyProject/MyPackage/MyClass.class"); final ConstantPool cp = new ConstantPool(bArr); final int pos = cp.tableSizeBytes + 10; final int flags = ((bArr[pos] & 0xFF) << 8) | (bArr[pos + 1] & 0xFF); System.out.println("Access Flags Bit-Mask: " + Integer.toBinaryString(flags));
- Code:
- Exact Field Declaration Expression:
public final int tableSizeBytes;
- The value stored in
-
tags
public final ReadOnlyList<java.lang.Byte> tags
tagNames
List
A Java'.class'
File's Constant-Pool is actually just a table, which is saved as a sequential list of number. Each constant which is defined in this table has at the very beginning of its definition a single one-byte number that defines what "type" or "kind" of constant it is. The types of constants available may all be viewed inside the definition for this Class' FieldtagNames
. Click on tht link to see the list of all tags.
tagNames
is apublic, static
andfinal-ReadOnly
that contains exactly 17, non-null, Category-Names for the 'type' or 'kind' of constants defined in a Constant-Pool. Because the Constant-Pool has been apart of Java'class'
Files since the JDK 1.0 was released, it isn't so easy to up and change the definitions of used by these tables. As such, explaining why the types that would be numbered2, 13
and14
are undefined, but suffice it to say, they the list indices in the Tag-Names table are, indeed, null.
'tags' (this) List
This field is a non-static
- "Instance Field" - that corresponds to whatever'.class'
File's Constant-Pool was parsed to build the instance that holds thispublic, final
(and Read-Only) field.
The values for thetagNames
-List are loaded, once, by the Class-Loader (because that's howstatic
-constant fields work!). The contents of this field are loaded every time a new instance of this class is built by this class' constructor.
The only values that may be stored into this table are the list of values from thetagNames
table which are non-null! That list is precisely equal to the numbers between 1 and 20 - except2, 13
and14
- See Also:
tagNames
- Code:
- Exact Field Declaration Expression:
public final ReadOnlyList<Byte> tags;
-
values
public final ReadOnlyList<java.lang.Object> values
A Java Constant-Pool stores values. These values have "types" or "kinds" - which are all saved into the very first byte of the Constant itself. For any given'.class'
File, if that file's Constant-Pool is loaded into an instance of this class, those "types" or "kids" are saved into thetags
Read-Only List. That list is considered a Parallel-Array to this List, and its length shall always be identical to the length of this List.
This contents of the'values'
Read-Only List are the constants, themselves.
The Types in'values'
:
TheConstantPool
field'values'
is declared as aReadOnlyList<Object>
. What are the types of theseObject's
in the list? The following table shall present the exact Java-Type that should be expected in the'values'
list, listed according to the Type-Kind that would be found in thetags
Parallel-Array Index-Location corresponding to the same element in this, the'values'
list. The following tables will hopefully make all of this more clear.
If: tags.get(1) == TAG_METHOD_REF
Then: values.get(1) instanceof
Ret2
<Integer, Integer>If: tags.get(3) == TAG_UTF_8
Then: values.get(3) instanceof java.lang.String
Note that in this table, alljava.lang.Integer
(other than the type for Tag-KindTAG_INTEGER
itself), the "integer" is intended to signify an "Index-Pointer" (also simply called a "Table-Index") which points back into another cell inside the Constant-Pool Table.
Furthermore, any instance ofRet2<Integer, Integer>
is intended to signify a "Pointer-Pair" - which is just two separate Pointer-Indices back into the Constant-Pool Table.
Tag-Kind Java-Type Purpose 01: TAG_UTF_8 java.lang.String UTF-8 String
-Constant03: TAG_INTEGER java.lang.Integer Actual 'int'
Constant04: TAG_FLOAT java.lang.Float Actual 'float'
Constant05: TAG_LONG java.lang.Long Actual 'long'
Constant06: TAG_DOUBLE java.lang.Double Actual 'double'
Constant07: TAG_CLASS java.lang.Integer Table-Pointer 08: TAG_STRING java.lang.Integer Table-Pointer 09: TAG_FIELD_REF Ret2<Integer, Integer> Table-Pointer Pair 10: TAG_METHOD_REF Ret2<Integer, Integer> Table-Pointer Pair 11: TAG_INTERFACE_METHOD_REF Ret2<Integer, Integer> Table-Pointer Pair 12: TAG_NAME_AND_TYPE Ret2<Integer, Integer> Table-Pointer Pair 15: TAG_METHOD_HANDLE Ret2<Byte, Integer> Kind & Table-Pointer 16: TAG_METHOD_TYPE java.lang.Integer Table-Pointer 17: TAG_DYNAMIC Ret2<Integer, Integer> Table-Pointer Pair 18: TAG_INVOKE_DYNAMIC Ret2<Integer, Integer> Table-Pointer Pair 19: TAG_MODULE java.lang.Integer Table-Pointer 20: TAG_PACKAGE java.lang.Integer Table-Pointer - Code:
- Exact Field Declaration Expression:
public final ReadOnlyList<Object> values;
-
dereferencedValues
public final ReadOnlyList<java.lang.Object> dereferencedValues
The values, but they have been "De-Referenced." De-Referencing the values means removing any and all Table-Index "pointers" which are present inside the Constants within the table, and replacing them with the instantiated objects that have been designed for them.
Dereferenced Types:
The following table shall exhibit which types are actually present inside thedereferencedValues
ReadOnlyList<Object>
. The meaning of "dereferenced" is such that any Constants which are either, solely, Table-Index Pointers to other actual Constants in the Table / Pool - or are combination / pairs of Table-Index Pointers - are instantiated & converted into the actual Java Objects / POJO's which were explicitly designed to represent the data to which these "Reference-Based" constants are pointing.
If that sounds like a mouthful, the concept (which is actually extremely simple), is delineated in the "Types Table" below. The Java POJO-Types associated with the Constant-Pool's "Reference Based" constants are listed here:
Tag-Kind 'values'
Type'dereferencedValues'
POJO9: TAG_FIELD_REF
Ret2<Integer, Integer>
ConstantPool.Reference
10: TAG_METHOD_REF
Ret2<Integer, Integer>
ConstantPool.Reference
11: TAG_INTERFACE_METHOD_REF
Ret2<Integer, Integer>
ConstantPool.Reference
15: TAG_METHOD_HANDLE
Ret2<Byte, Integer>
ConstantPool.MethodHandle
17: TAG_DYNAMIC
Ret2<Integer, Integer>
ConstantPool.Dynamic
18: TAG_INVOKE_DYNAMIC
Ret2<Integer, Integer>
ConstantPool.Dynamic
Simple De-Referenced POJO's
The following five Tag-Kind's, when present in thedereferencedValues
list, will have had theirInteger
-Values replaced by theUTF-8 String
-Values to which they are pointing:- 7:
TAG_CLASS
- 8:
TAG_STRING
- 16:
TAG_METHOD_TYPE
- 19:
TAG_MODULE
- 20:
TAG_PACKAGE
- Code:
- Exact Field Declaration Expression:
@LinkJavaSource(handle="DereferencedValue") public final ReadOnlyList<Object> dereferencedValues;
- 7:
-
indices
public final ReadOnlyList<java.lang.Integer> indices
Original Byte-Array Indices- Code:
- Exact Field Declaration Expression:
public final ReadOnlyList<Integer> indices;
-
indicesGroupBy
public final ReadOnlyList<ReadOnlyList<java.lang.Integer>> indicesGroupBy
This instance field is a Map-Like List. Specifically, it is aList
which functions as a typical JavaMap
whose May "keys" are the Tag-Bytes / Tag-Kinds, while simultneoously at the same time, a Tag-Byte is also the List Index-Pointer into thisReadOnlyList
.
Unused Constant-Pool Tags:
Their are exactly21 - 4
"types" or "kinds" of constants which may be placed in a Java'.class'
File Constant-Pool.21
is because each of these Type-Kinds is associated with a natural number, beginning with0
.-4
is because the natural number's0, 2, 13
and14
are actually not associated with a Constant-Pool 'kind'.
"Why" those four are skipped probably goes back to 1995 when these files were first written. Rather than explain why, instead, just know that when the Instance Field'indicesGroupBy'
is accessed using a List-Index of0, 2, 13
and14
, null shall always be returned.
2-Dimensional List:
Since21 - 4
is, indeed,17
there will be exactly'17'
non-null values residing within the'indicesGroupBy'
list. Each index into the Group-By 2-Dimensional'List'
which is non-null shall hold a single, 1-Dimensional,'List'
of Constant-Pool indices.
The indices which reside in each of these 1-Dimensional'Lists"
are simply pointers into the Constant-Pool table. The indices are sorted acording to their "Type-Kind", and are placed into the 2-D List-Location whose index matches each of the "Type-Kinds" of the 1-D Indice-Lists.
Group-By Exampe:
The following code will hopefully help elucidate the "incantation" printed in the text just above:
Example:
byte[] bArr = FileRW.readData("MyProject/MyPackage/MyClass.class"); ConstantPool cp = new ConstantPool(bArr); for (Integer i : cp.indicesGroupBy.get(ConstantPool.TAG_UTF8)) System.out.println( "constantPool.tags(" + i + "): '" + ConstantPool.tagNames(cp.tags(i)) + "'\n" + "constantPool.values(" + i + "): '" + cp.values(i) + '\'' ); // Prints All UTF-8 Constants: // // ConstantPool.tags(1): 'UTF-8' // ConstantPool.values(1): "Some UTF-8 String-Constant from the Pool" // ConstantPool.tags(2): 'UTF-8' // ConstantPool.values(2): "Another UTF-8 String-Constant from the Pool" // // ... And so on and so forth ...
Null-Safety and Unused-Tags:
Any index into the Top-Level Group-By List whose tag is valid and is allowed for associating a Constant-Pool Type-Kind will never be assigned null. While the following was just explained in this comment several lines above, and holds as per this example below. Indices0, 2, 13
and14
are always empty, and attempting to retrieve a 1-D List of numbers from this table shall always fail, and produce null, instead!
Example:
// Indices 0, 2, 13 and 14 are always empty. Referencing those indices will produce null. byte[] bArr = FileRW.readData("MyProject/MyPackage/MyClass.class"); ConstantPool cp = new ConstantPool(bArr); if (cp.indicesGroupingBy.get(2) == null) // This Statement is guaranteed to execute for all valid ConstantPool instances if (cp.indicesGroupingBy.get(13) != null) // This Statement can NEVER execute, this code is unreachable!
However, all other indices between'1' ... '20'
will never produce null! If there is a tag for a Type-Kind that was never used by the ConstantPool table - meaning that there aren't any of "those kinds" of constant to have been identified by the parser in a particular table, then the 2-D Group-By List will return an Empty-Non-Null 1-D'List'
, rather than null.
For a better understanding of this concept, please review the following code snippet below. Here, a less frequently Constant-Pool Type-Kind tag is retrieved from the 2-D Lookup-Table. If the'.class'
-File which was used to load'bArr'
was brief and didn't have ny "Invoke-Dynamic" constants, the code below is, indeed, null-safe and will not throw anyNullPointerException's
.
Example:
// The Tag Type-Kind for "Invoke-Dynamic" is less commonly used tag. ReadOnlyList<Integer> invokeDynameIndicesList = cp.indicesGroupingBy.get(ConstantPool.TAG_INVOKE_DYNAMIC); // If there are zero such constants, the above list will be EMPTY, NOT NULL ! System.out.println( "Num 'InvokeDynamic' Constants in the Pool: " + invokeDynamicIndicesList.size() ); // It is likely that the above statement will print '0' // It is NOT POSSIBLE for the code above to throw NPE, NullPointerException
- Code:
- Exact Field Declaration Expression:
public final ReadOnlyList<ReadOnlyList<Integer>> indicesGroupBy;
-
tagNames
public static final ReadOnlyList<java.lang.String> tagNames
This is the complete list of available Constant-Pool 'types' or 'kinds'. Each entry in a Java'.class'
-File's Constant-Pool has a type which is stored in the first byte of the constant. That byte must be one of the 17, non-null, types or kinds which are specified in this list! Please review the exact names of the kinds of constants which may be defined inside of a Java Constant-Pool in the list below.
tags
List
When this class is used to parse the Constant-Pool of a Java'.class'
File, to create an instance of this class, that instance shall have twopublic
andfinal
-ReadOnly Lists that store the information needed for each constant. Those two Lists are considered "Parallel Arrays" (or, preferably, "Parallel-Lists"). They are Instance-Fields, and they are named:tags
andvalues
.
TheString's
listed in this table will correspond to the names of the 'kinds' or 'types' of constants in the instance. Thetags
Parallel-Array contains that kind, specifying the Constant's type. The name for that contants type/kind may be looked up in thisstatic, final
andReadOnly
lookup-table list.
Tag #5 is stored in List-Index #5; while Tag #10 is saved to List-Index #10, and so on, and so forth...
You may view the contents of this Lookup-Table by either clicking the "External Java" Button (directly above) to see the actual Data-File which loads thisstatic, final
Read-Only Lookup-Table, or by reviewing the following HTML-Table.
ThisReadOnlyList
contains exactly 21 elements, and these elements are listed, explicitly, below:
Tag Index Name as String 0 UNUSED, null
1: TAG_UTF_8
"UTF-8"
2 UNUSED, null
3: TAG_INTEGER
"Integer"
4: TAG_FLOAT
"Float"
5: TAG_LONG
"Long"
6: TAG_DOUBLE
"Double"
7: TAG_CLASS
"Class"
8: TAG_STRING
"String"
9: TAG_FIELD_REF
"FieldRef"
10: TAG_METHOD_REF
"MethodRef"
11: TAG_INTERFACE_METHOD_REF
"InterfaceMethodRef"
12: TAG_NAME_AND_TYPE
"NameAndType"
13 UNUSED, null
14 UNUSED, null
15: TAG_METHOD_HANDLE
"MethodHandle"
16: TAG_METHOD_TYPE
"MethodType"
17: TAG_DYNAMIC
"Dynamic"
18: TAG_INVOKE_DYNAMIC
"InvokeDynamic"
19: TAG_MODULE
"Module"
20: TAG_PACKAGE
"Package"
- See Also:
List of Types/Kinds for each Constant
,List of Values for each Constant
- Code:
- Exact Field Declaration Expression:
@LinkJavaSource(handle="ConstantPoolData", entity=FIELD, name="tagNames") public static final ReadOnlyList<String> tagNames = dataFileR2.GET(1);
-
tagWidthBytes
public static final ReadOnlyList<java.lang.Byte> tagWidthBytes
The values saved into this table specify how many bytes of data are used for each of the 17 available constants.
Likely, thispublic, static
andfinal
-ReadOnly table is of little use to the end user of thisConstantPool
class. However, with the advent of the'ReadOnly'
-Package, leaving this as a public list / table certainly isn't causing anybody any harm.
You may view the contents of this Lookup-Table by either clicking the "External Java" Button (directly above) to see the actual Data-File which loads thisstatic, final
Read-Only Lookup-Table, or by reviewing the following HTML-Table.
ThisReadOnlyList
contains exactly 21 elements, and these elements are listed, explicitly, below:
Tag Index Width 0 UNUSED, null
1: TAG_UTF_8
Variable-Width 2 UNUSED, null
3: TAG_INTEGER
4 Bytes 4: TAG_FLOAT
4 Bytes 5: TAG_LONG
8 Bytes 6: TAG_DOUBLE
8 Bytes 7: TAG_CLASS
2 Bytes 8: TAG_STRING
2 Bytes 9: TAG_FIELD_REF
4 Bytes 10: TAG_METHOD_REF
4 Bytes 11: TAG_INTERFACE_METHOD_REF
4 Bytes 12: TAG_NAME_AND_TYPE
4 Bytes 13 UNUSED, null
14 UNUSED, null
15: TAG_METHOD_HANDLE
3 Bytes 16: TAG_METHOD_TYPE
2 Bytes 17: TAG_DYNAMIC
4 Bytes 18: TAG_INVOKE_DYNAMIC
4 Bytes 19: TAG_MODULE
2 Bytes 20: TAG_PACKAGE
2 Bytes - Code:
- Exact Field Declaration Expression:
@LinkJavaSource(handle="ConstantPoolData", entity=FIELD, name="tagWidthBytes") public static final ReadOnlyList<Byte> tagWidthBytes = dataFileR2.GET(2);
-
TAG_UTF_8
public static final byte TAG_UTF_8
Tag byte indicating the next constant in the table is a UTF-8 encodedString
. Typically used for identifiers such as names or other textual data.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_UTF_8 = 1;
-
TAG_INTEGER
public static final byte TAG_INTEGER
Tag byte indicating the next constant in the table is a 32-bit integer. Used for numeric constants within the constant pool.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_INTEGER = 3;
-
TAG_FLOAT
public static final byte TAG_FLOAT
Tag byte indicating the next constant in the table is a 32-bit floating-point number. Represents constants of thefloat
data type.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_FLOAT = 4;
-
TAG_LONG
public static final byte TAG_LONG
Tag byte indicating the next constant in the table is a 64-bit long integer. Represents constants of thelong
data type.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_LONG = 5;
-
TAG_DOUBLE
public static final byte TAG_DOUBLE
Tag byte indicating the next constant in the table is a 64-bit double-precision floating-point number. Represents constants of thedouble
data type.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_DOUBLE = 6;
-
TAG_CLASS
public static final byte TAG_CLASS
Tag byte indicating the next constant in the table is a class reference. Represents aClass
or interface type.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_CLASS = 7;
-
TAG_STRING
public static final byte TAG_STRING
Tag byte indicating the next constant in the table is aString
reference. The referencedString
is stored elsewhere in the constant pool.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_STRING = 8;
-
TAG_FIELD_REF
public static final byte TAG_FIELD_REF
Tag byte indicating the next constant in the table is a reference to a field. Used for fields within aClass
or interface.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_FIELD_REF = 9;
-
TAG_METHOD_REF
public static final byte TAG_METHOD_REF
Tag byte indicating the next constant in the table is a reference to a method. Refers to methods defined in aClass
.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_METHOD_REF = 10;
-
TAG_INTERFACE_METHOD_REF
public static final byte TAG_INTERFACE_METHOD_REF
Tag byte indicating the next constant in the table is a reference to an interface method. Refers to methods declared within an interface.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_INTERFACE_METHOD_REF = 11;
-
TAG_NAME_AND_TYPE
public static final byte TAG_NAME_AND_TYPE
Tag byte indicating the next constant in the table is a name-and-type descriptor. Encodes the name and type of a field or method.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_NAME_AND_TYPE = 12;
-
TAG_METHOD_HANDLE
public static final byte TAG_METHOD_HANDLE
Tag byte indicating the next constant in the table is a method handle. Represents a reference to a method handle for dynamic invocation.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_METHOD_HANDLE = 15;
-
TAG_METHOD_TYPE
public static final byte TAG_METHOD_TYPE
Tag byte indicating the next constant in the table is a method type. Encodes the method's descriptor, including parameter and return types.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_METHOD_TYPE = 16;
-
TAG_DYNAMIC
public static final byte TAG_DYNAMIC
Tag byte indicating the next constant in the table is a dynamic constant. Represents a runtime-computed constant, often linked to lambdas or invokedynamic instructions.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_DYNAMIC = 17;
-
TAG_INVOKE_DYNAMIC
public static final byte TAG_INVOKE_DYNAMIC
Tag byte indicating the next constant in the table is an invoke-dynamic constant. Represents a bootstrap method and dynamic call site for runtime resolution.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_INVOKE_DYNAMIC = 18;
-
TAG_MODULE
public static final byte TAG_MODULE
Tag byte indicating the next constant in the table is a module reference. Encodes the name of a module within the constant pool.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_MODULE = 19;
-
TAG_PACKAGE
public static final byte TAG_PACKAGE
Tag byte indicating the next constant in the table is a package reference. Encodes the name of a package within the constant pool.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final byte TAG_PACKAGE = 20;
-
-
Constructor Detail
-
ConstantPool
public ConstantPool(byte[] bArr)
Constructs an intance of this class using a pre-loadedbyte[]
-Array. Here below is an exmple of loading a Java'.class'
File into memory, and invoking this constructor in order to instantiate an instance of this class.
In this exmple, the names of allMethod References
andInterface-Method References
are printed out to the terminal.
Example:
byte[] bArr = FileRW.readData("MyProject/MyPackage/MyClass.class"); ConstantPool cp = new ConstantPool(bArr); cp.allMethodNames().forEach(System.out::println);
- Parameters:
bArr
- A Class-File which has been loaded from disk into a memory, as a Javabyte[]
Array.
-
-
Method Detail
-
toString
public java.lang.String toString()
Converts the contents of an instance of this class into a User-ReadableString
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- A Java-
String
which explicates a Java'.class'
File's Constant-Pool. - Code:
- Exact Method Body:
return CPToString.str(this.tags, this.values, this.indicesGroupBy);
-
equals
public boolean equals(java.lang.Object other)
Checks for equality with another instance ofConstantPool
.- Overrides:
equals
in classjava.lang.Object
- Parameters:
other
- Any Java-Object
, but only an instance ofConstantPool
whosetags
andvalues
Array-Lists are equal will cause this method to returnTRUE
.- Returns:
TRUE
if and only if the contents'this'
instance of Constant-Pool equals those found in parameter'other'
- Code:
- Exact Method Body:
if (! (other instanceof ConstantPool)) return false; ConstantPool o = (ConstantPool) other; return this.tags.equals(o.tags) && this.values.equals(o.values) && this.indices.equals(o.indices);
-
hashCode
public int hashCode()
Java's traditional Hash-Code generator Method.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- a (plausibly unique) Hash-Code, which may be used for placing instances of
ConstantPool
into Hash-Tables. - Code:
- Exact Method Body:
// No, this isn't any good... To-Do: Worry about it... int hash = 0; for (Byte tag : this.tags) if (tag != null) hash += tag; return hash;
-
allFieldNames
public java.util.stream.Stream<java.lang.String> allFieldNames()
Retrieves the names of all Field-References which were listed in the Constant-Pool table that was used to build'this'
instance.- Returns:
- A Java-
Stream
containing the names of any / all fields from the table. - Code:
- Exact Method Body:
return AllNames.getAllFieldNames( this.indicesGroupBy.get(ConstantPool.TAG_FIELD_REF), this.values );
-
allMethodNames
public java.util.stream.Stream<java.lang.String> allMethodNames()
Retrieves the names of all Method-References and Interface-Method-References which were listed in the Constant-Pool that was used to build'this'
instance.
Type-Kinds Available:Method's
: are identified bybyte
TAG_METHOD_REF
which hasbyte
-value'10'
.Interface-Method's
are identified bybyte
TAG_INTERFACE_METHOD_REF
, which hasbyte
-value'11'
.
- Returns:
- A Java-
Stream<String>
containing the names of any / all methods and "Method References" from the Constant-Pool Table. - Code:
- Exact Method Body:
return AllNames.getAllMethodNames( this.indicesGroupBy.get(ConstantPool.TAG_METHOD_REF), this.indicesGroupBy.get(ConstantPool.TAG_INTERFACE_METHOD_REF), this.values );
-
allFields
public java.util.stream.Stream<ConstantPool.Reference> allFields()
- Returns:
- All Field-References contained by
'this'
table. The JavaStream
which is returned contains instances ofConstantPool.Reference
. - See Also:
dereferencedValues
,indicesGroupBy
- Code:
- Exact Method Body:
return this.indicesGroupBy .get(ConstantPool.TAG_FIELD_REF) .stream() .map((Integer index) -> (ConstantPool.Reference) this.dereferencedValues.get(index));
-
allMethods
public java.util.stream.Stream<ConstantPool.Reference> allMethods()
- Returns:
- All Method-References contained by
'this'
table. The JavaStream
which is returned contains instances ofConstantPool.Reference
. - See Also:
dereferencedValues
,indicesGroupBy
- Code:
- Exact Method Body:
return Stream.concat( this.indicesGroupBy.get(ConstantPool.TAG_METHOD_REF).stream(), this.indicesGroupBy.get(ConstantPool.TAG_INTERFACE_METHOD_REF).stream() ) .map((Integer index) -> (ConstantPool.Reference) this.dereferencedValues.get(index));
-
-