use of javassist.bytecode.MethodInfo in project OpenTripPlanner by opentripplanner.
the class ClassCustomizer method addDoubleGetter.
/**
* Add a simple getter with signature "double getFoo()" to the class, which simply returns the value of the
* field fieldName
* @param ctClass
* @param classFile
* @param fieldName
* @throws DuplicateMemberException
*/
private void addDoubleGetter(ClassFile classFile, String fieldName) throws DuplicateMemberException {
ConstPool constPool = classFile.getConstPool();
// double getFoo()
MethodInfo getter = new MethodInfo(constPool, "get" + ucfirst(fieldName), "()D");
Bytecode code = new Bytecode(constPool, 2, 1);
// load this
code.addAload(0);
code.addGetfield(ctClass, fieldName, "D");
// return with value
code.addOpcode(Opcode.DRETURN);
getter.setCodeAttribute(code.toCodeAttribute());
getter.setAccessFlags(AccessFlag.PUBLIC);
classFile.addMethod(getter);
}
use of javassist.bytecode.MethodInfo in project OpenTripPlanner by opentripplanner.
the class ClassCustomizer method addDoubleSetter.
private void addDoubleSetter(ClassFile classFile, String fieldName) throws DuplicateMemberException {
ConstPool constPool = classFile.getConstPool();
// void setFoo(double)
MethodInfo setter = new MethodInfo(constPool, "set" + ucfirst(fieldName), "(D)V");
Bytecode code = new Bytecode(constPool, 3, 3);
// load this
code.addAload(0);
// load param
code.addDload(1);
code.addPutfield(ctClass, fieldName, "D");
code.addOpcode(Opcode.RETURN);
setter.setCodeAttribute(code.toCodeAttribute());
setter.setAccessFlags(AccessFlag.PUBLIC);
classFile.addMethod(setter);
}
use of javassist.bytecode.MethodInfo in project yyl_example by Relucent.
the class GetMethodParamNameTest method getMethodParamNames.
/**
* 获得参数名 (JDK 自带类 ,接口方法和抽象方法无法正确获取参数名)
*/
private static String[] getMethodParamNames(final Method method) throws NotFoundException {
final String methodName = method.getName();
final Class<?>[] methodParameterTypes = method.getParameterTypes();
final int methodParameterCount = methodParameterTypes.length;
final String className = method.getDeclaringClass().getName();
final boolean isStatic = Modifier.isStatic(method.getModifiers());
final String[] methodParametersNames = new String[methodParameterCount];
ClassPool pool = ClassPool.getDefault();
CtClass ctClass = pool.get(className);
CtClass[] ctTypes = new CtClass[methodParameterTypes.length];
for (int i = 0; i < methodParameterCount; i++) {
ctTypes[i] = pool.get(methodParameterTypes[i].getName());
}
CtMethod ctMethod = ctClass.getDeclaredMethod(methodName, ctTypes);
MethodInfo methodInfo = ctMethod.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attribute = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
// 如果是静态方法,第一个参数就是方法参数,非静态方法,则第一个参数是 this ,然后才是方法的参数
if (attribute != null) {
int variableCount = isStatic ? methodParameterCount : methodParameterCount + 1;
for (int index = 0; index < variableCount; index++) {
int methodParameterIndex = isStatic ? index : index - 1;
if (0 <= methodParameterIndex && methodParameterIndex < methodParameterCount) {
methodParametersNames[methodParameterIndex] = attribute.variableName(index);
}
}
}
return methodParametersNames;
}
use of javassist.bytecode.MethodInfo in project audit4j-core by audit4j.
the class AnnotationDB method scanMethods.
/**
* Scanns both the method and its parameters for annotations.
*
* @param cf
*/
protected void scanMethods(ClassFile cf) {
List<ClassFile> methods = cf.getMethods();
if (methods == null)
return;
for (Object obj : methods) {
MethodInfo method = (MethodInfo) obj;
if (scanMethodAnnotations) {
AnnotationsAttribute visible = (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.visibleTag);
AnnotationsAttribute invisible = (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.invisibleTag);
if (visible != null)
populate(visible.getAnnotations(), cf.getName());
if (invisible != null)
populate(invisible.getAnnotations(), cf.getName());
}
if (scanParameterAnnotations) {
ParameterAnnotationsAttribute paramsVisible = (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.visibleTag);
ParameterAnnotationsAttribute paramsInvisible = (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.invisibleTag);
if (paramsVisible != null && paramsVisible.getAnnotations() != null) {
for (Annotation[] anns : paramsVisible.getAnnotations()) {
populate(anns, cf.getName());
}
}
if (paramsInvisible != null && paramsInvisible.getAnnotations() != null) {
for (Annotation[] anns : paramsInvisible.getAnnotations()) {
populate(anns, cf.getName());
}
}
}
}
}
use of javassist.bytecode.MethodInfo in project leopard by tanhaichao.
the class CtClassUtil method getParameterNames.
/**
* 获取方法的参数名称.
*
* @param ctMethod
* @return
* @throws NotFoundException
*/
public static String[] getParameterNames(CtMethod ctMethod) throws NotFoundException {
MethodInfo methodInfo = ctMethod.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
// logger.info("methodInfo.getConstPool().getSize():");
LocalVariableAttribute attribute = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
// String[] names = new String[attribute.tableLength() - 1];
String[] paramNames = new String[ctMethod.getParameterTypes().length];
int pos = 0;
if (true) {
int size = attribute.tableLength();
if (size > 0) {
String[] names = new String[size - 1];
for (int i = 0; i < names.length; i++) {
names[i] = attribute.variableName(i);
if ("this".equals(names[i])) {
pos = i + 1;
break;
}
}
// logger.info(methodInfo.getName() + " pos:" + pos + " allNames:" + StringUtils.join(names, ", "));
}
}
// logger.info(methodInfo.getName() + " pos:" + pos);
for (int i = 0; i < paramNames.length; i++) {
// paramNames[i] = attribute.variableName(i + 1);
try {
paramNames[i] = attribute.variableName(i + pos);
// logger.info("paramNames[" + i + "]:" + paramNames[i]);
} catch (RuntimeException e) {
throw e;
}
}
// System.err.println("paramNames:" + StringUtils.join(paramNames));
return paramNames;
}
Aggregations