1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.generic;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import org.apache.bcel.Constants;
22 import org.apache.bcel.classfile.AccessFlags;
23 import org.apache.bcel.classfile.Attribute;
24
25 /***
26 * Super class for FieldGen and MethodGen objects, since they have
27 * some methods in common!
28 *
29 * @version $Id: FieldGenOrMethodGen.java 410087 2006-05-29 12:12:19Z tcurdt $
30 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
31 */
32 public abstract class FieldGenOrMethodGen extends AccessFlags implements NamedAndTyped, Cloneable {
33
34 protected String name;
35 protected Type type;
36 protected ConstantPoolGen cp;
37 private List attribute_vec = new ArrayList();
38
39
40 protected FieldGenOrMethodGen() {
41 }
42
43
44 public void setType( Type type ) {
45 if (type.getType() == Constants.T_ADDRESS) {
46 throw new IllegalArgumentException("Type can not be " + type);
47 }
48 this.type = type;
49 }
50
51
52 public Type getType() {
53 return type;
54 }
55
56
57 /*** @return name of method/field.
58 */
59 public String getName() {
60 return name;
61 }
62
63
64 public void setName( String name ) {
65 this.name = name;
66 }
67
68
69 public ConstantPoolGen getConstantPool() {
70 return cp;
71 }
72
73
74 public void setConstantPool( ConstantPoolGen cp ) {
75 this.cp = cp;
76 }
77
78
79 /***
80 * Add an attribute to this method. Currently, the JVM knows about
81 * the `Code', `ConstantValue', `Synthetic' and `Exceptions'
82 * attributes. Other attributes will be ignored by the JVM but do no
83 * harm.
84 *
85 * @param a attribute to be added
86 */
87 public void addAttribute( Attribute a ) {
88 attribute_vec.add(a);
89 }
90
91
92 /***
93 * Remove an attribute.
94 */
95 public void removeAttribute( Attribute a ) {
96 attribute_vec.remove(a);
97 }
98
99
100 /***
101 * Remove all attributes.
102 */
103 public void removeAttributes() {
104 attribute_vec.clear();
105 }
106
107
108 /***
109 * @return all attributes of this method.
110 */
111 public Attribute[] getAttributes() {
112 Attribute[] attributes = new Attribute[attribute_vec.size()];
113 attribute_vec.toArray(attributes);
114 return attributes;
115 }
116
117
118 /*** @return signature of method/field.
119 */
120 public abstract String getSignature();
121
122
123 public Object clone() {
124 try {
125 return super.clone();
126 } catch (CloneNotSupportedException e) {
127 System.err.println(e);
128 return null;
129 }
130 }
131 }