Java reflect array type

Identifying Array Types

Array types may be identified by invoking Class.isArray() . To obtain a Class use one of the methods described in Retrieving Class Objects section of this trail.

The ArrayFind example identifies the fields in the named class that are of array type and reports the component type for each of them.

import java.lang.reflect.Field; import java.lang.reflect.Type; import static java.lang.System.out; public class ArrayFind < public static void main(String. args) < boolean found = false; try < Classcls = Class.forName(args[0]); Field[] flds = cls.getDeclaredFields(); for (Field f : flds) < Classc = f.getType(); if (c.isArray()) < found = true; out.format("%s%n" + " Field: %s%n" + " Type: %s%n" + " Component Type: %s%n", f, f.getName(), c, c.getComponentType()); >> if (!found) < out.format("No array fields%n"); >// production code should handle this exception more gracefully > catch (ClassNotFoundException x) < x.printStackTrace(); >> >

The syntax for the returned value of Class.get*Type() is described in Class.getName() . The number of ‘ [ ‘ characters at the beginning of the type name indicates the number of dimensions (that is, depth of nesting) of the array.

Samples of the output follows. User input is in italics. An array of primitive type byte :

$java ArrayFind java.nio.ByteBuffer final byte[] java.nio.ByteBuffer.hb Field: hb Type: class [B Component Type: byte

An array of reference type StackTraceElement :

$ java ArrayFind java.lang.Throwable private java.lang.StackTraceElement[] java.lang.Throwable.stackTrace Field: stackTrace Type: class [Ljava.lang.StackTraceElement; Component Type: class java.lang.StackTraceElement

predefined is a one-dimensional array of reference type java.awt.Cursor and cursorProperties is a two-dimensional array of reference type String :

$ java ArrayFind java.awt.Cursor protected static java.awt.Cursor[] java.awt.Cursor.predefined Field: predefined Type: class [Ljava.awt.Cursor; Component Type: class java.awt.Cursor static final java.lang.String[][] java.awt.Cursor.cursorProperties Field: cursorProperties Type: class [[Ljava.lang.String; Component Type: class [Ljava.lang.String;

Источник

Читайте также:  Python выйти из вложенных циклов

Class Array

Array permits widening conversions to occur during a get or set operation, but throws an IllegalArgumentException if a narrowing conversion would occur.

Method Summary

Sets the value of the indexed component of the specified array object to the specified boolean value.

Methods declared in class java.lang.Object

Method Details

newInstance

Creates a new array with the specified component type and length. Invoking this method is equivalent to creating an array as follows:

int[] x = ; Array.newInstance(componentType, x);

newInstance

Creates a new array with the specified component type and dimensions. If componentType represents a non-array class or interface, the new array has dimensions.length dimensions and componentType as its component type. If componentType represents an array class, the number of dimensions of the new array is equal to the sum of dimensions.length and the number of dimensions of componentType . In this case, the component type of the new array is the component type of componentType . The number of dimensions of the new array must not exceed 255.

getLength

get

Returns the value of the indexed component in the specified array object. The value is automatically wrapped in an object if it has a primitive type.

getBoolean

getByte

getChar

getShort

getInt

getLong

getFloat

getDouble

set

Sets the value of the indexed component of the specified array object to the specified new value. The new value is first automatically unwrapped if the array has a primitive component type.

setBoolean

public static void setBoolean (Object array, int index, boolean z) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

Sets the value of the indexed component of the specified array object to the specified boolean value.

setByte

setChar

setShort

setInt

setLong

setFloat

setDouble

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Java reflect array type

The Array class provides static methods to dynamically create and access Java arrays. Array permits widening conversions to occur during a get or set operation, but throws an IllegalArgumentException if a narrowing conversion would occur.

Method Summary

Sets the value of the indexed component of the specified array object to the specified boolean value.

Methods inherited from class java.lang.Object

Method Detail

newInstance

public static Object newInstance(Class componentType, int length) throws NegativeArraySizeException

Creates a new array with the specified component type and length. Invoking this method is equivalent to creating an array as follows:

int[] x = ; Array.newInstance(componentType, x);

newInstance

public static Object newInstance(Class componentType, int. dimensions) throws IllegalArgumentException, NegativeArraySizeException

Creates a new array with the specified component type and dimensions. If componentType represents a non-array class or interface, the new array has dimensions.length dimensions and componentType as its component type. If componentType represents an array class, the number of dimensions of the new array is equal to the sum of dimensions.length and the number of dimensions of componentType . In this case, the component type of the new array is the component type of componentType . The number of dimensions of the new array must not exceed 255.

getLength

public static int getLength(Object array) throws IllegalArgumentException

get

public static Object get(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

Returns the value of the indexed component in the specified array object. The value is automatically wrapped in an object if it has a primitive type.

getBoolean

public static boolean getBoolean(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

getByte

public static byte getByte(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

getChar

public static char getChar(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

getShort

public static short getShort(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

getInt

public static int getInt(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

getLong

public static long getLong(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

getFloat

public static float getFloat(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

getDouble

public static double getDouble(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

set

public static void set(Object array, int index, Object value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

Sets the value of the indexed component of the specified array object to the specified new value. The new value is first automatically unwrapped if the array has a primitive component type.

setBoolean

public static void setBoolean(Object array, int index, boolean z) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

Sets the value of the indexed component of the specified array object to the specified boolean value.

setByte

public static void setByte(Object array, int index, byte b) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

setChar

public static void setChar(Object array, int index, char c) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

setShort

public static void setShort(Object array, int index, short s) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

setInt

public static void setInt(Object array, int index, int i) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

setLong

public static void setLong(Object array, int index, long l) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

setFloat

public static void setFloat(Object array, int index, float f) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

setDouble

public static void setDouble(Object array, int index, double d) throws IllegalArgumentException, ArrayIndexOutOfBoundsException

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Arrays

An array is an object of reference type which contains a fixed number of components of the same type; the length of an array is immutable. Creating an instance of an array requires knowledge of the length and component type. Each component may be a primitive type (such as byte , int , or double ), a reference type (such as String , Object , or java.nio.CharBuffer ), or an array. Multi-dimensional arrays are really just arrays which contain components of array type.

Arrays are implemented in the Java virtual machine. The only methods on arrays are those inherited from Object . The length of an array is not part of its type; arrays have a length field which is accessible via java.lang.reflect.Array.getLength() .

Reflection provides methods for accessing array types and array component types, creating new arrays, and retrieving and setting array component values. The following sections include examples of common operations on arrays:

  • Identifying Array Types describes how to determine if a class member is a field of array type
  • Creating New Arrays illustrates how to create new instances of arrays with simple and complex component types
  • Getting and Setting Arrays and Their Components shows how to access fields of type array and individually access array elements
  • Troubleshooting covers common errors and programming misconceptions

All of these operations are supported via static methods in java.lang.reflect.Array .

Источник

Lesson: Arrays and Enumerated Types

From the Java virtual machine’s perspective, arrays and enumerated types (or enums) are just classes. Many of the methods in Class may be used on them. Reflection provides a few specific APIs for arrays and enums. This lesson uses a series of code samples to describe how to distinguish each of these objects from other classes and operate on them. Various errors are also be examined.

Arrays

Arrays have a component type and a length (which is not part of the type). Arrays may be manipulated either in their entirety or component by component. Reflection provides the java.lang.reflect.Array class for the latter purpose.

  • Identifying Array Types describes how to determine if a class member is a field of array type
  • Creating New Arrays illustrates how to create new instances of arrays with simple and complex component types
  • Getting and Setting Arrays and Their Components shows how to access fields of type array and individually access array elements
  • Troubleshooting covers common errors and programming misconceptions

Enumerated Types

Enums are treated very much like ordinary classes in reflection code. Class.isEnum() tells whether a Class represents and enum . Class.getEnumConstants() retrieves the enum constants defined in an enum. java.lang.reflect.Field.isEnumConstant() indicates whether a field is an enumerated type.

  • Examining Enums illustrates how to retrieve an enum’s constants and any other fields, constructors, and methods
  • Getting and Setting Fields with Enum Types shows how to set and get fields with an enum constant value
  • Troubleshooting describes common errors associated with enums

Источник

Оцените статью