Home | History | Annotate | Download | only in editor
      1 /*
      2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
      3  *             of Java bytecode.
      4  *
      5  * Copyright (c) 2002-2014 Eric Lafortune (eric (at) 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.classfile.editor;
     22 
     23 import proguard.classfile.*;
     24 import proguard.classfile.attribute.CodeAttribute;
     25 import proguard.classfile.instruction.*;
     26 import proguard.classfile.instruction.visitor.InstructionVisitor;
     27 import proguard.classfile.util.SimplifiedVisitor;
     28 
     29 /**
     30  * This InstructionVisitor adds all instructions that it visits to the given
     31  * target code attribute.
     32  *
     33  * @author Eric Lafortune
     34  */
     35 public class InstructionAdder
     36 extends      SimplifiedVisitor
     37 implements   InstructionVisitor
     38 {
     39     private final ConstantAdder         constantAdder;
     40     private final CodeAttributeComposer codeAttributeComposer;
     41 
     42 
     43     /**
     44      * Creates a new InstructionAdder that will copy classes into the given
     45      * target code attribute.
     46      */
     47     public InstructionAdder(ProgramClass          targetClass,
     48                             CodeAttributeComposer targetComposer)
     49     {
     50         constantAdder         = new ConstantAdder(targetClass);
     51         codeAttributeComposer = targetComposer;
     52     }
     53 
     54 
     55     // Implementations for InstructionVisitor.
     56 
     57 
     58     public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction)
     59     {
     60         // Add the instruction.
     61         codeAttributeComposer.appendInstruction(offset, instruction);
     62     }
     63 
     64 
     65     public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
     66     {
     67         // Create a copy of the instruction.
     68         Instruction newConstantInstruction =
     69             new ConstantInstruction(constantInstruction.opcode,
     70                                     constantAdder.addConstant(clazz, constantInstruction.constantIndex),
     71                                     constantInstruction.constant);
     72 
     73         // Add the instruction.
     74         codeAttributeComposer.appendInstruction(offset, newConstantInstruction);
     75     }
     76 }