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 */
018package org.apache.bcel.generic;
019
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.bcel.Const;
024import org.apache.bcel.classfile.AccessFlags;
025import org.apache.bcel.classfile.Attribute;
026
027/**
028 * Super class for FieldGen and MethodGen objects, since they have
029 * some methods in common!
030 *
031 * @version $Id: FieldGenOrMethodGen.java 1749603 2016-06-21 20:50:19Z ggregory $
032 */
033public abstract class FieldGenOrMethodGen extends AccessFlags implements NamedAndTyped, Cloneable {
034
035    /**
036     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
037     */
038    @Deprecated
039    protected String name;
040
041    /**
042     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
043     */
044    @Deprecated
045    protected Type type;
046
047    /**
048     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
049     */
050    @Deprecated
051    protected ConstantPoolGen cp;
052
053    private final List<Attribute> attribute_vec = new ArrayList<>();
054
055    // @since 6.0
056    private final List<AnnotationEntryGen>       annotation_vec= new ArrayList<>();
057
058
059    protected FieldGenOrMethodGen() {
060    }
061
062
063    /**
064     * @since 6.0
065     */
066    protected FieldGenOrMethodGen(final int access_flags) { // TODO could this be package protected?
067        super(access_flags);
068    }
069
070    @Override
071    public void setType( final Type type ) { // TODO could be package-protected?
072        if (type.getType() == Const.T_ADDRESS) {
073            throw new IllegalArgumentException("Type can not be " + type);
074        }
075        this.type = type;
076    }
077
078
079    @Override
080    public Type getType() {
081        return type;
082    }
083
084
085    /** @return name of method/field.
086     */
087    @Override
088    public String getName() {
089        return name;
090    }
091
092
093    @Override
094    public void setName( final String name ) { // TODO could be package-protected?
095        this.name = name;
096    }
097
098
099    public ConstantPoolGen getConstantPool() {
100        return cp;
101    }
102
103
104    public void setConstantPool( final ConstantPoolGen cp ) { // TODO could be package-protected?
105        this.cp = cp;
106    }
107
108
109    /**
110     * Add an attribute to this method. Currently, the JVM knows about
111     * the `Code', `ConstantValue', `Synthetic' and `Exceptions'
112     * attributes. Other attributes will be ignored by the JVM but do no
113     * harm.
114     *
115     * @param a attribute to be added
116     */
117    public void addAttribute( final Attribute a ) {
118        attribute_vec.add(a);
119    }
120
121    /**
122     * @since 6.0
123     */
124    protected void addAnnotationEntry(final AnnotationEntryGen ag) // TODO could this be package protected?
125    {
126        annotation_vec.add(ag);
127    }
128
129
130    /**
131     * Remove an attribute.
132     */
133    public void removeAttribute( final Attribute a ) {
134        attribute_vec.remove(a);
135    }
136
137    /**
138     * @since 6.0
139     */
140    protected void removeAnnotationEntry(final AnnotationEntryGen ag) // TODO could this be package protected?
141    {
142        annotation_vec.remove(ag);
143    }
144
145
146    /**
147     * Remove all attributes.
148     */
149    public void removeAttributes() {
150        attribute_vec.clear();
151    }
152
153    /**
154     * @since 6.0
155     */
156    protected void removeAnnotationEntries() // TODO could this be package protected?
157    {
158        annotation_vec.clear();
159    }
160
161
162    /**
163     * @return all attributes of this method.
164     */
165    public Attribute[] getAttributes() {
166        final Attribute[] attributes = new Attribute[attribute_vec.size()];
167        attribute_vec.toArray(attributes);
168        return attributes;
169    }
170
171    public AnnotationEntryGen[] getAnnotationEntries() {
172        final AnnotationEntryGen[] annotations = new AnnotationEntryGen[annotation_vec.size()];
173          annotation_vec.toArray(annotations);
174          return annotations;
175      }
176
177
178    /** @return signature of method/field.
179     */
180    public abstract String getSignature();
181
182
183    @Override
184    public Object clone() {
185        try {
186            return super.clone();
187        } catch (final CloneNotSupportedException e) {
188            throw new Error("Clone Not Supported"); // never happens
189        }
190    }
191}