2

I am trying to do this

First I have an interface like this:

public void doSomething (String input, MyClass myMethod);

and at the implementation I had

public void doSomething (String input, MyClass myMethod){
    myMethod.mySpecificMethod(input2);
}

So now I was checking the java.util.function class and I found out that there is a FunctionalInterface Function.

So far I just try to replace MyClass myMethod with this Function but Im sure there are better ways (also this doesnt work).

Any idea how to? When using generics later I cannot invoke my method, should I just cast the generic to the desired class?

jpganz18
  • 5,508
  • 17
  • 66
  • 115
  • The `java.util.function` package is really meant for streams and lambdas utilities, and it sounds like what you're trying to do is related to reflection. It would probably help if you posted the code for `MyClass`. – Mike Oct 24 '18 at 13:32
  • possible you should do this https://stackoverflow.com/questions/6765024/generic-type-as-parameter-in-java-method – Asad Ali Oct 24 '18 at 13:49

1 Answers1

3

Based on your example, it looks like doSomething requires a method that accepts a String and returns nothing. This fits the Consumer<String> interface:

public void doSomething (String input, Consumer<String> consumer){
    consumer.accept(input);
}

And you call it with:

doSomething ("some string", myMethod::mySpecificMethod);

where myMethod is an instance of MyClass.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Im marking this as valid answer but I just realized I gave wrong information, in the implementation is input2 the one that is processed, any idea how to deal with these cases? – jpganz18 Oct 24 '18 at 14:04
  • @jpganz18 what's `input2`? I don't see `input2` anywhere in your question. – Eran Oct 24 '18 at 14:06
  • I didnt updated it before because this might change the answer, please take a look – jpganz18 Oct 24 '18 at 14:07
  • @jpganz18 where is input2 coming from? You are not passing it to the doSomething method. – Eran Oct 24 '18 at 14:09
  • I am using a DDD, so this method is sort of a validation of a previously created object, input2 will be one field of that DTO – jpganz18 Oct 24 '18 at 14:12
  • so later is myObject.validate(input1, functionToValidate) – jpganz18 Oct 24 '18 at 14:12
  • @jpganz18 I didn't understand your explanation, but you can pass whatever String you want to `consumer.accept();` – Eran Oct 24 '18 at 14:14
  • but consumer.accept(); is called at the interface, at the interface the field is not present – jpganz18 Oct 24 '18 at 14:16
  • lets supposed I have a static field on the implementation class I want to pass as parameter of my function – jpganz18 Oct 24 '18 at 14:16
  • @jpganz18 Isn't the implementation of `doSomething` located in the implementation class? Either way, if `doSomething` has no direct access to the parameter you want to use, just pass it when you call it : `doSomething (input2, myMethod::mySpecificMethod);` – Eran Oct 24 '18 at 14:33
  • is something like this . public implementationClass implements myInterface{ private static final String TEXT = "abcdef"; @override public void doSomething(String input, MyClass myMethod){ myMethod.evaluate(input + TEXT); } } – jpganz18 Oct 24 '18 at 14:37
  • @jpganz18 How about: `public class implementationClass implements myInterface{ private static final String TEXT = "abcdef"; @override public void doSomething(String input, Consumer consumer) { consumer.accept(input + TEXT); } }` ? – Eran Oct 24 '18 at 15:03