本文整理汇总了Java中javassist.bytecode.CodeAttribute类的典型用法代码示例。如果您正苦于以下问题:Java CodeAttribute类的具体用法?Java CodeAttribute怎么用?Java CodeAttribute使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CodeAttribute类属于javassist.bytecode包,在下文中一共展示了CodeAttribute类的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: transformInvokevirtualsIntoPutAndGetfields
import javassist.bytecode.CodeAttribute; //导入依赖的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.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;
}
示例5: 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);
}
}
}
示例6: 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());
}
}
示例7: 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);
}
}
示例8: 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;
}
示例9: scan
import javassist.bytecode.CodeAttribute; //导入依赖的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;
}
示例10: insertAuxInitializer
import javassist.bytecode.CodeAttribute; //导入依赖的package包/类
private static void insertAuxInitializer(CodeAttribute codeAttr,
Bytecode initializer,
int stacksize)
throws BadBytecode
{
CodeIterator it = codeAttr.iterator();
int index = it.skipSuperConstructor();
if (index < 0) {
index = it.skipThisConstructor();
if (index >= 0)
return; // this() is called.
// Neither this() or super() is called.
}
int pos = it.insertEx(initializer.get());
it.insert(initializer.getExceptionTable(), pos);
int maxstack = codeAttr.getMaxStack();
if (maxstack < stacksize)
codeAttr.setMaxStack(stacksize);
}
示例11: runEditor
import javassist.bytecode.CodeAttribute; //导入依赖的package包/类
protected void runEditor(ExprEditor ed, CodeIterator oldIterator)
throws CannotCompileException
{
CodeAttribute codeAttr = oldIterator.get();
int orgLocals = codeAttr.getMaxLocals();
int orgStack = codeAttr.getMaxStack();
int newLocals = locals();
codeAttr.setMaxStack(stack());
codeAttr.setMaxLocals(newLocals);
ExprEditor.LoopContext context
= new ExprEditor.LoopContext(newLocals);
int size = oldIterator.getCodeLength();
int endPos = oldIterator.lookAhead();
oldIterator.move(currentPos);
if (ed.doit(thisClass, thisMethod, context, oldIterator, endPos))
edited = true;
oldIterator.move(endPos + oldIterator.getCodeLength() - size);
codeAttr.setMaxLocals(orgLocals);
codeAttr.setMaxStack(orgStack);
maxLocals = context.maxLocals;
maxStack += context.maxStack;
}
示例12: 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;
}
示例13: 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;
}
示例14: getBehaviourBytecode
import javassist.bytecode.CodeAttribute; //导入依赖的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();
}
示例15: 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;
}