286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/Apackage com.sun.org.apache.bcel.internal.classfile;
286N/A
286N/A/* ====================================================================
286N/A * The Apache Software License, Version 1.1
286N/A *
286N/A * Copyright (c) 2001 The Apache Software Foundation. All rights
286N/A * reserved.
286N/A *
286N/A * Redistribution and use in source and binary forms, with or without
286N/A * modification, are permitted provided that the following conditions
286N/A * are met:
286N/A *
286N/A * 1. Redistributions of source code must retain the above copyright
286N/A * notice, this list of conditions and the following disclaimer.
286N/A *
286N/A * 2. Redistributions in binary form must reproduce the above copyright
286N/A * notice, this list of conditions and the following disclaimer in
286N/A * the documentation and/or other materials provided with the
286N/A * distribution.
286N/A *
286N/A * 3. The end-user documentation included with the redistribution,
286N/A * if any, must include the following acknowledgment:
286N/A * "This product includes software developed by the
286N/A * Apache Software Foundation (http://www.apache.org/)."
286N/A * Alternately, this acknowledgment may appear in the software itself,
286N/A * if and wherever such third-party acknowledgments normally appear.
286N/A *
286N/A * 4. The names "Apache" and "Apache Software Foundation" and
286N/A * "Apache BCEL" must not be used to endorse or promote products
286N/A * derived from this software without prior written permission. For
286N/A * written permission, please contact apache@apache.org.
286N/A *
286N/A * 5. Products derived from this software may not be called "Apache",
286N/A * "Apache BCEL", nor may "Apache" appear in their name, without
286N/A * prior written permission of the Apache Software Foundation.
286N/A *
286N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
286N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
286N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
286N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
286N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
286N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
286N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
286N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
286N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
286N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
286N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
286N/A * SUCH DAMAGE.
286N/A * ====================================================================
286N/A *
286N/A * This software consists of voluntary contributions made by many
286N/A * individuals on behalf of the Apache Software Foundation. For more
286N/A * information on the Apache Software Foundation, please see
286N/A * <http://www.apache.org/>.
286N/A */
286N/Aimport com.sun.org.apache.bcel.internal.Constants;
286N/Aimport com.sun.org.apache.bcel.internal.generic.Type;
286N/Aimport java.io.*;
286N/A
286N/A/**
286N/A * This class represents the method info structure, i.e., the representation
286N/A * for a method in the class. See JVM specification for details.
286N/A * A method has access flags, a name, a signature and a number of attributes.
286N/A *
286N/A * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
286N/A */
286N/Apublic final class Method extends FieldOrMethod {
286N/A /**
286N/A * Empty constructor, all attributes have to be defined via `setXXX'
286N/A * methods. Use at your own risk.
286N/A */
286N/A public Method() {}
286N/A
286N/A /**
286N/A * Initialize from another object. Note that both objects use the same
286N/A * references (shallow copy). Use clone() for a physical copy.
286N/A */
286N/A public Method(Method c) {
286N/A super(c);
286N/A }
286N/A
286N/A /**
286N/A * Construct object from file stream.
286N/A * @param file Input stream
286N/A * @throws IOException
286N/A * @throws ClassFormatException
286N/A */
286N/A Method(DataInputStream file, ConstantPool constant_pool)
286N/A throws IOException, ClassFormatException
286N/A {
286N/A super(file, constant_pool);
286N/A }
286N/A
286N/A /**
286N/A * @param access_flags Access rights of method
286N/A * @param name_index Points to field name in constant pool
286N/A * @param signature_index Points to encoded signature
286N/A * @param attributes Collection of attributes
286N/A * @param constant_pool Array of constants
286N/A */
286N/A public Method(int access_flags, int name_index, int signature_index,
286N/A Attribute[] attributes, ConstantPool constant_pool)
286N/A {
286N/A super(access_flags, name_index, signature_index, attributes, constant_pool);
286N/A }
286N/A
286N/A /**
286N/A * Called by objects that are traversing the nodes of the tree implicitely
286N/A * defined by the contents of a Java class. I.e., the hierarchy of methods,
286N/A * fields, attributes, etc. spawns a tree of objects.
286N/A *
286N/A * @param v Visitor object
286N/A */
286N/A public void accept(Visitor v) {
286N/A v.visitMethod(this);
286N/A }
286N/A
286N/A /**
286N/A * @return Code attribute of method, if any
286N/A */
286N/A public final Code getCode() {
286N/A for(int i=0; i < attributes_count; i++)
286N/A if(attributes[i] instanceof Code)
286N/A return (Code)attributes[i];
286N/A
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * @return ExceptionTable attribute of method, if any, i.e., list all
286N/A * exceptions the method may throw not exception handlers!
286N/A */
286N/A public final ExceptionTable getExceptionTable() {
286N/A for(int i=0; i < attributes_count; i++)
286N/A if(attributes[i] instanceof ExceptionTable)
286N/A return (ExceptionTable)attributes[i];
286N/A
286N/A return null;
286N/A }
286N/A
286N/A /** @return LocalVariableTable of code attribute if any, i.e. the call is forwarded
286N/A * to the Code atribute.
286N/A */
286N/A public final LocalVariableTable getLocalVariableTable() {
286N/A Code code = getCode();
286N/A
286N/A if(code != null)
286N/A return code.getLocalVariableTable();
286N/A else
286N/A return null;
286N/A }
286N/A
286N/A /** @return LineNumberTable of code attribute if any, i.e. the call is forwarded
286N/A * to the Code atribute.
286N/A */
286N/A public final LineNumberTable getLineNumberTable() {
286N/A Code code = getCode();
286N/A
286N/A if(code != null)
286N/A return code.getLineNumberTable();
286N/A else
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * Return string representation close to declaration format,
286N/A * `public static void _main(String[] args) throws IOException', e.g.
286N/A *
286N/A * @return String representation of the method.
286N/A */
286N/A public final String toString() {
286N/A ConstantUtf8 c;
286N/A String name, signature, access; // Short cuts to constant pool
286N/A StringBuffer buf;
286N/A
286N/A access = Utility.accessToString(access_flags);
286N/A
286N/A // Get name and signature from constant pool
286N/A c = (ConstantUtf8)constant_pool.getConstant(signature_index,
286N/A Constants.CONSTANT_Utf8);
286N/A signature = c.getBytes();
286N/A
286N/A c = (ConstantUtf8)constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8);
286N/A name = c.getBytes();
286N/A
286N/A signature = Utility.methodSignatureToString(signature, name, access, true,
286N/A getLocalVariableTable());
286N/A buf = new StringBuffer(signature);
286N/A
286N/A for(int i=0; i < attributes_count; i++) {
286N/A Attribute a = attributes[i];
286N/A
286N/A if(!((a instanceof Code) || (a instanceof ExceptionTable)))
286N/A buf.append(" [" + a.toString() + "]");
286N/A }
286N/A
286N/A ExceptionTable e = getExceptionTable();
286N/A if(e != null) {
286N/A String str = e.toString();
286N/A if(!str.equals(""))
286N/A buf.append("\n\t\tthrows " + str);
286N/A }
286N/A
286N/A return buf.toString();
286N/A }
286N/A
286N/A /**
286N/A * @return deep copy of this method
286N/A */
286N/A public final Method copy(ConstantPool constant_pool) {
286N/A return (Method)copy_(constant_pool);
286N/A }
286N/A
286N/A /**
286N/A * @return return type of method
286N/A */
286N/A public Type getReturnType() {
286N/A return Type.getReturnType(getSignature());
286N/A }
286N/A
286N/A /**
286N/A * @return array of method argument types
286N/A */
286N/A public Type[] getArgumentTypes() {
286N/A return Type.getArgumentTypes(getSignature());
286N/A }
286N/A}