Description
Sets a new value of a static field.
License
Open Source License
Parameter
Parameter | Description |
---|
clazz | the class thats static field value has to be set |
fieldName | the name of the static field |
fieldValue | the new value to be set |
Exception
Parameter | Description |
---|
SecurityException | an exception |
NoSuchFieldException | an exception |
IllegalArgumentException | an exception |
IllegalAccessException | an exception |
Declaration
public static void setFieldValue(final Class<? extends Object> clazz, final String fieldName,
final Object fieldValue)
throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
Method Source Code
//package com.java2s;
/*******************************************************************************
* Copyright (c) 2012 SAP AG and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:/*from w ww . j av a 2 s .c om*/
* SAP AG - initial API and implementation
*******************************************************************************/
import java.lang.reflect.Field;
public class Main {
/**
* Sets a new value of an instance field. This method doesn't only considers fields declared in the passed instance but also those declared in the
* whole super-hierarchy of the instance.
*
* @param instance
* the object thats field value has to be set
* @param fieldName
* the name of the instance variable
* @param fieldValue
* the new value to be set
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void setFieldValue(final Object instance, final String fieldName, final Object fieldValue)
throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
assert instance != null && fieldValue != null && fieldName != null;
final Field field = getField(instance.getClass(), fieldName);
field.setAccessible(true);
field.set(instance, fieldValue);
}
/**
* Sets a new value of a static field. This method doesn't only considers fields declared in the passed instance but also those declared in the
* whole super-hierarchy of the instance.
*
* @param clazz
* the class thats static field value has to be set
* @param fieldName
* the name of the static field
* @param fieldValue
* the new value to be set
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void setFieldValue(final Class<? extends Object> clazz, final String fieldName,
final Object fieldValue)
throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
assert clazz != null && fieldName != null;
final Field field = getField(clazz, fieldName);
field.setAccessible(true);
field.set(null, fieldValue);
}
private static Field getField(final Class<?> instanceClass, final String fieldName)
throws NoSuchFieldException {
assert fieldName != null;
try {
return instanceClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
final Class<?> superClass = instanceClass.getSuperclass();
if (superClass != null) {
return getField(superClass, fieldName);
}
throw e;
}
}
}
Related
- setFieldValue(Field field, Object object, Object value)
- setFieldValue(Field field, Object object, Object value)
- setFieldValue(Field field, Object target, Object value)
- setFieldValue(Field field, Object value, Object instance)
- setFieldValue(Field field, Object value, Object target)
- setFieldValue(final Field field, final Object instance, final Object value)
- setFieldValue(final Field field, final Object obj, final Object value)
- setFieldValue(final Field field, final Object value, final Object object)
- setFieldValue(final Object bean, final Field field, final Object value)