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 */
032public abstract class FieldGenOrMethodGen extends AccessFlags implements NamedAndTyped, Cloneable {
033
034    /**
035     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
036     */
037    @Deprecated
038    protected String name;
039
040    /**
041     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
042     */
043    @Deprecated
044    protected Type type;
045
046    /**
047     * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
048     */
049    @Deprecated
050    protected ConstantPoolGen cp;
051
052    private final List<Attribute> attribute_vec = new ArrayList<>();
053
054    // @since 6.0
055    private final List<AnnotationEntryGen>       annotation_vec= new ArrayList<>();
056
057
058    protected FieldGenOrMethodGen() {
059    }
060
061
062    /**
063     * @since 6.0
064     */
065    protected FieldGenOrMethodGen(final int access_flags) { // TODO could this be package protected?
066        super(access_flags);
067    }
068
069    @Override
070    public void setType( final Type type ) { // TODO could be package-protected?
071        if (type.getType() == Const.T_ADDRESS) {
072            throw new IllegalArgumentException("Type can not be " + type);
073        }
074        this.type = type;
075    }
076
077
078    @Override
079    public Type getType() {
080        return type;
081    }
082
083
084    /** @return name of method/field.
085     */
086    @Override
087    public String getName() {
088        return name;
089    }
090
091
092    @Override
093    public void setName( final String name ) { // TODO could be package-protected?
094        this.name = name;
095    }
096
097
098    public ConstantPoolGen getConstantPool() {
099        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        attribute_vec.add(a);
118    }
119
120    /**
121     * @since 6.0
122     */
123    protected void addAnnotationEntry(final AnnotationEntryGen ag) // TODO could this be package protected?
124    {
125        annotation_vec.add(ag);
126    }
127
128
129    /**
130     * Remove an attribute.
131     */
132    public void removeAttribute( final Attribute a ) {
133        attribute_vec.remove(a);
134    }
135
136    /**
137     * @since 6.0
138     */
139    protected void removeAnnotationEntry(final AnnotationEntryGen ag) // TODO could this be package protected?
140    {
141        annotation_vec.remove(ag);
142    }
143
144
145    /**
146     * Remove all attributes.
147     */
148    public void removeAttributes() {
149        attribute_vec.clear();
150    }
151
152    /**
153     * @since 6.0
154     */
155    protected void removeAnnotationEntries() // TODO could this be package protected?
156    {
157        annotation_vec.clear();
158    }
159
160
161    /**
162     * @return all attributes of this method.
163     */
164    public Attribute[] getAttributes() {
165        final Attribute[] attributes = new Attribute[attribute_vec.size()];
166        attribute_vec.toArray(attributes);
167        return attributes;
168    }
169
170    public AnnotationEntryGen[] getAnnotationEntries() {
171        final AnnotationEntryGen[] annotations = new AnnotationEntryGen[annotation_vec.size()];
172          annotation_vec.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}