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  import org.apache.bcel.Constants;
23  import org.apache.bcel.classfile.AccessFlags;
24  import org.apache.bcel.classfile.Attribute;
25  
26  /**
27   * Super class for FieldGen and MethodGen objects, since they have
28   * some methods in common!
29   *
30   * @version $Id: FieldGenOrMethodGen.java 1152072 2011-07-29 01:54:05Z dbrosius $
31   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
32   */
33  public abstract class FieldGenOrMethodGen extends AccessFlags implements NamedAndTyped, Cloneable {
34  
35      private static final long serialVersionUID = -2549303846821589647L;
36      protected String name;
37      protected Type type;
38      protected ConstantPoolGen cp;
39      private List<Attribute> attribute_vec = new ArrayList<Attribute>();
40      protected List<AnnotationEntryGen>       annotation_vec= new ArrayList<AnnotationEntryGen>();
41  
42  
43      protected FieldGenOrMethodGen() {
44      }
45  
46  
47      public void setType( Type type ) {
48          if (type.getType() == Constants.T_ADDRESS) {
49              throw new IllegalArgumentException("Type can not be " + type);
50          }
51          this.type = type;
52      }
53  
54  
55      public Type getType() {
56          return type;
57      }
58  
59  
60      /** @return name of method/field.
61       */
62      public String getName() {
63          return name;
64      }
65  
66  
67      public void setName( String name ) {
68          this.name = name;
69      }
70  
71  
72      public ConstantPoolGen getConstantPool() {
73          return cp;
74      }
75  
76  
77      public void setConstantPool( ConstantPoolGen cp ) {
78          this.cp = cp;
79      }
80  
81  
82      /**
83       * Add an attribute to this method. Currently, the JVM knows about
84       * the `Code', `ConstantValue', `Synthetic' and `Exceptions'
85       * attributes. Other attributes will be ignored by the JVM but do no
86       * harm.
87       *
88       * @param a attribute to be added
89       */
90      public void addAttribute( Attribute a ) {
91          attribute_vec.add(a);
92      }
93      
94      public void addAnnotationEntry(AnnotationEntryGen ag)
95  	{
96  		annotation_vec.add(ag);
97  	}
98  
99  
100     /**
101      * Remove an attribute.
102      */
103     public void removeAttribute( Attribute a ) {
104         attribute_vec.remove(a);
105     }
106     
107     public void removeAnnotationEntry(AnnotationEntryGen ag)
108 	{
109 		annotation_vec.remove(ag);
110 	}
111 
112 
113     /**
114      * Remove all attributes.
115      */
116     public void removeAttributes() {
117         attribute_vec.clear();
118     }
119     
120     public void removeAnnotationEntries()
121 	{
122 		annotation_vec.clear();
123 	}
124 
125 
126     /**
127      * @return all attributes of this method.
128      */
129     public Attribute[] getAttributes() {
130         Attribute[] attributes = new Attribute[attribute_vec.size()];
131         attribute_vec.toArray(attributes);
132         return attributes;
133     }
134     
135     public AnnotationEntryGen[] getAnnotationEntries() {
136     	AnnotationEntryGen[] annotations = new AnnotationEntryGen[annotation_vec.size()];
137       	annotation_vec.toArray(annotations);
138       	return annotations;
139       }
140 
141 
142     /** @return signature of method/field.
143      */
144     public abstract String getSignature();
145 
146 
147     @Override
148     public Object clone() {
149         try {
150             return super.clone();
151         } catch (CloneNotSupportedException e) {
152             System.err.println(e);
153             return null;
154         }
155     }
156 }