001: /***
002: * ASM Guide
003: * Copyright (c) 2007 Eric Bruneton
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package ch6.sec1;
030:
031: import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
032: import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
033: import static org.objectweb.asm.Opcodes.ALOAD;
034: import static org.objectweb.asm.Opcodes.ATHROW;
035: import static org.objectweb.asm.Opcodes.DUP;
036: import static org.objectweb.asm.Opcodes.F_SAME;
037: import static org.objectweb.asm.Opcodes.GETFIELD;
038: import static org.objectweb.asm.Opcodes.GOTO;
039: import static org.objectweb.asm.Opcodes.IFLT;
040: import static org.objectweb.asm.Opcodes.ILOAD;
041: import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
042: import static org.objectweb.asm.Opcodes.IRETURN;
043: import static org.objectweb.asm.Opcodes.NEW;
044: import static org.objectweb.asm.Opcodes.PUTFIELD;
045: import static org.objectweb.asm.Opcodes.RETURN;
046: import static org.objectweb.asm.Opcodes.V1_6;
047:
048: import java.io.PrintWriter;
049:
050: import org.objectweb.asm.ClassWriter;
051: import org.objectweb.asm.tree.ClassNode;
052: import org.objectweb.asm.tree.FieldInsnNode;
053: import org.objectweb.asm.tree.FieldNode;
054: import org.objectweb.asm.tree.FrameNode;
055: import org.objectweb.asm.tree.InsnList;
056: import org.objectweb.asm.tree.InsnNode;
057: import org.objectweb.asm.tree.JumpInsnNode;
058: import org.objectweb.asm.tree.LabelNode;
059: import org.objectweb.asm.tree.MethodInsnNode;
060: import org.objectweb.asm.tree.MethodNode;
061: import org.objectweb.asm.tree.TypeInsnNode;
062: import org.objectweb.asm.tree.VarInsnNode;
063:
064: /**
065: * ASM Guide example class.
066: *
067: * @author Eric Bruneton
068: */
069: public class BeanGenerator {
070:
071: public byte[] generate(PrintWriter printWriter) {
072: ClassNode cn = new ClassNode();
073: cn.version = V1_6;
074: cn.access = ACC_PUBLIC;
075: cn.name = "pkg/Bean";
076: cn.super Name = "java/lang/Object";
077: cn.sourceDebug = "Bean.java";
078: cn.fields.add(new FieldNode(ACC_PRIVATE, "f", "I", null, null));
079: {
080: MethodNode mn = new MethodNode(ACC_PUBLIC, "<init>", "()V",
081: null, null);
082: cn.methods.add(mn);
083: InsnList il = mn.instructions;
084: il.add(new VarInsnNode(ALOAD, 0));
085: il.add(new MethodInsnNode(INVOKESPECIAL,
086: "java/lang/Object", "<init>", "()V"));
087: il.add(new InsnNode(RETURN));
088: mn.maxStack = 1;
089: mn.maxLocals = 1;
090: }
091: {
092: MethodNode mn = new MethodNode(ACC_PUBLIC, "getF", "()I",
093: null, null);
094: cn.methods.add(mn);
095: InsnList il = mn.instructions;
096: il.add(new VarInsnNode(ALOAD, 0));
097: il.add(new FieldInsnNode(GETFIELD, "pkg/Bean", "f", "I"));
098: il.add(new InsnNode(IRETURN));
099: mn.maxStack = 1;
100: mn.maxLocals = 1;
101: }
102: {
103: MethodNode mn = new MethodNode(ACC_PUBLIC, "setF", "(I)V",
104: null, null);
105: cn.methods.add(mn);
106: InsnList il = mn.instructions;
107: il.add(new VarInsnNode(ALOAD, 0));
108: il.add(new VarInsnNode(ILOAD, 1));
109: il.add(new FieldInsnNode(PUTFIELD, "pkg/Bean", "f", "I"));
110: il.add(new InsnNode(RETURN));
111: mn.maxStack = 2;
112: mn.maxLocals = 2;
113: }
114: MethodNode mn = new MethodNode(ACC_PUBLIC, "checkAndSetF",
115: "(I)V", null, null);
116: cn.methods.add(mn);
117: InsnList il = mn.instructions;
118: il.add(new VarInsnNode(ILOAD, 1));
119: LabelNode label = new LabelNode();
120: il.add(new JumpInsnNode(IFLT, label));
121: il.add(new VarInsnNode(ALOAD, 0));
122: il.add(new VarInsnNode(ILOAD, 1));
123: il.add(new FieldInsnNode(PUTFIELD, "pkg/Bean", "f", "I"));
124: LabelNode end = new LabelNode();
125: il.add(new JumpInsnNode(GOTO, end));
126: il.add(label);
127: il.add(new FrameNode(F_SAME, 0, null, 0, null));
128: il.add(new TypeInsnNode(NEW,
129: "java/lang/IllegalArgumentException"));
130: il.add(new InsnNode(DUP));
131: il.add(new MethodInsnNode(INVOKESPECIAL,
132: "java/lang/IllegalArgumentException", "<init>", "()V"));
133: il.add(new InsnNode(ATHROW));
134: il.add(end);
135: il.add(new FrameNode(F_SAME, 0, null, 0, null));
136: il.add(new InsnNode(RETURN));
137: mn.maxStack = 2;
138: mn.maxLocals = 2;
139: ClassWriter cw = new ClassWriter(0);
140: cn.accept(cw);
141: return cw.toByteArray();
142: }
143: }
|