use of javassist.bytecode.MethodInfo in project coprhd-controller by CoprHD.
the class DbSchemaChanger method removeAnnotation.
/**
* remove an annotation from a method
*
* @param methodName the method to which the annotation to be removed
* @param annotationName the annotation name, it should be a full name
*/
public DbSchemaChanger removeAnnotation(String methodName, String annotationName) throws Exception {
// looking for the method to apply the annotation on
CtMethod methodDescriptor = cc.getDeclaredMethod(methodName);
// create the annotation
ClassFile ccFile = cc.getClassFile();
ccFile.setVersionToJava5();
ConstPool constpool = ccFile.getConstPool();
MethodInfo minfo = methodDescriptor.getMethodInfo();
AnnotationsAttribute attr = (AnnotationsAttribute) minfo.getAttribute(AnnotationsAttribute.visibleTag);
Annotation[] annotations = attr.getAnnotations();
List<Annotation> list = new ArrayList();
for (Annotation annotation : annotations) {
if (!annotation.getTypeName().equals(annotationName)) {
list.add(annotation);
}
}
Annotation[] newAnnotations = list.toArray(new Annotation[0]);
attr.setAnnotations(newAnnotations);
minfo.addAttribute(attr);
return this;
}
use of javassist.bytecode.MethodInfo in project tutorials by eugenp.
the class JavasisstUnitTest method givenLoadedClass_whenAddConstructorToClass_shouldCreateClassWithConstructor.
@Test
public void givenLoadedClass_whenAddConstructorToClass_shouldCreateClassWithConstructor() throws NotFoundException, CannotCompileException, BadBytecode {
// given
ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.Point").getClassFile();
Bytecode code = new Bytecode(cf.getConstPool());
code.addAload(0);
code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V");
code.addReturn(null);
// when
MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V");
minfo.setCodeAttribute(code.toCodeAttribute());
cf.addMethod(minfo);
// then
CodeIterator ci = code.toCodeAttribute().iterator();
List<String> operations = new LinkedList<>();
while (ci.hasNext()) {
int index = ci.next();
int op = ci.byteAt(index);
operations.add(Mnemonic.OPCODE[op]);
}
assertEquals(operations, Arrays.asList("aload_0", "invokespecial", "return"));
}
use of javassist.bytecode.MethodInfo in project hibernate-orm by hibernate.
the class BulkAccessorFactory method addDefaultConstructor.
/**
* Declares a constructor that takes no parameter.
*
* @param classfile The class descriptor
*
* @throws CannotCompileException Indicates trouble with the underlying Javassist calls
*/
private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException {
final ConstPool constPool = classfile.getConstPool();
final String constructorSignature = "()V";
final MethodInfo constructorMethodInfo = new MethodInfo(constPool, MethodInfo.nameInit, constructorSignature);
final Bytecode code = new Bytecode(constPool, 0, 1);
// aload_0
code.addAload(0);
// invokespecial
code.addInvokespecial(BulkAccessor.class.getName(), MethodInfo.nameInit, constructorSignature);
// return
code.addOpcode(Opcode.RETURN);
constructorMethodInfo.setCodeAttribute(code.toCodeAttribute());
constructorMethodInfo.setAccessFlags(AccessFlag.PUBLIC);
classfile.addMethod(constructorMethodInfo);
}
use of javassist.bytecode.MethodInfo in project reflections by ronmamo.
the class MethodParameterNamesScanner method scan.
@Override
public List<Map.Entry<String, String>> scan(ClassFile classFile) {
List<Map.Entry<String, String>> entries = new ArrayList<>();
for (MethodInfo method : classFile.getMethods()) {
String key = JavassistHelper.methodName(classFile, method);
String value = getString(method);
if (!value.isEmpty()) {
entries.add(entry(key, value));
}
}
return entries;
}
Aggregations