FileDocCategorySizeDatePackage
ParamReader.javaAPI DocApache Axis 1.47370Sat Apr 22 18:57:28 BST 2006org.apache.axis.utils.bytecode

ParamReader

public class ParamReader extends ClassReader
This is the class file reader for obtaining the parameter names for declared methods in a class. The class must have debugging attributes for us to obtain this information.

This does not work for inherited methods. To obtain parameter names for inherited methods, you must use a paramReader for the class that originally declared the method.

don't get tricky, it's the bare minimum. Instances of this class are not threadsafe -- don't share them.

author
Edwin Smith, Macromedia

Fields Summary
private String
methodName
private Map
methods
private Class[]
paramTypes
Constructors Summary
public ParamReader(Class c)
process a class file, given it's class. We'll use the defining classloader to locate the bytecode.

param
c
throws
IOException


                              
         
        this(getBytes(c));
    
public ParamReader(byte[] b)
process the given class bytes directly.

param
b
throws
IOException

        super(b, findAttributeReaders(ParamReader.class));

        // check the magic number
        if (readInt() != 0xCAFEBABE) {
            // not a class file!
            throw new IOException(Messages.getMessage("badClassFile00"));
        }

        readShort(); // minor version
        readShort(); // major version

        readCpool(); // slurp in the constant pool

        readShort(); // access flags
        readShort(); // this class name
        readShort(); // super class name

        int count = readShort(); // ifaces count
        for (int i = 0; i < count; i++) {
            readShort(); // interface index
        }

        count = readShort(); // fields count
        for (int i = 0; i < count; i++) {
            readShort(); // access flags
            readShort(); // name index
            readShort(); // descriptor index
            skipAttributes(); // field attributes
        }

        count = readShort(); // methods count
        for (int i = 0; i < count; i++) {
            readShort(); // access flags
            int m = readShort(); // name index
            String name = resolveUtf8(m);
            int d = readShort(); // descriptor index
            this.methodName = name + resolveUtf8(d);
            readAttributes(); // method attributes
        }

    
Methods Summary
private org.apache.axis.utils.bytecode.ParamReader$MethodInfogetMethodInfo()

        MethodInfo info = null;
        if (methods != null && methodName != null)
        {
            info = (MethodInfo) methods.get(methodName);
        }
        return info;
    
public java.lang.String[]getParameterNames(java.lang.reflect.Constructor ctor)
return the names of the declared parameters for the given constructor. If we cannot determine the names, return null. The returned array will have one name per parameter. The length of the array will be the same as the length of the Class[] array returned by Constructor.getParameterTypes().

param
ctor
return
String[] array of names, one per parameter, or null

        paramTypes = ctor.getParameterTypes();
        return getParameterNames(ctor, paramTypes);
    
public java.lang.String[]getParameterNames(java.lang.reflect.Method method)
return the names of the declared parameters for the given method. If we cannot determine the names, return null. The returned array will have one name per parameter. The length of the array will be the same as the length of the Class[] array returned by Method.getParameterTypes().

param
method
return
String[] array of names, one per parameter, or null

        paramTypes = method.getParameterTypes();
        return getParameterNames(method, paramTypes);
    
protected java.lang.String[]getParameterNames(java.lang.reflect.Member member, java.lang.Class[] paramTypes)

        // look up the names for this method
        MethodInfo info = (MethodInfo) methods.get(getSignature(member, paramTypes));

        // we know all the local variable names, but we only need to return
        // the names of the parameters.

        if (info != null) {
            String[] paramNames = new String[paramTypes.length];
            int j = Modifier.isStatic(member.getModifiers()) ? 0 : 1;

            boolean found = false;  // did we find any non-null names
            for (int i = 0; i < paramNames.length; i++) {
                if (info.names[j] != null) {
                    found = true;
                    paramNames[i] = info.names[j];
                }
                j++;
                if (paramTypes[i] == double.class || paramTypes[i] == long.class) {
                    // skip a slot for 64bit params
                    j++;
                }
            }

            if (found) {
                return paramNames;
            } else {
                return null;
            }
        } else {
            return null;
        }
    
public voidreadCode()

        readShort(); // max stack
        int maxLocals = readShort(); // max locals

        MethodInfo info = new MethodInfo(maxLocals);
        if (methods != null && methodName != null)
        {
            methods.put(methodName, info);
        }

        skipFully(readInt()); // code
        skipFully(8 * readShort()); // exception table
        // read the code attributes (recursive).  This is where
        // we will find the LocalVariableTable attribute.
        readAttributes();
    
public voidreadLocalVariableTable()
this is invoked when a LocalVariableTable attribute is encountered.

throws
IOException

        int len = readShort(); // table length
        MethodInfo info = getMethodInfo();
        for (int j = 0; j < len; j++) {
            readShort(); // start pc
            readShort(); // length
            int nameIndex = readShort(); // name_index
            readShort(); // descriptor_index
            int index = readShort(); // local index
            if (info != null) {
                info.names[index] = resolveUtf8(nameIndex);
            }
        }