Java examples for Reflection:Setter
Invoke the setter method for the given source and target object.
//package com.java2s; import java.lang.reflect.Method; public class Main { /**// www.j ava 2 s . c om * Invoke the setter method for the given source and target object. * * @param setterMethod the setter method to invoke * @param source the source object to invoke the setter method on * @param target the target object to set * @param property the setter method property name (used for logging) * @param path the full expression path (used for logging) */ public final static void invokeSetter(Method setterMethod, Object source, Object target, String property, String path) { try { Object[] objectArgs = { target }; setterMethod.invoke(source, objectArgs); } catch (Exception e) { // Log detailed error message of why setter failed StringBuilder buffer = new StringBuilder(); buffer.append("Result: error occurred while trying to set an"); buffer.append(" instance of '"); buffer.append(target.getClass().getName()).append( "' using method '"); buffer.append(setterMethod.getName()).append("("); buffer.append(target.getClass()); buffer.append(")' of class '").append(source.getClass()) .append("'."); throw new RuntimeException(buffer.toString(), e); } } }