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