I'm trying to call a class method from a java agent, but I keep getting a IllegalArgumentException error.
This is my code:
Class cls = Class.forName("class");
Method method = cls.getDeclaredMethod("stringMethod", String.class);
method.invoke(cls, "example string");
And this is the error I get:
java.lang.IllegalArgumentException: object is not an instance of declaring class
I'm able to get the class and the method, but invoking the method causes the error. Does anyone know what's causing this or how I can fix it?
Related
I am writing a custom debugger for JVM using the JVMTI interface's APIs and i need to get the class name in which the method is declared, i get the method id of the method using error = (*jvmti)->GetMethodName(jvmti,frames[i].method,&methodName,NULL,NULL); and i am trying to get the class name using error = (*jvmti)->GetMethodDeclaringClass(jvmti,frames[i].method,&declaring_class_ptr);
this returns the jclass reference in the declaring_class_ptr, my question is how to convert this to a string which gives the class name?
To get the class name you need to use GetClassSignature, something like:
char* name;
jvmti->GetClassSignature(class, &name, NULL);
trace(jvmti, "Class prepared: %s", fix_class_name(name));
jvmti->Deallocate((unsigned char*)name);
I've seen a lot of examples, and I know what has been discussed.
I do everything right, but I receive an error. Why is that? What am i doing wrong?
Class superClass = rootObject.getSuperclass();
Method addErrorMethod = superClass.getDeclaredMethod("addErrorMessage", ErrorType.class, String.class, String.class, String.class);
_log.info(addErrorMethod.getName());
addErrorMethod.invoke(superClass, ErrorType.FIELD, propertyName, message, "");
I get method, but when you call the invoker. I get the following error.
java.lang.IllegalArgumentException: object is not an instance of declaring class
Thanks.
When you call Method.invoke the first parameter must be either:
when method is non-static instance of the class which contains the method
when method is static null or class itself.
Since you pass the class itself and you got error it suggests that method you are trying to invoke is not static, so you should invoke it like
addErrorMethod.invoke(rootObject, ErrorType.FIELD, propertyName, message, "");
// ^^^^^^^^^^- assuming it is instance on which we want to invoke this method
You did not do everything right:
addErrorMethod.invoke(superClass, ErrorType.FIELD, propertyName, message, "");
should read
addErrorMethod.invoke(rootObject, ErrorType.FIELD, propertyName, message, "");
superClass is an instance of Class, to which has no addErrorMessage() method, as the error message is telling you. The first parameter to the method is a reference to the object that will be used as this within the method.
I have following code line:
when(htmlEmailSpy.setFrom(anyString())).thenReturn(null);
Following code executes real htmlEmailSpy.setFrom(...) but it throws exception.
#Spy
HtmlEmail htmlEmailSpy = new HtmlEmail();
What do I wrong?
my aim - set new behaviour to spy object.
I have resolved problem:
doReturn(null).when(htmlEmailSpy).setFrom(anyString());
took from: Mockito: Trying to spy on method is calling the original method
I want to access a JAVA function from CFML script:
<cfscript>
authToken = createobject("java","coldfusion.security.SecurityManager").createAuthToken('admin', 'cfadmin', '12345', false);
</cfscript>
<cfoutput>authToken: #authToken#</cfoutput>
I got the following error:
Object instantiation exception.
An exception occurred while instantiating a Java object. The class
must not be an interface or an abstract class. If the class has a
constructor that accepts an argument, you must call the constructor
explicitly using the init(args) method. Error :
coldfusion.security.SecurityManager
I know is because the arguments passed directly to createAuthToken that I can't do and I should init() it. How can I correctly pass the arguments in this scenario?
It pretty much tells you what you need to do in the error message. But for the sake of clarity:
<cfset authToken = createobject("java","coldfusion.security.SecurityManager")
.init() // you might need some init() args here?
.createAuthToken('admin', 'cfadmin', '12345', false)
>
I'm not really sure why you bother to put that sinple statement into a <cfscript> block, btw. Would not a <cfset> do?
I'm wanting to use Java reflection to call a method on a class of mine that has the following signature:
public Object execute(Object...params)
In my loader class, I have the class loaded, but I'm not sure how to setup my getMethod call. Currently, I have something like this:
Method classEntry = _loadedClass.getMethod("execute", new Class[]{Object[].class});
I then try to invoke this method after creating a newInstance of my class by calling:
Object classObj = _loadedClass.newInstance();
classEntry.invoke(classObj, params); // params comes in from the method as Object...params
This is giving me a java.lang.NoSuchMethodException Exception. I know that my issue lies in my getMethod call. How should I set that up to accept a params object?
If params is of type Object [] then you need to call invoke like this:
classEntry.invoke(classObj, new Object [] {params});
But this does not explain NoSuchMethodException