Thursday, September 1, 2022

Reflection in Java - Array

In this post you'll see how you can manipulate an array using Reflection API in Java. Array in Java is also a class so many of the methods in java.lang.Class may be used with the array.

Reflection in Java also provides a specific class java.lang.reflect.Array for arrays. In this post we'll see how you can get information about Array using Reflection API.

How to identify array using reflection

If you want to determine if a class member is a field of array type that can be done by invoking Class.isArray() method.

For example let’s say we have a class TestClass which has one array field numArray. In another class ReflectArray using the class.isArray() method it is determined if there is any array in the TestClass.

TestClass

public class TestClass {
 private int value;
 private int[] numArray;
 public int getValue() {
  return value;
 }
 public void setValue(int value) {
  this.value = value;
 }
 public int[] getNumArray() {
  return numArray;
 }
 public void setNumArray(int[] numArray) {
  this.numArray = numArray;
 }
}

ReflectArray

import java.lang.reflect.Field;

public class ReflectArray {
  public static void main(String[] args) {
    try {
      Class<?> c = Class.forName("org.netjs.prog.TestClass");
      Field[] fields = c.getDeclaredFields();
      for (Field f : fields) {
        Class<?> type = f.getType();
        // Looking for array
        if(type.isArray()){
          System.out.println("Array found " + f.getName());
        }
      }            
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

Output

Array found numArray

Creating new array using Java reflection

Using reflection API in Java you can dynamically create arrays of arbitrary type and dimensions using java.lang.reflect.Array.newInstance() method.

int[] testArray = (int[])Array.newInstance(int.class, 5);
System.out.println("length of array " + testArray.length);

Output

length of array 5

Getting and setting array using reflection

Using Java reflection API you can get or set an entire array. You can set an entire array using Field.set(Object obj, Object value) method. To retrieve an entire array use Field.get(Object obj) method.

If you want to get or set an array individual element by element, Array class has methods for that also. In order to set or get an int array you can use setInt(Object array, int index, int i) and getInt(Object array, int index) methods. Same way if you have a float array you can use setFloat(Object array, int index, float f) and getFloat(Object array, int index) methods.

There is also a method which takes Object for value as parameter that can be used with any type- set(Object array, int index, Object value) and get(Object array, int index) methods.

int[] testArray = (int[])Array.newInstance(int.class, 5);
System.out.println("length of array " + testArray.length);
// Setting values using setInt and set methods
Array.setInt(testArray, 0, 1);
Array.set(testArray, 1, 2);
Array.setInt(testArray, 2, 3);

// Getting values using getInt and get methods
System.out.println("First element " + Array.get(testArray, 0));
System.out.println("Second element " + Array.getInt(testArray, 1));
System.out.println("Third element " + Array.getInt(testArray, 2));

Output

length of array 5
First element 1
Second element 2
Third element 3

That's all for this topic Reflection in Java - Array. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. Reflection in Java - Getting Field Information
  2. Reflection in Java - Getting Method Information
  3. Reflection in Java - Getting Constructor Information
  4. Generating Getters And Setters Using Reflection in Java
  5. Invoking Getters And Setters Using Reflection in Java

You may also like-

  1. How to Pass Command Line Arguments in Eclipse
  2. Nested class and Inner class in Java
  3. BigDecimal in Java With Examples
  4. SerialVersionUID And Versioning in Java Serialization
  5. Type Erasure in Java Generics
  6. AutoBoxing and UnBoxing in Java
  7. Is String Thread Safe in Java
  8. CopyOnWriteArrayList in Java With Examples