From 57f7576170dd832b269d0001eb1393f1fe3f9713 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 18 Jul 2011 22:24:08 +0200 Subject: Heh, these weren't included due to .gitignore. Fixed that, so now I can add them. --- src/core/lombok/core/debug/DebugSnapshot.java | 72 ++++++++++++++++++++++ src/core/lombok/core/debug/DebugSnapshotStore.java | 52 ++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/core/lombok/core/debug/DebugSnapshot.java create mode 100644 src/core/lombok/core/debug/DebugSnapshotStore.java (limited to 'src/core/lombok') 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 { + private final long when, nano; + private final List trace; + private final Thread thread; + private final String message; + private final Object[] params; + private final WeakReference owner; + + public DebugSnapshot(CompilationUnitDeclaration owner, int stackHiding, String message, Object... params) { + this.when = System.currentTimeMillis(); + StackTraceElement[] stackTrace = new Throwable().getStackTrace(); + this.trace = new ArrayList(); + 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(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> map = + new WeakHashMap>(); + + public void snapshot(CompilationUnitDeclaration owner, String message, Object... params) { + DebugSnapshot snapshot = new DebugSnapshot(owner, 1, message, params); + List list; + + synchronized (map) { + list = map.get(owner); + if (list == null) { + list = new ArrayList(); + map.put(owner, list); + } + list.add(snapshot); + } + } + + public void print(CompilationUnitDeclaration owner, String message, Object... params) { + List list; + + synchronized (map) { + snapshot(owner, message == null ? "Printing" : message, params); + list = new ArrayList(); + 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()); + } + } +} -- cgit