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.Collections;
022import java.util.List;
023
024import org.apache.bcel.Const;
025import org.apache.bcel.classfile.AccessFlags;
026import org.apache.bcel.classfile.Attribute;
027
028/**
029 * Super class for FieldGen and MethodGen objects, since they have 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> attributeList = new ArrayList<>();
053
054    // @since 6.0
055    private final List<AnnotationEntryGen> annotationList = new ArrayList<>();
056
057    protected FieldGenOrMethodGen() {
058    }
059
060    /**
061     * @since 6.0
062     */
063    protected FieldGenOrMethodGen(final int access_flags) { // TODO could this be package protected?
064        super(access_flags);
065    }
066
067    protected void addAll(final Attribute[] attrs) {
068        Collections.addAll(attributeList, attrs);
069    }
070
071    /**
072     * @since 6.0
073     */
074    public void addAnnotationEntry(final AnnotationEntryGen ag) {
075        annotationList.add(ag);
076    }
077
078    /**
079     * Add an attribute to this method. Currently, the JVM knows about the `Code', `ConstantValue', `Synthetic' and
080     * `Exceptions' attributes. Other attributes will be ignored by the JVM but do no harm.
081     *
082     * @param a attribute to be added
083     */
084    public void addAttribute(final Attribute a) {
085        attributeList.add(a);
086    }
087
088    @Override
089    public Object clone() {
090        try {
091            return super.clone();
092        } catch (final CloneNotSupportedException e) {
093            throw new Error("Clone Not Supported"); // never happens
094        }
095    }
096
097    public AnnotationEntryGen[] getAnnotationEntries() {
098        return annotationList.toArray(AnnotationEntryGen.EMPTY_ARRAY);
099    }
100
101    /**
102     * @return all attributes of this method.
103     */
104    public Attribute[] getAttributes() {
105        return attributeList.toArray(Attribute.EMPTY_ARRAY);
106    }
107
108    public ConstantPoolGen getConstantPool() {
109        return cp;
110    }
111
112    /**
113     * @return name of method/field.
114     */
115    @Override
116    public String getName() {
117        return name;
118    }
119
120    /**
121     * @return signature of method/field.
122     */
123    public abstract String getSignature();
124
125    @Override
126    public Type getType() {
127        return type;
128    }
129
130    /**
131     * @since 6.0
132     */
133    public void removeAnnotationEntries() {
134        annotationList.clear();
135    }
136
137    /**
138     * @since 6.0
139     */
140    public void removeAnnotationEntry(final AnnotationEntryGen ag) {
141        annotationList.remove(ag);
142    }
143
144    /**
145     * Remove an attribute.
146     */
147    public void removeAttribute(final Attribute a) {
148        attributeList.remove(a);
149    }
150
151    /**
152     * Remove all attributes.
153     */
154    public void removeAttributes() {
155        attributeList.clear();
156    }
157
158    public void setConstantPool(final ConstantPoolGen cp) { // TODO could be package-protected?
159        this.cp = cp;
160    }
161
162    @Override
163    public void setName(final String name) { // TODO could be package-protected?
164        this.name = name;
165    }
166
167    @Override
168    public void setType(final Type type) { // TODO could be package-protected?
169        if (type.getType() == Const.T_ADDRESS) {
170            throw new IllegalArgumentException("Type can not be " + type);
171        }
172        this.type = type;
173    }
174}