Detect if current piece of code is called by own classes:

[code lang=”java”]
/**
* @return True if current execution points originates from own classes
*/
public static boolean isOwn() {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();

int len = stack.length;
for (int i = 2; i < len; i++) { if (stack[i].getClassName().startsWith("org.kari.")) { return true; } } return false; } [/code]

Detect if StringBuilder is called by own classes:

[code lang=”java”]
/**
* @return true if StringBuilder is directly called by own class
*/
public static boolean isImmediateOwn() {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();

int index = 2;
int len = stack.length;

for (; index < len; index++) { String className = stack[index].getClassName(); if (className.startsWith("org.kari.")) { return true; } if (!(className.equals("java.lang.AbstractStringBuilder") || className.equals("java.lang.StringBuilder")) ) { return false; } } return false; } [/code]

And now actual utilitization in Eclipse.

  1. Open AbstractStringBuilder -class
  2. Add breakpoint into beginning of expandCapacity() -method
  3. Paste following code as breakpoint condition:
    [code lang=”java”]
    StackTraceElement[] stack = Thread.currentThread().getStackTrace();

    int index = 2;
    int len = stack.length;

    for (; index < len; index++) { String className = stack[index].getClassName(); if (className.startsWith("org.kari.")) { return true; } if (!(className.equals("java.lang.AbstractStringBuilder") || className.equals("java.lang.StringBuilder")) ) { return false; } } return false; [/code]

Now Eclipse will hit the breakpoint whenever StringBuilder is expanded as a direct call from application logic. So what is the point of this exercise. Answer is the curiosity of analyzing how much String trashing application logic is performing.

Sadly just calling directly org.kari.util.ClassUtil.isImmediateOwn()from breakpoint does not work, but logic must be copy-pasted into breakpoint condition.

However, since we want to simplify debugging, and simply write this:
[code lang=”java”]
org.kari.util.ClassUtil.isImmediateOwn()
[/code]

Reason why it doesn’t work is that when break point is placed into some class coming from ”bootclasspath”, java class loading mechanism prevents access into this rogue ”org.kari.util.ClassUtil” -class. However, problem can be worked around by adjusting bootstrap class path:

  1. Create new folder into project, named ”bootclasses”
  2. Take: Run -> Open Debug Dialog…
  3. Select ”Classpath” -tab
  4. Select ”Bootstrap entries”
  5. Select ”Advanced…”
  6. Select ”Add Folders”
  7. Select ”Project/bootclasses”
  8. Go into command line, and copy org.kari.util.ClassUtil from Project/bin into Project/bootclasses

After this org.kari.util.ClassUtil.isImmediateOwn() will work as breakpoint for system classes.

And our final breakpoint condition is then
[code lang=”java”]
arg0 > 50 && org.kari.util.ClassUtil.isImmediateOwn()
[/code]

Note ”arg0” must be used to refer ”minimumCapacity” argument of expandCapacity() -method.

PS. Simple breakpoint without condition in expandCapasity() -method, shows that AWT/Swing are doing lots of reduntant String trashing internally.

/ java

Vastaa

Sähköpostiosoitettasi ei julkaista. Pakolliset kentät on merkitty *

This site uses Akismet to reduce spam. Learn how your comment data is processed.