All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.bytecode.ParamReader Maven / Gradle / Ivy

There is a newer version: 3.2.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.bytecode;


import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;


/**
 * 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.

*/ public class ParamReader extends ClassReader { private String methodName; private Map methods = new HashMap(); private Class[] paramTypes; /** * Processes a class file, given it's class. We'll use the defining * classloader to locate the bytecode. * * @param c * @throws java.io.IOException */ public ParamReader(Class c) throws IOException { this(getBytes(c)); } /** * Processes the given class bytes directly. * * @param b * @throws IOException */ public ParamReader(byte[] b) throws IOException { super(b, findAttributeReaders(ParamReader.class)); // check the magic number if (readInt() != 0xCAFEBABE) { // not a class file! throw new IOException("Error looking for paramter names in bytecode: input does not appear to be a valid class file"); } 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 } } public void readCode() throws IOException { 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(); } /** * Reads a MethodParameters attribute. * * @throws IOException */ public void readMethodParameters() throws IOException { int c = read(); // parameters count MethodInfo info = new MethodInfo(c); for(int i = 0; i





© 2015 - 2024 Weber Informatics LLC | Privacy Policy