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.V1_6;
034:
035: import java.io.PrintWriter;
036:
037: import org.objectweb.asm.ClassWriter;
038: import org.objectweb.asm.FieldVisitor;
039: import org.objectweb.asm.Label;
040: import org.objectweb.asm.MethodVisitor;
041: import org.objectweb.asm.Type;
042: import org.objectweb.asm.commons.GeneratorAdapter;
043: import org.objectweb.asm.commons.Method;
044: import org.objectweb.asm.util.CheckClassAdapter;
045: import org.objectweb.asm.util.TraceClassVisitor;
046:
047: /**
048: * ASM Guide example class.
049: *
050: * @author Eric Bruneton
051: */
052: public class BeanGenerator2 {
053:
054: public byte[] generate(PrintWriter printWriter) {
055: ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
056: TraceClassVisitor tcv = new TraceClassVisitor(cw, printWriter);
057: CheckClassAdapter cv = new CheckClassAdapter(tcv);
058: cv.visit(V1_6, ACC_PUBLIC, "pkg/Bean", null,
059: "java/lang/Object", null);
060: cv.visitSource("Bean.java", null);
061: FieldVisitor fv = cv.visitField(ACC_PRIVATE, "f", "I", null,
062: null);
063: if (fv != null) {
064: fv.visitEnd();
065: }
066: MethodVisitor mv;
067: mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
068: if (mv != null) {
069: Method m = Method.getMethod("void <init>()");
070: GeneratorAdapter ga = new GeneratorAdapter(ACC_PUBLIC, m,
071: mv);
072: ga.visitCode();
073: ga.loadThis();
074: ga.invokeConstructor(Type.getType("Ljava/lang/Object;"), m);
075: ga.returnValue();
076: ga.endMethod();
077: }
078: mv = cv.visitMethod(ACC_PUBLIC, "getF", "()I", null, null);
079: if (mv != null) {
080: Type bean = Type.getType("Lpkg/Bean;");
081: Method m = Method.getMethod("int getF()");
082: GeneratorAdapter ga = new GeneratorAdapter(ACC_PUBLIC, m,
083: mv);
084: ga.visitCode();
085: ga.loadThis();
086: ga.getField(bean, "f", Type.INT_TYPE);
087: ga.returnValue();
088: ga.endMethod();
089: }
090: mv = cv.visitMethod(ACC_PUBLIC, "setF", "(I)V", null, null);
091: if (mv != null) {
092: Type bean = Type.getType("Lpkg/Bean;");
093: Method m = Method.getMethod("void setF(int)");
094: GeneratorAdapter ga = new GeneratorAdapter(ACC_PUBLIC, m,
095: mv);
096: ga.visitCode();
097: ga.loadThis();
098: ga.loadArg(0);
099: ga.putField(bean, "f", Type.INT_TYPE);
100: ga.returnValue();
101: ga.endMethod();
102: }
103: mv = cv.visitMethod(ACC_PUBLIC, "checkAndSetF", "(I)V", null,
104: null);
105: if (mv != null) {
106: Type bean = Type.getType("Lpkg/Bean;");
107: Method m = Method.getMethod("void checkAndSetF(int)");
108: GeneratorAdapter ga = new GeneratorAdapter(ACC_PUBLIC, m,
109: mv);
110: ga.visitCode();
111: ga.loadArg(0);
112: Label label = new Label();
113: ga.ifZCmp(GeneratorAdapter.LT, label);
114: ga.loadThis();
115: ga.loadArg(0);
116: ga.putField(bean, "f", Type.INT_TYPE);
117: Label end = new Label();
118: ga.goTo(end);
119: ga.mark(label);
120: ga.throwException(Type
121: .getType(IllegalArgumentException.class), "f");
122: ga.mark(end);
123: ga.returnValue();
124: ga.endMethod();
125: }
126: cv.visitEnd();
127: byte[] b = cw.toByteArray();
128: return b;
129: }
130: }
|