KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > optimize > peephole > NopRemover


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * 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
36 extends SimplifiedVisitor
37 implements InstructionVisitor
38 {
39     private CodeAttributeEditor codeAttributeEditor;
40     private InstructionVisitor extraInstructionVisitor;
41
42
43     /**
44      * Creates a new NopRemover.
45      * @param codeAttributeEditor a code editor that can be used for
46      * accumulating changes to the code.
47      */

48     public NopRemover(CodeAttributeEditor codeAttributeEditor)
49     {
50         this(codeAttributeEditor, null);
51     }
52
53
54     /**
55      * Creates a new NopRemover.
56      * @param codeAttributeEditor a code editor that can be used for
57      * accumulating changes to the code.
58      * @param extraInstructionVisitor an optional extra visitor for all removed
59      * nop instructions.
60      */

61     public NopRemover(CodeAttributeEditor codeAttributeEditor,
62                       InstructionVisitor extraInstructionVisitor)
63     {
64         this.codeAttributeEditor = codeAttributeEditor;
65         this.extraInstructionVisitor = extraInstructionVisitor;
66     }
67
68
69     // Implementations for InstructionVisitor.
70

71     public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
72
73
74     public void visitSimpleInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SimpleInstruction simpleInstruction)
75     {
76         // Check if the instruction is a nop instruction.
77
if (simpleInstruction.opcode == InstructionConstants.OP_NOP &&
78             !codeAttributeEditor.isModified(offset))
79         {
80             codeAttributeEditor.deleteInstruction(offset);
81
82             // Visit the instruction, if required.
83
if (extraInstructionVisitor != null)
84             {
85                 extraInstructionVisitor.visitSimpleInstruction(clazz, method, codeAttribute, offset, simpleInstruction);
86             }
87         }
88     }
89 }
90
Popular Tags