int _in = cp.addFieldref(class_name, "_in", "Ljava/io/BufferedReader;");
int out = cp.addFieldref("java.lang.System", "out",
"Ljava/io/PrintStream;");
il.append(new GETSTATIC(out));
il.append(new PUSH(cp, "Please enter a number> "));
il.append(new INVOKEVIRTUAL(cp.addMethodref("java.io.PrintStream",
"print",
"(Ljava/lang/String;)V")));
il.append(new GETSTATIC(_in));
il.append(new INVOKEVIRTUAL(cp.addMethodref("java.io.BufferedReader",
"readLine",
"()Ljava/lang/String;")));
il.append(new INVOKESTATIC(cp.addMethodref("java.lang.Integer",
"parseInt",
"(Ljava/lang/String;)I")));
il.append(InstructionConstants.IRETURN);
/* private static final int _readInt() throws IOException
*/
method = new MethodGen(ACC_STATIC | ACC_PRIVATE | ACC_FINAL,
Type.INT, Type.NO_ARGS, null,
"_readInt", class_name, il, cp);
method.addException("java.io.IOException");
method.setMaxStack(2);
class_gen.addMethod(method.getMethod());
/* private static final int _writeInt(int i) throws IOException
*/
Type[] args = { Type.INT };
String[] argv = { "i" } ;
il = new InstructionList();
il.append(new GETSTATIC(out));
il.append(new NEW(cp.addClass("java.lang.StringBuffer")));
il.append(InstructionConstants.DUP);
il.append(new PUSH(cp, "Result: "));
il.append(new INVOKESPECIAL(cp.addMethodref("java.lang.StringBuffer",
"<init>",
"(Ljava/lang/String;)V")));
il.append(new ILOAD(0));
il.append(new INVOKEVIRTUAL(cp.addMethodref("java.lang.StringBuffer",
"append",
"(I)Ljava/lang/StringBuffer;")));
il.append(new INVOKEVIRTUAL(cp.addMethodref("java.lang.StringBuffer",
"toString",
"()Ljava/lang/String;")));
il.append(new INVOKEVIRTUAL(cp.addMethodref("java.io.PrintStream",
"println",
"(Ljava/lang/String;)V")));
il.append(new PUSH(cp, 0));
il.append(InstructionConstants.IRETURN); // Reuse objects, if possible
method = new MethodGen(ACC_STATIC | ACC_PRIVATE | ACC_FINAL,
Type.INT, args, argv,
"_writeInt", class_name, il, cp);
method.setMaxStack(4);
class_gen.addMethod(method.getMethod());
/* public <init> -- constructor
*/
il.dispose(); // Dispose instruction handles for better memory utilization
il = new InstructionList();
il.append(new ALOAD(0)); // Push `this'
il.append(new INVOKESPECIAL(cp.addMethodref("java.lang.Object",
"<init>", "()V")));
il.append(new RETURN());
method = new MethodGen(ACC_PUBLIC, Type.VOID, Type.NO_ARGS, null,
"<init>", class_name, il, cp);
method.setMaxStack(1);
class_gen.addMethod(method.getMethod());
/* class initializer
*/
il.dispose(); // Dispose instruction handles for better memory utilization
il = new InstructionList();
il.append(new NEW(cp.addClass("java.io.BufferedReader")));
il.append(InstructionConstants.DUP);
il.append(new NEW(cp.addClass("java.io.InputStreamReader")));
il.append(InstructionConstants.DUP);
il.append(new GETSTATIC(cp.addFieldref("java.lang.System", "in",
"Ljava/io/InputStream;")));
il.append(new INVOKESPECIAL(cp.addMethodref("java.io.InputStreamReader",
"<init>", "(Ljava/io/InputStream;)V")));
il.append(new INVOKESPECIAL(cp.addMethodref("java.io.BufferedReader",
"<init>", "(Ljava/io/Reader;)V")));