public class Pointer<T> extends java.lang.Object implements java.lang.Comparable<Pointer<T>>
BYADDR
parameter declarations and the ADDR()
builtin functions.
Direct pointers can be typed, e.g., Pointer<FixedBin>
or Pointer<MyLevel1>
, or untyped, e.g,
Pointer<Object>
or Pointer<?>
.
Direct pointers may be assigned and referenced to access the underlying object.
In Java, the assignment results in replacing the object instance to which
the pointer refers (Java cannot update the value of immutable classes like Integer or String).
So long as programs always refer to the pointer (via its ref()
method) it will
appear as if the "immutable" object is changeable.
Typed and untyped Pointers are efficient when used in these ways.
Typed direct pointers contain a reference to another variable as in one passed to
a BYADDR PROCEDURE parameter as new Pointer<FixedBin>(MyFB53)
the reference would hold the object pointer to MyFB53 as in,
The type for theFixedBin MyFB53 = new FixedBin("10.111B", 5,3); // 2-7/8ths Pointer<FixedBin> typedPointer = new Pointer<FixedBin>(MyFB53); System.out.println(typedPointer.ref().toString()); // 2.875
Pointer<?>
constructor and the variable type
must agree (for structures the Group type would be and Group methods perform
mapping among disparate Groups). The ref() function returns the Object reference of the type
associated with the Pointer and assign(x) assigns the Pointer reference to the
new Object also of the same type. In other words, you cannot assign typed
pointers an object reference that differs in type
Untyped direct pointers are declared and used to hold Object objects as in,// *** DOES NOT WORK *** Short MyShort = Short.valueOf(22); Pointer<Long> = badPointer = new Pointer<Long>(MyShort); // compile error
A reference to a untyped direct pointers can't be used directly, of course, but can be cast to the type they hold as in,Short MyShort = Short.valueOf(22); Pointer<Object> untypedPointer = new Pointer<Object>(MyShort);
A typed or untyped indirect handle is used to reference a variable of a different type when it is desirable to indirectly map between the different classes. This is most often used when the PL/I program requires a redefine of one variable to the type of another as in the PL/I constructif (untypedPointer.ref() instanceof Short) { short s = ((Short) untypedPointer.ref()).shortValue(); ... }
in which case variable mapping occurs between the two. Handles are created by constructing aDCL TABULADOR_ASCII BIT (08) INIT('00000101'B); DCL TAB_ASCII CHAR(01) BASED(ADDR(TABULADOR_ASCII));
Pointer
with a Pointer
, such as,
or by assigning a Pointer with another pointer, such as,@CHAR(2)
Pointer<String> MyChar = new Pointer<String>(); MyChar.assign(new String("ab")); Pointer<Short> handlePointer = new Pointer<Short>(MyChar2); if (handlePointer.ref().shortValue() == (short) 0x6162) return true;
The ref() and assign() methods of a handle refer to the underlying object associated with the handle and mapping to it occurs with each reference. (assign does not reset a handle to a different object pointer). Since effectively a type conversion occurs with each use of a handle they are slower than direct Pointers. The epli compiler will produce typed pointers when the POINTER is only used with a single object so that Java casts are not necessary to "dereference". The@CHAR(2)
Pointer<String> MyChar = new Pointer<String>(); MyChar.assign(new String("ab")); Pointer<Short> handlePointer = new Pointer<Short>(); handlePointer.assign(MyChar); if (handlePointer.ref().shortValue() == (short) 0x6162) return true;
ref()
method is used to "dereference" a Pointer. Dereferencing NULL pointers will throw
the NULLPOINTER
ERROR Condition
.
PL/I code
Java codeDCL P POINTER; DCL 1 A BASED(P), 2 B FIXED BIN(15), 2 C FIXED BIN(31), 2 D CHAR(10); ALLOCATE A SET(P); P->C = 11;
This is a wrapper class of either a typed or untyped object class primarily to cover raising the PL/Iclass A extends Group {@OFF(0)
b Short = new Short((short)0);@OFF(2)
c Integer = Integer.valueOf((int)0);@OFF(6)
@CHAR(10)
d String = new String(" "); } Pointer<A> p = new Pointer<A>(new A()); p.ref().c = 11;
NULLPOINTER
ERROR
Condition
instead of Java NullPointerException.
Note: There are several notions of "assigning a pointer" depending on the
type of pointer and desired outcome.
Dereference a pointer with ref()
and use
an assignment statement to change the value of a field within a group (e.g.,
myPtrToGroupA.ref().myFieldOfGroupA = 22;
).
Use assign(Object)
to assign
either the pointer reference in this or the handle's pointer. Use
assign(Pointer)
to assign the handle itself to refer
to a different REDEFINED object.
Use assign(Object, Area)
to change a portion of the object to which
this handle Pointer refers.
Finally, dereferencing a pointer with ref()
and replacing the entire
object with a new object instance (e.g., MyPtrToInteger.ref() = Integer.valueOf(22);
probably never results in the desired effect because the reference within the pointer
refers to a old object rather than the one being assigned. The assign(Object)
(e.g., MyPtrToInteger.assign(Integer.valueOf(22);
) should be used in this case.
Modifier and Type | Field and Description |
---|---|
static int |
BYTES
size in bytes if converted to a byte array
|
T |
model
it is so difficult to find the class of T at runtime, and in many
ways impossible due to javac's _class erasure_, we require a "model"
object (any instance of T, which is not modified) to be passed when
creating handles to assist in pointer datatype conversions.
|
static Pointer<java.lang.Object> |
NULL
an undefined POINTER
|
protected T |
ref
the Object to which this static Pointer refers
|
Constructor and Description |
---|
Pointer()
Construct a untyped null Pointer.
|
Pointer(EPLITYPES type)
Construct a typed null Pointer.
|
Pointer(T ref)
Construct a typed Pointer referencing another object of that type.
|
Pointer(T model,
Pointer<?> handle)
Construct a handle mapped to an object contained within another pointer.
|
Pointer(T model,
Pointer<?> handle,
int offset)
Construct a handle mapped to an object contained within another pointer
with a offset, as is the case in a variable DEFINED on another
with a POSITION, as in,
DCL A CHAR(4); // 4 chars, overlaid with
DCL B DEFINED A CHAR(2) POSITION(3); // last 2 chars of A
|
Modifier and Type | Method and Description |
---|---|
Pointer<T> |
assign(int off,
int len,
java.lang.Object source)
Assign a portion of a (usually) Group member of class T
a value referred to by this HANDLE.
|
Pointer<T> |
assign(Pointer<T> ptr)
Assign a handle to a new object pointer.
|
Pointer<T> |
assign(T source)
Assign the pointer a reference to the given object of type T.
|
Pointer<T> |
assign(T ref,
Area inArea)
Assign a pointer within an area a new object reference.
|
int |
compareTo(Pointer<T> op2)
compareTo - compare two typed pointers
|
boolean |
equal(java.lang.Object ob2)
equal - compare two typed pointers
|
void |
free()
Invalidate the object reference.
|
void |
free(boolean ignore)
Invalidate the object reference, ignoring the ONNULL condition.
|
Pointer<T> |
fromBytes(byte[] bytes)
turn a sequence of bytes into a Pointer
|
Pointer<?> |
handle()
Return the "handle" to an object of this type T used with this HANDLE or
"template" structure associated with this HANDLE or REDEFINED structure.
|
boolean |
isNull()
return true if this Pointer is null
|
static void |
main(java.lang.String[] args)
Pointer unit test
|
T |
model()
Return the "model" or "template" of the class on which this Pointer
|
T |
ref()
De-reference the POINTER by returning the object, regardless of whether
the Pointer is used to hold direct pointers or indirect handles.
|
T |
ref(EPLITYPES check)
De-reference the POINTER by returning the object, first checking to
see if it is a valid POINTER reference for this variable type.
|
T |
ref(java.lang.String check)
De-reference the POINTER by returning the object, first checking to
see if it is a valid POINTER reference for this variable type.
|
Pointer<T> |
refAssign(byte[] bytes)
assign the underlying (referenced) structure to which this POINTER points the
sequence of bytes
|
byte[] |
toBytes()
turn a Pointer into a sequence of bytes
|
java.lang.String |
toString()
for debugging info, dump ptr info, put list ptr, etc.
|
EPLITYPES |
typeof()
return the EPLITYPES type of the object pointed to by this pointer or handle
|
public static final int BYTES
public static final Pointer<java.lang.Object> NULL
protected T ref
public final T model
public Pointer()
public Pointer(EPLITYPES type)
type
- - the EPLITYPES (e.g., FIXEDBIN31).public Pointer(T ref)
ref
- - an object of type T that is the reference
to the variable this pointer holds.public Pointer(T model, Pointer<?> handle)
model
- - an object of type T that helps in
converting a handle's value from its type T to this type T.
Required because of Javas type erasure concept for
parameterized classes. Helper's value is inconsequential,
but typically ZERO.handle
- - a pointer of a different type other than T
to which this pointer is mapped.public Pointer(T model, Pointer<?> handle, int offset)
model
- - an object of type T that helps in
converting a handle's value from its type T to this type T.
Required because of Javas type erasure concept for
parameterized classes. Helper's value is inconsequential,
but typically ZERO.handle
- - a pointer of a different type other than T
to which this pointer is mapped.offset
- - an int indicating the offset within the
handle the model object is to pointpublic T ref()
ERROR
- Condition
with NULLPOINTER for null pointers.public T ref(java.lang.String check)
check
- - a String representation of the Java class name
to verify before returning the pointer.ERROR
- Condition
with NULLPOINTER or WRONGPOINTER for null or
invalid pointers.public T ref(EPLITYPES check)
check
- - an EPLITYPES of the PL/I datatype
to verify before returning the pointer.ERROR
- Condition
with NULLPOINTER or WRONGPOINTER for null or
invalid pointers.public Pointer<T> assign(T source)
source
- the object to assign to this pointer or handle.ERROR
- Condition
with NULLPOINTER or WRONGPOINTER for null or
invalid pointers.public Pointer<T> assign(int off, int len, java.lang.Object source)
off
- - the offset within the handle's reference to begin assigninglen
- - the length from the offset within the handle's reference to assignsource
- - the data to assignpublic Pointer<T> assign(Pointer<T> ptr)
ptr
- the Pointer to replace this with.ERROR
- Condition
WRONGPOINTER for null or
invalid pointers.public Pointer<T> assign(T ref, Area inArea)
ref
- the object this Pointer points to.public Pointer<T> refAssign(byte[] bytes)
bytes
- - sequence of bytes representing a Pointerpublic Pointer<?> handle()
ref()
or assign(Object)
is the better way of doing it.ERROR
- Condition
if you are using a model with a regular Pointer and
not one that refers to another Pointer as obtained from the ADDR() function (i.e., a handle)public T model()
ERROR
- Condition
if you are using a model with a regular Pointer and
not one that refers to another Pointer as obtained from the ADDR() function (i.e., a handle)public void free()
public void free(boolean ignore)
public boolean isNull()
public EPLITYPES typeof()
public byte[] toBytes()
public Pointer<T> fromBytes(byte[] bytes)
bytes
- - sequence of bytes representing a PointerERROR
- Condition
if the bytes don't represent a Pointer
previously Remembered in this JVMpublic java.lang.String toString()
toString
in class java.lang.Object
public static void main(java.lang.String[] args)
public int compareTo(Pointer<T> op2)
compareTo
in interface java.lang.Comparable<Pointer<T>>
op2
- - the operand to compare topublic boolean equal(java.lang.Object ob2)
ob2
- - the operand to compare to, which should be a Pointer