List of usage examples for java.lang.reflect Modifier isTransient
public static boolean isTransient(int mod)
From source file:com.ejisto.modules.web.util.FieldSerializationUtil.java
public static List<MockedField> translateObject(Object object, String containerClassName, String fieldName, String contextPath) {/*from w w w . java 2 s. c om*/ if (object == null) { return emptyList(); } if (isBlackListed(object.getClass())) { String fieldType = object.getClass().getName(); MockedField out = buildMockedField(containerClassName, fieldName, contextPath, fieldType); return asList(out); } DefaultSupportedType type = DefaultSupportedType.evaluate(object); if (type != null && type.isPrimitiveOrSimpleValue()) { return asList(getSingleFieldFromDefaultSupportedType(object, type, contextPath, containerClassName, fieldName)); } List<MockedField> out = new ArrayList<MockedField>(); Class<?> objectClass = object.getClass(); String className = objectClass.getName(); for (Field field : objectClass.getDeclaredFields()) { if (!Modifier.isTransient(field.getModifiers()) && !isBlackListed(field.getType())) { out.add(translateField(field, object, className, contextPath)); } } return out; }
From source file:io.fabric8.cxf.endpoint.BeanValidationAnnotationIntrospector.java
@Override public boolean hasIgnoreMarker(AnnotatedMember m) { Member member = m.getMember(); int modifiers = member.getModifiers(); if (Modifier.isTransient(modifiers)) { if (LOG.isLoggable(Level.FINE)) { LOG.fine("Ignoring transient member " + m); }//from ww w .j a v a 2 s . com return true; } else if (m instanceof AnnotatedMethod) { AnnotatedMethod method = (AnnotatedMethod) m; String methodName = method.getName(); // lets see if there is a transient field of the same name as the getter if (methodName.startsWith("get") && method.getParameterCount() == 0) { String fieldName = Introspector.decapitalize(methodName.substring(3)); Class<?> declaringClass = method.getDeclaringClass(); Field field = findField(fieldName, declaringClass); if (field != null) { int fieldModifiers = field.getModifiers(); if (Modifier.isTransient(fieldModifiers)) { LOG.fine("Ignoring member " + m + " due to transient field called " + fieldName); return true; } } } } return super.hasIgnoreMarker(m); }
From source file:springobjectmapper.ReflectionHelper.java
private static boolean isEntityField(Field field) { return !field.isSynthetic() && !Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers()); }
From source file:org.opentele.server.core.util.CustomGroovyBeanJSONMarshaller.java
public void marshalObject(Object o, JSON json) throws ConverterException { JSONWriter writer = json.getWriter(); try {// w w w . j a v a2s .c om writer.object(); for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) { String name = property.getName(); Method readMethod = property.getReadMethod(); if (readMethod != null && !(name.equals("metaClass")) && !(name.equals("class"))) { Object value = readMethod.invoke(o, (Object[]) null); writer.key(name); json.convertAnother(value); } } for (Field field : o.getClass().getDeclaredFields()) { int modifiers = field.getModifiers(); if (Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) { writer.key(field.getName()); json.convertAnother(field.get(o)); } } writer.endObject(); } catch (ConverterException ce) { throw ce; } catch (Exception e) { throw new ConverterException("Error converting Bean with class " + o.getClass().getName(), e); } }
From source file:net.buffalo.protocal.util.ClassUtil.java
private static HashMap getFieldMap(Class cl) { HashMap fieldMap = new HashMap(); for (; cl != null; cl = cl.getSuperclass()) { Field[] fields = cl.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) continue; field.setAccessible(true);/*w w w .j av a2s. com*/ fieldMap.put(field.getName(), field); } } return fieldMap; }
From source file:org.vulpe.commons.util.VulpeBeanComparatorUtil.java
/** * Compare beans and return map with differences by field. * /*from ww w . j a va 2s.com*/ * @param bean1 * @param bean2 * @return Map with differences by field. */ public static Map<String, Object[]> compare(final Object bean1, final Object bean2, boolean skipCollections, boolean skipTransient) { if (VulpeValidationUtil.isEmpty(bean1, bean2)) { throw new NullArgumentException("bean1(" + bean1 + ") bean2(" + bean2 + ")"); } if (VulpeValidationUtil.isNotEmpty(bean1, bean2) && !bean1.getClass().equals(bean2.getClass())) { throw new IllegalClassException(bean1.getClass(), bean2.getClass()); } final Class<?> baseClass = bean1 != null ? bean1.getClass() : bean2.getClass(); final Map<String, Object[]> diffMap = new HashMap<String, Object[]>(); final List<Field> fields = VulpeReflectUtil.getFields(baseClass); for (final Field field : fields) { if (VulpeReflectUtil.isAnnotationInField(SkipCompare.class, baseClass, field) || (skipCollections && Collection.class.isAssignableFrom(field.getType())) || (skipTransient && (Modifier.isTransient(field.getModifiers()) || field.isAnnotationPresent(Transient.class)))) { continue; } try { Object value1 = VulpeValidationUtil.isNotEmpty(bean1) ? VulpeReflectUtil.getFieldValue(bean1, field.getName()) : null; Object value2 = VulpeValidationUtil.isNotEmpty(bean2) ? VulpeReflectUtil.getFieldValue(bean2, field.getName()) : null; if (VulpeValidationUtil.isNull(value1, value2)) { continue; } boolean diff = false; if ((VulpeValidationUtil.isEmpty(value1) && VulpeValidationUtil.isNotEmpty(value2)) || (VulpeValidationUtil.isNotEmpty(value1) && VulpeValidationUtil.isEmpty(value2))) { diff = true; if (Date.class.isAssignableFrom(field.getType())) { if (VulpeValidationUtil.isNotEmpty(value1)) { if (value1 instanceof Timestamp) { value1 = VulpeDateUtil.getDate((Date) value1, VulpeConfigHelper.getDateTimePattern()); } else { value1 = VulpeDateUtil.getDate((Date) value1, VulpeConfigHelper.getDatePattern()); } } if (VulpeValidationUtil.isNotEmpty(value2)) { if (value2 instanceof Timestamp) { value2 = VulpeDateUtil.getDate((Date) value2, VulpeConfigHelper.getDateTimePattern()); } else { value2 = VulpeDateUtil.getDate((Date) value2, VulpeConfigHelper.getDatePattern()); } } } else if (VulpeEntity.class.isAssignableFrom(field.getType())) { if (VulpeValidationUtil.isNotEmpty(value1)) { value1 = ((VulpeEntity<?>) value1).getId(); } if (VulpeValidationUtil.isNotEmpty(value2)) { value2 = ((VulpeEntity<?>) value2).getId(); } } } else { if (Date.class.isAssignableFrom(field.getType())) { if (((Date) value1).getTime() != ((Date) value2).getTime()) { if (value1 instanceof Timestamp || value2 instanceof Timestamp) { value1 = VulpeDateUtil.getDate((Date) value1, VulpeConfigHelper.getDateTimePattern()); value2 = VulpeDateUtil.getDate((Date) value2, VulpeConfigHelper.getDateTimePattern()); } else { value1 = VulpeDateUtil.getDate((Date) value1, VulpeConfigHelper.getDatePattern()); value2 = VulpeDateUtil.getDate((Date) value2, VulpeConfigHelper.getDatePattern()); } diff = true; } } else if (VulpeEntity.class.isAssignableFrom(field.getType())) { if (!((VulpeEntity<?>) value1).getId().equals(((VulpeEntity<?>) value2).getId())) { value1 = ((VulpeEntity<?>) value1).getId(); value2 = ((VulpeEntity<?>) value2).getId(); diff = true; } } else if (!value1.equals(value2)) { diff = true; } } if (diff) { diffMap.put(field.getName(), new Object[] { value1, value2 }); } } catch (Exception e) { LOG.error(e.getMessage()); } } return diffMap; }
From source file:org.springmodules.cache.util.Reflections.java
/** * <p>/*from w ww . j a v a2s . co m*/ * This method uses reflection to build a valid hash code. * </p> * * <p> * It uses <code>AccessibleObject.setAccessible</code> to gain access to * private fields. This means that it will throw a security exception if run * under a security manager, if the permissions are not set up correctly. It * is also not as efficient as testing explicitly. * </p> * * <p> * Transient members will not be used, as they are likely derived fields, * and not part of the value of the <code>Object</code>. * </p> * * <p> * Static fields will not be tested. Superclass fields will be included. * </p> * * @param obj * the object to create a <code>hashCode</code> for * @return the generated hash code, or zero if the given object is * <code>null</code> */ public static int reflectionHashCode(Object obj) { if (obj == null) return 0; Class targetClass = obj.getClass(); if (Objects.isArrayOfPrimitives(obj) || Objects.isPrimitiveOrWrapper(targetClass)) { return Objects.nullSafeHashCode(obj); } if (targetClass.isArray()) { return reflectionHashCode((Object[]) obj); } if (obj instanceof Collection) { return reflectionHashCode((Collection) obj); } if (obj instanceof Map) { return reflectionHashCode((Map) obj); } // determine whether the object's class declares hashCode() or has a // superClass other than java.lang.Object that declares hashCode() Class clazz = (obj instanceof Class) ? (Class) obj : obj.getClass(); Method hashCodeMethod = ReflectionUtils.findMethod(clazz, "hashCode", new Class[0]); if (hashCodeMethod != null) { return obj.hashCode(); } // could not find a hashCode other than the one declared by java.lang.Object int hash = INITIAL_HASH; try { while (targetClass != null) { Field[] fields = targetClass.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; int modifiers = field.getModifiers(); if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) { hash = MULTIPLIER * hash + reflectionHashCode(field.get(obj)); } } targetClass = targetClass.getSuperclass(); } } catch (IllegalAccessException exception) { // ///CLOVER:OFF ReflectionUtils.handleReflectionException(exception); // ///CLOVER:ON } return hash; }
From source file:Main.java
public static boolean equals(Object target, Object o) { if (target == o) return true; if (target == null || o == null) return false; if (!target.getClass().equals(o.getClass())) return false; Class<?> current = target.getClass(); do {/*from ww w .ja va 2 s . co m*/ for (Field f : current.getDeclaredFields()) { if (Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers()) || f.isSynthetic()) { continue; } Object self; Object other; try { f.setAccessible(true); self = f.get(target); other = f.get(o); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } if (self == null) { if (other != null) return false; } else if (self.getClass().isArray()) { if (self.getClass().equals(boolean[].class)) { if (!Arrays.equals((boolean[]) self, (boolean[]) other)) return false; } else if (self.getClass().equals(char[].class)) { if (!Arrays.equals((char[]) self, (char[]) other)) return false; } else if (self.getClass().equals(byte[].class)) { if (!Arrays.equals((byte[]) self, (byte[]) other)) return false; } else if (self.getClass().equals(short[].class)) { if (!Arrays.equals((short[]) self, (short[]) other)) return false; } else if (self.getClass().equals(int[].class)) { if (!Arrays.equals((int[]) self, (int[]) other)) return false; } else if (self.getClass().equals(long[].class)) { if (!Arrays.equals((long[]) self, (long[]) other)) return false; } else if (self.getClass().equals(float[].class)) { if (!Arrays.equals((float[]) self, (float[]) other)) return false; } else if (self.getClass().equals(double[].class)) { if (!Arrays.equals((double[]) self, (double[]) other)) return false; } else { if (!Arrays.equals((Object[]) self, (Object[]) other)) return false; } } else if (!self.equals(other)) { return false; } } current = current.getSuperclass(); } while (!Object.class.equals(current)); return true; }
From source file:py.una.pol.karaku.test.test.ValidationMessagesTest.java
@Test public void testConstants() { ReflectionUtils.doWithFields(ValidationMessages.class, new FieldCallback() { @Override//from ww w. ja v a 2s. co m public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { String value = (String) field.get(null); assertNotNull(value); assertTrue("Key: " + value + " not found", keys.contains(value)); } }, new FieldFilter() { @Override public boolean matches(Field field) { int modifiers = field.getModifiers(); return Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers) && Modifier.isPublic(modifiers); } }); }
From source file:pl.maciejwalkowiak.plist.FieldSerializer.java
private String serializeField(Object o, Field field) { String result = ""; int modifiers = field.getModifiers(); if (!Modifier.isTransient(modifiers) && !Modifier.isStatic(modifiers)) { ReflectionUtils.makeAccessible(field); if (!field.isAnnotationPresent(PlistIgnore.class)) { result = processField(field, o).toString(); } else {/* w w w. ja va 2 s . co m*/ logger.debug("field {} is ignored", field.getName()); } } return result; }