diff options
Diffstat (limited to 'src/core')
4 files changed, 141 insertions, 3 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..d94798e8 --- /dev/null +++ b/src/core/lombok/core/debug/DebugSnapshot.java @@ -0,0 +1,62 @@ +package lombok.core.debug; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; + +import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; + +public class DebugSnapshot implements Comparable<DebugSnapshot> { + private static AtomicLong counter = new AtomicLong(); + + private final long when, id = counter.getAndIncrement(); + private final List<StackTraceElement> trace; + private final String threadName; + 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>(Math.max(0, stackTrace.length - stackHiding - 1)); + for (int i = 1 + stackHiding; i < stackTrace.length; i++) trace.add(stackTrace[i]); + this.threadName = Thread.currentThread().getName(); + this.message = message; + this.params = params == null ? new Object[0] : params; + this.owner = new WeakReference<CompilationUnitDeclaration>(owner); + } + + 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(" ").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) { + return Long.valueOf(id).compareTo(o.id); + } +} diff --git a/src/core/lombok/core/debug/DebugSnapshotStore.java b/src/core/lombok/core/debug/DebugSnapshotStore.java new file mode 100644 index 00000000..7abfc07c --- /dev/null +++ b/src/core/lombok/core/debug/DebugSnapshotStore.java @@ -0,0 +1,70 @@ +package lombok.core.debug; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; +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 String 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; + StringBuilder out = new StringBuilder(); + out.append("---------------------------\n"); + for (DebugSnapshot snapshot : list) { + out.append(String.format("%3d: %s\n", idx++, snapshot.shortToString())); + } + out.append("******\n"); + idx = 1; + for (DebugSnapshot snapshot : list) { + out.append(String.format("%3d: %s", idx++, snapshot.toString())); + } + + try { + File logFile = new File(System.getProperty("user.home", "."), String.format("lombok164-%d.err", System.currentTimeMillis())); + OutputStream stream = new FileOutputStream(logFile); + try { + stream.write(out.toString().getBytes("UTF-8")); + } finally { + stream.close(); + } + return logFile.getAbsolutePath(); + } catch (Exception e) { + System.err.println(out); + return "(can't write log file - emitted to system err)"; + } + } +} diff --git a/src/core/lombok/eclipse/TransformEclipseAST.java b/src/core/lombok/eclipse/TransformEclipseAST.java index 7ef06fca..b362f399 100644 --- a/src/core/lombok/eclipse/TransformEclipseAST.java +++ b/src/core/lombok/eclipse/TransformEclipseAST.java @@ -23,6 +23,7 @@ package lombok.eclipse; import java.lang.reflect.Field; +import lombok.core.debug.DebugSnapshotStore; import lombok.patcher.Symbols; import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; @@ -126,9 +127,12 @@ public class TransformEclipseAST { // Do NOT abort if (ast.bits & ASTNode.HasAllMethodBodies) != 0 - that doesn't work. try { + DebugSnapshotStore.INSTANCE.snapshot(ast, "transform entry"); EclipseAST existing = getAST(ast, false); new TransformEclipseAST(existing).go(); + DebugSnapshotStore.INSTANCE.snapshot(ast, "transform exit"); } catch (Throwable t) { + DebugSnapshotStore.INSTANCE.snapshot(ast, "transform error: %s", t.getClass().getSimpleName()); try { String message = "Lombok can't parse this source: " + t.toString(); diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 627acecb..45d58df3 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -25,6 +25,7 @@ import static lombok.eclipse.Eclipse.*; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; @@ -37,6 +38,7 @@ import lombok.Getter; import lombok.Lombok; import lombok.core.AnnotationValues; import lombok.core.AST.Kind; +import lombok.core.debug.DebugSnapshotStore; import lombok.core.handlers.TransformationsUtil; import lombok.eclipse.Eclipse; import lombok.eclipse.EclipseNode; @@ -464,9 +466,9 @@ public class EclipseHandlerUtil { } if (report) { - Eclipse.warning("We believe you may have just stumbled on lombok issue #164. Please " + - "report the stack trace associated with this message at:\n" + - "http://code.google.com/p/projectlombok/issues/detail?id=164", new Throwable()); + CompilationUnitDeclaration cud = (CompilationUnitDeclaration) type.top().get(); + String logFileLocation = DebugSnapshotStore.INSTANCE.print(cud, "Printing: injecting whilst scope is already built."); + Eclipse.warning("We believe you may have stumbled on issue 164. Please upload file " + logFileLocation + " to: http://code.google.com/p/projectlombok/issues/detail?id=164", new Throwable()); } } |