0

This may be a really stupid question or not even possible.

I am not sure if this would be called as a method, but going with this. How can I pass getCountryName(); as parameter? like show below;

public static String Utils(Something something){
    Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
    List<Address> address = null;

    if (geoCoder != null) {
        try {
            address = geoCoder.getFromLocation(lat, lng, 1);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        if (address.size() > 0) {
            return address.get(0).getCountryName(); // pass this as parameter (something)
        }
    }
    return null;
}

and then call it like so,

Utils(getCountryName());
Utils(getCountryCode());
etc.
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
SumOne
  • 817
  • 3
  • 13
  • 24

5 Answers5

2

This Answer helped me to do it. You can try it, too.

Methods aren't first-class objects in Java, so they can't be passed as parameters. You could use wrap your method call in an annoymous class that extends e.g. the Runnable interface.

Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
1

You can use reflection.

e.g.

Method[] methods = Address.class.getMethods();
for (Method method : methods){
    if(method.getName().equals(param)){
        return (String) method.invoke(address.get(0));
    }
}

param would be of type String with value of the method you want to call.

EDIT: Full example, using getMethod(String)

public static String Utils(String param){
    Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
    List<Address> address = null;

    if (geoCoder != null) {
        try { 
            address = geoCoder.getFromLocation(lat, lng, 1);
        } catch (IOException e1) {
            e1.printStackTrace();
        } 
        if (address.size() > 0) {
            Method method = Address.class.getMethod(param);
            if(method == null) return null;
            try{
                return (String) method.invoke(address.get(0));
            }catch(ReflectiveOperationException e){
                // Expected param maybe?
                return null;
            }
        } 
    } 
    return null; 
} 
Ryan
  • 1,863
  • 13
  • 20
  • do I type Method[] methods = Address.class.getMethods(); this as the parameter? – SumOne Jan 12 '17 at 11:21
  • No, your method signature would now look like `public static String Utils(String param)` – Ryan Jan 12 '17 at 11:22
  • I am getting an error on this: method.invoke(address.get(0)); saying unhandled exception java.lang.exception.illegalstateexception ... – SumOne Jan 12 '17 at 11:26
  • I assume you mean `IllegealAccessException`, that's just a throwable from the method invoke. Wrap it in a `try... catch` – Ryan Jan 12 '17 at 11:27
  • Check edit, you can catch `ReflectiveOperationException` as 2 of the throwable inherit from it. There's also an `IllegalArgumentException` you should probably handle too. – Ryan Jan 12 '17 at 11:29
  • Still getting an error on Address.class.getMethod(param); saying unhandled exception java.lang.noSuchMethodExpception – SumOne Jan 12 '17 at 11:33
  • Handle that exception then – Ryan Jan 12 '17 at 12:19
0

In java that is the language that you are finally using. You can't pass a method or function as a parameter to a function. If you are able to use it, from Java 8 you have lambda expressions that are a kind of "anonymous" function. Try with that.

http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

In this SO answer you have a very detailed guide. Java Pass Method as Parameter

In that thread there are more info with some patterns if you can't use java8

Community
  • 1
  • 1
acostela
  • 2,597
  • 3
  • 33
  • 50
  • Is there any other way to achieve this. I just want to be able to call one method but receive different values back. e.g. postcode, country name etc. – SumOne Jan 12 '17 at 11:20
  • that's another question. You should implement one method for each value type. Maybe it's not a correct design having a method whose return values sometimes is a thing with a signifiance and in the next call the same method return a value whose mean is completely different... – acostela Jan 12 '17 at 11:22
0

try this;

First define an Interface with the method you want to pass as a parameter

public interface Callable {
  public void getCountryName();
  public void getCountryCode();
}

Implement a class with the method

class Method_passing implements Callable {
  public void getCountryName() {
     //do your task
  }

  public void getCountryCode() {
     //do your task
  }
}

Invoke like that

Callable cmd = new Method_passing();
//This allows you to pass cmd as parameter and invoke the method call defined in the interface

public invoke( Callable callable ) {
  cmd.getCountryName(); //return name etc..
  cmd.getCountryCode();
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
0

You could implement a getUtil function inside your adress class and then use identifiers to execute a function of your choice.

public class Address {
public static final int GET_COUNTRY_NAME = 1;
public static final int GET_COUNTRY_CODE = 2;

public String getUtil(int code){
    switch(code){
    case GET_COUNTRY_NAME: return this.getCountryName();
    case GET_COUNTRY_CODE: return this.getCountryCode();
    default: return null;
    }
}

private String getCountryName(){
    //do something
    return "";
}

private String getCountryCode(){
    //do something
    return "";
}}

and then define your function like

public static String Utils(int something){
Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
List<Address> address = null;

if (geoCoder != null) {
    try {
        address = geoCoder.getFromLocation(lat, lng, 1);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    if (address.size() > 0) {
        return address.get(0).getUtil(something);
    }
}
return null; }}

which would be called using

Utils(Address.GET_COUNTRY_NAME);
Utils(Address.GET_COUNTRY_CODE);
Robin
  • 1
  • 1