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