List of usage examples for java.lang.reflect AccessibleObject setAccessible
@CallerSensitive public static void setAccessible(AccessibleObject[] array, boolean flag)
From source file:org.projectforge.database.XmlDump.java
/** * @param o1//from w w w. j av a 2s. com * @param o2 * @param logDifference If true than the difference is logged. * @return True if the given objects are equal. */ private boolean equals(final Object o1, final Object o2, final boolean logDifference) { if (o1 == null) { final boolean equals = (o2 == null); if (equals == false && logDifference == true) { log.error("Value 1 is null and value 2 is " + o2); } return equals; } else if (o2 == null) { if (logDifference == true) { log.error("Value 2 is null and value 1 is " + o1); } return false; } final Class<?> cls1 = o1.getClass(); final Field[] fields = cls1.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); for (final Field field : fields) { if (accept(field) == false) { continue; } try { final Object fieldValue1 = getValue(o1, o2, field); final Object fieldValue2 = getValue(o2, o1, field); if (field.getType().isPrimitive() == true) { if (ObjectUtils.equals(fieldValue2, fieldValue1) == false) { if (logDifference == true) { log.error("Field '" + field.getName() + "': value 1 '" + fieldValue1 + "' is different from value 2 '" + fieldValue2 + "'."); } return false; } continue; } else if (fieldValue1 == null) { if (fieldValue2 != null) { if (logDifference == true) { log.error("Field '" + field.getName() + "': value 1 '" + fieldValue1 + "' is different from value 2 '" + fieldValue2 + "'."); } return false; } } else if (fieldValue2 == null) { if (fieldValue1 != null) { if (logDifference == true) { log.error("Field '" + field.getName() + "': value 1 '" + fieldValue1 + "' is different from value 2 '" + fieldValue2 + "'."); } return false; } } else if (fieldValue1 instanceof Collection<?>) { final Collection<?> col1 = (Collection<?>) fieldValue1; final Collection<?> col2 = (Collection<?>) fieldValue2; if (col1.size() != col2.size()) { if (logDifference == true) { log.error("Field '" + field.getName() + "': colection's size '" + col1.size() + "' is different from collection's size '" + col2.size() + "'."); } return false; } if (equals(field, col1, col2, logDifference) == false || equals(field, col2, col1, logDifference) == false) { return false; } } else if (HibernateUtils.isEntity(fieldValue1.getClass()) == true) { if (fieldValue2 == null || ObjectUtils.equals(HibernateUtils.getIdentifier(fieldValue1), HibernateUtils.getIdentifier(fieldValue2)) == false) { if (logDifference == true) { log.error("Field '" + field.getName() + "': Hibernate object id '" + HibernateUtils.getIdentifier(fieldValue1) + "' is different from id '" + HibernateUtils.getIdentifier(fieldValue2) + "'."); } return false; } } else if (fieldValue1 instanceof BigDecimal) { if (fieldValue2 == null || ((BigDecimal) fieldValue1).compareTo((BigDecimal) fieldValue2) != 0) { if (logDifference == true) { log.error("Field '" + field.getName() + "': value 1 '" + fieldValue1 + "' is different from value 2 '" + fieldValue2 + "'."); } return false; } } else if (ObjectUtils.equals(fieldValue2, fieldValue1) == false) { if (logDifference == true) { log.error("Field '" + field.getName() + "': value 1 '" + fieldValue1 + "' is different from value 2 '" + fieldValue2 + "'."); } return false; } } catch (final IllegalAccessException ex) { throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage()); } } return true; }
From source file:org.projectforge.framework.configuration.ConfigXml.java
/** * Copies only not null values of the configuration. */// w w w .j a v a 2 s . c o m private static void copyDeclaredFields(final String prefix, final Class<?> srcClazz, final Object src, final Object dest, final String... ignoreFields) { final Field[] fields = srcClazz.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); for (final Field field : fields) { if (ignoreFields != null && ArrayUtils.contains(ignoreFields, field.getName()) == false && accept(field)) { try { final Object srcFieldValue = field.get(src); if (srcFieldValue == null) { // Do nothing } else if (srcFieldValue instanceof ConfigurationData) { final Object destFieldValue = field.get(dest); Validate.notNull(destFieldValue); final StringBuffer buf = new StringBuffer(); if (prefix != null) { buf.append(prefix); } String alias = null; if (field.isAnnotationPresent(XmlField.class)) { final XmlField xmlFieldAnn = field.getAnnotation(XmlField.class); if (xmlFieldAnn != null) { alias = xmlFieldAnn.alias(); } } if (alias != null) { buf.append(alias); } else { buf.append(field.getClass().getName()); } buf.append("."); copyDeclaredFields(buf.toString(), srcFieldValue.getClass(), srcFieldValue, destFieldValue, ignoreFields); } else if (PLUGIN_CONFIGS_FIELD_NAME.equals(field.getName()) == true) { // Do nothing. } else { field.set(dest, srcFieldValue); if (field.isAnnotationPresent(ConfigXmlSecretField.class) == true) { log.info(StringUtils.defaultString(prefix) + field.getName() + " = " + SECRET_PROPERTY_STRING); } else { log.info(StringUtils.defaultString(prefix) + field.getName() + " = " + srcFieldValue); } } } catch (final IllegalAccessException ex) { throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage()); } } } final Class<?> superClazz = srcClazz.getSuperclass(); if (superClazz != null) { copyDeclaredFields(prefix, superClazz, src, dest, ignoreFields); } }
From source file:org.projectforge.framework.persistence.database.XmlDump.java
/** * @param o1//from w ww .j a va 2s. c o m * @param o2 * @param logDifference If true than the difference is logged. * @return True if the given objects are equal. */ private boolean equals(final Object o1, final Object o2, final boolean logDifference) { if (o1 == null) { final boolean equals = (o2 == null); if (equals == false && logDifference == true) { log.error("Value 1 is null and value 2 is " + o2); } return equals; } else if (o2 == null) { if (logDifference == true) { log.error("Value 2 is null and value 1 is " + o1); } return false; } final Class<?> cls1 = o1.getClass(); final Field[] fields = cls1.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); for (final Field field : fields) { if (accept(field) == false) { continue; } try { final Object fieldValue1 = getValue(o1, o2, field); final Object fieldValue2 = getValue(o2, o1, field); if (field.getType().isPrimitive() == true) { if (ObjectUtils.equals(fieldValue2, fieldValue1) == false) { if (logDifference == true) { log.error("Field is different: " + field.getName() + "; value 1 '" + fieldValue1 + "' 2 '" + fieldValue2 + "'."); } return false; } continue; } else if (fieldValue1 == null) { if (fieldValue2 != null) { if (fieldValue2 instanceof Collection<?>) { if (CollectionUtils.isEmpty((Collection<?>) fieldValue2) == true) { // null is equals to empty collection in this case. return true; } } if (logDifference == true) { log.error("Field '" + field.getName() + "': value 1 '" + fieldValue1 + "' is different from value 2 '" + fieldValue2 + "'."); } return false; } } else if (fieldValue2 == null) { if (fieldValue1 != null) { if (logDifference == true) { log.error("Field '" + field.getName() + "': value 1 '" + fieldValue1 + "' is different from value 2 '" + fieldValue2 + "'."); } return false; } } else if (fieldValue1 instanceof Collection<?>) { final Collection<?> col1 = (Collection<?>) fieldValue1; final Collection<?> col2 = (Collection<?>) fieldValue2; if (col1.size() != col2.size()) { if (logDifference == true) { log.error("Field '" + field.getName() + "': colection's size '" + col1.size() + "' is different from collection's size '" + col2.size() + "'."); } return false; } if (equals(field, col1, col2, logDifference) == false || equals(field, col2, col1, logDifference) == false) { return false; } } else if (HibernateUtils.isEntity(fieldValue1.getClass()) == true) { if (fieldValue2 == null || ObjectUtils.equals(HibernateUtils.getIdentifier(fieldValue1), HibernateUtils.getIdentifier(fieldValue2)) == false) { if (logDifference == true) { log.error("Field '" + field.getName() + "': Hibernate object id '" + HibernateUtils.getIdentifier(fieldValue1) + "' is different from id '" + HibernateUtils.getIdentifier(fieldValue2) + "'."); } return false; } } else if (fieldValue1 instanceof BigDecimal) { if (fieldValue2 == null || ((BigDecimal) fieldValue1).compareTo((BigDecimal) fieldValue2) != 0) { if (logDifference == true) { log.error("Field '" + field.getName() + "': value 1 '" + fieldValue1 + "' is different from value 2 '" + fieldValue2 + "'."); } return false; } } else if (fieldValue1.getClass().isArray() == true) { if (ArrayUtils.isEquals(fieldValue1, fieldValue2) == false) { if (logDifference == true) { log.error("Field '" + field.getName() + "': value 1 '" + fieldValue1 + "' is different from value 2 '" + fieldValue2 + "'."); } return false; } } else if (ObjectUtils.equals(fieldValue2, fieldValue1) == false) { if (logDifference == true) { log.error("Field '" + field.getName() + "': value 1 '" + fieldValue1 + "' is different from value 2 '" + fieldValue2 + "'."); } return false; } } catch (final IllegalAccessException ex) { throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage()); } } return true; }
From source file:org.projectforge.framework.xstream.XmlObjectReader.java
/** * Please note: Does not set the default values, this should be done by the class itself (when declaring the fields or in the * constructors).//from www. j ava 2 s . c om * @param obj The object to assign the parameters parsed from the given xml element. * @param str */ public void read(final Object obj, final Element el) { if (el == null) { return; } final Field[] fields = BeanHelper.getAllDeclaredFields(obj.getClass()); AccessibleObject.setAccessible(fields, true); for (final Object listObject : el.attributes()) { final Attribute attr = (Attribute) listObject; final String key = attr.getName(); if (StringHelper.isIn(key, XmlObjectWriter.ATTR_ID, XmlObjectWriter.ATTR_REF_ID) == true) { // Do not try to find fields for o-id and ref-id. continue; } final String value = attr.getText(); proceedElement(obj, fields, el, key, value, true); } for (final Object listObject : el.elements()) { final Element childElement = (Element) listObject; final String key = childElement.getName(); proceedElement(obj, fields, childElement, key, null, false); } putProcessedElement(el); }
From source file:org.projectforge.framework.xstream.XmlObjectWriter.java
private Element write(final Branch parent, final Object obj, String name, final boolean asAttribute, final boolean asCDATA) { if (obj == null) { return null; }//from w ww.jav a2 s . co m final Class<?> type = obj.getClass(); if (name == null) { name = getAliasMap().getAliasForClass(type); } if (name == null && obj.getClass().isAnnotationPresent(XmlObject.class) == true) { final XmlObject xmlObject = obj.getClass().getAnnotation(XmlObject.class); if (StringUtils.isNotEmpty(xmlObject.alias()) == true) { name = xmlObject.alias(); } } if (name == null) { name = xmlRegistry.getAliasForClass(type); } if (name == null) { name = obj.getClass().getName(); } if (isRegistered(obj) == true) { final Element el = getRegisteredElement(obj); Integer refId; final Attribute attr = el.attribute(ATTR_ID); if (attr == null) { // Id attribute not yet written. So add this attribute: refId = refIdCounter++; el.addAttribute(ATTR_ID, String.valueOf(refId)); } else { refId = NumberHelper.parseInteger(attr.getText()); } if (refId == null) { log.error("Can't parse ref id: " + attr.getText()); } else { final Element element = parent.addElement(name); element.addAttribute(ATTR_REF_ID, String.valueOf(refId)); return element; } } IConverter<?> converter = xmlRegistry.getConverter(type); if (converter != null) { final String sValue = converter.toString(obj); writeValue(parent, obj, name, sValue, asAttribute, asCDATA); return (Element) parent; } else if (Enum.class.isAssignableFrom(type) == true) { final String sValue = ((Enum<?>) obj).name(); writeValue(parent, obj, name, sValue, asAttribute, asCDATA); return (Element) parent; } else if (obj instanceof Collection<?>) { final Element listElement = parent.addElement(name); final Iterator<?> it = ((Collection<?>) obj).iterator(); while (it.hasNext() == true) { write(listElement, it.next(), null, false, false); } return listElement; } final Element element = parent.addElement(name); registerElement(obj, element); final Field[] fields = BeanHelper.getAllDeclaredFields(obj.getClass()); AccessibleObject.setAccessible(fields, true); for (final Field field : fields) { if (field.isAnnotationPresent(XmlOmitField.class) == true || ignoreField(obj, field) == true) { continue; } final XmlField ann = field.isAnnotationPresent(XmlField.class) == true ? field.getAnnotation(XmlField.class) : null; if (onlyAnnotatedFields == true && field.isAnnotationPresent(XmlField.class) == false) { continue; } final Object value = BeanHelper.getFieldValue(obj, field); writeField(field, obj, value, ann, element); } return element; }
From source file:org.projectforge.user.UserPrefDao.java
/** * Fill object fields from the parameters of the given userPref. * @param userPref/*from w ww . j av a2 s .c o m*/ * @param obj * @see #addUserPrefParameters(UserPrefDO, Object) */ public void fillFromUserPrefParameters(final UserPrefDO userPref, final Object obj) { Validate.notNull(userPref); Validate.notNull(obj); final Field[] fields = obj.getClass().getDeclaredFields(); AccessibleObject.setAccessible(fields, true); if (userPref.getUserPrefEntries() != null) { for (final UserPrefEntryDO entry : userPref.getUserPrefEntries()) { Field field = null; for (final Field f : fields) { if (f.getName().equals(entry.getParameter()) == true) { field = f; break; } } if (field == null) { log.error("Declared field '" + entry.getParameter() + "' not found for " + obj.getClass() + ". Ignoring parameter."); } else { final Object value = getParameterValue(field.getType(), entry.getValue()); try { field.set(obj, value); } catch (final IllegalArgumentException ex) { log.error(ex.getMessage() + " While setting declared field '" + entry.getParameter() + "' of " + obj.getClass() + ". Ignoring parameter.", ex); } catch (final IllegalAccessException ex) { log.error(ex.getMessage() + " While setting declared field '" + entry.getParameter() + "' of " + obj.getClass() + ". Ignoring parameter.", ex); } } } } }
From source file:org.tinygroup.commons.tools.EqualsUtil.java
/** * <p>Appends the fields and values defined by the given object of the * given Class.</p>//w ww . j av a 2s . c om * * @param lhs the left hand object * @param rhs the right hand object * @param clazz the class to append details of * @param builder the builder to append to * @param useTransients whether to test transient fields * @param excludeFields array of field names to exclude from testing */ private static void reflectionCompareAppend(Object lhs, Object rhs, Class clazz, EqualsBuilder builder, boolean useTransients, String[] compareFields) { Field[] fields = clazz.getDeclaredFields(); List compareFieldList = compareFields != null ? Arrays.asList(compareFields) : Collections.EMPTY_LIST; AccessibleObject.setAccessible(fields, true); for (int i = 0; i < fields.length && builder.isEquals(); i++) { Field f = fields[i]; if (compareFieldList.contains(f.getName()) && (f.getName().indexOf('$') == -1) && (useTransients || !Modifier.isTransient(f.getModifiers())) && (!Modifier.isStatic(f.getModifiers()))) { try { builder.append(f.get(lhs), f.get(rhs)); } catch (IllegalAccessException e) { //this can't happen. Would get a Security exception instead //throw a runtime exception in case the impossible happens. throw new InternalError("Unexpected IllegalAccessException"); } } } }
From source file:org.tinygroup.commons.tools.HashCodeUtil.java
private static void reflectionAppend(Object object, Class clazz, HashCodeBuilder builder, boolean useTransients, String[] excludeFields) { if (isRegistered(object)) { return;//from w w w . j av a2s .c om } try { register(object); Field[] fields = clazz.getDeclaredFields(); List excludedFieldList = excludeFields != null ? Arrays.asList(excludeFields) : Collections.EMPTY_LIST; AccessibleObject.setAccessible(fields, true); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (!excludedFieldList.contains(field.getName()) && (field.getName().indexOf('$') == -1) && (useTransients || !Modifier.isTransient(field.getModifiers())) && (!Modifier.isStatic(field.getModifiers()))) { try { Object fieldValue = field.get(object); builder.append(fieldValue); } catch (IllegalAccessException e) { // this can't happen. Would get a Security exception // instead // throw a runtime exception in case the impossible // happens. throw new InternalError("Unexpected IllegalAccessException"); } } } } finally { unregister(object); } }
From source file:org.unitils.mock.core.proxy.CloneUtil.java
/** * Clones all values in all fields of the given class and superclasses. * * @param clazz The current class * @param instanceToClone The instance, not null * @param clonedInstance The clone, not null * @param cloneCache The cached clones, not null *//* w w w . j av a 2s . c o m*/ protected static void cloneFields(Class<?> clazz, Object instanceToClone, Object clonedInstance, Map<Object, Object> cloneCache) throws Throwable { if (clazz == null || Object.class.equals(clazz)) { return; } Field[] fields = clazz.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); for (Field field : fields) { // skip static fields if (isStatic(field.getModifiers())) { continue; } if (field.getDeclaringClass().getName().startsWith("org.hibernate") && isTransient(field.getModifiers())) { continue; } Object fieldValue = field.get(instanceToClone); Object clonedFieldValue = cloneObject(fieldValue, cloneCache); field.set(clonedInstance, clonedFieldValue); } cloneFields(clazz.getSuperclass(), instanceToClone, clonedInstance, cloneCache); }
From source file:stc.app.bean.lang.ReflectionToStringBuilder.java
/** * <p>//from ww w. j a v a2 s . c om * Appends the fields and values defined by the given object of the given Class. * </p> * * <p> * If a cycle is detected as an object is "toString()'ed", such an object is rendered as * if <code>Object.toString()</code> had been called and not implemented by the object. * </p> * * @param clazz The class of object parameter */ protected void appendFieldsIn(Class clazz) { if (clazz.isArray()) { this.reflectionAppendArray(this.getObject()); return; } Field[] fields = clazz.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; String fieldName = field.getName(); if (this.accept(field)) { try { // Warning: Field.get(Object) creates wrappers objects // for primitive types. Object fieldValue = this.getValue(field); this.append(fieldName, fieldValue); } catch (IllegalAccessException ex) { // this can't happen. Would get a Security exception // instead // throw a runtime exception in case the impossible // happens. throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage()); } } } }