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 */ 017package org.apache.bcel.generic; 018 019import java.util.ArrayList; 020import java.util.Collections; 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 some methods in common! 029 */ 030public abstract class FieldGenOrMethodGen extends AccessFlags implements NamedAndTyped, Cloneable { 031 032 /** 033 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter 034 */ 035 @Deprecated 036 protected String name; 037 038 /** 039 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter 040 */ 041 @Deprecated 042 protected Type type; 043 044 /** 045 * @deprecated (since 6.0) will be made private; do not access directly, use getter/setter 046 */ 047 @Deprecated 048 protected ConstantPoolGen cp; 049 050 private final List<Attribute> attributeList = new ArrayList<>(); 051 052 // @since 6.0 053 private final List<AnnotationEntryGen> annotationList = new ArrayList<>(); 054 055 protected FieldGenOrMethodGen() { 056 } 057 058 /** 059 * @since 6.0 060 */ 061 protected FieldGenOrMethodGen(final int accessFlags) { // TODO could this be package protected? 062 super(accessFlags); 063 } 064 065 protected void addAll(final Attribute[] attributes) { 066 if (attributes != null) { 067 Collections.addAll(attributeList, attributes); 068 } 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 UnsupportedOperationException("Clone Not Supported", e); // 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}