本文整理匯總了Java中javassist.bytecode.MethodInfo.getCodeAttribute方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodInfo.getCodeAttribute方法的具體用法?Java MethodInfo.getCodeAttribute怎麽用?Java MethodInfo.getCodeAttribute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javassist.bytecode.MethodInfo
的用法示例。
在下文中一共展示了MethodInfo.getCodeAttribute方法的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: 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 );
}
}
示例4: 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;
}
示例5: 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());
}
}
示例6: 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);
}
}
示例7: 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;
}
示例8: 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;
}
示例9: getBehaviourBytecode
import javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
/**
* This is basically the InstructionPrinter.getMethodBytecode but with a
* CtBehaviour parameter instead of a CtMethod
*
* @param behavior
* @return
*/
public static String getBehaviourBytecode(CtBehavior behavior) {
MethodInfo info = behavior.getMethodInfo2();
CodeAttribute code = info.getCodeAttribute();
if (code == null) {
return "";
}
ConstPool pool = info.getConstPool();
StringBuilder sb = new StringBuilder(1024);
CodeIterator iterator = code.iterator();
while (iterator.hasNext()) {
int pos;
try {
pos = iterator.next();
} catch (BadBytecode e) {
throw new JVoidIntrumentationException("BadBytecoode", e);
}
sb.append(pos + ": " + InstructionPrinter.instructionString(iterator, pos, pool) + "\n");
}
return sb.toString();
}
示例10: getReflectParamName
import javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
public static String[] getReflectParamName(Class<?> clazz, String methodName) {
String[] paramNames = null;
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) {
// exception
}
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);
} catch (NotFoundException e) {
e.printStackTrace();
}
return paramNames;
}
示例11: getMethodArgNames
import javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
/**
* 通過Javaassist獲取方法的參數名
*/
public static String[] getMethodArgNames(String className, String methodName){
List<String> names = new ArrayList<String>();
try {
ClassPool classPool = ClassPool.getDefault();
classPool.insertClassPath(new ClassClassPath(ReflectUtil.class));
CtClass ctClass = classPool.getCtClass(className);
CtMethod method = ctClass.getDeclaredMethod(methodName);
MethodInfo methodInfo = method.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
return (String[])names.toArray();
}
int argNum = method.getParameterTypes().length;
int pos = javassist.Modifier.isStatic(method.getModifiers()) ? 0 : 1;
for (int i=0; i<argNum; i++) {
names.add(attr.variableName(i + pos));
}
} catch (NotFoundException e) {
log.error(e);
}
return names.toArray(new String[0]);
}
示例12: tes
import javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
static void tes() throws NotFoundException {
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.get("org.rural.test.Test");
CtMethod[] methods = ctClass.getDeclaredMethods();
for(CtMethod method : methods){
MethodInfo methodInfo = method.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
return;
}
String[] paramNames = new String[method.getParameterTypes().length];
int pos = Modifier.isStatic(method.getModifiers()) ? 0 : 1;
for (int i = 0; i < paramNames.length; i++)
paramNames[i] = attr.variableName(i + pos);
}
}
示例13: getLocalVariableAttribute
import javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
public static LocalVariableAttribute getLocalVariableAttribute(String className, String methodName) throws NotFoundException {
ClassPool clsPool = ClassPool.getDefault();
CtClass ctCls = clsPool.get(className);
CtMethod[] ctMethods = ctCls.getMethods();
CtMethod ctMethod = null;
for(CtMethod methodTmp : ctMethods) {
if(methodTmp.getName().equals(methodName)
&& (methodTmp.getModifiers() & Modifier.PUBLIC) != 0
) {
ctMethod = methodTmp;
break;
}
}
MethodInfo methodInfo = ctMethod.getMethodInfo();
CodeAttribute codeAttr = methodInfo.getCodeAttribute();
LocalVariableAttribute localVarAttr = (LocalVariableAttribute) codeAttr.getAttribute(LocalVariableAttribute.tag);
return localVarAttr;
}
示例14: lookupLocalVariableAttribute
import javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
/**
* get LocalVariableAttribute
*
* @param method
* @return null if the class is not compiled with debug option
*/
public static LocalVariableAttribute lookupLocalVariableAttribute(CtBehavior method) {
if (method == null) {
throw new NullPointerException("method must not be null");
}
MethodInfo methodInfo = method.getMethodInfo2();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
if (codeAttribute == null) {
return null;
}
AttributeInfo localVariableTable = codeAttribute.getAttribute(LocalVariableAttribute.tag);
LocalVariableAttribute local = (LocalVariableAttribute) localVariableTable;
return local;
}
示例15: testReflectParamName
import javassist.bytecode.MethodInfo; //導入方法依賴的package包/類
/**
* 反射獲取方法參數名稱
*/
public static void testReflectParamName() {
Class clazz = MyClass.class;
try {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get(clazz.getName());
CtMethod cm = cc.getDeclaredMethod("concatString");
// 使用javaassist的反射方法獲取方法的參數名
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute
.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
// exception
}
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);
// paramNames即參數名
for (int i = 0; i < paramNames.length; i++) {
System.out.println("參數名" + i + ":" + paramNames[i]);
}
} catch (NotFoundException e) {
e.printStackTrace();
}
}