aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2011-07-18 22:24:08 +0200
committerReinier Zwitserloot <reinier@zwitserloot.com>2011-07-18 22:24:08 +0200
commit57f7576170dd832b269d0001eb1393f1fe3f9713 (patch)
tree192142d66780eb6a0469fa23a368a46588adf609 /src
parent43b3f2c0f0b3c519e84d221d4d6a5f41b74e2ec0 (diff)
downloadlombok-57f7576170dd832b269d0001eb1393f1fe3f9713.tar.gz
lombok-57f7576170dd832b269d0001eb1393f1fe3f9713.tar.bz2
lombok-57f7576170dd832b269d0001eb1393f1fe3f9713.zip
Heh, these weren't included due to .gitignore. Fixed that, so now I can add them.
Diffstat (limited to 'src')
-rw-r--r--src/core/lombok/core/debug/DebugSnapshot.java72
-rw-r--r--src/core/lombok/core/debug/DebugSnapshotStore.java52
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());
+ }
+ }
+}