0

I need to instantiate a function passed as a string, and i wrote a wrapper for a "Function" object that let me execute the function after it's defined.

The wrapper uses the JavaAssist library, using object and method as fields of the wrapper object and a method that let me use the function outside the wrapper class.

My troubles are about the function body syntax, i have a lot of java.lang.VerifyError: (class: MyClass, method: myMethod signature: (*my signature, unimportant *) Expecting to find *a primitive type* on stack

related to casts on the fly, for example

 Double c = (double) intVariable;

which I solved (not really good solution, but it is working) avoiding those casting and always instantiating variables of the defined type

 Double c = new Double( (String) intVariable.toString() );

Now I have another problem, and it's driving me crazy: this is the content of the string

Double t2 =  new Double ( (String) parametri.get("pigreco").toString() );
Double t3 = new Double ( (String) (new Integer(2)).toString() );";
Double mysum = t2;
mysum+= t3;
return my sum;

t2.getClass() and t3.getClass() return java.lang.Double but mysum is the concatenation of t2 and t3, not the arithmetic sum... how come is it possible?

alessiop86
  • 1,285
  • 2
  • 19
  • 38
  • 1
    Hi! Instead of new Integer(x).toString() i usually use Integer.toString(x). Same result but without instantiation. – dbalakirev Jul 13 '12 at 09:20
  • Nice, I save an instantiation but the Double+Double = String issue still remains – alessiop86 Jul 13 '12 at 09:24
  • Are you sure mysum is a Double? You would get that behaviour if mysum was of type String. – david Jul 13 '12 at 09:26
  • mysum is a String, but I don't know why, I declare it as Double mysum = t2; – alessiop86 Jul 13 '12 at 09:29
  • This makes no sense, you must have done something wrong defining your function with JavaAssist. From your first error message, it looks like the intVariable is not an int after all, so probably you have something wrong before that. – Flavio Jul 13 '12 at 09:57
  • @flavio the function is the one defined before (obviously inside a String bodyFunction with escaped "). (Double c eccetera.. ). I use it with myClass.addMethod(CtNewMethod.make("public Object execute(ResultSet rs) { " + bodyFunction + " }", myClass)); – alessiop86 Jul 13 '12 at 10:13
  • I still think either you found a bug in the Javassist compiler, or you have some error in your body. Can you post it entirely? – Flavio Jul 13 '12 at 10:56

1 Answers1

0

For now I have resolved by providing a

public static Double sommaDouble(Double a, Double b) {
    return a+b;
}

in another class. Im looking for a better solution, and I'd like to understand why the "+" is executed as the concatenate operator by Javaassist's method

alessiop86
  • 1,285
  • 2
  • 19
  • 38