List of usage examples for java.lang.reflect Field isAccessible
@Deprecated(since = "9") public boolean isAccessible()
From source file:com.austin.base.commons.util.ReflectUtil.java
/** * @param object//from w w w. j a va 2 s . c o m * @param propertyName * @param newValue * @throws NoSuchFieldException */ public static void forceSetProperty(Object object, String propertyName, Object newValue) throws NoSuchFieldException { Assert.notNull(object); Assert.hasText(propertyName); Field field = getDeclaredField(object, propertyName); boolean accessible = field.isAccessible(); field.setAccessible(true); try { field.set(object, newValue); } catch (IllegalAccessException e) { log.info("Error won't happen"); } field.setAccessible(accessible); }
From source file:it.sample.parser.util.CommonsUtil.java
/** * Metodo di utilita' che copia i campi non nulli della classe sorgente in quelli della classe di destinazione * /* www. j a va 2s . c o m*/ * @param source * @param destination */ public static <K, T> void copyNotNullProperties(K source, T destination) { PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(source); if (descriptors != null) { for (PropertyDescriptor descriptor : descriptors) { try { String propertyName = descriptor.getName(); Field field = getDeclaredField(propertyName, source.getClass()); if (field != null && field.getAnnotation(IgnoreField.class) == null) { boolean wasAccessible = field.isAccessible(); field.setAccessible(true); if (PropertyUtils.getReadMethod(descriptor) != null) { Object val = PropertyUtils.getSimpleProperty(source, propertyName); if (val != null && descriptor.getWriteMethod() != null) { PropertyUtils.setProperty(destination, propertyName, val); } } field.setAccessible(wasAccessible); } } catch (Exception e) { e.printStackTrace(); } } } }
From source file:io.github.benas.randombeans.util.ReflectionUtils.java
/** * Set a value (accessible or not accessible) in a field of a target object. * * @param object instance to set the property on * @param field field to set the property on * @param value value to set//from w w w . ja va 2 s . c om * @throws IllegalAccessException if the property cannot be set */ public static void setProperty(final Object object, final Field field, final Object value) throws IllegalAccessException { boolean access = field.isAccessible(); field.setAccessible(true); field.set(object, value); field.setAccessible(access); }
From source file:com.austin.base.commons.util.ReflectUtil.java
/** * @param object//from www . j a va2 s. c o m * @param propertyName * @return * @throws NoSuchFieldException */ public static Object forceGetProperty(Object object, String propertyName) throws NoSuchFieldException { Assert.notNull(object); Assert.hasText(propertyName); Field field = getDeclaredField(object, propertyName); boolean accessible = field.isAccessible(); field.setAccessible(true); Object result = null; try { result = field.get(object); } catch (IllegalAccessException e) { log.info("error wont' happen"); } field.setAccessible(accessible); return result; }
From source file:$.Reflections.java
/** * ?private/protected???public?????JDKSecurityManager *///from w ww .j ava 2s.c om public static void makeAccessible(Field field) { if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) { field.setAccessible(true); } }
From source file:ambroafb.general.AnnotiationUtils.java
private static Object[] getNodesTypeAndContent(Field field, Object ownerClassObject) { Object[] results = new Object[2]; try {//from ww w. j a v a 2 s . c om boolean accessible = field.isAccessible(); field.setAccessible(true); if (field.getType().equals(TextField.class) || field.getType().getSuperclass().equals(NumberField.class)) { TextField textField = (TextField) field.get(ownerClassObject); results[0] = textField; results[1] = textField.getText(); } else if (field.getType().equals(ADatePicker.class)) { ADatePicker datePicker = (ADatePicker) field.get(ownerClassObject); results[0] = datePicker; results[1] = datePicker.getEditor().getText(); } else if (field.getType().equals(MapEditor.class)) { MapEditor mapEditor = (MapEditor) field.get(ownerClassObject); results[0] = mapEditor; results[1] = mapEditor.getEditor().getText(); } else if (field.getType().equals(ImageGalleryController.class)) { ImageGalleryController gallery = (ImageGalleryController) field.get(ownerClassObject); results[0] = gallery.getRoot(); results[1] = (gallery.isEmpty()) ? null : "not empty"; } else if (field.getType().equals(CountComboBox.class)) { CountComboBox countComboBox = (CountComboBox) field.get(ownerClassObject); results[0] = countComboBox; results[1] = (countComboBox.getBasket().isEmpty()) ? null : countComboBox.getValue(); } // Note: ClientComboBox is not ComboBox extened, so this case specific case: else if (field.getType().equals(ClientComboBox.class)) { ClientComboBox clientComboBox = (ClientComboBox) field.get(ownerClassObject); results[0] = clientComboBox; results[1] = (clientComboBox.getValue() == null) ? null : clientComboBox.getValue(); } else if (field.getType().toString().contains("ComboBox")) { ComboBox comboBox = (ComboBox) field.get(ownerClassObject); results[0] = comboBox; // Note: comboBox.getValue() may be null but some class may provides to make some action that avoid nullable and return empty string for example. So we check selection index. // int selectedIndex = comboBox.getSelectionModel().getSelectedIndex(); // results[1] = (comboBox.getValue() == null || selectedIndex < 0) ? null : comboBox.getValue(); results[1] = (comboBox.getValue() == null) ? null : comboBox.getValue(); } field.setAccessible(accessible); } catch (IllegalArgumentException | IllegalAccessException ex) { Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex); } return results; }
From source file:com.cnksi.core.tools.utils.Reflections.java
/** * ?private/protected???public?????JDKSecurityManager *//*from w w w .ja va 2 s . co m*/ public static void makeAccessible(Field field) { if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) { field.setAccessible(true); } }
From source file:activiti.common.persistence.util.ReflectHelper.java
/** * ?objfieldName//w w w .j av a 2s .com * * @param obj * @param fieldName * @return */ public static Object getValueByFieldName(Object obj, String fieldName) { Object value = null; try { Field field = getFieldByFieldName(obj, fieldName); if (field != null) { if (field.isAccessible()) { value = field.get(obj); } else { field.setAccessible(true); value = field.get(obj); field.setAccessible(false); } } } catch (Exception e) { } return value; }
From source file:com.blackducksoftware.integration.hub.detect.workflow.report.util.ObjectPrinter.java
public static void populateField(final Field field, final String prefix, final Object guy, Map<String, String> fieldMap) { if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) { return; // don't print static fields. }/* w w w . ja va2 s . com*/ final String name = field.getName(); String value = "unknown"; Object obj = null; try { if (!field.isAccessible()) { field.setAccessible(true); } obj = field.get(guy); } catch (final Exception e) { e.printStackTrace(); } boolean shouldPrintObjectsFields = false; if (obj == null) { value = "null"; } else { value = obj.toString(); shouldPrintObjectsFields = shouldRecursivelyPrintType(obj.getClass()); } if (!shouldPrintObjectsFields) { if (StringUtils.isBlank(prefix)) { fieldMap.put(name, value); } else { fieldMap.put(prefix + "." + name, value); } } else { String nestedPrefix = name; if (StringUtils.isNotBlank(prefix)) { nestedPrefix = prefix + "." + nestedPrefix; } populateObject(nestedPrefix, obj, fieldMap); } }
From source file:activiti.common.persistence.util.ReflectHelper.java
/** * ?objfieldName/* w w w. j a v a 2 s. c o m*/ * * @param obj * @param fieldName * @return */ @SuppressWarnings("unchecked") public static <T> T getValueByFieldType(Object obj, Class<T> fieldType) { Object value = null; for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass .getSuperclass()) { try { Field[] fields = superClass.getDeclaredFields(); for (Field f : fields) { if (f.getType() == fieldType) { if (f.isAccessible()) { value = f.get(obj); break; } else { f.setAccessible(true); value = f.get(obj); f.setAccessible(false); break; } } } if (value != null) { break; } } catch (Exception e) { } } return (T) value; }