Javassist call PolymorphicSignature method - java

I am trying to call MethodHandle.invokeExact in a generated Javassist method, but I always get Exception in thread "main" javassist.CannotCompileException: [source error] invokeExact(int) not found in java.lang.invoke.MethodHandle
This seems to be because invokeExact is a #PolymorphicSignature method.
How can I call it from a Javassist method?

Related

NoSuchMethod exception observed when I tried to access private method from third party jar

I have some private method inside a jar file due to some restriction of visibility to outer class. I just added that jar file through maven dependency. And I want to call that private method and return the value after invoke. but when I tried to execute below code throws NoSuchMethod Exception.
Code:
Method method = BankFlowTestExecutor.class.getDeclaredMethod("executeBatchCommand", String.class);
method.setAccessible(true);
The exception observed as soon as its executed the first line in the above code.
Exception:
java.lang.NoSuchMethodException:
com.paypal.test.BankFlowTestExecutor.executeBatchCommand(java.lang.String) at
java.lang.Class.getDeclaredMethod(Class.java:2130)
Also, I used Method[] to get all the methods in the class but it returns only public method not private methods which is in the class.
Any leads?

Why jni not loading after changing package name

Hey guys I need some help. The problem is my jni is not loading after changing package name. Before it was working pretty cool. I have tried to solve it but I can't. So please help me.
This is the error:
JNI DETECTED ERROR IN APPLICATION: JNI NewGlobalRef called with pending exception 'java.lang.ClassNotFoundExceptio
in call to NewGlobalRef
from java.lang.String java.lang.Runtime.nativeLoad(java.lang.String, java.lang.ClassLoader, java.lang.String)
JNI DETECTED ERROR IN APPLICATION: JNI NewGlobalRef called with
pending exception 'java.lang.ClassNotFoundException' in call to
NewGlobalRef from java.lang.String
java.lang.Runtime.nativeLoad(java.lang.String, java.lang.ClassLoader,
java.lang.String)
You had a pending exception when you called NewGlobalRef. When an exception occurs when executing JNI code, your app doesn't crash, but a "pending exception" is created. It is your responsibility to check for exceptions when doing JNI work which might throw an exception.
After a pending exceptions is created, only a handful of JNI methods are safe to call( mentioned here).
In your case, your app crashed because before you called NewGlobalRef after a pending exception was thrown. NewGlobalRef is not in the list of methods safe to call after an exception. However, you also see the cause of the pending exception: java.lang.ClassNotFoundException.
Most likely this happened because you tried to do something similar:
cls = (*env)->FindClass(env, "com/example/ndktest/SomeClass");
but then you changed the package name of SomeClass from com.example.ndktest.SomeClass to com.other.package.SomeClass. However, you probably didn't also change how you searched for the class..so you need to also update your FindClass(...) call to:
cls = (*env)->FindClass(env, "com/other/package/SomeClass");
Hope this helps

Javassist java.lang.NoClassDefFoundError

I want to instrument a library method to call one of my Class present in javassist agent.
Lets say I have a class:
Class A {
void display() {
System.out.println("Inside A.dipslay()");
}
}
then when I invoke:
method.insertAfter("packageName.A.display()");
it throws the exception:
Exception in thread "main" java.lang.NoClassDefFoundError: packageName/A
at java.net.HttpURLConnection.setRequestMethod(libraryClass)
at sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(libraryClass)
at com.avekshaa.app.SecurityFilter.sendGet(Class where i call the Library Class)
at com.avekshaa.app.SecurityFilter.main(Class where i call the Library Class)
And when I call it just by name:
method.insertAfter("A.display()");
then it gives a No such class found Exception.
I solved the issue by adding the jars path in the javassist manifest file.
Reffer - this

Cloning an object in java: org.objenesis.ObjenesisException: java.lang.NoSuchMethodException: newInstance [class java.lang.Class, int]

I am using Espresso to test my application.
I am also using the library uk.com.robust-it:cloning (Java Deep Cloning Library) version 1.9.2 to clone an object.
If I am using the application, I have no exception, but when the tests are running, I have the following exception:
org.objenesis.ObjenesisException: java.lang.NoSuchMethodException: newInstance [class java.lang.Class, int]
at org.objenesis.instantiator.android.AndroidInstantiator.getNewInstanceMethod(AndroidInstantiator.java:62)
at org.objenesis.instantiator.android.AndroidInstantiator.<init>(AndroidInstantiator.java:38)
at org.objenesis.strategy.StdInstantiatorStrategy.newInstantiatorOf(StdInstantiatorStrategy.java:75)
at org.objenesis.ObjenesisBase.getInstantiatorOf(ObjenesisBase.java:90)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
at com.rits.cloning.ObjenesisInstantiationStrategy.newInstance(ObjenesisInstantiationStrategy.java:18)
at com.rits.cloning.Cloner.newInstance(Cloner.java:271)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:436)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.deepClone(Cloner.java:301)
Does someone also have this exception?
This guy has the same problem that I ...
https://groups.google.com/forum/#!topic/objenesis-dev/jdqWCwy5E78

Throwing Java exception in JRuby and catching it in Java

I have created my own UI component in Java. It has model and a few of model's methods can throw my exception called ModelException. I want to use this component in JRuby but I can't raise my ModelException:
raise ModelException # it cause TypeError: exception class/object expected
So I tried to create method throwing ModelException in Java and then invoke it in JRuby:
public class ScriptUtils {
private ScriptUtils() {
}
public static void throwModelException(ModelException e)
throws ModelException {
throw e;
}
}
but when I call throwModelException from JRuby I get:
org.jruby.exceptions.RaiseException: Native Exception: 'class app.ui.ModelException'; Message:
; StackTrace: app.ui.ModelException
...
Caused by: app.ui.ModelException
this native exception cannot be handled by Java code.
Any ideas how to throw Java exception in JRuby and catch it in Java?
This is a complete re-write of my original answer as I originally mis-read your question!
You can raise Java exceptions and catch them in JRuby but you need to be a bit careful how you call raise:
raise ModelException
Will cause a type error (as you saw) because to JRuby ModelException looks like a plain constant. Remember that in Ruby class names are constants. You can raise direct subclasses of Ruby Exception like this, e.g.:
raise RuntimeError
But I think such subclasses are a special case. Those that are actually Java classes you need to call with a constructor:
raise ModelException.new
Or whatever constructor/s you have for that class. An instance of ModelException, in JRuby, is a subclass of Exception because JRuby creates it as such, but the Java class itself isn't. All this is assuming that you have imported your ModelException class correctly.
As for your second example, I couldn't replicate that error at all. As long as I created the exception object correctly, as above, it worked and I didn't see any complaints about "Native Exception" at all. So I'm not sure what is happening there, sorry.

Categories