I have a method which I am trying to call dynamically. That method has an argument 'stringValue'. The method I am trying to invoke is in the parent (receivers??) class.
I can't figure out how to get the reference back to the parent
java.lang.reflect.Method method;
method = Class.forName("com.blah.MyActivity").getMethod("myFunction", String.class);
method.invoke(this,stringValue);
Gives the error:
'Message expected receiver of type com.blah.MyActivity, but got com.blah.MyActivity$SubTask'
I suspect you want:
method.invoke(MyActivity.this, stringValue);
The MyActivity.this part is the way of getting to the enclosing instance of MyActivity.
See section 15.8.4 of the JLS for more details.
Related
I have a Optional.
Optional<AssetEvent> latestEvent = ...
I want to a add a field from inside the Event to a List if Present.
latestEvent.map(event -> event.getTimestamp()).ifPresent(latestList::add);
My IDE (Intelij) shows me "Lambda can be replaced with method reference" on event.getTimestamp()
refactoring to
latestEvent.map(this::getTimestamp).ifPresent(latestList::add);
gives me an Error because this will refer to the surrounding Object in what the code is executed. How can I refer to the method of the Object inside the Optional latestEvent
Use the class name. For example:
latestEvent.map(AssetEvent::getTimestamp).ifPresent(latestList::add);
Assuming AssetEvent is the name of the class having the getTimestamp method.
In the following code if retString is a method name , then how do I pass a parameter to that method if it has one?
You need a Method object to invoke methods. And then use the invoke() method of the Method class to well, invoke the method you intend to run. And there you pass parameters, like:
Object o = m.invoke(t, new Locale(args[1], args[2], args[3]));
( that is a snippet from the official documentation )
I want to do something different with java reflection. The program i written to add global listeners to java components when applets are opened from browser. An event fired and i get event source object. Here i don't know the actual class name that object referring to.
if(object.getClass.getName().contains("oracle.ewt.laf.basic.BasicTabBarUI$Menu"))
{
// here we can invoke methods,fields,etc using reflection
}
I can call the methods of BasicTabBarUI$Menu class with reflection.
Suppose now i have the following lines with me in the above if block
LWMenuItem menuItem = (LWMenuItem)object;
menuItem.getLabel());
I don't want to specify LWMenuItem class name , instead i want to call its method getLabel(). If we know the class name , we can do as above. But how can we do same with reflection. How can we do casting in reflection?
You don't need to do casting, except of the result of calling the method. Just use the object's Class object, which has a getMethod method that will return a Method object for the method you want, then invoke it:
Class cls = object.getClass();
Method getLabel = cls.getMethod("getLabel", null);
String label = (String)getLabel.invoke(object, null);
You can continue working with the basic object when using the return value from getLabel():
Method getLabelMethod = object.getClass().getMethod("getLabel");
Object menuItem = getLabelMethod.invoke(object);
menuItem.getClass().getMethod("getName").invoke(menuItem); // or whatever...
SubArray = Key.Ikey;
Ikey being the variable which holds the name of the method.
I tried:
SubArray = Key.(this.IKey);
but the compiler gave the error identifier excpected.
I think you are interested in reflection. It goes something like:
method = object.getClass().getMethod(methodName, param1.class, param2.class, ..);
So you are calling the method specified by the variable 'methodName' of the object 'object'
You can read more here Reflection
and in this tutorial link
I'm telling mock object to wait for method with command:
mockObject.registerSQLDriver(isA(SomeName.class));
At runtime method is called exactly with instance of SomeName class, but the test fails telling that "Unexpected method call registerSQLDriver()"
What can cause this problem?
Here is the code:
resetToDefault(_SQLDriverManager);
_SQLDriverManager.registerSQLDriver(isA(SQLDriver.class));
expectLastCall().anyTimes();
replay(_SQLDriverManager);
Probably, you are mocking one instance and testing another instance.
To check this you should add a name parameter in the mock creation:
Comparable<String> mock01 = EasyMock.createMock("M1", Comparable.class);
Comparable<String> mock02 = EasyMock.createMock("M2", Comparable.class);
EasyMock.expect(mock01.compareTo(EasyMock.isA(String.class))).andReturn(1);
EasyMock.replay(mock01, mock02);
mock02.compareTo("Test");
EasyMock.verify(mock01, mock02);
In this case the test will fail with the message:
Unexpected method call M2.compareTo("Test")
instead of:
Unexpected method call Comparable.compareTo("Test")