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 AddTimerAdapter3 extends ClassAdapter {
055:
056: private String owner;
057:
058: private boolean isInterface;
059:
060: public AddTimerAdapter3(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 AddTimerMethodAdapter3(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 AddTimerMethodAdapter3 extends AnalyzerAdapter {
095:
096: public AddTimerMethodAdapter3(String owner, int access,
097: String name, String desc, MethodVisitor mv) {
098: super (owner, access, name, desc, mv);
099: }
100:
101: public void visitCode() {
102: super .visitCode();
103: super .visitFieldInsn(GETSTATIC, owner, "timer", "J");
104: super .visitMethodInsn(INVOKESTATIC, "java/lang/System",
105: "currentTimeMillis", "()J");
106: super .visitInsn(LSUB);
107: super .visitFieldInsn(PUTSTATIC, owner, "timer", "J");
108: }
109:
110: public void visitInsn(int opcode) {
111: if ((opcode >= IRETURN && opcode <= RETURN)
112: || opcode == ATHROW) {
113: super .visitFieldInsn(GETSTATIC, owner, "timer", "J");
114: super .visitMethodInsn(INVOKESTATIC, "java/lang/System",
115: "currentTimeMillis", "()J");
116: super .visitInsn(LADD);
117: super .visitFieldInsn(PUTSTATIC, owner, "timer", "J");
118: }
119: super.visitInsn(opcode);
120: }
121: }
122: }
|