1. package com.sun.org.apache.bcel.internal.generic;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import com.sun.org.apache.bcel.internal.Constants;
  56. import com.sun.org.apache.bcel.internal.classfile.*;
  57. import java.util.ArrayList;
  58. import java.util.Iterator;
  59. /**
  60. * Template class for building up a field. The only extraordinary thing
  61. * one can do is to add a constant value attribute to a field (which must of
  62. * course be compatible with to the declared type).
  63. *
  64. * @version $Id: FieldGen.java,v 1.1.1.1 2001/10/29 20:00:12 jvanzyl Exp $
  65. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  66. * @see Field
  67. */
  68. public class FieldGen extends FieldGenOrMethodGen {
  69. private Object value = null;
  70. /**
  71. * Declare a field. If it is static (isStatic() == true) and has a
  72. * basic type like int or String it may have an initial value
  73. * associated with it as defined by setInitValue().
  74. *
  75. * @param access_flags access qualifiers
  76. * @param type field type
  77. * @param name field name
  78. * @param cp constant pool
  79. */
  80. public FieldGen(int access_flags, Type type, String name, ConstantPoolGen cp) {
  81. setAccessFlags(access_flags);
  82. setType(type);
  83. setName(name);
  84. setConstantPool(cp);
  85. }
  86. /**
  87. * Instantiate from existing field.
  88. *
  89. * @param field Field object
  90. * @param cp constant pool (must contain the same entries as the field's constant pool)
  91. */
  92. public FieldGen(Field field, ConstantPoolGen cp) {
  93. this(field.getAccessFlags(), Type.getType(field.getSignature()), field.getName(), cp);
  94. Attribute[] attrs = field.getAttributes();
  95. for(int i=0; i < attrs.length; i++) {
  96. if(attrs[i] instanceof ConstantValue)
  97. setValue(((ConstantValue)attrs[i]).getConstantValueIndex());
  98. else
  99. addAttribute(attrs[i]);
  100. }
  101. }
  102. private void setValue(int index) {
  103. ConstantPool cp = this.cp.getConstantPool();
  104. Constant c = cp.getConstant(index);
  105. value = ((ConstantObject)c).getConstantValue(cp);
  106. }
  107. /**
  108. * Set (optional) initial value of field, otherwise it will be set to null/0/false
  109. * by the JVM automatically.
  110. */
  111. public void setInitValue(String str) {
  112. checkType(new ObjectType("java.lang.String"));
  113. if(str != null)
  114. value = str;
  115. }
  116. public void setInitValue(long l) {
  117. checkType(Type.LONG);
  118. if(l != 0L)
  119. value = new Long(l);
  120. }
  121. public void setInitValue(int i) {
  122. checkType(Type.INT);
  123. if(i != 0)
  124. value = new Integer(i);
  125. }
  126. public void setInitValue(short s) {
  127. checkType(Type.SHORT);
  128. if(s != 0)
  129. value = new Integer(s);
  130. }
  131. public void setInitValue(char c) {
  132. checkType(Type.CHAR);
  133. if(c != 0)
  134. value = new Integer(c);
  135. }
  136. public void setInitValue(byte b) {
  137. checkType(Type.BYTE);
  138. if(b != 0)
  139. value = new Integer(b);
  140. }
  141. public void setInitValue(boolean b) {
  142. checkType(Type.BOOLEAN);
  143. if(b)
  144. value = new Integer(1);
  145. }
  146. public void setInitValue(float f) {
  147. checkType(Type.FLOAT);
  148. if(f != 0.0)
  149. value = new Float(f);
  150. }
  151. public void setInitValue(double d) {
  152. checkType(Type.DOUBLE);
  153. if(d != 0.0)
  154. value = new Double(d);
  155. }
  156. /** Remove any initial value.
  157. */
  158. public void cancelInitValue() {
  159. value = null;
  160. }
  161. private void checkType(Type atype) {
  162. if(type == null)
  163. throw new ClassGenException("You haven't defined the type of the field yet");
  164. if(!isFinal())
  165. throw new ClassGenException("Only final fields may have an initial value!");
  166. if(!type.equals(atype))
  167. throw new ClassGenException("Types are not compatible: " + type + " vs. " + atype);
  168. }
  169. /**
  170. * Get field object after having set up all necessary values.
  171. */
  172. public Field getField() {
  173. String signature = getSignature();
  174. int name_index = cp.addUtf8(name);
  175. int signature_index = cp.addUtf8(signature);
  176. if(value != null) {
  177. checkType(type);
  178. int index = addConstant();
  179. addAttribute(new ConstantValue(cp.addUtf8("ConstantValue"),
  180. 2, index, cp.getConstantPool()));
  181. }
  182. return new Field(access_flags, name_index, signature_index, getAttributes(),
  183. cp.getConstantPool());
  184. }
  185. private int addConstant() {
  186. switch(type.getType()) {
  187. case Constants.T_INT: case Constants.T_CHAR: case Constants.T_BYTE:
  188. case Constants.T_BOOLEAN: case Constants.T_SHORT:
  189. return cp.addInteger(((Integer)value).intValue());
  190. case Constants.T_FLOAT:
  191. return cp.addFloat(((Float)value).floatValue());
  192. case Constants.T_DOUBLE:
  193. return cp.addDouble(((Double)value).doubleValue());
  194. case Constants.T_LONG:
  195. return cp.addLong(((Long)value).longValue());
  196. case Constants.T_REFERENCE:
  197. return cp.addString(((String)value));
  198. default:
  199. throw new RuntimeException("Oops: Unhandled : " + type.getType());
  200. }
  201. }
  202. public String getSignature() { return type.getSignature(); }
  203. private ArrayList observers;
  204. /** Add observer for this object.
  205. */
  206. public void addObserver(FieldObserver o) {
  207. if(observers == null)
  208. observers = new ArrayList();
  209. observers.add(o);
  210. }
  211. /** Remove observer for this object.
  212. */
  213. public void removeObserver(FieldObserver o) {
  214. if(observers != null)
  215. observers.remove(o);
  216. }
  217. /** Call notify() method on all observers. This method is not called
  218. * automatically whenever the state has changed, but has to be
  219. * called by the user after he has finished editing the object.
  220. */
  221. public void update() {
  222. if(observers != null)
  223. for(Iterator e = observers.iterator(); e.hasNext(); )
  224. ((FieldObserver)e.next()).notify(this);
  225. }
  226. public String getInitValue() {
  227. if(value != null) {
  228. return value.toString();
  229. } else
  230. return null;
  231. }
  232. /**
  233. * Return string representation close to declaration format,
  234. * `public static final short MAX = 100', e.g..
  235. *
  236. * @return String representation of field
  237. */
  238. public final String toString() {
  239. String name, signature, access; // Short cuts to constant pool
  240. access = Utility.accessToString(access_flags);
  241. access = access.equals("")? "" : (access + " ");
  242. signature = type.toString();
  243. name = getName();
  244. StringBuffer buf = new StringBuffer(access + signature + " " + name);
  245. String value = getInitValue();
  246. if(value != null)
  247. buf.append(" = " + value);
  248. return buf.toString();
  249. }
  250. /** @return deep copy of this field
  251. */
  252. public FieldGen copy(ConstantPoolGen cp) {
  253. FieldGen fg = (FieldGen)clone();
  254. fg.setConstantPool(cp);
  255. return fg;
  256. }
  257. }