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.sec2;
030:
031: import static org.objectweb.asm.Opcodes.ALOAD;
032: import static org.objectweb.asm.Opcodes.GETFIELD;
033: import static org.objectweb.asm.Opcodes.PUTFIELD;
034:
035: import org.objectweb.asm.MethodVisitor;
036:
037: /**
038: * ASM Guide example class.
039: *
040: * @author Eric Bruneton
041: */
042: public class RemoveGetFieldPutFieldAdapter extends PatternMethodAdapter {
043:
044: private final static int SEEN_ALOAD_0 = 1;
045:
046: private final static int SEEN_ALOAD_0ALOAD_0 = 2;
047:
048: private final static int SEEN_ALOAD_0ALOAD_0GETFIELD = 3;
049:
050: private String fieldOwner;
051:
052: private String fieldName;
053:
054: private String fieldDesc;
055:
056: public RemoveGetFieldPutFieldAdapter(MethodVisitor mv) {
057: super (mv);
058: }
059:
060: public void visitVarInsn(int opcode, int var) {
061: switch (state) {
062: case SEEN_NOTHING: // S0 -> S1
063: if (opcode == ALOAD && var == 0) {
064: state = SEEN_ALOAD_0;
065: return;
066: }
067: break;
068: case SEEN_ALOAD_0: // S1 -> S2
069: if (opcode == ALOAD && var == 0) {
070: state = SEEN_ALOAD_0ALOAD_0;
071: return;
072: }
073: break;
074: case SEEN_ALOAD_0ALOAD_0: // S2 -> S2
075: if (opcode == ALOAD && var == 0) {
076: mv.visitVarInsn(ALOAD, 0);
077: return;
078: }
079: break;
080: }
081: visitInsn();
082: mv.visitVarInsn(opcode, var);
083: }
084:
085: public void visitFieldInsn(int opcode, String owner, String name,
086: String desc) {
087: switch (state) {
088: case SEEN_ALOAD_0ALOAD_0: // S2 -> S3
089: if (opcode == GETFIELD) {
090: state = SEEN_ALOAD_0ALOAD_0GETFIELD;
091: fieldOwner = owner;
092: fieldName = name;
093: fieldDesc = desc;
094: return;
095: }
096: break;
097: case SEEN_ALOAD_0ALOAD_0GETFIELD: // S3 -> S0
098: if (opcode == PUTFIELD && name.equals(fieldName)) {
099: state = SEEN_NOTHING;
100: return;
101: }
102: break;
103: }
104: visitInsn();
105: mv.visitFieldInsn(opcode, owner, name, desc);
106: }
107:
108: protected void visitInsn() {
109: switch (state) {
110: case SEEN_ALOAD_0: // S1 -> S0
111: mv.visitVarInsn(ALOAD, 0);
112: break;
113: case SEEN_ALOAD_0ALOAD_0: // S2 -> S0
114: mv.visitVarInsn(ALOAD, 0);
115: mv.visitVarInsn(ALOAD, 0);
116: break;
117: case SEEN_ALOAD_0ALOAD_0GETFIELD: // S3 -> S0
118: mv.visitVarInsn(ALOAD, 0);
119: mv.visitVarInsn(ALOAD, 0);
120: mv.visitFieldInsn(GETFIELD, fieldOwner, fieldName,
121: fieldDesc);
122: break;
123: }
124: state = SEEN_NOTHING;
125: }
126: }
|