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