01: /*
02: * ProGuard -- shrinking, optimization, obfuscation, and preverification
03: * of Java bytecode.
04: *
05: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the Free
09: * Software Foundation; either version 2 of the License, or (at your option)
10: * any later version.
11: *
12: * This program is distributed in the hope that it will be useful, but WITHOUT
13: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15: * more details.
16: *
17: * You should have received a copy of the GNU General Public License along
18: * with this program; if not, write to the Free Software Foundation, Inc.,
19: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: */
21: package proguard.optimize.peephole;
22:
23: import proguard.classfile.*;
24: import proguard.classfile.attribute.CodeAttribute;
25: import proguard.classfile.editor.CodeAttributeEditor;
26: import proguard.classfile.instruction.*;
27: import proguard.classfile.instruction.visitor.InstructionVisitor;
28: import proguard.classfile.util.SimplifiedVisitor;
29:
30: /**
31: * This InstructionVisitor removes all nop instructions that it encounters.
32: *
33: * @author Eric Lafortune
34: */
35: public class NopRemover extends SimplifiedVisitor implements
36: InstructionVisitor {
37: private final CodeAttributeEditor codeAttributeEditor;
38: private final InstructionVisitor extraInstructionVisitor;
39:
40: /**
41: * Creates a new NopRemover.
42: * @param codeAttributeEditor a code editor that can be used for
43: * accumulating changes to the code.
44: */
45: public NopRemover(CodeAttributeEditor codeAttributeEditor) {
46: this (codeAttributeEditor, null);
47: }
48:
49: /**
50: * Creates a new NopRemover.
51: * @param codeAttributeEditor a code editor that can be used for
52: * accumulating changes to the code.
53: * @param extraInstructionVisitor an optional extra visitor for all removed
54: * nop instructions.
55: */
56: public NopRemover(CodeAttributeEditor codeAttributeEditor,
57: InstructionVisitor extraInstructionVisitor) {
58: this .codeAttributeEditor = codeAttributeEditor;
59: this .extraInstructionVisitor = extraInstructionVisitor;
60: }
61:
62: // Implementations for InstructionVisitor.
63:
64: public void visitAnyInstruction(Clazz clazz, Method method,
65: CodeAttribute codeAttribute, int offset,
66: Instruction instruction) {
67: }
68:
69: public void visitSimpleInstruction(Clazz clazz, Method method,
70: CodeAttribute codeAttribute, int offset,
71: SimpleInstruction simpleInstruction) {
72: // Check if the instruction is a nop instruction.
73: if (simpleInstruction.opcode == InstructionConstants.OP_NOP
74: && !codeAttributeEditor.isModified(offset)) {
75: codeAttributeEditor.deleteInstruction(offset);
76:
77: // Visit the instruction, if required.
78: if (extraInstructionVisitor != null) {
79: extraInstructionVisitor.visitSimpleInstruction(clazz,
80: method, codeAttribute, offset,
81: simpleInstruction);
82: }
83: }
84: }
85: }
|