You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FieldGen.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package org.aspectj.apache.bcel.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 java.util.Iterator;
  56. import java.util.List;
  57. import org.aspectj.apache.bcel.Constants;
  58. import org.aspectj.apache.bcel.classfile.Attribute;
  59. import org.aspectj.apache.bcel.classfile.Constant;
  60. import org.aspectj.apache.bcel.classfile.ConstantDouble;
  61. import org.aspectj.apache.bcel.classfile.ConstantFloat;
  62. import org.aspectj.apache.bcel.classfile.ConstantInteger;
  63. import org.aspectj.apache.bcel.classfile.ConstantLong;
  64. import org.aspectj.apache.bcel.classfile.ConstantObject;
  65. import org.aspectj.apache.bcel.classfile.ConstantPool;
  66. import org.aspectj.apache.bcel.classfile.ConstantString;
  67. import org.aspectj.apache.bcel.classfile.ConstantValue;
  68. import org.aspectj.apache.bcel.classfile.Field;
  69. import org.aspectj.apache.bcel.classfile.Utility;
  70. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  71. import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnos;
  72. /**
  73. * Template class for building up a field. The only extraordinary thing one can do is to add a constant value attribute to a field
  74. * (which must of course be compatible with the declared type).
  75. *
  76. * @version $Id: FieldGen.java,v 1.11 2011/10/03 22:41:24 aclement Exp $
  77. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  78. * @see Field
  79. */
  80. public class FieldGen extends FieldGenOrMethodGen {
  81. private Object value = null;
  82. /**
  83. * Declare a field. If it is static (isStatic() == true) and has a basic type like int or String it may have an initial value
  84. * associated with it as defined by setInitValue().
  85. *
  86. * @param modifiers access qualifiers
  87. * @param type field type
  88. * @param name field name
  89. * @param cpool constant pool
  90. */
  91. public FieldGen(int modifiers, Type type, String name, ConstantPool cpool) {
  92. setModifiers(modifiers);
  93. setType(type);
  94. setName(name);
  95. setConstantPool(cpool);
  96. }
  97. /**
  98. * Instantiate from existing field.
  99. *
  100. * @param field Field object
  101. * @param cp constant pool (must contain the same entries as the field's constant pool)
  102. */
  103. public FieldGen(Field field, ConstantPool cp) {
  104. this(field.getModifiers(), Type.getType(field.getSignature()), field.getName(), cp);
  105. Attribute[] attrs = field.getAttributes();
  106. for (int i = 0; i < attrs.length; i++) {
  107. if (attrs[i] instanceof ConstantValue) {
  108. setValue(((ConstantValue) attrs[i]).getConstantValueIndex());
  109. } else if (attrs[i] instanceof RuntimeAnnos) {
  110. RuntimeAnnos runtimeAnnotations = (RuntimeAnnos) attrs[i];
  111. List<AnnotationGen> l = runtimeAnnotations.getAnnotations();
  112. for (Iterator<AnnotationGen> it = l.iterator(); it.hasNext();) {
  113. AnnotationGen element = it.next();
  114. addAnnotation(new AnnotationGen(element, cp, false));
  115. }
  116. } else {
  117. addAttribute(attrs[i]);
  118. }
  119. }
  120. }
  121. public void setValue(int index) {
  122. ConstantPool cp = this.cp;
  123. Constant c = cp.getConstant(index);
  124. if (c instanceof ConstantInteger) {
  125. value = ((ConstantInteger) c).getIntValue();
  126. } else if (c instanceof ConstantFloat) {
  127. value = ((ConstantFloat) c).getValue();
  128. } else if (c instanceof ConstantDouble) {
  129. value = ((ConstantDouble) c).getValue();
  130. } else if (c instanceof ConstantLong) {
  131. value = ((ConstantLong) c).getValue();
  132. } else if (c instanceof ConstantString) {
  133. value = ((ConstantString)c).getString(cp);
  134. } else {
  135. value = ((ConstantObject) c).getConstantValue(cp);
  136. }
  137. }
  138. public void setValue(String constantString) {
  139. value = constantString;
  140. }
  141. public void wipeValue() {
  142. value = null;
  143. }
  144. private void checkType(Type atype) {
  145. if (type == null)
  146. throw new ClassGenException("You haven't defined the type of the field yet");
  147. if (!isFinal())
  148. throw new ClassGenException("Only final fields may have an initial value!");
  149. if (!type.equals(atype))
  150. throw new ClassGenException("Types are not compatible: " + type + " vs. " + atype);
  151. }
  152. /**
  153. * Get field object after having set up all necessary values.
  154. */
  155. public Field getField() {
  156. String signature = getSignature();
  157. int nameIndex = cp.addUtf8(name);
  158. int signatureIndex = cp.addUtf8(signature);
  159. if (value != null) {
  160. checkType(type);
  161. int index = addConstant();
  162. addAttribute(new ConstantValue(cp.addUtf8("ConstantValue"), 2, index, cp));
  163. }
  164. addAnnotationsAsAttribute(cp);
  165. return new Field(modifiers, nameIndex, signatureIndex, getAttributesImmutable(), cp);
  166. }
  167. private int addConstant() {
  168. switch (type.getType()) {
  169. case Constants.T_INT:
  170. case Constants.T_CHAR:
  171. case Constants.T_BYTE:
  172. case Constants.T_BOOLEAN:
  173. case Constants.T_SHORT:
  174. return cp.addInteger(((Integer) value).intValue());
  175. case Constants.T_FLOAT:
  176. return cp.addFloat(((Float) value).floatValue());
  177. case Constants.T_DOUBLE:
  178. return cp.addDouble(((Double) value).doubleValue());
  179. case Constants.T_LONG:
  180. return cp.addLong(((Long) value).longValue());
  181. case Constants.T_REFERENCE:
  182. return cp.addString(((String) value));
  183. default:
  184. throw new RuntimeException("Oops: Unhandled : " + type.getType());
  185. }
  186. }
  187. @Override
  188. public String getSignature() {
  189. return type.getSignature();
  190. }
  191. public String getInitialValue() {
  192. return (value == null ? null : value.toString());
  193. }
  194. public void setInitialStringValue(String value) {
  195. this.value = value;
  196. }
  197. /**
  198. * Return string representation close to declaration format, `public static final short MAX = 100', e.g..
  199. */
  200. @Override
  201. public final String toString() {
  202. String access = Utility.accessToString(modifiers);
  203. access = access.equals("") ? "" : (access + " ");
  204. String signature = type.toString();
  205. String name = getName();
  206. StringBuffer buf = new StringBuffer(access).append(signature).append(" ").append(name);
  207. String value = getInitialValue();
  208. if (value != null) {
  209. buf.append(" = ").append(value);
  210. }
  211. // TODO: Add attributes and annotations to the string
  212. return buf.toString();
  213. }
  214. }