本文整理匯總了Java中javassist.bytecode.MethodInfo類的典型用法代碼示例。如果您正苦於以下問題:Java MethodInfo類的具體用法?Java MethodInfo怎麽用?Java MethodInfo使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MethodInfo類屬於javassist.bytecode包,在下文中一共展示了MethodInfo類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getParamNames
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
/**
* 獲取方法的參數名
* @param clazz 類
* @param method 方法
* @return 方法名稱數組
* @throws NotFoundException .{@link NotFoundException}
*/
public static String[] getParamNames(Class clazz, String method) throws NotFoundException {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get(clazz.getName());
CtMethod cm = cc.getDeclaredMethod(method);
// 使用javaassist的反射方法獲取方法的參數名
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
throw new NotFoundException("cannot get LocalVariableAttribute");
}
String[] paramNames = new String[cm.getParameterTypes().length];
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i < paramNames.length; i++) {
paramNames[i] = attr.variableName(i + pos);
}
return paramNames;
}
示例2: getParamNames
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
/**
* get method parameter names
* @param cls
* @param method
* @return
* @throws Exception
*/
public static String[] getParamNames(Class<?> cls, Method method) throws Exception {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get(cls.getName());
Class<?>[] paramAry = method.getParameterTypes();
String[] paramTypeNames = new String[paramAry.length];
for (int i = 0; i < paramAry.length; i++) {
paramTypeNames[i] = paramAry[i].getName();
}
CtMethod cm = cc.getDeclaredMethod(method.getName(), pool.get(paramTypeNames));
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
throw new Exception("class:" + cls.getName() + ", have no LocalVariableTable, please use javac -g:{vars} to compile the source file");
}
String[] paramNames = new String[cm.getParameterTypes().length];
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i < paramNames.length; i++) {
paramNames[i] = attr.variableName(i + pos);
}
return paramNames;
}
示例3: replaceGenericResponseMethods
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
private void replaceGenericResponseMethods(CtClass clazz)
throws NotFoundException, CannotCompileException, BadBytecode {
for (CtMethod method : clazz.getMethods()) {
if (method.getReturnType().subtypeOf(getGenericResponseClass())) {
MethodInfo methodInfo = method.getMethodInfo();
CtMethod replacementMethod = CtNewMethod
.abstractMethod(
getResponseClass(), method.getName(), method.getParameterTypes(),
method.getExceptionTypes(), clazz);
for (Object attributeInfo : methodInfo.getAttributes()) {
if (attributeInfo instanceof SignatureAttribute) {
String sig = transformMethodSignature((SignatureAttribute) attributeInfo);
replacementMethod.getMethodInfo().addAttribute(
new SignatureAttribute(((SignatureAttribute) attributeInfo).getConstPool(), sig));
} else {
replacementMethod.getMethodInfo().addAttribute((AttributeInfo) attributeInfo);
}
}
clazz.removeMethod(method);
clazz.addMethod(replacementMethod);
}
}
}
示例4: addDefaultConstructor
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
/**
* Declares a constructor that takes no parameter.
*
* @param classfile The class descriptor
*
* @throws CannotCompileException Indicates trouble with the underlying Javassist calls
*/
private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException {
final ConstPool constPool = classfile.getConstPool();
final String constructorSignature = "()V";
final MethodInfo constructorMethodInfo = new MethodInfo( constPool, MethodInfo.nameInit, constructorSignature );
final Bytecode code = new Bytecode( constPool, 0, 1 );
// aload_0
code.addAload( 0 );
// invokespecial
code.addInvokespecial( BulkAccessor.class.getName(), MethodInfo.nameInit, constructorSignature );
// return
code.addOpcode( Opcode.RETURN );
constructorMethodInfo.setCodeAttribute( code.toCodeAttribute() );
constructorMethodInfo.setAccessFlags( AccessFlag.PUBLIC );
classfile.addMethod( constructorMethodInfo );
}
示例5: transformInvokevirtualsIntoPutAndGetfields
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
private void transformInvokevirtualsIntoPutAndGetfields(ClassFile classfile) throws CannotCompileException, BadBytecode {
for ( Object o : classfile.getMethods() ) {
final MethodInfo methodInfo = (MethodInfo) o;
final String methodName = methodInfo.getName();
if ( methodName.startsWith( EACH_READ_METHOD_PREFIX )
|| methodName.startsWith( EACH_WRITE_METHOD_PREFIX )
|| methodName.equals( GETFIELDHANDLER_METHOD_NAME )
|| methodName.equals( SETFIELDHANDLER_METHOD_NAME ) ) {
continue;
}
final CodeAttribute codeAttr = methodInfo.getCodeAttribute();
if ( codeAttr == null ) {
continue;
}
final CodeIterator iter = codeAttr.iterator();
while ( iter.hasNext() ) {
int pos = iter.next();
pos = transformInvokevirtualsIntoGetfields( classfile, iter, pos );
transformInvokevirtualsIntoPutfields( classfile, iter, pos );
}
final StackMapTable smt = MapMaker.make( classPool, methodInfo );
codeAttr.setAttribute( smt );
}
}
示例6: getParamName
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
/**
* 獲取方法參數名
* @param method
* @return
*/
public static String[] getParamName(Method method) throws Exception {
ClassPool pool = ClassPool.getDefault();
CtClass ctClass = pool.get(method.getDeclaringClass().getName());
CtMethod ctMethod = ctClass.getDeclaredMethod(method.getName());
// 使用javassist的反射方法的參數名
MethodInfo methodInfo = ctMethod.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute)
codeAttribute.getAttribute(LocalVariableAttribute.tag);
int length = ctMethod.getParameterTypes().length;
String[] array = new String[length];
for (int i = 0; i < length; i++) {
array[i] = attr.variableName(i + 1);
}
return array;
}
示例7: updateClass
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
public static void updateClass(MethodInfo info, List<String> names) {
// add the names to the class const pool
ConstPool constPool = info.getConstPool();
List<Integer> parameterNameIndices = new ArrayList<Integer>();
for (String name : names) {
if (name != null) {
parameterNameIndices.add(constPool.addUtf8Info(name));
} else {
parameterNameIndices.add(0);
}
}
// add the attribute to the method
info.addAttribute(new MethodParametersAttribute(constPool, parameterNameIndices));
}
示例8: initMethodParamInfo
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
private void initMethodParamInfo(Object controller, String methodName) {
methodParamInfo = new LinkedHashMap<Class<?>, String>();
Class<?> clazz = controller.getClass();
try {
ClassPool classPool = ClassPool.getDefault();
classPool.insertClassPath(new ClassClassPath(clazz));
CtClass ctClass = classPool.get(clazz.getName());
CtMethod ctMethod = ctClass.getDeclaredMethod(methodName);
MethodInfo methodInfo = ctMethod.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute
.getAttribute("LocalVariableTable");
CtClass[] parameterTypes = ctMethod.getParameterTypes();
int pos = Modifier.isStatic(ctMethod.getModifiers()) ? 0 : 1;
for (int i = 0; i < parameterTypes.length; i++)
methodParamInfo.put(Class.forName(parameterTypes[i].getName()),
attr.variableName(i + pos));
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
示例9: getMethodArgNames
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
/**
* Get method arg names.
*
* @param clazz
* @param methodName
* @return
*/
public static <T> String[] getMethodArgNames(Class<T> clazz, String methodName) {
try {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get(clazz.getName());
CtMethod cm = cc.getDeclaredMethod(methodName);
// 使用javaassist的反射方法獲取方法的參數名
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
throw new RuntimeException("LocalVariableAttribute of method is null! Class " + clazz.getName() + ",method name is " + methodName);
}
String[] paramNames = new String[cm.getParameterTypes().length];
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i < paramNames.length; i++)
paramNames[i] = attr.variableName(i + pos);
return paramNames;
} catch (NotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
示例10: getMethodVariableName
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
/**
* Get method variable names for the provided method.
*
* @param classname String
* @param methodname String
* @return the String[] of method variable name.
*/
public static String[] getMethodVariableName(String classname, String methodname) {
try {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get(classname);
CtMethod cm = cc.getDeclaredMethod(methodname);
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
String[] paramNames = new String[cm.getParameterTypes().length];
LocalVariableAttribute attr =
(LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr != null) {
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i < paramNames.length; i++) {
paramNames[i] = attr.variableName(i + pos);
}
return paramNames;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
示例11: scan
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
public Subroutine[] scan(MethodInfo method) throws BadBytecode {
CodeAttribute code = method.getCodeAttribute();
CodeIterator iter = code.iterator();
subroutines = new Subroutine[code.getCodeLength()];
subTable.clear();
done.clear();
scan(0, iter, null);
ExceptionTable exceptions = code.getExceptionTable();
for (int i = 0; i < exceptions.size(); i++) {
int handler = exceptions.handlerPc(i);
// If an exception is thrown in subroutine, the handler
// is part of the same subroutine.
scan(handler, iter, subroutines[exceptions.startPc(i)]);
}
return subroutines;
}
示例12: buildExceptionInfo
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
private ExceptionInfo[] buildExceptionInfo(MethodInfo method) {
ConstPool constPool = method.getConstPool();
ClassPool classes = clazz.getClassPool();
ExceptionTable table = method.getCodeAttribute().getExceptionTable();
ExceptionInfo[] exceptions = new ExceptionInfo[table.size()];
for (int i = 0; i < table.size(); i++) {
int index = table.catchType(i);
Type type;
try {
type = index == 0 ? Type.THROWABLE : Type.get(classes.get(constPool.getClassInfo(index)));
} catch (NotFoundException e) {
throw new IllegalStateException(e.getMessage());
}
exceptions[i] = new ExceptionInfo(table.startPc(i), table.endPc(i), table.handlerPc(i), type);
}
return exceptions;
}
示例13: firstFrame
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
private Frame firstFrame(MethodInfo method, int maxLocals, int maxStack) {
int pos = 0;
Frame first = new Frame(maxLocals, maxStack);
if ((method.getAccessFlags() & AccessFlag.STATIC) == 0) {
first.setLocal(pos++, Type.get(clazz));
}
CtClass[] parameters;
try {
parameters = Descriptor.getParameterTypes(method.getDescriptor(), clazz.getClassPool());
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
for (int i = 0; i < parameters.length; i++) {
Type type = zeroExtend(Type.get(parameters[i]));
first.setLocal(pos++, type);
if (type.getSize() == 2)
first.setLocal(pos++, Type.TOP);
}
return first;
}
示例14: getClassMethods
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
public List<MethodInfo> getClassMethods(String classname, ConstPool cp) {
ClassPathList list = pathList;
List<MethodInfo> methods = null;
while (list != null) {
if (!(list.path instanceof DalvikClassPath)) {
list = list.next;
continue;
}
methods = ((DalvikClassPath)list.path).getClassMethods(classname, cp);
if (methods == null)
list = list.next;
else
return methods;
}
return null; // not found
}
示例15: makeBehaviorCache
import javassist.bytecode.MethodInfo; //導入依賴的package包/類
private void makeBehaviorCache(CtMember.Cache cache) {
List<MethodInfo> list = getClassFile2().getMethods();
if (null == list) {
return;
}
int n = list.size();
for (int i = 0; i < n; ++i) {
MethodInfo minfo = (MethodInfo)list.get(i);
if (minfo.isMethod()) {
CtMethod newMethod = new CtMethod(minfo, this);
cache.addMethod(newMethod);
}
else {
CtConstructor newCons = new CtConstructor(minfo, this);
cache.addConstructor(newCons);
}
}
}