View Javadoc

1   /*
2    * Copyright  2000-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License"); 
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License. 
15   *
16   */
17  package org.apache.bcel.generic;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import org.apache.bcel.Constants;
22  import org.apache.bcel.classfile.AccessFlags;
23  import org.apache.bcel.classfile.Attribute;
24  
25  /***
26   * Super class for FieldGen and MethodGen objects, since they have
27   * some methods in common!
28   *
29   * @version $Id: FieldGenOrMethodGen.java 410087 2006-05-29 12:12:19Z tcurdt $
30   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
31   */
32  public abstract class FieldGenOrMethodGen extends AccessFlags implements NamedAndTyped, Cloneable {
33  
34      protected String name;
35      protected Type type;
36      protected ConstantPoolGen cp;
37      private List attribute_vec = new ArrayList();
38  
39  
40      protected FieldGenOrMethodGen() {
41      }
42  
43  
44      public void setType( Type type ) {
45          if (type.getType() == Constants.T_ADDRESS) {
46              throw new IllegalArgumentException("Type can not be " + type);
47          }
48          this.type = type;
49      }
50  
51  
52      public Type getType() {
53          return type;
54      }
55  
56  
57      /*** @return name of method/field.
58       */
59      public String getName() {
60          return name;
61      }
62  
63  
64      public void setName( String name ) {
65          this.name = name;
66      }
67  
68  
69      public ConstantPoolGen getConstantPool() {
70          return cp;
71      }
72  
73  
74      public void setConstantPool( ConstantPoolGen cp ) {
75          this.cp = cp;
76      }
77  
78  
79      /***
80       * Add an attribute to this method. Currently, the JVM knows about
81       * the `Code', `ConstantValue', `Synthetic' and `Exceptions'
82       * attributes. Other attributes will be ignored by the JVM but do no
83       * harm.
84       *
85       * @param a attribute to be added
86       */
87      public void addAttribute( Attribute a ) {
88          attribute_vec.add(a);
89      }
90  
91  
92      /***
93       * Remove an attribute.
94       */
95      public void removeAttribute( Attribute a ) {
96          attribute_vec.remove(a);
97      }
98  
99  
100     /***
101      * Remove all attributes.
102      */
103     public void removeAttributes() {
104         attribute_vec.clear();
105     }
106 
107 
108     /***
109      * @return all attributes of this method.
110      */
111     public Attribute[] getAttributes() {
112         Attribute[] attributes = new Attribute[attribute_vec.size()];
113         attribute_vec.toArray(attributes);
114         return attributes;
115     }
116 
117 
118     /*** @return signature of method/field.
119      */
120     public abstract String getSignature();
121 
122 
123     public Object clone() {
124         try {
125             return super.clone();
126         } catch (CloneNotSupportedException e) {
127             System.err.println(e);
128             return null;
129         }
130     }
131 }