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.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.MethodVisitor;
047: import org.objectweb.asm.commons.AnalyzerAdapter;
048:
049: /**
050: * ASM Guide example class.
051: *
052: * @author Eric Bruneton
053: */
054: public class AddTimerAdapter2 extends ClassAdapter {
055:
056: private String owner;
057:
058: private boolean isInterface;
059:
060: public AddTimerAdapter2(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 AddTimerMethodAdapter2(owner, access, name, desc,
078: mv);
079: }
080: return mv;
081: }
082:
083: public void visitEnd() {
084: if (!isInterface) {
085: FieldVisitor fv = cv.visitField(ACC_PUBLIC + ACC_STATIC,
086: "timer", "J", null, null);
087: if (fv != null) {
088: fv.visitEnd();
089: }
090: cv.visitEnd();
091: }
092: }
093:
094: class AddTimerMethodAdapter2 extends AnalyzerAdapter {
095:
096: private int maxStack;
097:
098: public AddTimerMethodAdapter2(String owner, int access,
099: String name, String desc, MethodVisitor mv) {
100: super (owner, access, name, desc, mv);
101: }
102:
103: public void visitCode() {
104: super .visitCode();
105: mv.visitFieldInsn(GETSTATIC, owner, "timer", "J");
106: mv.visitMethodInsn(INVOKESTATIC, "java/lang/System",
107: "currentTimeMillis", "()J");
108: mv.visitInsn(LSUB);
109: mv.visitFieldInsn(PUTSTATIC, owner, "timer", "J");
110: maxStack = 4;
111: }
112:
113: public void visitInsn(int opcode) {
114: if ((opcode >= IRETURN && opcode <= RETURN)
115: || opcode == ATHROW) {
116: mv.visitFieldInsn(GETSTATIC, owner, "timer", "J");
117: mv.visitMethodInsn(INVOKESTATIC, "java/lang/System",
118: "currentTimeMillis", "()J");
119: mv.visitInsn(LADD);
120: mv.visitFieldInsn(PUTSTATIC, owner, "timer", "J");
121: maxStack = Math.max(maxStack, stack.size() + 4);
122: }
123: super .visitInsn(opcode);
124: }
125:
126: public void visitMaxs(int maxStack, int maxLocals) {
127: super.visitMaxs(Math.max(this.maxStack, maxStack),
128: maxLocals);
129: }
130: }
131: }
|