KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > instrumentor > CreateFieldInstrumentor


1 /*
2  * Copyright (C) 2001 Mika Riekkinen, Joni Suominen
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.1 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 package alt.jiapi.instrumentor;
20
21 import java.lang.reflect.Modifier JavaDoc;
22
23 import org.apache.log4j.Category;
24
25 import alt.jiapi.Runtime;
26 import alt.jiapi.reflect.FieldExistsException;
27 import alt.jiapi.reflect.InstructionList;
28 import alt.jiapi.reflect.JiapiClass;
29 import alt.jiapi.reflect.JiapiField;
30
31 /**
32  * A ClassInstrumentor which creates a new field for a class.
33  *
34  * @author Mika Riekkinen
35  * @author Joni Suominen
36  * @version $Revision: 1.7 $ $Date: 2004/03/15 14:47:53 $
37  */

38 public class CreateFieldInstrumentor extends AbstractInstrumentor {
39     private static Category log = Runtime.getLogCategory(CreateFieldInstrumentor.class);
40     private int modifiers;
41     private String JavaDoc type;
42     private String JavaDoc name;
43
44     /**
45      * Constructor.
46      * @param modifiers Modifiers of the Field to be created
47      * @param type Type of the field to be created
48      * @param name Name of the field to be created
49      */

50     public CreateFieldInstrumentor(int modifiers, String JavaDoc type, String JavaDoc name) {
51         this.modifiers = modifiers;
52         this.type = type;
53         this.name = name;
54     }
55
56     /**
57      * Constructs CreateFieldInstrumentor with existing JiapiField.
58      * @param field JiapiField, that is used to get relevant information
59      */

60     public CreateFieldInstrumentor(JiapiField field) {
61         this(field.getModifiers(), field.getTypeName(), field.getName());
62     }
63
64     /**
65      * Instrument a JiapiClass.
66      *
67      * @param clazz A Class to be instrumented
68      */

69     public void instrument(InstructionList il) {
70         JiapiClass clazz = getCurrentClass();
71         log.info("Adding field '" + modifiers + " " + type + " " + name +
72                  "' to " + clazz.getName());
73
74         // If the class is interface, then the modifiers must be public,
75
// static and final.
76
if (Modifier.isInterface(clazz.getModifiers())) {
77             if (!(Modifier.isPublic(modifiers) &&
78                   Modifier.isStatic(modifiers) &&
79                   Modifier.isFinal(modifiers))) {
80
81                 log.info("Cannot add '" + modifiers + " " + type + " " + name +
82                          "' to " + clazz.getName());
83
84                 forward(il);
85                 return;
86             }
87         }
88
89         try {
90             clazz.addField(modifiers, type, name);
91         }
92         catch (FieldExistsException fee) {
93             log.info("field " + fee.getField() + " already exists in a class "+
94                      clazz.getName());
95         }
96
97         forward(il);
98     }
99 }
100
Popular Tags