Loop Over All Fields in a Java Class

Loop over all fields in a Java class

Use getDeclaredFields on [Class]

ClasWithStuff myStuff = new ClassWithStuff();
Field[] fields = myStuff.getClass().getDeclaredFields();
for(Field f : fields){
Class t = f.getType();
Object v = f.get(myStuff);
if(t == boolean.class && Boolean.FALSE.equals(v))
// found default value
else if(t.isPrimitive() && ((Number) v).doubleValue() == 0)
// found default value
else if(!t.isPrimitive() && v == null)
// found default value
}

(http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html)

How to loop over a Class attributes in Java?

There is no linguistic support to do what you're asking for.

You can reflectively access the members of a type at run-time using reflection (e.g. with Class.getDeclaredFields() to get an array of Field), but depending on what you're trying to do, this may not be the best solution.

See also

  • Java Tutorials: Reflection API / Advanced Language Topics: Reflection

Related questions

  • What is reflection, and why is it useful?
  • Java Reflection: Why is it so bad?
  • How could Reflection not lead to code smells?
  • Dumping a java object’s properties


Example

Here's a simple example to show only some of what reflection is capable of doing.

import java.lang.reflect.*;

public class DumpFields {
public static void main(String[] args) {
inspect(String.class);
}
static <T> void inspect(Class<T> klazz) {
Field[] fields = klazz.getDeclaredFields();
System.out.printf("%d fields:%n", fields.length);
for (Field field : fields) {
System.out.printf("%s %s %s%n",
Modifier.toString(field.getModifiers()),
field.getType().getSimpleName(),
field.getName()
);
}
}
}

The above snippet uses reflection to inspect all the declared fields of class String; it produces the following output:

7 fields:
private final char[] value
private final int offset
private final int count
private int hash
private static final long serialVersionUID
private static final ObjectStreamField[] serialPersistentFields
public static final Comparator CASE_INSENSITIVE_ORDER


Effective Java 2nd Edition, Item 53: Prefer interfaces to reflection

These are excerpts from the book:

Given a Class object, you can obtain Constructor, Method, and Field instances representing the constructors, methods and fields of the class. [They] let you manipulate their underlying counterparts reflectively. This power, however, comes at a price:

  • You lose all the benefits of compile-time checking.
  • The code required to perform reflective access is clumsy and verbose.
  • Performance suffers.

As a rule, objects should not be accessed reflectively in normal applications at runtime.

There are a few sophisticated applications that require reflection. Examples include [...omitted on purpose...] If you have any doubts as to whether your application falls into one of these categories, it probably doesn't.

Java iterate over class fields to create other class

This is something you can do easily with reflection. In the example below, I renamed class Test to Property because it represents a key-value pair. If you are happy with using whatever toString() returns as the value for a field, then the solution is pretty simple:

public class Property {
private final String name;
private final String value;

public Property(String name, String value) {
this.name = name;
this.value = value;
}

public static List<Property> toProperties(Object object, String... fieldNames)
throws ReflectiveOperationException
{
ArrayList<Property> properties = new ArrayList<>();
for( String fieldName : fieldNames ) {
Field field = object.getClass().getDeclaredField(fieldName);
properties.add(new Property(fieldName, field.get(object).toString()));
}
return properties;
}

public String toString() {
return String.format("%s: \"%s\"", name, value);
}
}

This sample requires you to specify the names of the desired fields explicitly, for example:

List<Property> properties = Property.toProperties(myExample, "a", "b", "c");

If you'd rather have the fields be auto-detected based on some criterion (for example all primitive and String-typed fields, then you could add this logic within toProperties and get rid of the varargs.

How to loop over a Class attributes in Java - from a given list (NOT reflection getDeclaredFields())?

Your question is somewhat ambiguous about using reflection. If you are OK with reflection, but want specific fields only without iterating over getDeclaredFields(), then the following code should work for you:

for (String var : myStrings) {
Field field = MyClass.class.getDeclaredField(var);
field.setAccessible(true);
System.out.println(var);
System.out.println(field.get(myObject));
System.out.println();
}

Note that this code works for private fields, too. Also, keep in mind that you'll have to handle exception associated with the reflection calls.

UPDATE: Exceptions thrown in this code.

MyClass.class.getDeclaredField(var) declares a checked NoSuchFieldException. You must handle it because obviously there is no mechanism to make sure that the fields in myString match an actual implementation of MyClass.

field.get(myObject) throws a checked IllegalAccessException if the field is inaccessible. Which it should not be because of field.setAccessible(true), but you still have to catch or re-throw the exception.

There are also unchecked exceptions you may want to handle. See the javadoc for details

  • java.lang.Class.getDeclaredField(String)
  • java.lang.reflect.AccessibleObject.setAccessible(boolean) inherited by java.lang.reflect.Field
  • java.lang.reflect.Field.get(Object)

loop through field's values (or methods) of an object in java

The following example retrieves the field names of the concrete class (declaredField = fields declared by the class, (public)field = public fields declared by the class and it's super classes), derives a name of a corresponding getter from it (i.e. private String example -> getExample), searches for the method with the same name and invokes it on the instance m of Material

Material m = ...;
Stream.of(Material.class.getDeclaredFields())
.map(field -> "get" + ucFirst(field.getName()))
.map(getterName -> Material.class.getMethod(getterName))
.map(getterMethod -> getterMethod.invoke(m))
.forEach(fieldValue -> {
//do something
});

...
private String ucFirst(String input) {
if(input.length() <= 1){
return input.toUpperCase();
}
return input.substring(0, 1).toUpperCase() + input.substring(1);
}

(I removed the exception handling for better readability)

This is just one way to do it, there are many others. The same way you could access the toString method:

String toString = (String)Material.class.getMethod("toString").invoke(m);

Or you could get all the getter you want:

Stream.of(Material.class.getMethods())
.filter(method -> method.getName().startsWith("get"))
.map(getterMethod -> getterMethod.invoke(m))
.forEach(fieldValue -> {
//do something
});

Note that getMethods will retrieve only public methods, but from all the super classes as well, while getDeclaredMethods will retrieve only method from the current classes. This may be important to know when dealing with the subclasses.

Loop through an object and get the value from all its variables

You can use reflection to do this:

Field[] fields = games.getClass().getDeclaredFields();

for(Field field: fields) {
//do stuff
}

getDeclaredFields():

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields.

Iterate Fields of Fields continuously with reflection

I discovered that I was on the right path with the instance fields, but was missing part of the necessary information to invoke the method correctly at the end.

When iterating the fields I was only using the Class of each field, which was working perfectly fine before with the static class references since those weren't instances, but now it requires the instance of the field in order to work correctly.

In the end the iterating method used in place of classHasSubClass that got this to work is as follows:

private static Object getFieldClass(Class<?> currentClass, Object currentObject, final String fieldName) {

Field[] fieldList;

fieldList = currentClass.getDeclaredFields();

for (final Field field : fieldList) {
if (field.getName().toLowerCase().equals(fieldName)) {

try {
return field.get(currentObject);
} catch (IllegalAccessException e) {

e.printStackTrace();
break;

}

}
}

return null;

}

With this I always keep an instance object reference to the final field that I want to invoke to pass as the 1st parameter (someMethod.invoke(objectInstance);) instead of null.

How to loop through fields in a subclass using reflection

There is a getFields method that will return all public fields of this class and its superclass.

If you want all fields of this class and its superclass, just use getSuperClass() and call the same method.

As the comment by @Sotirios suggests, you can keep doing this all the way up the hierarchy.

Class<?> clazz = cls.getSuperclass();
while (clazz != null) {
// Print fields.
clazz = clazz.getSuperclass();
}

Iterate over object attributes in java

Class cls = Class.forName("Foo");
Field[] fields = cls.getDeclaredFields();

Should return all the declared fields for the class using reflection.
More info @ http://java.sun.com/developer/technicalArticles/ALT/Reflection/

How do I iterate over class members?

Yes, you do need reflection. It would go something like this:

public static void getObject(Object obj) {
for (Field field : obj.getClass().getDeclaredFields()) {
//field.setAccessible(true); // if you want to modify private fields
System.out.println(field.getName()
+ " - " + field.getType()
+ " - " + field.get(obj));
}
}

(As pointed out by ceving, the method should now be declared as void since it does not return anything, and as static since it does not use any instance variables or methods.)

See the reflection tutorial for more.



Related Topics



Leave a reply



Submit