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 * Super class 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 * @return name of method/field.
142 */
143 @Override
144 public String getName() {
145 return name;
146 }
147
148 /**
149 * Gets the signature of method or field.
150 *
151 * @return signature of method/field.
152 */
153 public abstract String getSignature();
154
155 @Override
156 public Type getType() {
157 return type;
158 }
159
160 /**
161 * Removes all annotation entries.
162 *
163 * @since 6.0
164 */
165 public void removeAnnotationEntries() {
166 annotationList.clear();
167 }
168
169 /**
170 * Removes an annotation entry.
171 *
172 * @param ag the annotation entry to remove.
173 * @since 6.0
174 */
175 public void removeAnnotationEntry(final AnnotationEntryGen ag) {
176 annotationList.remove(ag);
177 }
178
179 /**
180 * Removes an attribute.
181 *
182 * @param a the attribute to remove.
183 */
184 public void removeAttribute(final Attribute a) {
185 attributeList.remove(a);
186 }
187
188 /**
189 * Removes all attributes.
190 */
191 public void removeAttributes() {
192 attributeList.clear();
193 }
194
195 /**
196 * Sets the constant pool.
197 *
198 * @param cp the constant pool.
199 */
200 public void setConstantPool(final ConstantPoolGen cp) { // TODO could be package-protected?
201 this.cp = cp;
202 }
203
204 @Override
205 public void setName(final String name) { // TODO could be package-protected?
206 this.name = name;
207 }
208
209 @Override
210 public void setType(final Type type) { // TODO could be package-protected?
211 if (type.getType() == Const.T_ADDRESS) {
212 throw new IllegalArgumentException("Type cannot be " + type);
213 }
214 this.type = type;
215 }
216 }