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.*;
     25 import proguard.classfile.attribute.visitor.BootstrapMethodInfoVisitor;
     26 
     27 /**
     28  * This BootstrapMethodInfoVisitor adds all bootstrap methods that it visits to
     29  * the given target class, creating a bootstrap methods attribute if necessary.
     30  */
     31 public class BootstrapMethodsAttributeAdder
     32 implements   BootstrapMethodInfoVisitor
     33 {
     34     private final ProgramClass             targetClass;
     35     private final ConstantPoolEditor       constantPoolEditor;
     36     private       BootstrapMethodInfoAdder bootstrapMethodInfoAdder;
     37 
     38 
     39     /**
     40      * Creates a new BootstrapMethodsAttributeAdder that will copy bootstrap
     41      * methods into the given target class/
     42      */
     43     public BootstrapMethodsAttributeAdder(ProgramClass targetClass)
     44     {
     45         this.targetClass        = targetClass;
     46         this.constantPoolEditor = new ConstantPoolEditor(targetClass);
     47     }
     48 
     49 
     50     /**
     51      * Returns the index of the most recently added bootstrap method.
     52      */
     53     public int getBootstrapMethodIndex()
     54     {
     55         return bootstrapMethodInfoAdder.getBootstrapMethodIndex();
     56     }
     57 
     58 
     59     // Implementations for BootstrapMethodInfoVisitor.
     60 
     61     public void visitBootstrapMethodInfo(Clazz clazz, BootstrapMethodInfo bootstrapMethodInfo)
     62     {
     63         // Make sure we have a bootstrap methods attribute adder.
     64         if (bootstrapMethodInfoAdder == null)
     65         {
     66             // Make sure we have a target bootstrap methods attribute.
     67             AttributesEditor attributesEditor =
     68                 new AttributesEditor(targetClass, false);
     69 
     70             BootstrapMethodsAttribute targetBootstrapMethodsAttribute =
     71                 (BootstrapMethodsAttribute)attributesEditor.findAttribute(ClassConstants.ATTR_BootstrapMethods);
     72 
     73             if (targetBootstrapMethodsAttribute == null)
     74             {
     75                 targetBootstrapMethodsAttribute =
     76                     new BootstrapMethodsAttribute(constantPoolEditor.addUtf8Constant(ClassConstants.ATTR_BootstrapMethods),
     77                                                   0,
     78                                                   new BootstrapMethodInfo[0]);
     79 
     80                 attributesEditor.addAttribute(targetBootstrapMethodsAttribute);
     81             }
     82 
     83             // Create a bootstrap method adder for it.
     84             bootstrapMethodInfoAdder = new BootstrapMethodInfoAdder(targetClass,
     85                                                                     targetBootstrapMethodsAttribute);
     86         }
     87 
     88         // Delegate to the bootstrap method adder.
     89         bootstrapMethodInfoAdder.visitBootstrapMethodInfo(clazz, bootstrapMethodInfo);
     90     }
     91 }