I have a Java class package.SStream where I have implemented the method getValue() which returns a JavaPairDStream<String, Integer>
I want to use this method in Scala's main method
I've imported the class package.SStream in Scala
and when I try to call the method like this in Scala
val x = SStream.getValue()
I get this error :
[error] value getValue is not a member of object package.SStream
That error sounds like your getValue() is not a static method.
Is it defined as a static method or an instance method in your package.SStream class?
Related
I am still learning lambdas in java. I have a lambda function like this.
static MyClass myFunc(ClassB user) {
return (info, cn) -> info.user.equals(user);
}
Now this is being called/used like this:
protected final MyClass myClassObject = MyClass.myFunc(userObject);
Where MyClass is an interface
I have two doubts here:
Since the lambda function requires info as well, where am I passing info object to it? I am definitely using it but not passing it anywhere. So, how is it using info
I want to print the value of info object inside lambda function using System.println. How can I do that?
I am a newbie in java functional interfaces so I want to see if this is possible and if not please explain me why not and what is possible in order to achieve my idea
I have these classes
public class A {
...
public String getInfo();
...
}
public class B {
...
public String getOtherInfo();
...
}
I want to pass the references to these functions to another object like this:
obj.init(A::getInfo)
obj.init(B::getOtherInfo)
so that later I can use/call these functions on different objects of type A or B inside the build functions:
obj.build(a1);
obj.build(a2);
...
obj.build(b1);
obj.build(b2);
PS1 I cannot use regular interfaces for this cause there are lot of getters and lots of classes similar to A which I want to use for this procedure and they are not related with one another
PS2 I try to avoid reflection cause you cannot trace the function calls
PS3 my example is not exactly working as is it throws this error: "non static method cannot be referenced from a static context"
A::get is a Java Method Reference. You should be able to store it for use later. As it's an instance method you'll need the instance object as well. So something like this might work:
Function<A,String> getFunction = A::get;
And whenever you need to use it you can do
//assuming you have an object instance of A which is a
getFunction.apply(a)
You can also pass it to other methods by declaring the method to take a functional parameter like this:
public void someOtherMethod(Function<A,String> param) {
//do whatever with param.
//invoke this with an instance of A when you're ready
param.apply(a);
}
Here's a reference that might help: https://www.baeldung.com/java-8-double-colon-operator
Made 2 mistakes
should have used Function<T, R> instead of Supplier
the error is thrown even if there is a slight mismatch of parameters even on the generic types. So an example of my function which accepts the Function parameter should be declared like this:
public <T extends Base> init (Function <T, String> f){
this.f = f;
}
and later I do something like:
public String build (A a){
return this.f.apply(a);
}
(so I had to make A, B implement some useless interface)
I have a class that, in essence, looks like this:
class Checkpointer {
public <Input,Output> Output runFunction(Input input, Function<Input,Output> function) {
Output output;
// Sometimes run the function, sometimes return an Output from a cache
return output
}
}
I would like to mock this class using Mockito doAnswer:
Checkpointer checkpointer; // mocked via #Mock annotation
Mockito
.doAnswer(/* ??? */)
.when(checkpointer)
.runFunction(Mockito.any(), Mockito.any());
The function I want to mock needs to be generic. Can this be done?
For example, my first attempt produced the following. Not only did I resort to Object as the type arguments for Function, but the compiler was still unhappy with unchecked casting:
Mockito.doAnswer((invocation) ->
{
// compiler is not happy with this cast V
Function<Object,Object> function = (Function<Object,Object>)invocation.getArguments()[1];
return function.apply(invocation.getArgument(0));
}).when(checkpointer).runFunction(Mockito.any(), Mockito.any());
If this can't be done, I think can try writing my own mock class extending the first and use Mockito.spy.
The problem here is that you insist on using getArguments, which returns an Object[]
Since you know the index of the Function argument, you can use getArgument(index), as you're doing the line after that.
final Function<String, String> argument = invocation.getArgument(1);
Is this what you're looking for? Type inference for the getArgument generic type is working fine.
If not, can you provide a more elaborate example?
This question is being asked everywhere on Google but I'm still having trouble with it. Here is what I'm trying to do. So like my title states, I'm getting an 'object is not an instance of declaring class' error. Any ideas? Thanks!
Main.java
Class<?> base = Class.forName("server.functions.TestFunction");
Method serverMethod = base.getMethod("execute", HashMap.class);
serverMethod.invoke(base, new HashMap<String, String>());
TestFunction.java
package server.functions;
import java.util.HashMap;
import java.util.Map;
import server.*;
public class TestFunction extends ServerBase {
public String execute(HashMap<String, String> params)
{
return "Test function successfully called";
}
}
You're invoking the method with the class, but you need an instance of it. Try this:
serverMethod.invoke(base.newInstance(), new HashMap<String, String>());
You are trying to invoke the execute method on the object base, which is actually a Class object returned by your Class.forName() call.
This would work for a static (class) method - but execute is a non-static (instance) method.
(It would also work for calling an instance method of an object of type Class - but that's not what you are trying to achieve here!)
You need an actual instance of TestFunction to invoke the method on, or you need to make the method static.
When invoking a static method by reflection, the first argument to invoke() is ignored, so it is conventional to set it to null, which clarifies the fact that there's no instance involved.
Although your current example method would do the same thing for any TestFunction object, in general an instance method could produce a different result for each object - so the .invoke() reflection method needs to know which object to run the method on.
I have created a hash table that will keep as a key a string that will represent the name of the method that the user will give, and as value the actual method call, as string too. The code I am using is the one here:
public void getMethod(String givenMethod){
Map<String, String> methods = new HashMap<String, String>();
methods.put("length", "length();");
methods.get(givenMethod);
}
From the main method I call objectX.getMethod("length");, but the method length(); is not executed. Can someone help me please?
You are getting the method but you are not invoking it. You'll have to do something like this:
Method yourMethod = objectX.getClass().getDeclaredMethod("yourMethodName"); //This is the string that represents the name of the method.
Then you invoke the method. All this through reflection:
yourMethod.invoke(YourObject);
The parameter of the invoke method is first the object, and then the attributes.
You can also get the return type of the method and cast the result, since the invoking the method will result in an Object type method:
yourMethod.getReturnType(); //This line gives you the type returned by your method.
Use Java **reflection to call method by its name (as you said you are storing method name in map).
For more detail read following article :
http://java.sun.com/developer/technicalArticles/ALT/Reflection/
You need to use reflection to call a method by name. So your data structure would look more like
Map<String, Method> meth = new Hashmap<String,Method>();
Where Method is an actual object.