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