0

Is there any data structure (or similar) in Java, where it is possible to set values for variables in an object like this:

myInstanceVariable.set("myVariableName").value("200")

I found out that something similar is available by using reflection or maps, but not with the above syntax.

Is there such a data structure or mechanism, or do I have to implement it by myself?

Magnilex
  • 11,584
  • 9
  • 62
  • 84
pichlbaer
  • 923
  • 1
  • 10
  • 18

4 Answers4

1

This sounds like a good case where you can use a Map, probably a HashMap.

See the Map documentation here: https://docs.oracle.com/javase/8/docs/api/java/util/Map.html

See this question here for information on various collections:

What Java Collection should I use?

A map doesn't have quite the syntax you gave above but it's close. myMap.put("myVariableName", "200"); If it was a Map<String, Integer> you could also do myMap.put("myVariableName", 200) although then every value would need to be an Integer.

Community
  • 1
  • 1
Tim B
  • 40,716
  • 16
  • 83
  • 128
  • Thanks for the answer. But I'm looking for something with the syntax from above. I know how Maps are working...sounds like I have to Code my own. – pichlbaer Apr 07 '16 at 14:00
  • Why do you want the syntax above? It's bad syntax (for a number of reasons). You could code your own around a map but you'd need to return an object from the set call and then have the value call on that object do the `put` into the map. This is going to involve a lot of object overhead – Tim B Apr 07 '16 at 14:11
  • Perhaps it would be better to say what you are trying to achieve and then look at how to achieve it? – Tim B Apr 07 '16 at 14:11
  • You've convinced me. I'll stick to a "normal" Map. Thank you – pichlbaer Apr 07 '16 at 14:21
1

You can use a map:

Map<String, String> data = new HashMap();
data.put("myVariableName", "200");

String value = data.get("myVariableName");
System.out.println(value); //shows 200

Or, on your own using reflection:

private class Attribute {

    public Object target;
    public String varName;
    public Class clazz;

    public Attribute(String varName, Object target, Class clazz) {
        this.varName = varName;
        this.target = target;
        this.clazz = clazz;
    }

    public void value(String value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //Call setter method
        Method method = target.getClass().getMethod("set" + varName, clazz);
        method.invoke(target, value);
    }

}

Example:

public class Person {

    private String name;

    public String getName() {
      return name;
    }

    //This method must exist
    public void setName(String name) {
      this.name = name;
    }

    public void set(String varName) throws Exception {
      return new Attribute(varName, this, Person.class);
    }

}

And the main:

public static void main(String[] args) throws Exception {
    Person p = new Person();
    p.set("Name").value("John"); //Notice the first letter uppercase
}

I don't recommend to use this solution, but it is what you are looking for, so... I recommend you to use a wrapped Map.

Héctor
  • 24,444
  • 35
  • 132
  • 243
0

If you want precisely the syntax you've provided then no, there isn't. You may create your own data structure however. It will wrap a Map most probably.

ACV
  • 9,964
  • 5
  • 76
  • 81
0

To start with, using reflection is often not the best solution. Perhaps you should think about what you really want to achieve and think about if it is another way to do it?

Having said that, I think the closest you can get to the fluent reflection you are refering to is jOOR, which is a lightweight third party fluent reflection API for Java.

In your case, you could set the variable using reflection the following way (provided that you have statically imported the necessary methods):

on(myInstanceVariable).set("myVariableName", 200);
Magnilex
  • 11,584
  • 9
  • 62
  • 84