001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License. 
016     *
017     */
018    package org.apache.bcel.generic;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    import org.apache.bcel.Constants;
023    import org.apache.bcel.classfile.AccessFlags;
024    import org.apache.bcel.classfile.Attribute;
025    
026    /**
027     * Super class for FieldGen and MethodGen objects, since they have
028     * some methods in common!
029     *
030     * @version $Id: FieldGenOrMethodGen.java 1152072 2011-07-29 01:54:05Z dbrosius $
031     * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
032     */
033    public abstract class FieldGenOrMethodGen extends AccessFlags implements NamedAndTyped, Cloneable {
034    
035        private static final long serialVersionUID = -2549303846821589647L;
036        protected String name;
037        protected Type type;
038        protected ConstantPoolGen cp;
039        private List<Attribute> attribute_vec = new ArrayList<Attribute>();
040        protected List<AnnotationEntryGen>       annotation_vec= new ArrayList<AnnotationEntryGen>();
041    
042    
043        protected FieldGenOrMethodGen() {
044        }
045    
046    
047        public void setType( Type type ) {
048            if (type.getType() == Constants.T_ADDRESS) {
049                throw new IllegalArgumentException("Type can not be " + type);
050            }
051            this.type = type;
052        }
053    
054    
055        public Type getType() {
056            return type;
057        }
058    
059    
060        /** @return name of method/field.
061         */
062        public String getName() {
063            return name;
064        }
065    
066    
067        public void setName( String name ) {
068            this.name = name;
069        }
070    
071    
072        public ConstantPoolGen getConstantPool() {
073            return cp;
074        }
075    
076    
077        public void setConstantPool( ConstantPoolGen cp ) {
078            this.cp = cp;
079        }
080    
081    
082        /**
083         * Add an attribute to this method. Currently, the JVM knows about
084         * the `Code', `ConstantValue', `Synthetic' and `Exceptions'
085         * attributes. Other attributes will be ignored by the JVM but do no
086         * harm.
087         *
088         * @param a attribute to be added
089         */
090        public void addAttribute( Attribute a ) {
091            attribute_vec.add(a);
092        }
093        
094        public void addAnnotationEntry(AnnotationEntryGen ag)
095            {
096                    annotation_vec.add(ag);
097            }
098    
099    
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    }