
lombok.core.debug.DebugSnapshotStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lombok Show documentation
Show all versions of lombok Show documentation
Spice up your java: Automatic Resource Management, automatic generation of getters, setters, equals, hashCode and toString, and more!
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();
public static boolean GLOBAL_DSS_DISABLE_SWITCH = true;
private final Map> map =
new WeakHashMap>();
public void snapshot(CompilationUnitDeclaration owner, String message, Object... params) {
if (GLOBAL_DSS_DISABLE_SWITCH) return;
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);
} else if (!list.isEmpty()) {
list.add(snapshot);
} else {
// An empty list is an indicator that we no longer care about that particular CUD.
}
}
}
public void log(CompilationUnitDeclaration owner, String message, Object... params) {
if (GLOBAL_DSS_DISABLE_SWITCH) return;
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);
} else if (!list.isEmpty()) {
list.add(snapshot);
} else {
// An empty list is an indicator that we no longer care about that particular CUD.
}
}
}
public String print(CompilationUnitDeclaration owner, String message, Object... params) {
if (GLOBAL_DSS_DISABLE_SWITCH) return null;
List list;
synchronized (map) {
snapshot(owner, message == null ? "Printing" : message, params);
list = new ArrayList();
list.addAll(map.get(owner));
if (list.isEmpty()) return null; // An empty list is an indicator that we no longer care about that particular CUD.
map.get(owner).clear();
}
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)";
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy