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 */ 18 package org.apache.bcel.generic; 19 20 import java.util.ArrayList; 21 import java.util.List; 22 23 import org.apache.bcel.Const; 24 import org.apache.bcel.classfile.AccessFlags; 25 import org.apache.bcel.classfile.Attribute; 26 27 /** 28 * Super class for FieldGen and MethodGen objects, since they have 29 * some methods in common! 30 * 31 * @version $Id$ 32 */ 33 public abstract class FieldGenOrMethodGen extends AccessFlags implements NamedAndTyped, Cloneable { 34 35 /** 36 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter 37 */ 38 @Deprecated 39 protected String name; 40 41 /** 42 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter 43 */ 44 @Deprecated 45 protected Type type; 46 47 /** 48 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter 49 */ 50 @Deprecated 51 protected ConstantPoolGen cp; 52 53 private final List<Attribute> attribute_vec = new ArrayList<>(); 54 55 // @since 6.0 56 private final List<AnnotationEntryGen> annotation_vec= new ArrayList<>(); 57 58 FieldGenOrMethodGen()59 protected FieldGenOrMethodGen() { 60 } 61 62 63 /** 64 * @since 6.0 65 */ FieldGenOrMethodGen(final int access_flags)66 protected FieldGenOrMethodGen(final int access_flags) { // TODO could this be package protected? 67 super(access_flags); 68 } 69 70 @Override setType( final Type type )71 public void setType( final Type type ) { // TODO could be package-protected? 72 if (type.getType() == Const.T_ADDRESS) { 73 throw new IllegalArgumentException("Type can not be " + type); 74 } 75 this.type = type; 76 } 77 78 79 @Override getType()80 public Type getType() { 81 return type; 82 } 83 84 85 /** @return name of method/field. 86 */ 87 @Override getName()88 public String getName() { 89 return name; 90 } 91 92 93 @Override setName( final String name )94 public void setName( final String name ) { // TODO could be package-protected? 95 this.name = name; 96 } 97 98 getConstantPool()99 public ConstantPoolGen getConstantPool() { 100 return cp; 101 } 102 103 setConstantPool( final ConstantPoolGen cp )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 */ addAttribute( final Attribute a )117 public void addAttribute( final Attribute a ) { 118 attribute_vec.add(a); 119 } 120 121 /** 122 * @since 6.0 123 */ addAnnotationEntry(final AnnotationEntryGen ag)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 */ removeAttribute( final Attribute a )133 public void removeAttribute( final Attribute a ) { 134 attribute_vec.remove(a); 135 } 136 137 /** 138 * @since 6.0 139 */ removeAnnotationEntry(final AnnotationEntryGen ag)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 */ removeAttributes()149 public void removeAttributes() { 150 attribute_vec.clear(); 151 } 152 153 /** 154 * @since 6.0 155 */ removeAnnotationEntries()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 */ getAttributes()165 public Attribute[] getAttributes() { 166 final Attribute[] attributes = new Attribute[attribute_vec.size()]; 167 attribute_vec.toArray(attributes); 168 return attributes; 169 } 170 getAnnotationEntries()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 */ getSignature()180 public abstract String getSignature(); 181 182 183 @Override clone()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 } 192