View Javadoc
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      protected FieldGenOrMethodGen() {
58      }
59  
60      /**
61       * @since 6.0
62       */
63      protected FieldGenOrMethodGen(final int accessFlags) { // TODO could this be package protected?
64          super(accessFlags);
65      }
66  
67      protected void addAll(final Attribute[] attributes) {
68          if (attributes != null) {
69              Collections.addAll(attributeList, attributes);
70          }
71      }
72  
73      /**
74       * @since 6.0
75       */
76      public void addAnnotationEntry(final AnnotationEntryGen ag) {
77          annotationList.add(ag);
78      }
79  
80      /**
81       * Add an attribute to this method. Currently, the JVM knows about the 'Code', 'ConstantValue', 'Synthetic' and
82       * 'Exceptions' attributes. Other attributes will be ignored by the JVM but do no harm.
83       *
84       * @param a attribute to be added
85       */
86      public void addAttribute(final Attribute a) {
87          attributeList.add(a);
88      }
89  
90      @Override
91      public Object clone() {
92          try {
93              return super.clone();
94          } catch (final CloneNotSupportedException e) {
95              throw new UnsupportedOperationException("Clone Not Supported", e); // never happens
96          }
97      }
98  
99      public AnnotationEntryGen[] getAnnotationEntries() {
100         return annotationList.toArray(AnnotationEntryGen.EMPTY_ARRAY);
101     }
102 
103     /**
104      * @return all attributes of this method.
105      */
106     public Attribute[] getAttributes() {
107         return attributeList.toArray(Attribute.EMPTY_ARRAY);
108     }
109 
110     public ConstantPoolGen getConstantPool() {
111         return cp;
112     }
113 
114     /**
115      * @return name of method/field.
116      */
117     @Override
118     public String getName() {
119         return name;
120     }
121 
122     /**
123      * @return signature of method/field.
124      */
125     public abstract String getSignature();
126 
127     @Override
128     public Type getType() {
129         return type;
130     }
131 
132     /**
133      * @since 6.0
134      */
135     public void removeAnnotationEntries() {
136         annotationList.clear();
137     }
138 
139     /**
140      * @since 6.0
141      */
142     public void removeAnnotationEntry(final AnnotationEntryGen ag) {
143         annotationList.remove(ag);
144     }
145 
146     /**
147      * Remove an attribute.
148      */
149     public void removeAttribute(final Attribute a) {
150         attributeList.remove(a);
151     }
152 
153     /**
154      * Remove all attributes.
155      */
156     public void removeAttributes() {
157         attributeList.clear();
158     }
159 
160     public void setConstantPool(final ConstantPoolGen cp) { // TODO could be package-protected?
161         this.cp = cp;
162     }
163 
164     @Override
165     public void setName(final String name) { // TODO could be package-protected?
166         this.name = name;
167     }
168 
169     @Override
170     public void setType(final Type type) { // TODO could be package-protected?
171         if (type.getType() == Const.T_ADDRESS) {
172             throw new IllegalArgumentException("Type cannot be " + type);
173         }
174         this.type = type;
175     }
176 }