< prev index next >

src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/Method.java

Print this page




  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 package com.sun.org.apache.bcel.internal.classfile;
  22 
  23 import java.io.DataInput;
  24 import java.io.IOException;
  25 import java.util.Objects;
  26 
  27 import com.sun.org.apache.bcel.internal.Const;
  28 import com.sun.org.apache.bcel.internal.generic.Type;
  29 import com.sun.org.apache.bcel.internal.util.BCELComparator;
  30 
  31 /**
  32  * This class represents the method info structure, i.e., the representation
  33  * for a method in the class. See JVM specification for details.
  34  * A method has access flags, a name, a signature and a number of attributes.
  35  *
  36  * @version $Id$
  37  */
  38 public final class Method extends FieldOrMethod {
  39 
  40     private static BCELComparator bcelComparator = new BCELComparator() {
  41 
  42         @Override
  43         public boolean equals( final Object o1, final Object o2 ) {
  44             final Method THIS = (Method) o1;
  45             final Method THAT = (Method) o2;
  46             return Objects.equals(THIS.getName(), THAT.getName())
  47                     && Objects.equals(THIS.getSignature(), THAT.getSignature());
  48         }
  49 
  50 
  51         @Override
  52         public int hashCode( final Object o ) {
  53             final Method THIS = (Method) o;
  54             return THIS.getSignature().hashCode() ^ THIS.getName().hashCode();
  55         }
  56     };


  99         super(access_flags, name_index, signature_index, attributes, constant_pool);
 100     }
 101 
 102 
 103     /**
 104      * Called by objects that are traversing the nodes of the tree implicitely
 105      * defined by the contents of a Java class. I.e., the hierarchy of methods,
 106      * fields, attributes, etc. spawns a tree of objects.
 107      *
 108      * @param v Visitor object
 109      */
 110     @Override
 111     public void accept( final Visitor v ) {
 112         v.visitMethod(this);
 113     }
 114 
 115 
 116     /**
 117      * @return Code attribute of method, if any
 118      */
 119     public final Code getCode() {
 120         for (final Attribute attribute : super.getAttributes()) {
 121             if (attribute instanceof Code) {
 122                 return (Code) attribute;
 123             }
 124         }
 125         return null;
 126     }
 127 
 128 
 129     /**
 130      * @return ExceptionTable attribute of method, if any, i.e., list all
 131      * exceptions the method may throw not exception handlers!
 132      */
 133     public final ExceptionTable getExceptionTable() {
 134         for (final Attribute attribute : super.getAttributes()) {
 135             if (attribute instanceof ExceptionTable) {
 136                 return (ExceptionTable) attribute;
 137             }
 138         }
 139         return null;
 140     }
 141 
 142 
 143     /** @return LocalVariableTable of code attribute if any, i.e. the call is forwarded
 144      * to the Code atribute.
 145      */
 146     public final LocalVariableTable getLocalVariableTable() {
 147         final Code code = getCode();
 148         if (code == null) {
 149             return null;
 150         }
 151         return code.getLocalVariableTable();
 152     }
 153 
 154 
 155     /** @return LineNumberTable of code attribute if any, i.e. the call is forwarded
 156      * to the Code atribute.
 157      */
 158     public final LineNumberTable getLineNumberTable() {
 159         final Code code = getCode();
 160         if (code == null) {
 161             return null;
 162         }
 163         return code.getLineNumberTable();
 164     }
 165 
 166 
 167     /**
 168      * Return string representation close to declaration format,
 169      * `public static void main(String[] args) throws IOException', e.g.
 170      *
 171      * @return String representation of the method.
 172      */
 173     @Override
 174     public final String toString() {
 175         final String access = Utility.accessToString(super.getAccessFlags());
 176         // Get name and signature from constant pool
 177         ConstantUtf8 c = (ConstantUtf8) super.getConstantPool().getConstant(super.getSignatureIndex(), Const.CONSTANT_Utf8);
 178         String signature = c.getBytes();
 179         c = (ConstantUtf8) super.getConstantPool().getConstant(super.getNameIndex(), Const.CONSTANT_Utf8);
 180         final String name = c.getBytes();
 181         signature = Utility.methodSignatureToString(signature, name, access, true,
 182                 getLocalVariableTable());
 183         final StringBuilder buf = new StringBuilder(signature);
 184         for (final Attribute attribute : super.getAttributes()) {
 185             if (!((attribute instanceof Code) || (attribute instanceof ExceptionTable))) {
 186                 buf.append(" [").append(attribute).append("]");
 187             }
 188         }
 189         final ExceptionTable e = getExceptionTable();
 190         if (e != null) {
 191             final String str = e.toString();
 192             if (!str.isEmpty()) {
 193                 buf.append("\n\t\tthrows ").append(str);
 194             }
 195         }
 196         return buf.toString();
 197     }
 198 
 199 
 200     /**
 201      * @return deep copy of this method
 202      */
 203     public final Method copy( final ConstantPool _constant_pool ) {
 204         return (Method) copy_(_constant_pool);
 205     }
 206 
 207 
 208     /**
 209      * @return return type of method
 210      */
 211     public Type getReturnType() {
 212         return Type.getReturnType(getSignature());
 213     }
 214 
 215 
 216     /**
 217      * @return array of method argument types
 218      */
 219     public Type[] getArgumentTypes() {
 220         return Type.getArgumentTypes(getSignature());
 221     }
 222 
 223 




  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 package com.sun.org.apache.bcel.internal.classfile;
  22 
  23 import java.io.DataInput;
  24 import java.io.IOException;
  25 import java.util.Objects;
  26 
  27 import com.sun.org.apache.bcel.internal.Const;
  28 import com.sun.org.apache.bcel.internal.generic.Type;
  29 import com.sun.org.apache.bcel.internal.util.BCELComparator;
  30 
  31 /**
  32  * This class represents the method info structure, i.e., the representation
  33  * for a method in the class. See JVM specification for details.
  34  * A method has access flags, a name, a signature and a number of attributes.
  35  *

  36  */
  37 public final class Method extends FieldOrMethod {
  38 
  39     private static BCELComparator bcelComparator = new BCELComparator() {
  40 
  41         @Override
  42         public boolean equals( final Object o1, final Object o2 ) {
  43             final Method THIS = (Method) o1;
  44             final Method THAT = (Method) o2;
  45             return Objects.equals(THIS.getName(), THAT.getName())
  46                     && Objects.equals(THIS.getSignature(), THAT.getSignature());
  47         }
  48 
  49 
  50         @Override
  51         public int hashCode( final Object o ) {
  52             final Method THIS = (Method) o;
  53             return THIS.getSignature().hashCode() ^ THIS.getName().hashCode();
  54         }
  55     };


  98         super(access_flags, name_index, signature_index, attributes, constant_pool);
  99     }
 100 
 101 
 102     /**
 103      * Called by objects that are traversing the nodes of the tree implicitely
 104      * defined by the contents of a Java class. I.e., the hierarchy of methods,
 105      * fields, attributes, etc. spawns a tree of objects.
 106      *
 107      * @param v Visitor object
 108      */
 109     @Override
 110     public void accept( final Visitor v ) {
 111         v.visitMethod(this);
 112     }
 113 
 114 
 115     /**
 116      * @return Code attribute of method, if any
 117      */
 118     public Code getCode() {
 119         for (final Attribute attribute : super.getAttributes()) {
 120             if (attribute instanceof Code) {
 121                 return (Code) attribute;
 122             }
 123         }
 124         return null;
 125     }
 126 
 127 
 128     /**
 129      * @return ExceptionTable attribute of method, if any, i.e., list all
 130      * exceptions the method may throw not exception handlers!
 131      */
 132     public ExceptionTable getExceptionTable() {
 133         for (final Attribute attribute : super.getAttributes()) {
 134             if (attribute instanceof ExceptionTable) {
 135                 return (ExceptionTable) attribute;
 136             }
 137         }
 138         return null;
 139     }
 140 
 141 
 142     /** @return LocalVariableTable of code attribute if any, i.e. the call is forwarded
 143      * to the Code atribute.
 144      */
 145     public LocalVariableTable getLocalVariableTable() {
 146         final Code code = getCode();
 147         if (code == null) {
 148             return null;
 149         }
 150         return code.getLocalVariableTable();
 151     }
 152 
 153 
 154     /** @return LineNumberTable of code attribute if any, i.e. the call is forwarded
 155      * to the Code atribute.
 156      */
 157     public LineNumberTable getLineNumberTable() {
 158         final Code code = getCode();
 159         if (code == null) {
 160             return null;
 161         }
 162         return code.getLineNumberTable();
 163     }
 164 
 165 
 166     /**
 167      * Return string representation close to declaration format,
 168      * `public static void main(String[] args) throws IOException', e.g.
 169      *
 170      * @return String representation of the method.
 171      */
 172     @Override
 173     public String toString() {
 174         final String access = Utility.accessToString(super.getAccessFlags());
 175         // Get name and signature from constant pool
 176         ConstantUtf8 c = (ConstantUtf8) super.getConstantPool().getConstant(super.getSignatureIndex(), Const.CONSTANT_Utf8);
 177         String signature = c.getBytes();
 178         c = (ConstantUtf8) super.getConstantPool().getConstant(super.getNameIndex(), Const.CONSTANT_Utf8);
 179         final String name = c.getBytes();
 180         signature = Utility.methodSignatureToString(signature, name, access, true,
 181                 getLocalVariableTable());
 182         final StringBuilder buf = new StringBuilder(signature);
 183         for (final Attribute attribute : super.getAttributes()) {
 184             if (!((attribute instanceof Code) || (attribute instanceof ExceptionTable))) {
 185                 buf.append(" [").append(attribute).append("]");
 186             }
 187         }
 188         final ExceptionTable e = getExceptionTable();
 189         if (e != null) {
 190             final String str = e.toString();
 191             if (!str.isEmpty()) {
 192                 buf.append("\n\t\tthrows ").append(str);
 193             }
 194         }
 195         return buf.toString();
 196     }
 197 
 198 
 199     /**
 200      * @return deep copy of this method
 201      */
 202     public Method copy( final ConstantPool _constant_pool ) {
 203         return (Method) copy_(_constant_pool);
 204     }
 205 
 206 
 207     /**
 208      * @return return type of method
 209      */
 210     public Type getReturnType() {
 211         return Type.getReturnType(getSignature());
 212     }
 213 
 214 
 215     /**
 216      * @return array of method argument types
 217      */
 218     public Type[] getArgumentTypes() {
 219         return Type.getArgumentTypes(getSignature());
 220     }
 221 
 222 


< prev index next >