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_8java.lang.StringNone Provided 2 UNUSED 3: TAG_INTEGERjava.lang.IntegerNone Provided 4: TAG_FLOATjava.lang.FloatNone Provided 5: TAG_LONGjava.lang.LongNone Provided 6: TAG_DOUBLEjava.lang.DoubleNone Provided 7: TAG_CLASSjava.lang.Integer
(Table-Pointer)None Provided 8: TAG_STRINGjava.lang.Integer
(Table-Pointer)None Provided 9: TAG_FIELD_REFRet2<Integer, Integer>(Pointer-Pair)ConstantPool.Reference10: TAG_METHOD_REFRet2<Integer, Integer>(Pointer-Pair)ConstantPool.Reference11: TAG_INTERFACE_METHOD_REFRet2<Integer, Integer>(Pointer-Pair)ConstantPool.Reference12: TAG_NAME_AND_TYPERet2<Integer, Integer>(Pointer-Pair)None Provided 13 UNUSED 14 UNUSED 15: TAG_METHOD_HANDLERet2<Byte, Integer>(Method 'Kind' & Table-Pointer)ConstantPool.MethodHandle16: TAG_METHOD_TYPEjava.lang.Integer
(Table-Pointer)None Provided 17: TAG_DYNAMICRet2<Integer, Integer>(Pointer-Pair)ConstantPool.Dynamic18: TAG_INVOKE_DYNAMICRet2<Integer, Integer>(Pointer-Pair)ConstantPool.Dynamic19: TAG_MODULEjava.lang.Integer
(Table-Pointer)None Provided 20: TAG_PACKAGEjava.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,915 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 classConstantPool.Dynamicstatic classConstantPool.MethodHandlestatic classConstantPool.Reference
-
Field Summary
Instance Fields: Constant-Pool Table-Size Modifier and Type Field inttableSizeinttableSizeBytesInstance Fields: All Table Entries: Constant-Kind, Value & byte[]-Array Index, as Parallel-Lists Modifier and Type Field ReadOnlyList<Integer>indicesReadOnlyList<Byte>tagsReadOnlyList<Object>valuesInstance Fields: Constant-Pool Table-Indices, Grouped By 'Type-Kind' Modifier and Type Field ReadOnlyList<ReadOnlyList<Integer>>indicesGroupByInstance Fields: Constant-Pool Values, Pointers De-Referenced into Objects Modifier and Type Field ReadOnlyList<Object>dereferencedValuesStatic, Final Fields: The Names and Sizes of each 'Type-Kind' Modifier and Type Field static ReadOnlyList<String>tagNamesstatic ReadOnlyList<Byte>tagWidthBytesStatic, Final Fields: The JDK / Java Assigned Tag-Number of each 'Type-Kind' Modifier and Type Field static byteTAG_CLASSstatic byteTAG_DOUBLEstatic byteTAG_DYNAMICstatic byteTAG_FIELD_REFstatic byteTAG_FLOATstatic byteTAG_INTEGERstatic byteTAG_INTERFACE_METHOD_REFstatic byteTAG_INVOKE_DYNAMICstatic byteTAG_LONGstatic byteTAG_METHOD_HANDLEstatic byteTAG_METHOD_REFstatic byteTAG_METHOD_TYPEstatic byteTAG_MODULEstatic byteTAG_NAME_AND_TYPEstatic byteTAG_PACKAGEstatic byteTAG_STRINGstatic byteTAG_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 booleanequals(Object other)inthashCode()StringtoString()
-
-
-
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&dereferencedValuesis 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,
LongandDoubleconstants - 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 finalconstant shall be precisely equal to the Byte-Number, within the inputbyte[]-Array, where theAccess-FlagsUnsigned-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-FlagsBit-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
tagNamesList
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.
tagNamesis apublic, staticandfinal-ReadOnlythat 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, 13and14are 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 thetagNamestable which are non-null! That list is precisely equal to the numbers between 1 and 20 - except2, 13and14- 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 thetagsRead-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':
TheConstantPoolfield'values'is declared as aReadOnlyList<Object>. What are the types of theseObject'sin 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 thetagsParallel-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_REFThen: values.get(1) instanceofRet2<Integer, Integer>If: tags.get(3) == TAG_UTF_8Then: values.get(3) instanceof java.lang.String
Note that in this table, alljava.lang.Integer(other than the type for Tag-KindTAG_INTEGERitself), 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 thedereferencedValuesReadOnlyList<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_REFRet2<Integer, Integer>ConstantPool.Reference10: TAG_METHOD_REFRet2<Integer, Integer>ConstantPool.Reference11: TAG_INTERFACE_METHOD_REFRet2<Integer, Integer>ConstantPool.Reference15: TAG_METHOD_HANDLERet2<Byte, Integer>ConstantPool.MethodHandle17: TAG_DYNAMICRet2<Integer, Integer>ConstantPool.Dynamic18: TAG_INVOKE_DYNAMICRet2<Integer, Integer>ConstantPool.Dynamic
Simple De-Referenced POJO's
The following five Tag-Kind's, when present in thedereferencedValueslist, 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 aListwhich functions as a typical JavaMapwhose 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.21is because each of these Type-Kinds is associated with a natural number, beginning with0.-4is because the natural number's0, 2, 13and14are 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, 13and14, null shall always be returned.
2-Dimensional List:
Since21 - 4is, indeed,17there 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, 13and14are 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.
tagsList
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 twopublicandfinal-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:tagsandvalues.
TheString'slisted in this table will correspond to the names of the 'kinds' or 'types' of constants in the instance. ThetagsParallel-Array contains that kind, specifying the Constant's type. The name for that contants type/kind may be looked up in thisstatic, finalandReadOnlylookup-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, finalRead-Only Lookup-Table, or by reviewing the following HTML-Table.
ThisReadOnlyListcontains exactly 21 elements, and these elements are listed, explicitly, below:
Tag Index Name as String 0 UNUSED, null1: TAG_UTF_8"UTF-8"2 UNUSED, null3: 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, null14 UNUSED, null15: 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, staticandfinal-ReadOnly table is of little use to the end user of thisConstantPoolclass. 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, finalRead-Only Lookup-Table, or by reviewing the following HTML-Table.
ThisReadOnlyListcontains exactly 21 elements, and these elements are listed, explicitly, below:
Tag Index Width 0 UNUSED, null1: TAG_UTF_8Variable-Width 2 UNUSED, null3: TAG_INTEGER4 Bytes 4: TAG_FLOAT4 Bytes 5: TAG_LONG8 Bytes 6: TAG_DOUBLE8 Bytes 7: TAG_CLASS2 Bytes 8: TAG_STRING2 Bytes 9: TAG_FIELD_REF4 Bytes 10: TAG_METHOD_REF4 Bytes 11: TAG_INTERFACE_METHOD_REF4 Bytes 12: TAG_NAME_AND_TYPE4 Bytes 13 UNUSED, null14 UNUSED, null15: TAG_METHOD_HANDLE3 Bytes 16: TAG_METHOD_TYPE2 Bytes 17: TAG_DYNAMIC4 Bytes 18: TAG_INVOKE_DYNAMIC4 Bytes 19: TAG_MODULE2 Bytes 20: TAG_PACKAGE2 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 thefloatdata 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 thelongdata 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 thedoubledata 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 aClassor 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 aStringreference. The referencedStringis 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 aClassor 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 ReferencesandInterface-Method Referencesare 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:
toStringin classjava.lang.Object- Returns:
- A Java-
Stringwhich 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:
equalsin classjava.lang.Object- Parameters:
other- Any Java-Object, but only an instance ofConstantPoolwhosetagsandvaluesArray-Lists are equal will cause this method to returnTRUE.- Returns:
TRUEif 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:
hashCodein classjava.lang.Object- Returns:
- a (plausibly unique) Hash-Code, which may be used for placing instances of
ConstantPoolinto 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-
Streamcontaining 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 bybyteTAG_METHOD_REFwhich hasbyte-value'10'.Interface-Method'sare identified bybyteTAG_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 JavaStreamwhich 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 JavaStreamwhich 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));
-
-