KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > tools > ClassFieldModifier


1 /**
2  * Copyright (C) 2001-2005 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  *
20  * Authors: S.Chassande-Barrioz.
21  * Created on 26 mars 2005
22  *
23  */

24 package org.objectweb.speedo.tools;
25
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import org.objectweb.asm.Attribute;
32 import org.objectweb.asm.ClassAdapter;
33 import org.objectweb.asm.ClassReader;
34 import org.objectweb.asm.ClassVisitor;
35 import org.objectweb.asm.ClassWriter;
36 import org.objectweb.asm.Constants;
37 import org.objectweb.asm.attrs.Attributes;
38
39 /**
40  * This ASM visitor assignes a value to a static field. If the field does not
41  * exist, it is added.
42  *
43  * @author S.Chassande-Barrioz
44  */

45 public class ClassFieldModifier extends ClassAdapter {
46
47     private String JavaDoc className;
48     
49     /**
50      * The name of the field to modify
51      */

52     private String JavaDoc fieldName;
53     
54     /**
55      * The value to set to the field
56      */

57     private String JavaDoc fieldValue;
58     
59     /**
60      * indicates if the field has been found in the class
61      */

62     private boolean fieldFound;
63     
64     private boolean classModified = false;
65     
66     /**
67      * @param fieldName is the name of the field to modify
68      * @param fieldValue is the value to set to the field
69      */

70     public ClassFieldModifier(final ClassVisitor cv,
71             final String JavaDoc fieldName,
72             final String JavaDoc fieldValue) {
73         super(cv);
74         this.fieldName = fieldName;
75         this.fieldValue = fieldValue;
76         fieldFound = false;
77     }
78
79     public void visit(final int version, final int access,
80             final String JavaDoc name,
81             final String JavaDoc superName,
82             final String JavaDoc[] interfaces,
83             final String JavaDoc sourceFile) {
84         className = name;
85         super.visit(version, access, name, superName, interfaces, sourceFile);
86     }
87     public void visitField(final int access,
88             final String JavaDoc name,
89             final String JavaDoc desc,
90             final Object JavaDoc value,
91             final Attribute attrs) {
92         Object JavaDoc val = value;
93         if (name.equals(fieldName)) {
94             fieldFound = true;
95             if (value == null || !value.equals(fieldValue)) {
96                 val = fieldValue;
97                 classModified = true;
98                 System.out.println("Set the field '" + fieldName
99                         + "' of the class '" + className
100                         + "' to '" + val
101                         + "' (old value: '" + value + "').");
102             }
103         }
104         super.visitField(access, name, desc, val, attrs);
105     }
106
107     public void visitEnd() {
108         if (!fieldFound) {
109             classModified = true;
110             super.visitField(Constants.ACC_PUBLIC
111                     | Constants.ACC_STATIC
112                     | Constants.ACC_FINAL,
113                 fieldName,
114                 "Ljava/lang/String;",
115                 fieldValue,
116                 null);
117             System.out.println("Add the field '" + fieldName
118                     + "' to the class '" + className
119                     + "' with the value '" + fieldValue + "'.");
120         }
121         super.visitEnd();
122     }
123
124     public boolean isClassModified() {
125         return classModified;
126     }
127     
128     public static void main(String JavaDoc[] args) throws IOException JavaDoc {
129         if (args == null || args.length < 3) {
130             usage();
131             return;
132         }
133         String JavaDoc classFileName = args[0];
134         String JavaDoc fieldName = args[1];
135         String JavaDoc fieldValue = args[2];
136         ClassReader cr;
137         File JavaDoc f = new File JavaDoc(classFileName);
138         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(f);
139         cr = new ClassReader(fis);
140         fis.close();
141         ClassWriter cw = new ClassWriter(true);
142         ClassFieldModifier cfm = new ClassFieldModifier(cw, fieldName, fieldValue);
143         cr.accept(cfm, Attributes.getDefaultAttributes(), false);
144         if (cfm.isClassModified()) {
145             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(f);
146             fos.write(cw.toByteArray());
147             fos.close();
148         }
149     }
150         
151     public final static void usage() {
152         System.out.println("Usage: <class file> <field name> <field value>");
153     }
154 }
155
Popular Tags