1 package org.apache.velocity.runtime.compiler; 2 3 18 19 import org.apache.bcel.generic.*; 20 import org.apache.bcel.Constants; 21 22 28 public class Compiler implements InstructionConstants 29 { 30 public static void main(String [] args) 31 { 32 String template = args[0].substring(0, args[0].indexOf(".")); 33 ClassGen cg = 34 new ClassGen(template, "java.lang.Object", "<generated>", 35 Constants.ACC_PUBLIC | Constants.ACC_SUPER, null); 36 37 ConstantPoolGen cp = cg.getConstantPool(); InstructionList il = new InstructionList(); 39 MethodGen mg = new MethodGen(Constants.ACC_STATIC | 40 Constants.ACC_PUBLIC, Type.VOID, new Type[]{ new ArrayType(Type.STRING, 1)}, 44 new String []{ "argv" }, "main", template, il, cp); 47 48 50 int br_index = cp.addClass("java.io.BufferedReader"); 51 int ir_index = cp.addClass("java.io.InputStreamReader"); 52 int system_out = cp.addFieldref("java.lang.System", "out", "Ljava/io/PrintStream;"); 54 int system_in = cp.addFieldref("java.lang.System", "in", "Ljava/io/InputStream;"); 56 57 59 il.append(new NEW(br_index)); 60 il.append(DUP); 61 il.append(new NEW(ir_index)); 62 il.append(DUP); 63 il.append(new GETSTATIC(system_in)); 64 65 67 il.append( new INVOKESPECIAL( 68 cp.addMethodref("java.io.InputStreamReader", "<init>", 69 "(Ljava/io/InputStream;)V"))); 70 il.append( new INVOKESPECIAL( 71 cp.addMethodref("java.io.BufferedReader", "<init>", "(Ljava/io/Reader;)V"))); 72 73 75 LocalVariableGen lg = mg.addLocalVariable("in", 76 new ObjectType("java.io.BufferedReader"), null, null); 77 int in = lg.getIndex(); 78 lg.setStart(il.append(new ASTORE(in))); 80 82 lg = mg.addLocalVariable("name", Type.STRING, null, null); 83 int name = lg.getIndex(); 84 il.append(ACONST_NULL); 85 lg.setStart(il.append(new ASTORE(name))); 87 InstructionHandle try_start = il.append(new GETSTATIC(system_out)); 88 il.append(new PUSH(cp, "I will be a template compiler!")); 89 il.append( new INVOKEVIRTUAL( 90 cp.addMethodref("java.io.PrintStream", "println", "(Ljava/lang/String;)V"))); 91 92 95 GOTO g = new GOTO(null); 96 InstructionHandle try_end = il.append(g); 97 98 InstructionHandle handler = il.append(RETURN); 99 mg.addExceptionHandler(try_start, try_end, handler, 100 new ObjectType("java.io.IOException")); 101 102 104 InstructionHandle ih = il.append(new GETSTATIC(system_out)); 105 g.setTarget(ih); 106 107 109 il.append(new NEW(cp.addClass("java.lang.StringBuffer"))); 110 il.append(DUP); 111 il.append(new PUSH(cp, " ")); 112 il.append( new INVOKESPECIAL( 113 cp.addMethodref("java.lang.StringBuffer", "<init>", "(Ljava/lang/String;)V"))); 114 115 il.append(new ALOAD(name)); 116 117 120 String sig = Type.getMethodSignature(Type.STRINGBUFFER, 121 new Type[]{ Type.STRING }); 122 il.append( new INVOKEVIRTUAL( 123 cp.addMethodref("java.lang.StringBuffer", "append", sig))); 124 125 il.append( new INVOKEVIRTUAL( 126 cp.addMethodref("java.lang.StringBuffer", "toString", "()Ljava/lang/String;"))); 127 128 il.append(RETURN); 129 130 mg.setMaxStack(5); cg.addMethod(mg.getMethod()); 132 133 cg.addEmptyConstructor(Constants.ACC_PUBLIC); 135 136 try 138 { 139 cg.getJavaClass().dump(template + ".class"); 140 } 141 catch (java.io.IOException e) 142 { 143 System.err.println(e); 144 } 145 } 146 } 147 | Popular Tags |