どうにか、プログラム書ける程度には気力が回復してきました。リハビリに、きしださんとこより。
ある意味正攻法。
printメソッド以外の余計なものを(たとえprivateでも)定義しないように心がけました。
import java.util.Arrays;
public class PrinterOfFools {
public static void main(String[] args)
{
String[][] data = {
{"aa", "bb", "cc"},
{"dd", "ee"}};
print(data);
}
public static String print(Object[] data)
{
String result =
data == null
? "データがありません"
: data.length == 0
? "データが空です"
: data.length == 1
? data[0] instanceof String[]
? print((String[])data[0])
: (String)data[0]
: data[0] instanceof String[]
? print((String[])data[0]) + "\n" + print(Arrays.asList(data).subList(1, data.length).toArray())
: data[0] + "," + print(Arrays.asList(data).subList(1, data.length).toArray())
;
System.out.print((new Exception()).fillInStackTrace().getStackTrace()[1].getClassName() == "PrinterOfFools"
&& (new Exception()).fillInStackTrace().getStackTrace()[1].getMethodName() == "print"
? ""
: result);
return result;
}
}
ある意味ありがち
はっきり言って誰もが思い付く代物です。
public class Printer {
private static final byte[] PRINTER_DEFINITION = {
(byte)0xca, (byte)0xfe, (byte)0xba, (byte)0xbe, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x31,
(byte)0x00, (byte)0x21, (byte)0x0a, (byte)0x00, (byte)0x0a, (byte)0x00, (byte)0x10, (byte)0x09,
(byte)0x00, (byte)0x11, (byte)0x00, (byte)0x12, (byte)0x08, (byte)0x00, (byte)0x13, (byte)0x0a,
(byte)0x00, (byte)0x14, (byte)0x00, (byte)0x15, (byte)0x08, (byte)0x00, (byte)0x16, (byte)0x08,
(byte)0x00, (byte)0x17, (byte)0x08, (byte)0x00, (byte)0x18, (byte)0x08, (byte)0x00, (byte)0x19,
(byte)0x07, (byte)0x00, (byte)0x1a, (byte)0x07, (byte)0x00, (byte)0x1b, (byte)0x01, (byte)0x00,
(byte)0x06, (byte)0x3c, (byte)0x69, (byte)0x6e, (byte)0x69, (byte)0x74, (byte)0x3e, (byte)0x01,
(byte)0x00, (byte)0x03, (byte)0x28, (byte)0x29, (byte)0x56, (byte)0x01, (byte)0x00, (byte)0x04,
(byte)0x43, (byte)0x6f, (byte)0x64, (byte)0x65, (byte)0x01, (byte)0x00, (byte)0x05, (byte)0x70,
(byte)0x72, (byte)0x69, (byte)0x6e, (byte)0x74, (byte)0x01, (byte)0x00, (byte)0x17, (byte)0x28,
(byte)0x5b, (byte)0x5b, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f,
(byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53, (byte)0x74, (byte)0x72,
(byte)0x69, (byte)0x6e, (byte)0x67, (byte)0x3b, (byte)0x29, (byte)0x56, (byte)0x0c, (byte)0x00,
(byte)0x0b, (byte)0x00, (byte)0x0c, (byte)0x07, (byte)0x00, (byte)0x1c, (byte)0x0c, (byte)0x00,
(byte)0x1d, (byte)0x00, (byte)0x1e, (byte)0x01, (byte)0x00, (byte)0x1b, (byte)0xe3, (byte)0x83,
(byte)0x87, (byte)0xe3, (byte)0x83, (byte)0xbc, (byte)0xe3, (byte)0x82, (byte)0xbf, (byte)0xe3,
(byte)0x81, (byte)0x8c, (byte)0xe3, (byte)0x81, (byte)0x82, (byte)0xe3, (byte)0x82, (byte)0x8a,
(byte)0xe3, (byte)0x81, (byte)0xbe, (byte)0xe3, (byte)0x81, (byte)0x9b, (byte)0xe3, (byte)0x82,
(byte)0x93, (byte)0x07, (byte)0x00, (byte)0x1f, (byte)0x0c, (byte)0x00, (byte)0x0e, (byte)0x00,
(byte)0x20, (byte)0x01, (byte)0x00, (byte)0x15, (byte)0xe3, (byte)0x83, (byte)0x87, (byte)0xe3,
(byte)0x83, (byte)0xbc, (byte)0xe3, (byte)0x82, (byte)0xbf, (byte)0xe3, (byte)0x81, (byte)0x8c,
(byte)0xe7, (byte)0xa9, (byte)0xba, (byte)0xe3, (byte)0x81, (byte)0xa7, (byte)0xe3, (byte)0x81,
(byte)0x99, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x2c,
(byte)0x01, (byte)0x00, (byte)0x01, (byte)0x0a, (byte)0x01, (byte)0x00, (byte)0x09, (byte)0x50,
(byte)0x72, (byte)0x69, (byte)0x6e, (byte)0x74, (byte)0x65, (byte)0x72, (byte)0x24, (byte)0x32,
(byte)0x01, (byte)0x00, (byte)0x10, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f,
(byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x4f, (byte)0x62, (byte)0x6a,
(byte)0x65, (byte)0x63, (byte)0x74, (byte)0x01, (byte)0x00, (byte)0x10, (byte)0x6a, (byte)0x61,
(byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f,
(byte)0x53, (byte)0x79, (byte)0x73, (byte)0x74, (byte)0x65, (byte)0x6d, (byte)0x01, (byte)0x00,
(byte)0x03, (byte)0x6f, (byte)0x75, (byte)0x74, (byte)0x01, (byte)0x00, (byte)0x15, (byte)0x4c,
(byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x69, (byte)0x6f, (byte)0x2f,
(byte)0x50, (byte)0x72, (byte)0x69, (byte)0x6e, (byte)0x74, (byte)0x53, (byte)0x74, (byte)0x72,
(byte)0x65, (byte)0x61, (byte)0x6d, (byte)0x3b, (byte)0x01, (byte)0x00, (byte)0x13, (byte)0x6a,
(byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x69, (byte)0x6f, (byte)0x2f, (byte)0x50,
(byte)0x72, (byte)0x69, (byte)0x6e, (byte)0x74, (byte)0x53, (byte)0x74, (byte)0x72, (byte)0x65,
(byte)0x61, (byte)0x6d, (byte)0x01, (byte)0x00, (byte)0x15, (byte)0x28, (byte)0x4c, (byte)0x6a,
(byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67,
(byte)0x2f, (byte)0x53, (byte)0x74, (byte)0x72, (byte)0x69, (byte)0x6e, (byte)0x67, (byte)0x3b,
(byte)0x29, (byte)0x56, (byte)0x00, (byte)0x20, (byte)0x00, (byte)0x09, (byte)0x00, (byte)0x0a,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x00, (byte)0x00,
(byte)0x00, (byte)0x0b, (byte)0x00, (byte)0x0c, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x0d,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x11, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x05, (byte)0x2a, (byte)0xb7, (byte)0x00, (byte)0x01,
(byte)0xb1, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x09, (byte)0x00,
(byte)0x0e, (byte)0x00, (byte)0x0f, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x0d, (byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x8b, (byte)0x00, (byte)0x02, (byte)0x00, (byte)0x0b, (byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x7f, (byte)0x2a, (byte)0xc7, (byte)0x00, (byte)0x0e, (byte)0xb2,
(byte)0x00, (byte)0x02, (byte)0x12, (byte)0x03, (byte)0xb6, (byte)0x00, (byte)0x04, (byte)0xa7,
(byte)0x00, (byte)0x72, (byte)0x2a, (byte)0xbe, (byte)0x9a, (byte)0x00, (byte)0x0e, (byte)0xb2,
(byte)0x00, (byte)0x02, (byte)0x12, (byte)0x05, (byte)0xb6, (byte)0x00, (byte)0x04, (byte)0xa7,
(byte)0x00, (byte)0x62, (byte)0x12, (byte)0x06, (byte)0x4c, (byte)0x2a, (byte)0x4d, (byte)0x2c,
(byte)0xbe, (byte)0x3e, (byte)0x03, (byte)0x36, (byte)0x04, (byte)0x15, (byte)0x04, (byte)0x1d,
(byte)0xa2, (byte)0x00, (byte)0x51, (byte)0x2c, (byte)0x15, (byte)0x04, (byte)0x32, (byte)0x3a,
(byte)0x05, (byte)0xb2, (byte)0x00, (byte)0x02, (byte)0x2b, (byte)0xb6, (byte)0x00, (byte)0x04,
(byte)0x12, (byte)0x06, (byte)0x3a, (byte)0x06, (byte)0x19, (byte)0x05, (byte)0x3a, (byte)0x07,
(byte)0x19, (byte)0x07, (byte)0xbe, (byte)0x36, (byte)0x08, (byte)0x03, (byte)0x36, (byte)0x09,
(byte)0x15, (byte)0x09, (byte)0x15, (byte)0x08, (byte)0xa2, (byte)0x00, (byte)0x24, (byte)0x19,
(byte)0x07, (byte)0x15, (byte)0x09, (byte)0x32, (byte)0x3a, (byte)0x0a, (byte)0xb2, (byte)0x00,
(byte)0x02, (byte)0x19, (byte)0x06, (byte)0xb6, (byte)0x00, (byte)0x04, (byte)0xb2, (byte)0x00,
(byte)0x02, (byte)0x19, (byte)0x0a, (byte)0xb6, (byte)0x00, (byte)0x04, (byte)0x12, (byte)0x07,
(byte)0x3a, (byte)0x06, (byte)0x84, (byte)0x09, (byte)0x01, (byte)0xa7, (byte)0xff, (byte)0xdb,
(byte)0x12, (byte)0x08, (byte)0x4c, (byte)0x84, (byte)0x04, (byte)0x01, (byte)0xa7, (byte)0xff,
(byte)0xaf, (byte)0xb1, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00
};
public static void main(String[] args) {
String[][] data = {
{"aa", "bb", "cc"},
{"dd", "ee"}};
print(data);
}
public static void print(String[][] data) {
ClassLoader loader = new ClassLoader() {
protected Class findClass(String name) {
return defineClass(null, PRINTER_DEFINITION, 0, PRINTER_DEFINITION.length);
}
};
try {
Class printer = loader.loadClass("Printer$2");
printer.getMethod("print", String[][].class).invoke(null, (Object)data);
} catch (Exception ex) {
System.err.println(ex);
}
}
}
追記
最初のやつは、printf(@since 1.5)を使えばもうちょっときれいになるか。でも、呆れ度は減ってるかも。
それにしても、出力系で値を返すメソッドって少ないのね。
import java.util.Arrays;
public class PrinterOfFools
{
public static void main(String[] args)
{
String[][] data = {
{"aa", "bb", "cc"},
{"dd", "ee"}};
print(null);
}
public static int print(Object[] data)
{
return
data == null
? System.out.printf("%s", "データがありません).hashCode()
: data.length == 0
? System.out.printf("%s", "データが空です").hashCode()
: data.length == 1
? data[0] instanceof String[]
? print((String[])data[0])
: System.out.printf("%s", data[0]).hashCode()
: data[0] instanceof String[]
? print((String[])data[0]) + System.out.printf("\n").hashCode() + print(Arrays.asList(data).subList(1, data.length).toArray())
: System.out.printf("%s,", data[0]).hashCode() + print(Arrays.asList(data).subList(1, data.length).toArray())
;
}
}
コメント