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