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.1KB

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