本文整理汇总了Java中javassist.bytecode.CodeAttribute.getAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java CodeAttribute.getAttribute方法的具体用法?Java CodeAttribute.getAttribute怎么用?Java CodeAttribute.getAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javassist.bytecode.CodeAttribute
的用法示例。
在下文中一共展示了CodeAttribute.getAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getParamNames
import javassist.bytecode.CodeAttribute; //导入方法依赖的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.CodeAttribute; //导入方法依赖的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: getParamName
import javassist.bytecode.CodeAttribute; //导入方法依赖的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;
}
示例4: rename
import javassist.bytecode.CodeAttribute; //导入方法依赖的package包/类
public void rename(CtClass c) {
for (CtBehavior behavior : c.getDeclaredBehaviors()) {
// if there's a local variable table, just rename everything to v1, v2, v3, ... for now
CodeAttribute codeAttribute = behavior.getMethodInfo().getCodeAttribute();
if (codeAttribute == null) {
continue;
}
BehaviorEntry behaviorEntry = EntryFactory.getBehaviorEntry(behavior);
ConstPool constants = c.getClassFile().getConstPool();
LocalVariableAttribute table = (LocalVariableAttribute)codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (table != null) {
renameLVT(behaviorEntry, constants, table);
}
LocalVariableTypeAttribute typeTable = (LocalVariableTypeAttribute)codeAttribute.getAttribute(LocalVariableAttribute.typeTag);
if (typeTable != null) {
renameLVTT(typeTable, table);
}
}
}
示例5: initMethodParamInfo
import javassist.bytecode.CodeAttribute; //导入方法依赖的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.CodeAttribute; //导入方法依赖的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.CodeAttribute; //导入方法依赖的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: recordLocalVariables
import javassist.bytecode.CodeAttribute; //导入方法依赖的package包/类
/**
* Records local variables available at the specified program counter.
* If the LocalVariableAttribute is not available, this method does not
* record any local variable. It only returns false.
*
* @param pc program counter (>= 0)
* @return false if the CodeAttribute does not include a
* LocalVariableAttribute.
*/
public boolean recordLocalVariables(CodeAttribute ca, int pc)
throws CompileError
{
LocalVariableAttribute va
= (LocalVariableAttribute)
ca.getAttribute(LocalVariableAttribute.tag);
if (va == null)
return false;
int n = va.tableLength();
for (int i = 0; i < n; ++i) {
int start = va.startPc(i);
int len = va.codeLength(i);
if (start <= pc && pc < start + len)
gen.recordVariable(va.descriptor(i), va.variableName(i),
va.index(i), stable);
}
return true;
}
示例9: recordParamNames
import javassist.bytecode.CodeAttribute; //导入方法依赖的package包/类
/**
* Records parameter names if the LocalVariableAttribute is available.
* It returns false unless the LocalVariableAttribute is available.
*
* @param numOfLocalVars the number of local variables used
* for storing the parameters.
* @return false if the CodeAttribute does not include a
* LocalVariableAttribute.
*/
public boolean recordParamNames(CodeAttribute ca, int numOfLocalVars)
throws CompileError
{
LocalVariableAttribute va
= (LocalVariableAttribute)
ca.getAttribute(LocalVariableAttribute.tag);
if (va == null)
return false;
int n = va.tableLength();
for (int i = 0; i < n; ++i) {
int index = va.index(i);
if (index < numOfLocalVars)
gen.recordVariable(va.descriptor(i), va.variableName(i),
index, stable);
}
return true;
}
示例10: getReflectParamName
import javassist.bytecode.CodeAttribute; //导入方法依赖的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.CodeAttribute; //导入方法依赖的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.CodeAttribute; //导入方法依赖的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.CodeAttribute; //导入方法依赖的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: extractNameParameter
import javassist.bytecode.CodeAttribute; //导入方法依赖的package包/类
private Map<Integer, String> extractNameParameter(CtMethod m) throws NotFoundException {
Map<Integer, String> hashNameParam = new HashMap<Integer, String>();
CodeAttribute codeAttribute = (CodeAttribute) m.getMethodInfo().getAttribute("Code");
if (codeAttribute != null) {
LocalVariableAttribute localVariableAttribute = (LocalVariableAttribute) codeAttribute.getAttribute("LocalVariableTable");
if (localVariableAttribute != null && localVariableAttribute.tableLength() >= m.getParameterTypes().length) {
for (int i = 0; i < m.getParameterTypes().length + 2; i++) {
String name = localVariableAttribute.getConstPool().getUtf8Info(localVariableAttribute.nameIndex(i));
if (!name.equals("this")) {
hashNameParam.put(i, name);
}
}
}
}
return hashNameParam;
}
示例15: lookupLocalVariableAttribute
import javassist.bytecode.CodeAttribute; //导入方法依赖的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;
}