List of usage examples for java.lang.reflect Field set
@CallerSensitive @ForceInline public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException
From source file:Main.java
public static void copyBean(Object from, Object to) { Class<?> beanClass = from.getClass(); Field[] fields = beanClass.getFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; field.setAccessible(true);// www. ja v a2 s . c o m try { Object value = field.get(from); field.set(to, value); } catch (Exception e) { } } }
From source file:Main.java
/** * /*from w ww .j a v a2 s. co m*/ * @param node * @param classname * @return */ public static Object decode(Element node, String classname) { try { Class classObject = Class.forName(classname); Object object = classObject.newInstance(); NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node child = attributes.item(i); String nodeName = child.getNodeName(); Field field = classObject.getField(nodeName); field.setAccessible(true); field.set(object, child.getNodeValue()); } return object; } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } catch (InstantiationException e) { e.printStackTrace(); return null; } catch (IllegalAccessException e) { e.printStackTrace(); return null; } catch (SecurityException e) { e.printStackTrace(); return null; } catch (NoSuchFieldException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static void setVar(@NonNull Object obj, @NonNull String name, @Nullable Object val) throws Exception { for (Class cls = obj.getClass(); // cls != null && cls != Object.class; // cls = cls.getSuperclass()) { for (Field f : cls.getDeclaredFields()) { if (f.getName().equals(name)) { f.setAccessible(true);//from ww w .ja v a 2s. co m f.set(obj, val); return; } } } throw new RuntimeException("no var matching " + name); }
From source file:org.grails.orm.hibernate.cfg.GrailsIdentifierGeneratorFactory.java
public static void applyNewInstance(Configuration cfg) throws IllegalArgumentException, IllegalAccessException { Field field = ReflectionUtils.findField(Configuration.class, "identifierGeneratorFactory"); field.setAccessible(true);/*from w ww . j av a 2 s . co m*/ field.set(cfg, new GrailsIdentifierGeneratorFactory()); }
From source file:com.sun.faces.application.ResetUniqueRequestIdBean.java
public static void setPrivateField(String fieldName, Class containingClass, Object target, Object value) { try {/* w w w. j av a2 s . co m*/ Field field = containingClass.getDeclaredField(fieldName); field.setAccessible(true); field.set(target, value); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
private static void setField(Object paramObject1, Class<?> paramClass, String paramString, Object paramObject2) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException { Field localField = paramClass.getDeclaredField(paramString); localField.setAccessible(true);//w w w .ja v a 2s . c om localField.set(paramObject1, paramObject2); }
From source file:Main.java
public static void quitLoop() { try {/*from w ww.j ava 2s .c om*/ Field mQuitAllowed = MessageQueue.class.getDeclaredField("mQuitAllowed"); mQuitAllowed.setAccessible(true); mQuitAllowed.set(Looper.myQueue(), true); Looper.myLooper().quit(); Field mQuiting = MessageQueue.class.getDeclaredField("mQuiting"); mQuiting.setAccessible(true); mQuiting.set(Looper.myQueue(), false); mQuitAllowed.set(Looper.myQueue(), false); } catch (Exception e) { throw new IllegalStateException( "Sofia modal runnables are not " + "supported on this version of the Android API because the " + "MessageQueue.mQuitAllowed field could not be found or " + "there was a problem changing it."); } }
From source file:com.eclecticlogic.pedal.dm.internal.MetamodelUtil.java
/** * @param attribute JPA metamodel attribute. * @param entity Entity to set the value on. * @param value Value to set.//from w w w .j a v a 2 s . c o m */ public static <E extends Serializable, T extends Serializable> void set(Attribute<? super E, T> attribute, E entity, T value) { Member member = attribute.getJavaMember(); if (member instanceof Field) { Field field = (Field) member; field.setAccessible(true); try { field.set(entity, value); } catch (Exception e) { throw new RuntimeException(e); } } else if (member instanceof Method) { PropertyDescriptor pd = BeanUtils.findPropertyForMethod((Method) member); if (pd.getWriteMethod() != null) { pd.getWriteMethod().setAccessible(true); try { pd.getWriteMethod().invoke(entity, value); } catch (Exception e) { throw new RuntimeException(e); } } else { throw new RuntimeException( "No setter for " + attribute.getName() + " in " + entity.getClass().getName()); } } else { throw new RuntimeException("Failed to set " + attribute.getName() + " of type " + member.getClass().getName() + " in entity " + entity.getClass().getName()); } }
From source file:Main.java
public static void setField(Field field, Object target, Object value) { if (!Modifier.isPublic(field.getModifiers())) { field.setAccessible(true);//w ww.j av a 2s .c om } try { field.set(target, value); } catch (IllegalAccessException iae) { throw new IllegalArgumentException("Could not set field " + field, iae); } }
From source file:com.aw.support.reflection.AttributeAccessor.java
/** * @param target//from w w w. j a v a 2 s. c o m * @return */ public static void set(Object target, Field field, Object value) { field.setAccessible(true); try { field.set(target, value); } catch (IllegalAccessException e) { e.printStackTrace(); throw new IllegalArgumentException("Error setting the value of the attribute:<" + field.getName() + "> of object:<" + target + "> check: Attribute Type:<" + field.getType() + "> value Type: <" + value.getClass() + ">"); } }