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