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_INTERFACE;
032: import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
033: import static org.objectweb.asm.Opcodes.ACC_STATIC;
034: import static org.objectweb.asm.Opcodes.ATHROW;
035: import static org.objectweb.asm.Opcodes.GETSTATIC;
036: import static org.objectweb.asm.Opcodes.INVOKESTATIC;
037: import static org.objectweb.asm.Opcodes.IRETURN;
038: import static org.objectweb.asm.Opcodes.LADD;
039: import static org.objectweb.asm.Opcodes.LSUB;
040: import static org.objectweb.asm.Opcodes.PUTSTATIC;
041: import static org.objectweb.asm.Opcodes.RETURN;
042:
043: import org.objectweb.asm.ClassAdapter;
044: import org.objectweb.asm.ClassVisitor;
045: import org.objectweb.asm.FieldVisitor;
046: import org.objectweb.asm.MethodAdapter;
047: import org.objectweb.asm.MethodVisitor;
048:
049: /**
050: * ASM Guide example class.
051: *
052: * @author Eric Bruneton
053: */
054: public class AddTimerAdapter extends ClassAdapter {
055:
056: private String owner;
057:
058: private boolean isInterface;
059:
060: public AddTimerAdapter(ClassVisitor cv) {
061: super (cv);
062: }
063:
064: public void visit(int version, int access, String name,
065: String signature, String super Name, String[] interfaces) {
066: cv.visit(version, access, name, signature, super Name,
067: interfaces);
068: owner = name;
069: isInterface = (access & ACC_INTERFACE) != 0;
070: }
071:
072: public MethodVisitor visitMethod(int access, String name,
073: String desc, String signature, String[] exceptions) {
074: MethodVisitor mv = cv.visitMethod(access, name, desc,
075: signature, exceptions);
076: if (!isInterface && mv != null && !name.equals("<init>")) {
077: mv = new AddTimerMethodAdapter(mv);
078: }
079: return mv;
080: }
081:
082: public void visitEnd() {
083: if (!isInterface) {
084: FieldVisitor fv = cv.visitField(ACC_PUBLIC + ACC_STATIC,
085: "timer", "J", null, null);
086: if (fv != null) {
087: fv.visitEnd();
088: }
089: }
090: cv.visitEnd();
091: }
092:
093: class AddTimerMethodAdapter extends MethodAdapter {
094:
095: public AddTimerMethodAdapter(MethodVisitor mv) {
096: super (mv);
097: }
098:
099: public void visitCode() {
100: mv.visitCode();
101: mv.visitFieldInsn(GETSTATIC, owner, "timer", "J");
102: mv.visitMethodInsn(INVOKESTATIC, "java/lang/System",
103: "currentTimeMillis", "()J");
104: mv.visitInsn(LSUB);
105: mv.visitFieldInsn(PUTSTATIC, owner, "timer", "J");
106: }
107:
108: public void visitInsn(int opcode) {
109: if ((opcode >= IRETURN && opcode <= RETURN)
110: || opcode == ATHROW) {
111: mv.visitFieldInsn(GETSTATIC, owner, "timer", "J");
112: mv.visitMethodInsn(INVOKESTATIC, "java/lang/System",
113: "currentTimeMillis", "()J");
114: mv.visitInsn(LADD);
115: mv.visitFieldInsn(PUTSTATIC, owner, "timer", "J");
116: }
117: mv.visitInsn(opcode);
118: }
119:
120: public void visitMaxs(int maxStack, int maxLocals) {
121: mv.visitMaxs(maxStack + 4, maxLocals);
122: }
123: }
124: }
|