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.GETFIELD;
035: import static org.objectweb.asm.Opcodes.ILOAD;
036: import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
037: import static org.objectweb.asm.Opcodes.IRETURN;
038: import static org.objectweb.asm.Opcodes.PUTFIELD;
039: import static org.objectweb.asm.Opcodes.RETURN;
040: import static org.objectweb.asm.Opcodes.V1_1;
041:
042: import java.util.Iterator;
043: import java.util.Map;
044:
045: import org.objectweb.asm.ClassVisitor;
046: import org.objectweb.asm.MethodVisitor;
047: import org.objectweb.asm.Type;
048:
049: /**
050: * ASM Guide example class.
051: *
052: * @author Eric Bruneton
053: */
054: public class BeanGenerator3 {
055:
056: private String name;
057:
058: private Map<String, Type> fields;
059:
060: public BeanGenerator3(String name, Map<String, Type> fields) {
061: this .name = name;
062: this .fields = fields;
063: }
064:
065: public void generate(ClassVisitor cv) {
066: MethodVisitor mv;
067: cv
068: .visit(V1_1, ACC_PUBLIC, name, null,
069: "java/lang/Object", null);
070:
071: // generate default constructor
072: mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
073: if (mv != null) {
074: mv.visitCode();
075: mv.visitVarInsn(ALOAD, 0);
076: mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object",
077: "<init>", "()V");
078: mv.visitInsn(RETURN);
079: mv.visitMaxs(1, 1);
080: mv.visitEnd();
081: }
082:
083: Iterator<Map.Entry<String, Type>> i = fields.entrySet()
084: .iterator();
085: while (i.hasNext()) {
086: Map.Entry<String, Type> e = i.next();
087: String fName = e.getKey();
088: String fDesc = e.getValue().getDescriptor();
089: Type fType = e.getValue();
090: String mName;
091: String mDesc;
092:
093: // generate field
094: cv.visitField(ACC_PRIVATE, fName, fDesc, null, null)
095: .visitEnd();
096:
097: // generate getter
098: mName = "get" + up(fName);
099: mDesc = "()" + fDesc;
100: mv = cv.visitMethod(ACC_PUBLIC, mName, mDesc, null, null);
101: if (mv != null) {
102: mv.visitCode();
103: mv.visitVarInsn(ALOAD, 0);
104: mv.visitFieldInsn(GETFIELD, name, fName, fDesc);
105: mv.visitInsn(fType.getOpcode(IRETURN));
106: mv.visitMaxs(fType.getSize(), 1);
107: mv.visitEnd();
108: }
109:
110: // generate setter
111: mName = "set" + up(fName);
112: mDesc = "(" + fDesc + ")V";
113: mv = cv.visitMethod(ACC_PUBLIC, mName, mDesc, null, null);
114: if (mv != null) {
115: mv.visitCode();
116: mv.visitVarInsn(ALOAD, 0);
117: mv.visitVarInsn(fType.getOpcode(ILOAD), 1);
118: mv.visitFieldInsn(PUTFIELD, name, fName, fDesc);
119: mv.visitInsn(RETURN);
120: mv.visitMaxs(1 + fType.getSize(), 1 + fType.getSize());
121: mv.visitEnd();
122: }
123: }
124:
125: cv.visitEnd();
126: }
127:
128: private static String up(String name) {
129: return Character.toUpperCase(name.charAt(0))
130: + name.substring(1);
131: }
132: }
|