Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over member variables in Java

I have several situations where a class consists of an arbitrary (but fixed) number of variables of differing types and wish to iterate over those variables as well as use their names in code. Is there a smarter way to do this than to retype each variable name in each function?

My initial thought is to use a HashMap to store the local variables but that seems inefficient and doesn't handle multiple types.

Code below is simulated to shorten the required reading, but typically its more than 3 variables:

class Widget implements Parcelable {
    String name, code;
    Long price;

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(code);
        dest.writeLong(price);
    }

    public Widget(Parcel o) {
        name = o.name;
        code = o.code;
        price = o.price;
    }

    public String getXML() {
        return "<Widget><name>"+name+"</name><code>"+code
              +"</code><price>"+price+"</price></Widget>";
    }
}

Instead, I'd prefer to do something like (pseudo-code):

public Widget(Parcel o) {
    for (Map<Name,Data> item: o.getMap()) {
        this.setValue(Name, Data);
    }
}
public String getXML() {
    StringBuilder XML = new StringBuilder();
    XML.append("<Widget>");
    for (Map<Name,Data> item: o.getMap()) {
        XML.append("<"+Name+">" + Data + "</"+Name+">");
    }
    XML.append("</Widget>");
    return XML.toString();
}

I keep thinking there must be a pattern for doing this type of thing, but maybe its just my Python history kicking at my Java experience.

Update: this is not about solely fetching XML from member variables, but about looping through member variables in multiple member functions. It would appear that Java's reflection features were missing from my repertoire.

My new smaller constructor makes me glad to have learned something useful, and looks like:

public Widget(Widget other) {
    for (Field f : getClass().getDeclaredFields()) {
        if (f.getName() == "TAG") {
            continue;
        }
        f.set(this, f.get(other));
    }
}
like image 489
mikebabcock Avatar asked Nov 29 '12 20:11

mikebabcock


3 Answers

That is usually solved with reflection:

for (Field f : getClass().getDeclaredFields()) {
    String name = f.getName();
    String value = f.get(this);
}

However, for writing to / reading from XML, you probably want to use JAXB rather than reinventing the wheel.

like image 168
meriton Avatar answered Sep 28 '22 10:09

meriton


Use Jackson:

https://github.com/FasterXML/jackson-dataformat-xml

Serialization is done very similar to JSON serialization: all that needs to change is ObjectMapper instance to use:

// Important: create XmlMapper; it will use proper factories, workarounds
ObjectMapper xmlMapper = new XmlMapper();
String xml = xmlMapper.writeValue(new Simple());

and with POJO like:

public class Simple {
    public int x = 1;
    public int y = 2;
}

you would get something like:

<Simple>
  <x>1</x>
  <y>2</y>
</Simple>

(except that by default output is not indented: you can enabled indentation using standard Jackson mechanisms)

Deserializing POJOs from XML Similar to serialization, deserialization is not very different from JSON deserialization:

ObjectMapper xmlMapper = new XmlMapper();
Simple value = xmlMapper
   .readValue("<Simple><x>1</x><y>2</y></Simple>", Simple.class);

P.S. You can also use reflection for that and iterate via all declared fields using Class.getDeclaredFields(). But from my experience one rarely has to do that. There is a lot of 3rd party open-source libraries for different purposes and usually it's a better solution just to use them (e.g. Jackson in this particular case)

like image 30
Eugene Retunsky Avatar answered Sep 28 '22 10:09

Eugene Retunsky


If there are a number of classes whose code follows the same pattern, I would write a code generator to generate the Java source code from a definition file of some kind. Then you can have your generator create simple code like your first example, that is easier to read and understand (than using a generic map or reflection or something). The definition file becomes your "official" source of the fields of each kind of object.

Since you're already familiar with Python, your definition file could simply be built right in to a Python script to create the source code. For example:

Objects = {
    "Widget": [
        (TYPE_STRING, "name"),
        (TYPE_STRING, "code"),
        (TYPE_LONG, "price"),
    ],
    # ...
}

With appropriate definitions of TYPE_STRING and so on, it should be straightforward to write some code that generates Java source from this.

like image 23
Greg Hewgill Avatar answered Sep 28 '22 10:09

Greg Hewgill