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

12 anni fa
14 anni fa
14 anni fa
12 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
12 anni fa
12 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
14 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 (Attribute attr : attrs) {
  107. if (attr instanceof ConstantValue) {
  108. setValue(((ConstantValue) attr).getConstantValueIndex());
  109. } else if (attr instanceof RuntimeAnnos) {
  110. RuntimeAnnos runtimeAnnotations = (RuntimeAnnos) attr;
  111. List<AnnotationGen> l = runtimeAnnotations.getAnnotations();
  112. for (AnnotationGen element : l) {
  113. addAnnotation(new AnnotationGen(element, cp, false));
  114. }
  115. } else {
  116. addAttribute(attr);
  117. }
  118. }
  119. }
  120. public void setValue(int index) {
  121. ConstantPool cp = this.cp;
  122. Constant c = cp.getConstant(index);
  123. if (c instanceof ConstantInteger) {
  124. value = ((ConstantInteger) c).getIntValue();
  125. } else if (c instanceof ConstantFloat) {
  126. value = ((ConstantFloat) c).getValue();
  127. } else if (c instanceof ConstantDouble) {
  128. value = ((ConstantDouble) c).getValue();
  129. } else if (c instanceof ConstantLong) {
  130. value = ((ConstantLong) c).getValue();
  131. } else if (c instanceof ConstantString) {
  132. value = ((ConstantString)c).getString(cp);
  133. } else {
  134. value = ((ConstantObject) c).getConstantValue(cp);
  135. }
  136. }
  137. public void setValue(String constantString) {
  138. value = constantString;
  139. }
  140. public void wipeValue() {
  141. value = null;
  142. }
  143. private void checkType(Type atype) {
  144. if (type == null)
  145. throw new ClassGenException("You haven't defined the type of the field yet");
  146. if (!isFinal())
  147. throw new ClassGenException("Only final fields may have an initial value!");
  148. if (!type.equals(atype))
  149. throw new ClassGenException("Types are not compatible: " + type + " vs. " + atype);
  150. }
  151. /**
  152. * Get field object after having set up all necessary values.
  153. */
  154. public Field getField() {
  155. String signature = getSignature();
  156. int nameIndex = cp.addUtf8(name);
  157. int signatureIndex = cp.addUtf8(signature);
  158. if (value != null) {
  159. checkType(type);
  160. int index = addConstant();
  161. addAttribute(new ConstantValue(cp.addUtf8("ConstantValue"), 2, index, cp));
  162. }
  163. addAnnotationsAsAttribute(cp);
  164. return new Field(modifiers, nameIndex, signatureIndex, getAttributesImmutable(), cp);
  165. }
  166. private int addConstant() {
  167. switch (type.getType()) {
  168. case Constants.T_INT:
  169. case Constants.T_CHAR:
  170. case Constants.T_BYTE:
  171. case Constants.T_BOOLEAN:
  172. case Constants.T_SHORT:
  173. return cp.addInteger(((Integer) value).intValue());
  174. case Constants.T_FLOAT:
  175. return cp.addFloat(((Float) value).floatValue());
  176. case Constants.T_DOUBLE:
  177. return cp.addDouble(((Double) value).doubleValue());
  178. case Constants.T_LONG:
  179. return cp.addLong(((Long) value).longValue());
  180. case Constants.T_REFERENCE:
  181. return cp.addString(((String) value));
  182. default:
  183. throw new RuntimeException("Oops: Unhandled : " + type.getType());
  184. }
  185. }
  186. @Override
  187. public String getSignature() {
  188. return type.getSignature();
  189. }
  190. public String getInitialValue() {
  191. return (value == null ? null : value.toString());
  192. }
  193. public void setInitialStringValue(String value) {
  194. this.value = value;
  195. }
  196. /**
  197. * Return string representation close to declaration format, `public static final short MAX = 100', e.g..
  198. */
  199. @Override
  200. public final String toString() {
  201. String access = Utility.accessToString(modifiers);
  202. access = access.equals("") ? "" : (access + " ");
  203. String signature = type.toString();
  204. String name = getName();
  205. StringBuffer buf = new StringBuffer(access).append(signature).append(" ").append(name);
  206. String value = getInitialValue();
  207. if (value != null) {
  208. buf.append(" = ").append(value);
  209. }
  210. // TODO: Add attributes and annotations to the string
  211. return buf.toString();
  212. }
  213. }