|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.rode.thesis.meta.reflect.IdentifiedStackTraceElement
public class IdentifiedStackTraceElement
An identified stack trace element is used to identify a stack trace element representing the calling context.
The calling context can be:
element
, and the entire call-stack in
stack
. The zeroth element of the call-stack represents the
top of the stack, corresponding to one of the constructors declared
in this class or in a sub-class.
However, a stack offset can be specified at construction time
to identify a stack trace element from anywhere within the call-stack
(from the top of the stack), corresponding to a relative calling
context. The field offset
represents the stack offset used,
relative to the top of the stack: zero = top of stack, 1 = second element,
2 = third element, etc.
All instances of this class are immutable. If an instance can be created, a stack trace element will have been identified.
Example: a class Foo
defined in the Foo.java
file:
01 class Foo { 02 // Static initialisation block... 03 static { 04 System.out.print(new IdentifiedStackTraceElement().toString()); 05 } 06 07 // Instance initialisation block... 08 { 09 System.out.print(new IdentifiedStackTraceElement().toString()); 10 } 11 12 // Constructor... 13 public Foo() { 14 System.out.print(new IdentifiedStackTraceElement().toString()); 15 } 16 17 public void bar() { 18 System.out.print(new IdentifiedStackTraceElement().toString()); 19 this.barbar(); 20 } 21 22 public void barbar() { 23 System.out.print(new IdentifiedStackTraceElement(1).toString()); 24 } 25 }Line 4 generates the following output (the precise format of the output is dependent on the JVM):
"Foo.<clinit>(Foo.java:4)"Line 9 and 14 generates the following output, respectively:
"Foo.<init>(Foo.java:9)" and "Foo.<init>(Foo.java:14)"Line 18 and 23 generate the same output (provided access through
bar()
), since a stack offset (1) is specified in barbar()
:
"Foo.bar(Foo.java:18)"Note, that a JVM is not required to supply stack traces!
Field Summary | |
---|---|
StackTraceElement |
element
The identified stack trace element. |
int |
offset
The offset of element from the top of the call-stack
stored in stack . |
protected StackTraceElement[] |
stack
The call-stack used to identify the stack trace element stored in element . |
protected Throwable |
throwable
The throwable that supplied the stack trace stored in stack . |
Constructor Summary | |
---|---|
IdentifiedStackTraceElement()
No-arg constructor, which identifies the stack trace element corresponding to the calling context that invoked this constructor. |
|
IdentifiedStackTraceElement(IdentifiedStackTraceElement element)
Copy constructor, which identifies the same stack trace element as element in the same
call-stack at the same offset. |
|
IdentifiedStackTraceElement(IdentifiedStackTraceElement element,
int offset)
Constructor, which identifies the stack trace element at offset in the call-stack used by
element . |
|
IdentifiedStackTraceElement(int offset)
Constructor, which identifies the stack trace element at offset from the top of the current call-stack. |
|
IdentifiedStackTraceElement(Throwable throwable,
int offset)
Constructor, which identifies the stack trace element at offset from the top of the call-stack acquired
via throwable ; hence, this constructor need
not be in the call-stack at all! |
Method Summary | |
---|---|
boolean |
equals(Object object)
Returns true if object has the same type
as this element, same offset, same element, and
same stack trace, otherwise false. |
StackTraceElement[] |
getStackTrace()
Returns a copy of the call-stack used by this identified stack trace element. |
int |
hashCode()
Returns the hash code of this identified stack trace element. |
void |
printStackTrace(Log out)
Prints the call-stack to out with a
severity level of error. |
String |
toString()
Returns the string representation of this identified stack trace element. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public final StackTraceElement element
It represents the point of origin where this instance
was constructed from relative to the offset
supplied at construction time.
Never null.
public final int offset
element
from the top of the call-stack
stored in stack
. A value of zero represents the top of the call-stack.
Invariant: stack
[offset] ==
element
is
always true.
protected final StackTraceElement[] stack
element
.
The zeroth element of this call-stack represents the top of
the call-stack, corresponding to one of the constructors
declared in this class or in a sub-class. Hence, offset
zero =
top of stack, 1 = second element, 2 = third element, etc.
Never null.
protected final Throwable throwable
stack
. Never null.
Constructor Detail |
---|
public IdentifiedStackTraceElement()
public IdentifiedStackTraceElement(IdentifiedStackTraceElement element)
element
in the same
call-stack at the same offset.
element
- The identified stack trace element to copy;
cannot be null.
NullPointerException
- If element
is null.IdentifiedStackTraceElement(IdentifiedStackTraceElement element, int offset)
offset
in the call-stack used by
element
. The offset is used as is.
element
- The identified stack trace element;
cannot be null.offset
- The offset from the top of the call-stack;
must be within bounds.
NullPointerException
- If element
is null.
IndexOutOfBoundsException
- If offset
does not
denote a valid stack element.public IdentifiedStackTraceElement(int offset)
offset
from the top of the current call-stack. Invoking with a value of zero corresponds to invoking the default constructor, i.e., the identified stack trace element corresponds to the context that invoked this constructor; a value of one will resolve the context to the calling context of this constructor; and so forth.
offset
- The offset from the top of the call-stack;
must be within bounds.
IndexOutOfBoundsException
- If offset
does not
denote a valid stack element.public IdentifiedStackTraceElement(Throwable throwable, int offset)
offset
from the top of the call-stack acquired
via throwable
; hence, this constructor need
not be in the call-stack at all!
throwable
- The throwable to supply the call-stack;
cannot be null.offset
- The offset from the top of the call-stack;
must be within bounds.
NullPointerException
- If throwable
is null.
IndexOutOfBoundsException
- If offset
does not
denote a valid stack element.Method Detail |
---|
public boolean equals(Object object)
object
has the same type
as this element, same offset, same element, and
same stack trace, otherwise false.
equals
in class Object
object
- The object to test; can be null.
public StackTraceElement[] getStackTrace()
public int hashCode()
hashCode
in class Object
public void printStackTrace(Log out)
out
with a
severity level of error.
out
- The log to use; cannot be null.
NullPointerException
- If out
is null.public String toString()
The representation is that of the identified StackTraceElement
.
toString
in class Object
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |