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:com.helpinput.utils.Utils.java
public static void setFieldValue(Object target, Field theField, Object value) { if (theField != null) { accessible(theField);/*w w w.j ava 2 s. co m*/ try { Object newValue = ConvertUtils.convert(value, theField.getType()); theField.set(target, newValue); } catch (Exception e) { e.printStackTrace(); logger.info(MessageFormat.format("convert error:{0},{1},{2}", target, theField, value)); } } }
From source file:com.evolveum.midpoint.prism.xjc.PrismForJAXBUtil.java
private static <T> T getPropertyValue(PrismProperty<?> property, Class<T> requestedType) { if (property == null) { return null; }//w w w . j a v a 2s. c om PrismPropertyValue<?> pvalue = property.getValue(); if (pvalue == null) { return null; } Object propertyRealValue = pvalue.getValue(); if (propertyRealValue instanceof Element) { if (requestedType.isAssignableFrom(Element.class)) { return (T) propertyRealValue; } Field anyField = getAnyField(requestedType); if (anyField == null) { throw new IllegalArgumentException("Attempt to read raw property " + property + " while the requested class (" + requestedType + ") does not have 'any' field"); } anyField.setAccessible(true); Collection<?> anyElementList = property.getRealValues(); T requestedTypeInstance; try { requestedTypeInstance = requestedType.newInstance(); anyField.set(requestedTypeInstance, anyElementList); } catch (InstantiationException e) { throw new IllegalArgumentException("Instantiate error while reading raw property " + property + ", requested class (" + requestedType + "):" + e.getMessage(), e); } catch (IllegalAccessException e) { throw new IllegalArgumentException( "Illegal access error while reading raw property " + property + ", requested class (" + requestedType + ")" + ", field " + anyField + ": " + e.getMessage(), e); } return requestedTypeInstance; } return JaxbTypeConverter.mapPropertyRealValueToJaxb(propertyRealValue); }
From source file:com.helpinput.core.Utils.java
public static void setFieldValue(Object target, Field theField, Object value) { if (theField != null) { setAccess(theField);//from w w w . j a v a 2 s. co m try { Object newValue = ConvertUtils.convert(value, theField.getType()); theField.set(target, newValue); } catch (Exception e) { e.printStackTrace(); logger.info(MessageFormat.format("convert error:{0},{1},{2}", target, theField, value)); } } }
From source file:com.github.dactiv.common.utils.ReflectionUtils.java
/** * , private/protected, ??setter.// www . ja v a2 s . com * * @param target * Object * @param fieldName * ?? * @param value * */ public static void setFieldValue(final Object target, final String fieldName, final Object value) { Assert.notNull(target, "target?"); Assert.notNull(fieldName, "fieldName?"); Field field = getAccessibleField(target, fieldName); if (field == null) { throw new IllegalArgumentException( "? [" + fieldName + "] [" + target + "] "); } try { field.set(target, value); } catch (IllegalAccessException e) { logger.error("??:{}", e.getMessage()); } }
From source file:net.ceos.project.poi.annotated.core.CsvHandler.java
/** * Apply a String value to the object./*from ww w .j a v a 2s . c o m*/ * * @param o * the object * @param field * the field * @param values * the array with the content at one line * @param idx * the index of the field * @throws ConverterException * the conversion exception type */ protected static void stringReader(final Object o, final Field field, final String[] values, final int idx) throws ConverterException { try { field.set(o, values[idx]); } catch (IllegalArgumentException | IllegalAccessException e) { throw new ConverterException(ExceptionMessage.CONVERTER_STRING.getMessage(), e); } }
From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java
/** * https://stackoverflow.com/questions/3301635/change-private-static-final-field-using-java-reflection * @param field// www. java2 s . c o m * @param newValue * @throws SecurityException * @throws NoSuchFieldException * @throws Exception */ private static void setFinalField(Object target, Field field, Object newValue) throws NoSuchFieldException { field.setAccessible(true); Field modifiersField = null; try { modifiersField = Field.class.getDeclaredField("modifiers"); } catch (SecurityException e1) { e1.printStackTrace(); } modifiersField.setAccessible(true); try { modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); } catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } try { field.set(target, newValue); } catch (IllegalAccessException e) { e.printStackTrace(); } }
From source file:Main.java
public static void setField(Object object, String fieldName, Object fieldValue) { Class<?> objectClass = object.getClass(); if (objectClass != null) { try {/* w ww .ja v a2s . c om*/ Field field = objectClass.getDeclaredField(fieldName); field.setAccessible(true); Type type = field.getGenericType(); //This is bad, but dear God I can't figure out how to convert a runtime Type to its actual type through reflection. I know how in C#.... if (type.toString().toLowerCase().contains("short")) { fieldValue = Short.parseShort((String) fieldValue); } else if (type.toString().toLowerCase().contains("integer")) { fieldValue = Integer.parseInt((String) fieldValue); } else if (type.toString().toLowerCase().contains("long")) { fieldValue = Long.parseLong((String) fieldValue); } else if (type.toString().toLowerCase().contains("boolean")) { fieldValue = "1".equals(fieldValue); //Java, you're the worst language. And SQLite isn't helping. } field.set(object, fieldValue); } catch (NoSuchFieldException e) { return; } catch (Exception e) { throw new IllegalStateException(e); } } }
From source file:net.java.sip.communicator.impl.certificate.CertificateServiceImpl.java
/** * Appends an index number to the alias of each entry in the KeyStore. * /*w w w .j a va 2 s .com*/ * The Windows TrustStore might contain multiple entries with the same * "Friendly Name", which is directly used as the "Alias" for the KeyStore. * As all operations of the KeyStore operate with these non-unique names, * PKIX path building could fail and in the end lead to certificate warnings * for perfectly valid certificates. * * @throws Exception when the aliases could not be renamed. */ private static int keyStoreAppendIndex(KeyStore ks) throws Exception { Field keyStoreSpiField = ks.getClass().getDeclaredField("keyStoreSpi"); keyStoreSpiField.setAccessible(true); KeyStoreSpi keyStoreSpi = (KeyStoreSpi) keyStoreSpiField.get(ks); if ("sun.security.mscapi.KeyStore$ROOT".equals(keyStoreSpi.getClass().getName())) { Field entriesField = keyStoreSpi.getClass().getEnclosingClass().getDeclaredField("entries"); entriesField.setAccessible(true); Collection<?> entries = (Collection<?>) entriesField.get(keyStoreSpi); int i = 0; for (Object entry : entries) { Field aliasField = entry.getClass().getDeclaredField("alias"); aliasField.setAccessible(true); String alias = (String) aliasField.get(entry); aliasField.set(entry, alias.concat("_").concat(Integer.toString(i++))); } return i; } return -1; }
From source file:model.BaseModel.java
public boolean setAttribute(String key, String value) { try {//from ww w . jav a2s .com Field field = this.getClass().getDeclaredField(key); field.setAccessible(true); field.set(this, value); } catch (SecurityException | IllegalAccessException | NoSuchFieldException ex) { return false; } return true; }
From source file:cn.aposoft.util.spring.ReflectionUtils.java
/** * Given the source object and the destination, which must be the same class * or a subclass, copy all fields, including inherited fields. Designed to * work on objects with public no-arg constructors. *//* ww w . j a va 2 s . c om*/ public static void shallowCopyFieldState(final Object src, final Object dest) { if (src == null) { throw new IllegalArgumentException("Source for field copy cannot be null"); } if (dest == null) { throw new IllegalArgumentException("Destination for field copy cannot be null"); } if (!src.getClass().isAssignableFrom(dest.getClass())) { throw new IllegalArgumentException("Destination class [" + dest.getClass().getName() + "] must be same or subclass as source class [" + src.getClass().getName() + "]"); } doWithFields(src.getClass(), new FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { makeAccessible(field); Object srcValue = field.get(src); field.set(dest, srcValue); } }, COPYABLE_FIELDS); }