diff options
Diffstat (limited to 'src/core/lombok')
-rw-r--r-- | src/core/lombok/core/debug/DebugSnapshot.java | 72 | ||||
-rw-r--r-- | src/core/lombok/core/debug/DebugSnapshotStore.java | 52 |
2 files changed, 124 insertions, 0 deletions
diff --git a/src/core/lombok/core/debug/DebugSnapshot.java b/src/core/lombok/core/debug/DebugSnapshot.java new file mode 100644 index 00000000..a04a4820 --- /dev/null +++ b/src/core/lombok/core/debug/DebugSnapshot.java @@ -0,0 +1,72 @@ +package lombok.core.debug; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; + +public class DebugSnapshot implements Comparable<DebugSnapshot> { + private final long when, nano; + private final List<StackTraceElement> trace; + private final Thread thread; + private final String message; + private final Object[] params; + private final WeakReference<CompilationUnitDeclaration> owner; + + public DebugSnapshot(CompilationUnitDeclaration owner, int stackHiding, String message, Object... params) { + this.when = System.currentTimeMillis(); + StackTraceElement[] stackTrace = new Throwable().getStackTrace(); + this.trace = new ArrayList<StackTraceElement>(); + for (int i = 1 + stackHiding; i < stackTrace.length; i++) trace.add(stackTrace[i]); + this.nano = System.nanoTime(); + this.thread = Thread.currentThread(); + this.message = message; + this.params = params == null ? new Object[0] : params; + this.owner = new WeakReference<CompilationUnitDeclaration>(owner); + } + + private String threadName() { + return thread.getName(); + } + + private String ownerName() { + CompilationUnitDeclaration node = owner.get(); + if (node == null) return "GCed"; + char[] tn = node.getMainTypeName(); + char[] fs = node.getFileName(); + if (tn == null || tn.length == 0) { + return (fs == null || fs.length == 0) ? "--UNKNOWN--" : new String(fs); + } + + return new String(tn); + } + + public String shortToString() { + StringBuilder out = new StringBuilder(); + out.append(String.format("WHEN: %14d THREAD: %s AST: %s", when, threadName(), ownerName())); + if (message != null) out.append(String.format(message, params)); + return out.toString(); + } + + @Override public String toString() { + StringBuilder out = new StringBuilder(); + out.append(shortToString()).append("\n"); + for (StackTraceElement elem : trace) { + out.append(" ").append(elem.toString()).append("\n"); + } + return out.toString(); + } + + @Override public int compareTo(DebugSnapshot o) { + if (o.thread == this.thread) { + return Long.valueOf(nano).compareTo(Long.valueOf(o.nano)); + } + + if (o.when != when) { + return Long.valueOf(when).compareTo(Long.valueOf(o.when)); + } + + return Integer.valueOf(System.identityHashCode(thread)).compareTo(System.identityHashCode(o.thread)); + } +} diff --git a/src/core/lombok/core/debug/DebugSnapshotStore.java b/src/core/lombok/core/debug/DebugSnapshotStore.java new file mode 100644 index 00000000..cb15d2eb --- /dev/null +++ b/src/core/lombok/core/debug/DebugSnapshotStore.java @@ -0,0 +1,52 @@ +package lombok.core.debug; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; + +import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; + +public class DebugSnapshotStore { + public static final DebugSnapshotStore INSTANCE = new DebugSnapshotStore(); + + private final Map<CompilationUnitDeclaration, List<DebugSnapshot>> map = + new WeakHashMap<CompilationUnitDeclaration, List<DebugSnapshot>>(); + + public void snapshot(CompilationUnitDeclaration owner, String message, Object... params) { + DebugSnapshot snapshot = new DebugSnapshot(owner, 1, message, params); + List<DebugSnapshot> list; + + synchronized (map) { + list = map.get(owner); + if (list == null) { + list = new ArrayList<DebugSnapshot>(); + map.put(owner, list); + } + list.add(snapshot); + } + } + + public void print(CompilationUnitDeclaration owner, String message, Object... params) { + List<DebugSnapshot> list; + + synchronized (map) { + snapshot(owner, message == null ? "Printing" : message, params); + list = new ArrayList<DebugSnapshot>(); + list.addAll(map.get(owner)); + } + + Collections.sort(list); + int idx = 1; + System.out.println("---------------------------"); + for (DebugSnapshot snapshot : list) { + System.out.printf("%3d: %s\n", idx++, snapshot.shortToString()); + } + System.out.println("******"); + idx = 1; + for (DebugSnapshot snapshot : list) { + System.out.printf("%3d: %s", idx++, snapshot.toString()); + } + } +} |