From 8e891daa922a56a3e6d504dab4ec108505cdc6b4 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Wed, 17 Feb 2016 18:44:52 +0000 Subject: [PATCH] Quality of life improvements for PrettyPrinter --- checkstyle.xml | 2 +- .../asm/mixin/MixinEnvironment.java | 10 +- .../injection/callback/CallbackInjector.java | 12 +- .../modify/LocalVariableDiscriminator.java | 6 +- .../modify/ModifyVariableInjector.java | 27 +-- .../spongepowered/asm/util/PrettyPrinter.java | 212 +++++++++++++++--- 6 files changed, 204 insertions(+), 65 deletions(-) diff --git a/checkstyle.xml b/checkstyle.xml index 538b210c..b10493a1 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -24,7 +24,7 @@ - + diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java index 4eaf37b9..caf310c0 100644 --- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java +++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java @@ -722,17 +722,17 @@ public class MixinEnvironment implements ITokenProvider { if (this.getOption(Option.DEBUG_VERBOSE)) { PrettyPrinter printer = new PrettyPrinter(32); printer.add("SpongePowered MIXIN (Verbose debugging enabled)").centre().hr(); - printer.add("%28s : %s", "Code source", codeSource); - printer.add("%28s : %s", "Internal Version", version); - printer.add("%28s : %s", "Java 8 Supported", CompatibilityLevel.JAVA_8.isSupported()).hr(); + printer.kv("Code source", codeSource); + printer.kv("Internal Version", version); + printer.kv("Java 8 Supported", CompatibilityLevel.JAVA_8.isSupported()).hr(); for (Option option : Option.values()) { StringBuilder indent = new StringBuilder(); for (int i = 0; i < option.depth; i++) { indent.append("- "); } - printer.add("%28s : %s<%s>", option.property, indent, option); + printer.kv(option.property, "%s<%s>", indent, option); } - printer.hr().add("%28s : %s", "Detected Side", side); + printer.hr().kv("Detected Side", side); printer.print(System.err); } } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index ff0f77f1..7dc861c0 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -477,12 +477,12 @@ public class CallbackInjector extends Injector { handlerSig.setModifiers(this.methodNode); PrettyPrinter printer = new PrettyPrinter(); - printer.add("%20s : %s", "Target Class", this.classNode.name.replace('/', '.')); - printer.add("%20s : %s", "Target Method", methodSig); - printer.add("%20s : %d", "Target Max LOCALS", callback.target.getMaxLocals()); - printer.add("%20s : %d", "Initial Frame Size", callback.frameSize); - printer.add("%20s : %s", "Callback Name", this.methodNode.name); - printer.add("%20s : %s %s", "Instruction", callback.node.getClass().getSimpleName(), ASMHelper.getOpcodeName(callback.node.getOpcode())); + printer.kv("Target Class", this.classNode.name.replace('/', '.')); + printer.kv("Target Method", methodSig); + printer.kv("Target Max LOCALS", callback.target.getMaxLocals()); + printer.kv("Initial Frame Size", callback.frameSize); + printer.kv("Callback Name", this.methodNode.name); + printer.kv("Instruction", "%s %s", callback.node.getClass().getSimpleName(), ASMHelper.getOpcodeName(callback.node.getOpcode())); printer.hr(); if (callback.locals.length > callback.frameSize) { printer.add(" %s %20s %s", "LOCAL", "TYPE", "NAME"); diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java b/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java index 50ad557a..48d68c61 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/modify/LocalVariableDiscriminator.java @@ -169,7 +169,7 @@ public class LocalVariableDiscriminator { @Override public void print(PrettyPrinter printer) { - printer.add("%5s %7s %20s %-50s %s", "INDEX", "ORDINAL", "TYPE", "NAME", "CANDIDATE"); + printer.add("%5s %7s %30s %-50s %s", "INDEX", "ORDINAL", "TYPE", "NAME", "CANDIDATE"); for (int l = this.baseArgIndex; l < this.locals.length; l++) { Local local = this.locals[l]; if (local != null) { @@ -177,11 +177,11 @@ public class LocalVariableDiscriminator { String localName = local.name; int ordinal = local.ord; String candidate = this.returnType.equals(localType) ? "YES" : "-"; - printer.add("[%3d] [%3d] %20s %-50s %s", l, ordinal, SignaturePrinter.getTypeName(localType, false), localName, candidate); + printer.add("[%3d] [%3d] %30s %-50s %s", l, ordinal, SignaturePrinter.getTypeName(localType, false), localName, candidate); } else if (l > 0) { Local prevLocal = this.locals[l - 1]; boolean isTop = prevLocal != null && prevLocal.type != null && prevLocal.type.getSize() > 1; - printer.add("[%3d] %20s", l, isTop ? "" : "-"); + printer.add("[%3d] %30s", l, isTop ? "" : "-"); } } } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java index 743c98fe..c0f70853 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/modify/ModifyVariableInjector.java @@ -174,19 +174,20 @@ public class ModifyVariableInjector extends Injector { SignaturePrinter handlerSig = new SignaturePrinter(this.methodNode.name, this.returnType, this.methodArgs, new String[] { "var" }); handlerSig.setModifiers(this.methodNode); - PrettyPrinter printer = new PrettyPrinter(); - printer.add("%20s : %s", "Target Class", this.classNode.name.replace('/', '.')); - printer.add("%20s : %s", "Target Method", context.target.method.name); - printer.add("%20s : %s", "Callback Name", this.methodNode.name); - printer.add("%20s : %s", "Capture Type", SignaturePrinter.getTypeName(this.returnType, false)); - printer.add("%20s : %s %s", "Instruction", context.node.getClass().getSimpleName(), ASMHelper.getOpcodeName(context.node.getOpcode())).hr(); - printer.add("%20s : %s", "Match mode", this.discriminator.isImplicit(context) ? "IMPLICIT (match single)" : "EXPLICIT (match by criteria)"); - printer.add("%20s : %s", "Match ordinal", this.discriminator.getOrdinal() < 0 ? "any" : this.discriminator.getOrdinal()); - printer.add("%20s : %s", "Match index", this.discriminator.getIndex() < context.baseArgIndex ? "any" : this.discriminator.getIndex()); - printer.add("%20s : %s", "Match name(s)", this.discriminator.hasNames() ? this.discriminator.getNames() : "any"); - printer.add("%20s : %s", "Args only", this.discriminator.isArgsOnly()).hr(); - printer.add(context); - printer.print(System.err); + new PrettyPrinter() + .kvWidth(20) + .kv("Target Class", this.classNode.name.replace('/', '.')) + .kv("Target Method", context.target.method.name) + .kv("Callback Name", this.methodNode.name) + .kv("Capture Type", SignaturePrinter.getTypeName(this.returnType, false)) + .kv("Instruction", "%s %s", context.node.getClass().getSimpleName(), ASMHelper.getOpcodeName(context.node.getOpcode())).hr() + .kv("Match mode", this.discriminator.isImplicit(context) ? "IMPLICIT (match single)" : "EXPLICIT (match by criteria)") + .kv("Match ordinal", this.discriminator.getOrdinal() < 0 ? "any" : this.discriminator.getOrdinal()) + .kv("Match index", this.discriminator.getIndex() < context.baseArgIndex ? "any" : this.discriminator.getIndex()) + .kv("Match name(s)", this.discriminator.hasNames() ? this.discriminator.getNames() : "any") + .kv("Args only", this.discriminator.isArgsOnly()).hr() + .add(context) + .print(System.err); } /** diff --git a/src/main/java/org/spongepowered/asm/util/PrettyPrinter.java b/src/main/java/org/spongepowered/asm/util/PrettyPrinter.java index 67f08ac0..0516af5c 100644 --- a/src/main/java/org/spongepowered/asm/util/PrettyPrinter.java +++ b/src/main/java/org/spongepowered/asm/util/PrettyPrinter.java @@ -55,29 +55,109 @@ public class PrettyPrinter { } /** - * "Horizontal rule" marker + * Interface for objects which need their width calculated prior to printing */ - private static final char HR = '\253'; + interface IVariableWidthEntry { + + public abstract int getWidth(); + + } + + /** + * Interface for objects which control their own output format + */ + interface ISpecialEntry { + + } + + /** + * A key/value pair for convenient printing + */ + class KeyValue implements PrettyPrinter.IVariableWidthEntry { + + private final String key; + + private final Object value; + + public KeyValue(String key, Object value) { + this.key = key; + this.value = value; + } + + @Override + public String toString() { + return String.format(PrettyPrinter.this.kvFormat, this.key, this.value); + } + + @Override + public int getWidth() { + return this.toString().length(); + } + + } + + /** + * Horizontal rule + */ + class HorizontalRule implements PrettyPrinter.ISpecialEntry { + + private final char[] hrChars; + + public HorizontalRule(char... hrChars) { + this.hrChars = hrChars; + } + + @Override + public String toString() { + return Strings.repeat(new String(this.hrChars), PrettyPrinter.this.width + 2); + } + + } + + /** + * Centred text + */ + class CentredText { + + private final Object centred; + + public CentredText(Object centred) { + this.centred = centred; + } + + @Override + public String toString() { + String text = this.centred.toString(); + return String.format("%" + (((PrettyPrinter.this.width - (text.length())) / 2) + text.length()) + "s", text); + } + + } + + private final HorizontalRule horizontalRule = new HorizontalRule('*'); /** - * If specified as the first character on a line, centres the line + * Content lines */ - private static final char CENTRE = '\252'; + private final List lines = new ArrayList(); + + private boolean recalcWidth = false; /** * Box with (adapts to contents) */ - private int width = 100; + protected int width = 100; /** * Wrap width used when an explicit wrap width is not specified */ - private int wrapWidth = 80; + protected int wrapWidth = 80; /** - * Content lines + * Key/value key width */ - private final List lines = new ArrayList(); + protected int kvKeyWidth = 10; + + protected String kvFormat = PrettyPrinter.makeKvFormat(this.kvKeyWidth); public PrettyPrinter() { this(100); @@ -205,6 +285,45 @@ public class PrettyPrinter { return lines; } + /** + * Add a formatted key/value pair to the output + * + * @param key Key + * @param format Value format + * @param args Value args + * @return fluent interface + */ + public PrettyPrinter kv(String key, String format, Object... args) { + return this.kv(key, String.format(format, args)); + } + + /** + * Add a key/value pair to the output + * + * @param key Key + * @param value Value + * @return fluent interface + */ + public PrettyPrinter kv(String key, Object value) { + this.lines.add(new KeyValue(key, value)); + return this.kvWidth(key.length()); + } + + /** + * Set the minimum key display width + * + * @param width width to set + * @return fluent + */ + public PrettyPrinter kvWidth(int width) { + if (width > this.kvKeyWidth) { + this.kvKeyWidth = width; + this.kvFormat = PrettyPrinter.makeKvFormat(width); + } + this.recalcWidth = true; + return this; + } + /** * Adds a horizontal rule to the output * @@ -222,7 +341,7 @@ public class PrettyPrinter { * @return fluent interface */ public PrettyPrinter hr(char ruleChar) { - this.lines.add("" + PrettyPrinter.HR + ruleChar); + this.lines.add(new HorizontalRule(ruleChar)); return this; } @@ -233,7 +352,10 @@ public class PrettyPrinter { */ public PrettyPrinter centre() { if (!this.lines.isEmpty()) { - this.lines.add(PrettyPrinter.CENTRE + this.lines.remove(this.lines.size() - 1)); + Object lastLine = this.lines.get(this.lines.size() - 1); + if (lastLine instanceof String) { + this.lines.add(new CentredText(this.lines.remove(this.lines.size() - 1))); + } } return this; } @@ -244,26 +366,26 @@ public class PrettyPrinter { * @param stream stream to print to */ public void print(PrintStream stream) { - this.printHr(stream, '*'); - for (String line : this.lines) { - int len = line.length(); - if (len > 1 && line.charAt(0) == PrettyPrinter.HR) { - this.printHr(stream, line.charAt(1)); + this.updateWidth(); + this.printSpecial(stream, this.horizontalRule); + for (Object line : this.lines) { + if (line instanceof ISpecialEntry) { + this.printSpecial(stream, (ISpecialEntry)line); } else { - if (len > 0 && line.charAt(0) == PrettyPrinter.CENTRE) { - String text = line.substring(1); - line = String.format("%" + (((this.width - (text.length())) / 2) + text.length()) + "s", text); - } - stream.printf("/* %-" + this.width + "s */\n", line); + this.printString(stream, line.toString()); } } - this.printHr(stream, '*'); + this.printSpecial(stream, this.horizontalRule); } - private void printHr(PrintStream stream, char... hrChars) { - stream.printf("/*%s*/\n", Strings.repeat(new String(hrChars), this.width + 2)); + private void printSpecial(PrintStream stream, ISpecialEntry line) { + stream.printf("/*%s*/\n", line.toString()); } - + + private void printString(PrintStream stream, String string) { + stream.printf("/* %-" + this.width + "s */\n", string); + } + public void log(Logger logger) { this.log(logger, Level.INFO); } @@ -275,23 +397,39 @@ public class PrettyPrinter { * @param level log level */ public void log(Logger logger, Level level) { - this.logHr(logger, level, '*'); - for (String line : this.lines) { - int len = line.length(); - if (len > 1 && line.charAt(0) == PrettyPrinter.HR) { - this.logHr(logger, level, line.charAt(1)); + this.updateWidth(); + this.logSpecial(logger, level, this.horizontalRule); + for (Object line : this.lines) { + if (line instanceof ISpecialEntry) { + this.logSpecial(logger, level, (ISpecialEntry)line); } else { - if (len > 0 && line.charAt(0) == PrettyPrinter.CENTRE) { - String text = line.substring(1); - line = String.format("%" + (((this.width - (text.length())) / 2) + text.length()) + "s", text); - } - logger.log(level, String.format("/* %-" + this.width + "s */", line)); + this.logString(logger, level, line.toString()); } } - this.logHr(logger, level, '*'); + this.logSpecial(logger, level, this.horizontalRule); + } + + private void logSpecial(Logger logger, Level level, ISpecialEntry line) { + logger.log(level, "/*{}*/\n", line.toString()); + } + + private void logString(Logger logger, Level level, String line) { + logger.log(level, String.format("/* %-" + this.width + "s */", line)); } - private void logHr(Logger logger, Level level, char... hrChars) { - logger.log(level, String.format("/*%s*/", Strings.repeat(new String(hrChars), this.width + 2))); + private void updateWidth() { + if (this.recalcWidth) { + this.recalcWidth = false; + for (Object line : this.lines) { + if (line instanceof IVariableWidthEntry) { + this.width = Math.max(this.width, ((IVariableWidthEntry)line).getWidth()); + } + } + } } + + private static String makeKvFormat(int keyWidth) { + return String.format("%%%ds : %%s", keyWidth); + } + }