Search in sources :

Example 51 with MethodInfo

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;
}
Also used : ConstPool(javassist.bytecode.ConstPool) ClassFile(javassist.bytecode.ClassFile) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ArrayList(java.util.ArrayList) MethodInfo(javassist.bytecode.MethodInfo) CtMethod(javassist.CtMethod) Annotation(javassist.bytecode.annotation.Annotation)

Example 52 with MethodInfo

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"));
}
Also used : ClassFile(javassist.bytecode.ClassFile) CodeIterator(javassist.bytecode.CodeIterator) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) MethodInfo(javassist.bytecode.MethodInfo) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 53 with MethodInfo

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);
}
Also used : ConstPool(javassist.bytecode.ConstPool) MethodInfo(javassist.bytecode.MethodInfo) Bytecode(javassist.bytecode.Bytecode)

Example 54 with MethodInfo

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;
}
Also used : ArrayList(java.util.ArrayList) MethodInfo(javassist.bytecode.MethodInfo)

Aggregations

MethodInfo (javassist.bytecode.MethodInfo)54 Bytecode (javassist.bytecode.Bytecode)28 BadBytecode (javassist.bytecode.BadBytecode)19 CodeIterator (javassist.bytecode.CodeIterator)18 CodeAttribute (javassist.bytecode.CodeAttribute)17 ConstPool (javassist.bytecode.ConstPool)17 ClassFile (javassist.bytecode.ClassFile)12 LocalVariableAttribute (javassist.bytecode.LocalVariableAttribute)10 DuplicateMemberException (javassist.bytecode.DuplicateMemberException)9 List (java.util.List)7 ClassPool (javassist.ClassPool)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DataOutputStream (java.io.DataOutputStream)6 IOException (java.io.IOException)6 CtMethod (javassist.CtMethod)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 Set (java.util.Set)5 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)5 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4