FieldGenOrMethodGen.java

  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. package org.apache.bcel.generic;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import org.apache.bcel.Const;
  22. import org.apache.bcel.classfile.AccessFlags;
  23. import org.apache.bcel.classfile.Attribute;

  24. /**
  25.  * Super class for FieldGen and MethodGen objects, since they have some methods in common!
  26.  */
  27. public abstract class FieldGenOrMethodGen extends AccessFlags implements NamedAndTyped, Cloneable {

  28.     /**
  29.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  30.      */
  31.     @Deprecated
  32.     protected String name;

  33.     /**
  34.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  35.      */
  36.     @Deprecated
  37.     protected Type type;

  38.     /**
  39.      * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter
  40.      */
  41.     @Deprecated
  42.     protected ConstantPoolGen cp;

  43.     private final List<Attribute> attributeList = new ArrayList<>();

  44.     // @since 6.0
  45.     private final List<AnnotationEntryGen> annotationList = new ArrayList<>();

  46.     protected FieldGenOrMethodGen() {
  47.     }

  48.     /**
  49.      * @since 6.0
  50.      */
  51.     protected FieldGenOrMethodGen(final int accessFlags) { // TODO could this be package protected?
  52.         super(accessFlags);
  53.     }

  54.     protected void addAll(final Attribute[] attributes) {
  55.         if (attributes != null) {
  56.             Collections.addAll(attributeList, attributes);
  57.         }
  58.     }

  59.     /**
  60.      * @since 6.0
  61.      */
  62.     public void addAnnotationEntry(final AnnotationEntryGen ag) {
  63.         annotationList.add(ag);
  64.     }

  65.     /**
  66.      * Add an attribute to this method. Currently, the JVM knows about the 'Code', 'ConstantValue', 'Synthetic' and
  67.      * 'Exceptions' attributes. Other attributes will be ignored by the JVM but do no harm.
  68.      *
  69.      * @param a attribute to be added
  70.      */
  71.     public void addAttribute(final Attribute a) {
  72.         attributeList.add(a);
  73.     }

  74.     @Override
  75.     public Object clone() {
  76.         try {
  77.             return super.clone();
  78.         } catch (final CloneNotSupportedException e) {
  79.             throw new UnsupportedOperationException("Clone Not Supported", e); // never happens
  80.         }
  81.     }

  82.     public AnnotationEntryGen[] getAnnotationEntries() {
  83.         return annotationList.toArray(AnnotationEntryGen.EMPTY_ARRAY);
  84.     }

  85.     /**
  86.      * @return all attributes of this method.
  87.      */
  88.     public Attribute[] getAttributes() {
  89.         return attributeList.toArray(Attribute.EMPTY_ARRAY);
  90.     }

  91.     public ConstantPoolGen getConstantPool() {
  92.         return cp;
  93.     }

  94.     /**
  95.      * @return name of method/field.
  96.      */
  97.     @Override
  98.     public String getName() {
  99.         return name;
  100.     }

  101.     /**
  102.      * @return signature of method/field.
  103.      */
  104.     public abstract String getSignature();

  105.     @Override
  106.     public Type getType() {
  107.         return type;
  108.     }

  109.     /**
  110.      * @since 6.0
  111.      */
  112.     public void removeAnnotationEntries() {
  113.         annotationList.clear();
  114.     }

  115.     /**
  116.      * @since 6.0
  117.      */
  118.     public void removeAnnotationEntry(final AnnotationEntryGen ag) {
  119.         annotationList.remove(ag);
  120.     }

  121.     /**
  122.      * Remove an attribute.
  123.      */
  124.     public void removeAttribute(final Attribute a) {
  125.         attributeList.remove(a);
  126.     }

  127.     /**
  128.      * Remove all attributes.
  129.      */
  130.     public void removeAttributes() {
  131.         attributeList.clear();
  132.     }

  133.     public void setConstantPool(final ConstantPoolGen cp) { // TODO could be package-protected?
  134.         this.cp = cp;
  135.     }

  136.     @Override
  137.     public void setName(final String name) { // TODO could be package-protected?
  138.         this.name = name;
  139.     }

  140.     @Override
  141.     public void setType(final Type type) { // TODO could be package-protected?
  142.         if (type.getType() == Const.T_ADDRESS) {
  143.             throw new IllegalArgumentException("Type can not be " + type);
  144.         }
  145.         this.type = type;
  146.     }
  147. }