KickJava   Java API By Example, From Geeks To Geeks.

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


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 import proguard.classfile.attribute.Attribute;
25 import proguard.classfile.util.SimplifiedVisitor;
26 import proguard.classfile.visitor.MemberVisitor;
27
28 /**
29  * This ConstantVisitor adds all class members that it visits to the given
30  * target class.
31  *
32  * @author Eric Lafortune
33  */

34 public class MemberAdder
35 extends SimplifiedVisitor
36 implements MemberVisitor
37 {
38     private ProgramClass targetClass;
39     private boolean copyAttributes;
40
41     private ConstantAdder constantAdder = new ConstantAdder();
42     private MembersEditor membersEditor = new MembersEditor();
43
44
45     /**
46      * Creates a new MemberAdder that will copy methods into the given target
47      * class.
48      */

49     public MemberAdder(ProgramClass targetClass, boolean copyAttributes)
50     {
51         this.targetClass = targetClass;
52         this.copyAttributes = copyAttributes;
53
54         constantAdder.setTargetClass(targetClass);
55     }
56
57
58     // Implementations for MemberVisitor.
59

60     public void visitProgramField(ProgramClass programClass, ProgramField programField)
61     {
62         String JavaDoc name = programField.getName(programClass);
63         String JavaDoc descriptor = programField.getDescriptor(programClass);
64
65         if (targetClass.findMethod(name, descriptor) == null)
66         {
67             ProgramField newProgramField = new ProgramField();
68
69             // Copy the access flags.
70
newProgramField.u2accessFlags = programField.u2accessFlags;
71
72             // Make sure the name is set in the constant pool.
73
programClass.constantPoolEntryAccept(programField.u2nameIndex,
74                                                  constantAdder);
75
76             newProgramField.u2nameIndex = constantAdder.getConstantIndex();
77
78             // Make sure the descriptor is set in the constant pool.
79
programClass.constantPoolEntryAccept(programField.u2descriptorIndex,
80                                                  constantAdder);
81
82             newProgramField.u2descriptorIndex = constantAdder.getConstantIndex();
83
84             // Copy the attributes if requested.
85
if (copyAttributes)
86             {
87                 programField.attributesAccept(programClass,
88                                               new AttributeAdder(targetClass,
89                                                                  newProgramField));
90             }
91
92             // Actually add the completed field.
93
membersEditor.addField(targetClass, newProgramField);
94         }
95     }
96
97
98     public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
99     {
100         String JavaDoc name = programMethod.getName(programClass);
101         String JavaDoc descriptor = programMethod.getDescriptor(programClass);
102
103         if (targetClass.findMethod(name, descriptor) == null)
104         {
105             ProgramMethod newProgramMethod = new ProgramMethod();
106
107             // Copy the access flags.
108
newProgramMethod.u2accessFlags = programMethod.u2accessFlags;
109
110             // Make sure the name is set in the constant pool.
111
programClass.constantPoolEntryAccept(programMethod.u2nameIndex,
112                                                  constantAdder);
113
114             newProgramMethod.u2nameIndex = constantAdder.getConstantIndex();
115
116             // Make sure the descriptor is set in the constant pool.
117
programClass.constantPoolEntryAccept(programMethod.u2descriptorIndex,
118                                                  constantAdder);
119
120             newProgramMethod.u2descriptorIndex = constantAdder.getConstantIndex();
121
122             // Start with an empty list of attributes.
123
newProgramMethod.u2attributesCount = 0;
124             newProgramMethod.attributes = new Attribute[0];
125
126             // Copy the attributes if requested.
127
if (copyAttributes)
128             {
129                 programMethod.attributesAccept(programClass,
130                                                new AttributeAdder(targetClass,
131                                                                   newProgramMethod));
132             }
133
134             // Actually add the completed method.
135
membersEditor.addMethod(targetClass, newProgramMethod);
136         }
137     }
138 }
139
Popular Tags