Description
Set a field
License
Open Source License
Parameter
Parameter | Description |
---|
field | the field |
target | the target |
value | the value |
Exception
Parameter | Description |
---|
Throwable | for any error |
Return
null
Declaration
public static Object setField(Field field, Object target, Object value) throws Throwable
Method Source Code
//package com.java2s;
/*/*from ww w .j a va 2s . co m*/
* JBoss, Home of Professional Open Source
* Copyright 2006, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
public class Main {
/**
* Set a field
*
* @param field the field
* @param target the target
* @param value the value
* @return null
* @throws Throwable for any error
*/
public static Object setField(Field field, Object target, Object value) throws Throwable {
if (field == null)
throw new IllegalArgumentException("Null field");
try {
field.set(target, value);
return null;
} catch (Throwable t) {
throw handleErrors("set", field, target, value, t);
}
}
/**
* Handle errors
*
* @param context the context
* @param target the target
* @param parameters the parameters
* @param arguments the arguments
* @param t the error
* @return never
* @throws Throwable always
*/
public static Throwable handleErrors(String context, Object target, Class<?>[] parameters, Object[] arguments,
Throwable t) throws Throwable {
if (t instanceof IllegalArgumentException) {
if (target == null)
throw new IllegalArgumentException("Null target for " + context);
List<String> expected = new ArrayList<String>();
if (parameters != null) {
for (Class<?> parameter : parameters)
expected.add(parameter.getName());
}
List<String> actual = new ArrayList<String>();
if (arguments != null) {
for (Object argument : arguments) {
if (argument == null)
actual.add(null);
else
actual.add(argument.getClass().getName());
}
}
throw new IllegalArgumentException("Wrong arguments. " + context + " for target " + target
+ " expected=" + expected + " actual=" + actual);
} else if (t instanceof InvocationTargetException) {
throw ((InvocationTargetException) t).getTargetException();
}
throw t;
}
/**
* Handle errors
*
* @param context the context
* @param field the field
* @param target the target
* @param value the value
* @param t the error
* @return never
* @throws Throwable always
*/
public static Throwable handleErrors(String context, Field field, Object target, Object value, Throwable t)
throws Throwable {
if (t instanceof IllegalArgumentException) {
Class<?> clazz = field.getDeclaringClass();
if (clazz.isInstance(target) == false)
throw new IllegalArgumentException(
"Wrong target. " + target.getClass().getName() + " is not a " + clazz.getName());
String valueType = null;
if (value != null)
valueType = value.getClass().getName();
throw new IllegalArgumentException(
"Error invoking field " + context + " for target " + target + " field " + field.getName()
+ " expected=" + field.getType().getName() + " actual=" + valueType);
}
throw t;
}
}
Related
- setField(Field field, Object classWithField, Object value)
- setField(Field field, Object instance, Object value)
- setField(Field field, Object obj, String value)
- setField(Field field, Object objToSet, Object value)
- setField(Field field, Object target, Object value)
- setField(Field field, Object target, Object value)
- setField(Field field, Object target, Object value)
- setField(Field field, Object target, Object value)
- setField(Field field, Object target, Object value)