0
votes

My goal is to create in runtime an additional method inside a specific .class file.

A method that returns an Object[][].

For that I found an amazing framework called - Javassist, a bytecode modifier framework, which helps you modify your compiled class in runtime in order to add more bytecode that represents a new method.

Managed to create a void method, and a method that returns a string but, for some reason , I'm unable to generate a method that returns an array or a matrix..

So far iv'e been struggling to find the proper way of creating such method, and got a continuous CannotCompileException.

Code:

private static CtMethod generateMethod1(CtClass declaringClass)
            throws CannotCompileException {

        StringBuffer sb = new StringBuffer();
        sb.append("public ").append(Object[][].class.getName()).append(" ").append("method1").append("(){")
        .append("return new").append(Object[][].class.getName()).append("{{ 1,2 }}").append("; }");

        System.out.println(sb.toString());
        return CtMethod.make(sb.toString(), declaringClass);
    }

The toString of the generated method above is:

public [[Ljava.lang.Object; method1(){return [[Ljava.lang.Object;{{ 1,2 }}; }

Probably fails due to false jni syntax

Help would be appreciated, thanks in advanced

~Mont

1

1 Answers

0
votes

Well, solved it by just replacing the Object[][].class.getName() with Object[][] literally... hope this helps others in the future.