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 ch3.sec2;
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.FieldVisitor;
052: import org.objectweb.asm.Label;
053: import org.objectweb.asm.MethodVisitor;
054: import org.objectweb.asm.util.CheckClassAdapter;
055: import org.objectweb.asm.util.TraceClassVisitor;
056:
057: /**
058: * ASM Guide example class.
059: *
060: * @author Eric Bruneton
061: */
062: public class BeanGenerator {
063:
064: public byte[] generate(PrintWriter printWriter) {
065: ClassWriter cw = new ClassWriter(0);
066: TraceClassVisitor tcv = new TraceClassVisitor(cw, printWriter);
067: CheckClassAdapter cv = new CheckClassAdapter(tcv);
068: cv.visit(V1_6, ACC_PUBLIC, "pkg/Bean", null,
069: "java/lang/Object", null);
070: cv.visitSource("Bean.java", null);
071: FieldVisitor fv = cv.visitField(ACC_PRIVATE, "f", "I", null,
072: null);
073: if (fv != null) {
074: fv.visitEnd();
075: }
076: MethodVisitor mv;
077: mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
078: if (mv != null) {
079: mv.visitCode();
080: mv.visitVarInsn(ALOAD, 0);
081: mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object",
082: "<init>", "()V");
083: mv.visitInsn(RETURN);
084: mv.visitMaxs(1, 1);
085: mv.visitEnd();
086: }
087: mv = cv.visitMethod(ACC_PUBLIC, "getF", "()I", null, null);
088: if (mv != null) {
089: mv.visitCode();
090: mv.visitVarInsn(ALOAD, 0);
091: mv.visitFieldInsn(GETFIELD, "pkg/Bean", "f", "I");
092: mv.visitInsn(IRETURN);
093: mv.visitMaxs(1, 1);
094: mv.visitEnd();
095: }
096: mv = cv.visitMethod(ACC_PUBLIC, "setF", "(I)V", null, null);
097: if (mv != null) {
098: mv.visitCode();
099: mv.visitVarInsn(ALOAD, 0);
100: mv.visitVarInsn(ILOAD, 1);
101: mv.visitFieldInsn(PUTFIELD, "pkg/Bean", "f", "I");
102: mv.visitInsn(RETURN);
103: mv.visitMaxs(2, 2);
104: mv.visitEnd();
105: }
106: mv = cv.visitMethod(ACC_PUBLIC, "checkAndSetF", "(I)V", null,
107: null);
108: if (mv != null) {
109: mv.visitCode();
110: mv.visitVarInsn(ILOAD, 1);
111: Label label = new Label();
112: mv.visitJumpInsn(IFLT, label);
113: mv.visitVarInsn(ALOAD, 0);
114: mv.visitVarInsn(ILOAD, 1);
115: mv.visitFieldInsn(PUTFIELD, "pkg/Bean", "f", "I");
116: Label end = new Label();
117: mv.visitJumpInsn(GOTO, end);
118: mv.visitLabel(label);
119: mv.visitFrame(F_SAME, 0, null, 0, null);
120: mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
121: mv.visitInsn(DUP);
122: mv.visitMethodInsn(INVOKESPECIAL,
123: "java/lang/IllegalArgumentException", "<init>",
124: "()V");
125: mv.visitInsn(ATHROW);
126: mv.visitLabel(end);
127: mv.visitFrame(F_SAME, 0, null, 0, null);
128: mv.visitInsn(RETURN);
129: mv.visitMaxs(2, 2);
130: mv.visitEnd();
131: }
132: cv.visitEnd();
133: return cw.toByteArray();
134: }
135: }
|