Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return object based on input string name in java

Tags:

java

I have a requirement wherein I've to write a method which accepts "string" and based on this string i need to return an object of type MyObject. This can be done with using switch case, but how could this be achieved dynamically.

In below case method can be called by giving "myObject1" as string, then this method should return myObject1 object. How could this be done.

public class HelloWorld {

    MyObject myObject1 = new MyObject();
    MyObject myObject2 = new MyObject();
    MyObject myObject3 = new MyObject();


public MyObject getMyObject(String string)
{
    return <<myObject1 or 2 or 3 based on string parameter name>>;
}

}


class MyObject {

}
like image 393
user3393249 Avatar asked Oct 01 '22 09:10

user3393249


2 Answers

This can be done dynamically via reflection, but it would be highly impractical and unnecessary. You should either use a switch or a Map to associate your string identifiers with your actual objects.

Map<String, MyObject> identifiers = new HashMap<>();

...
identifiers.put("myObject1", myObject1);
identifiers.put("myObject2", myObject2);
identifiers.put("myObject3", myObject3);
...

public MyObject getMyObject(String string) {
    return identifiers.get(string);
}
like image 162
arshajii Avatar answered Oct 07 '22 18:10

arshajii


If you really want to do things like this reflection is your friend. You can look up declared fields by name, and then use them to look up an instance variable.

I've modified your example to include a main method that looks up each instance of MyObject and also has a failure case. I've also modified MyObject so you can easily tell which instance has been found.

import java.lang.reflect.Field;

public class Reflection {

    MyObject myObject1 = new MyObject("1");
    MyObject myObject2 = new MyObject("2");
    MyObject myObject3 = new MyObject("3");

    public MyObject getMyObject(final String string) {
        try {
            final Field declaredField = this.getClass()
                    .getDeclaredField(string);
            final Object o = declaredField.get(this);
            if (o instanceof MyObject) {
                return (MyObject) o;
            }

        } catch (final Exception e) {
        }
        return null;
    }

    class MyObject {
        final String name;

        public MyObject(final String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return name;
        }

    }

    public static void main(final String[] args) {
        final Reflection r = new Reflection();
        System.out.println(r.getMyObject("myObject1"));
        System.out.println(r.getMyObject("myObject2"));
        System.out.println(r.getMyObject("myObject3"));
        System.out.println(r.getMyObject("invalid"));
    }

}

There is some useful information about reflection in the Oracle Java documentation.

like image 23
Richard Miskin Avatar answered Oct 07 '22 17:10

Richard Miskin