1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.bcel.generic;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.apache.bcel.Const;
26 import org.apache.bcel.classfile.AccessFlags;
27 import org.apache.bcel.classfile.Attribute;
28
29 /**
30 * Superclass for FieldGen and MethodGen objects, since they have some methods in common!
31 */
32 public abstract class FieldGenOrMethodGen extends AccessFlags implements NamedAndTyped, Cloneable {
33
34 /**
35 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
36 */
37 @Deprecated
38 protected String name;
39
40 /**
41 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
42 */
43 @Deprecated
44 protected Type type;
45
46 /**
47 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
48 */
49 @Deprecated
50 protected ConstantPoolGen cp;
51
52 private final List<Attribute> attributeList = new ArrayList<>();
53
54 // @since 6.0
55 private final List<AnnotationEntryGen> annotationList = new ArrayList<>();
56
57 /**
58 * Constructs a FieldGenOrMethodGen.
59 */
60 protected FieldGenOrMethodGen() {
61 }
62
63 /**
64 * Constructs a FieldGenOrMethodGen.
65 *
66 * @param accessFlags The access flags.
67 * @since 6.0
68 */
69 protected FieldGenOrMethodGen(final int accessFlags) { // TODO could this be package protected?
70 super(accessFlags);
71 }
72
73 /**
74 * Adds all attributes from an array.
75 *
76 * @param attributes The attributes to add.
77 */
78 protected void addAll(final Attribute[] attributes) {
79 if (attributes != null) {
80 Collections.addAll(attributeList, attributes);
81 }
82 }
83
84 /**
85 * Adds an annotation entry.
86 *
87 * @param ag The annotation entry.
88 * @since 6.0
89 */
90 public void addAnnotationEntry(final AnnotationEntryGen ag) {
91 annotationList.add(ag);
92 }
93
94 /**
95 * Add an attribute to this method. Currently, the JVM knows about the 'Code', 'ConstantValue', 'Synthetic' and
96 * 'Exceptions' attributes. Other attributes will be ignored by the JVM but do no harm.
97 *
98 * @param a attribute to be added.
99 */
100 public void addAttribute(final Attribute a) {
101 attributeList.add(a);
102 }
103
104 @Override
105 public Object clone() {
106 try {
107 return super.clone();
108 } catch (final CloneNotSupportedException e) {
109 throw new UnsupportedOperationException("Clone Not Supported", e); // never happens
110 }
111 }
112
113 /**
114 * Gets all annotation entries.
115 *
116 * @return all annotation entries.
117 */
118 public AnnotationEntryGen[] getAnnotationEntries() {
119 return annotationList.toArray(AnnotationEntryGen.EMPTY_ARRAY);
120 }
121
122 /**
123 * Gets all attributes of this method or field.
124 *
125 * @return all attributes of this method.
126 */
127 public Attribute[] getAttributes() {
128 return attributeList.toArray(Attribute.EMPTY_ARRAY);
129 }
130
131 /**
132 * Gets the constant pool.
133 *
134 * @return The constant pool.
135 */
136 public ConstantPoolGen getConstantPool() {
137 return cp;
138 }
139
140 /**
141 * Gets the name of the method or field.
142 *
143 * @return name of method/field.
144 */
145 @Override
146 public String getName() {
147 return name;
148 }
149
150 /**
151 * Gets the signature of method or field.
152 *
153 * @return signature of method/field.
154 */
155 public abstract String getSignature();
156
157 @Override
158 public Type getType() {
159 return type;
160 }
161
162 /**
163 * Removes all annotation entries.
164 *
165 * @since 6.0
166 */
167 public void removeAnnotationEntries() {
168 annotationList.clear();
169 }
170
171 /**
172 * Removes an annotation entry.
173 *
174 * @param ag The annotation entry to remove.
175 * @since 6.0
176 */
177 public void removeAnnotationEntry(final AnnotationEntryGen ag) {
178 annotationList.remove(ag);
179 }
180
181 /**
182 * Removes an attribute.
183 *
184 * @param a The attribute to remove.
185 */
186 public void removeAttribute(final Attribute a) {
187 attributeList.remove(a);
188 }
189
190 /**
191 * Removes all attributes.
192 */
193 public void removeAttributes() {
194 attributeList.clear();
195 }
196
197 /**
198 * Sets the constant pool.
199 *
200 * @param cp The constant pool.
201 */
202 public void setConstantPool(final ConstantPoolGen cp) { // TODO could be package-protected?
203 this.cp = cp;
204 }
205
206 @Override
207 public void setName(final String name) { // TODO could be package-protected?
208 this.name = name;
209 }
210
211 @Override
212 public void setType(final Type type) { // TODO could be package-protected?
213 if (type.getType() == Const.T_ADDRESS) {
214 throw new IllegalArgumentException("Type cannot be " + type);
215 }
216 this.type = type;
217 }
218 }