KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > classfile > editor > MembersEditor


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.classfile.editor;
22
23 import proguard.classfile.*;
24
25 /**
26  * This class can add class members to classes. Class members to be added must
27  * be filled out beforehand, including their references to the constant pool.
28  *
29  * @author Eric Lafortune
30  */

31 public class MembersEditor
32 {
33     private static final boolean DEBUG = false;
34
35
36     /**
37      * Adds a given field to the given class.
38      */

39     public void addField(ProgramClass programClass,
40                          Field field)
41     {
42         int fieldsCount = programClass.u2fieldsCount;
43         Field[] fields = programClass.fields;
44
45         // Make sure there is enough space for the new field.
46
if (fields.length <= fieldsCount)
47         {
48             programClass.fields = new ProgramField[fieldsCount+1];
49             System.arraycopy(fields, 0,
50                              programClass.fields, 0,
51                              fieldsCount);
52             fields = programClass.fields;
53         }
54
55         if (DEBUG)
56         {
57             System.out.println(programClass.getName()+": adding ["+field.getName(programClass)+field.getDescriptor(programClass)+"]");
58         }
59
60         // Add the field.
61
fields[programClass.u2fieldsCount++] = field;
62     }
63
64
65     /**
66      * Adds a given method to the given class.
67      */

68     public void addMethod(ProgramClass programClass,
69                           Method method)
70     {
71         int methodsCount = programClass.u2methodsCount;
72         Method[] methods = programClass.methods;
73
74         // Make sure there is enough space for the new method.
75
if (methods.length <= methodsCount)
76         {
77             programClass.methods = new ProgramMethod[methodsCount+1];
78             System.arraycopy(methods, 0,
79                              programClass.methods, 0,
80                              methodsCount);
81             methods = programClass.methods;
82         }
83
84         if (DEBUG)
85         {
86             System.out.println(programClass.getName()+": adding ["+method.getName(programClass)+method.getDescriptor(programClass)+"]");
87         }
88
89         // Add the method.
90
methods[programClass.u2methodsCount++] = method;
91     }
92 }
93
Popular Tags