本文整理汇总了Java中scouter.javassist.bytecode.Bytecode.addGetfield方法的典型用法代码示例。如果您正苦于以下问题:Java Bytecode.addGetfield方法的具体用法?Java Bytecode.addGetfield怎么用?Java Bytecode.addGetfield使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scouter.javassist.bytecode.Bytecode
的用法示例。
在下文中一共展示了Bytecode.addGetfield方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getter
import scouter.javassist.bytecode.Bytecode; //导入方法依赖的package包/类
/**
* Creates a public getter method. The getter method returns the value
* of the specified field in the class to which this method is added.
* The created method is initially not static even if the field is
* static. Change the modifiers if the method should be static.
*
* @param methodName the name of the getter
* @param field the field accessed.
*/
public static CtMethod getter(String methodName, CtField field)
throws CannotCompileException
{
FieldInfo finfo = field.getFieldInfo2();
String fieldType = finfo.getDescriptor();
String desc = "()" + fieldType;
ConstPool cp = finfo.getConstPool();
MethodInfo minfo = new MethodInfo(cp, methodName, desc);
minfo.setAccessFlags(AccessFlag.PUBLIC);
Bytecode code = new Bytecode(cp, 2, 1);
try {
String fieldName = finfo.getName();
if ((finfo.getAccessFlags() & AccessFlag.STATIC) == 0) {
code.addAload(0);
code.addGetfield(Bytecode.THIS, fieldName, fieldType);
}
else
code.addGetstatic(Bytecode.THIS, fieldName, fieldType);
code.addReturn(field.getType());
}
catch (NotFoundException e) {
throw new CannotCompileException(e);
}
minfo.setCodeAttribute(code.toCodeAttribute());
CtClass cc = field.getDeclaringClass();
// a stack map is not needed.
return new CtMethod(minfo, cc);
}
示例2: addGetter
import scouter.javassist.bytecode.Bytecode; //导入方法依赖的package包/类
private static void addGetter(String classname, ClassFile cf, ConstPool cp)
throws CannotCompileException
{
MethodInfo minfo = new MethodInfo(cp, HANDLER_GETTER,
HANDLER_GETTER_TYPE);
minfo.setAccessFlags(AccessFlag.PUBLIC);
Bytecode code = new Bytecode(cp, 1, 1);
code.addAload(0);
code.addGetfield(classname, HANDLER, HANDLER_TYPE);
code.addOpcode(Bytecode.ARETURN);
minfo.setCodeAttribute(code.toCodeAttribute());
cf.addMethod(minfo);
}
示例3: makeForwarder
import scouter.javassist.bytecode.Bytecode; //导入方法依赖的package包/类
/**
* @param delegatorName null if the original method is abstract.
*/
private static MethodInfo makeForwarder(String thisClassName,
Method meth, String desc, ConstPool cp,
Class declClass, String delegatorName, int index,
ArrayList forwarders) {
MethodInfo forwarder = new MethodInfo(cp, meth.getName(), desc);
forwarder.setAccessFlags(Modifier.FINAL
| (meth.getModifiers() & ~(Modifier.ABSTRACT
| Modifier.NATIVE
| Modifier.SYNCHRONIZED)));
setThrows(forwarder, cp, meth);
int args = Descriptor.paramSize(desc);
Bytecode code = new Bytecode(cp, 0, args + 2);
/*
* static {
* methods[index * 2]
* = RuntimeSupport.findSuperMethod(this, <overridden name>, <desc>);
* methods[index * 2 + 1]
* = RuntimeSupport.findMethod(this, <delegator name>, <desc>);
* or = null // the original method is abstract.
* }
* :
* return ($r)handler.invoke(this, methods[index * 2],
* methods[index * 2 + 1], $args);
*/
int origIndex = index * 2;
int delIndex = index * 2 + 1;
int arrayVar = args + 1;
code.addGetstatic(thisClassName, HOLDER, HOLDER_TYPE);
code.addAstore(arrayVar);
forwarders.add(new Find2MethodsArgs(meth.getName(), delegatorName, desc, origIndex));
code.addAload(0);
code.addGetfield(thisClassName, HANDLER, HANDLER_TYPE);
code.addAload(0);
code.addAload(arrayVar);
code.addIconst(origIndex);
code.addOpcode(Opcode.AALOAD);
code.addAload(arrayVar);
code.addIconst(delIndex);
code.addOpcode(Opcode.AALOAD);
makeParameterList(code, meth.getParameterTypes());
code.addInvokeinterface(MethodHandler.class.getName(), "invoke",
"(Ljava/lang/Object;Ljava/lang/reflect/Method;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;",
5);
Class retType = meth.getReturnType();
addUnwrapper(code, retType);
addReturn(code, retType);
CodeAttribute ca = code.toCodeAttribute();
forwarder.setCodeAttribute(ca);
return forwarder;
}