List of usage examples for java.lang.reflect Field getModifiers
public int getModifiers()
From source file:io.github.huherto.springyRecords.RecordMapper.java
/** * Initialize the mapping metadata for the given class. * @param mappedClass the mapped class.//from w ww . j a va2 s . co m */ protected void initialize(Class<T> mappedClass) { this.mappedClass = mappedClass; this.mappedFields = new HashMap<String, Field>(); Field fields[] = mappedClass.getFields(); for (Field field : fields) { int mod = field.getModifiers(); if (Modifier.isPublic(mod) && !Modifier.isStatic(mod)) { for (Annotation a : field.getAnnotations()) { if (a.annotationType().isAssignableFrom(Column.class)) { Column c = (Column) a; String columnName = c.name(); mappedFields.put(columnName, field); } } } } }
From source file:com.github.braully.web.DescriptorHtmlEntity.java
private void addHtmlListElement(Field field) { if (elementsList == null) { elementsList = new ArrayList<>(); }//from ww w. j ava 2s. co m final int modifiers = field.getModifiers(); if (!Modifier.isStatic(modifiers) && !hiddenListProperties.contains(field.getName())) { elementsList.add(buildDescriptorHtmlEntity(field)); } }
From source file:com.github.braully.web.DescriptorHtmlEntity.java
private void addHtmlFormElement(Field field) { if (elementsForm == null) { elementsForm = new ArrayList<>(); }/*from w ww . j a va 2 s. c o m*/ final int modifiers = field.getModifiers(); if (!Modifier.isStatic(modifiers) && !hiddenFormProperties.contains(field.getName())) { DescriptorHtmlEntity htmlElement = buildDescriptorHtmlEntity(field); elementsForm.add(htmlElement); } }
From source file:com.shazam.shazamcrest.CyclicReferenceDetector.java
/** * Detects classes that have circular reference. * //ww w .jav a 2s . c o m * @param object the object to check if it has circular reference fields * @param clazz the class being used (necessary if we also checking super class as getDeclaredFields only returns * fields of a given class, but not its super class) */ private void detectCircularReferenceOnFields(Object object, Class<?> clazz) { if (objectsWithCircularReferences.contains(object)) { return; } for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); if (!isStatic(field.getModifiers())) { try { Object fieldValue = field.get(object); if (fieldValue != null) { detectCircularReferenceOnObject(fieldValue); } } catch (IllegalAccessException e) { throw new RuntimeException(e); } } } detectCircularReferencesFromTheSuperClass(object, clazz); }
From source file:com.zuoxiaolong.niubi.job.persistent.BaseDaoImpl.java
private <T> List<Object> generateValueListAndSetSql(Class<T> clazz, T entity, StringBuffer sqlBuffer, boolean useLike) { List<Object> valueList = new ArrayList<>(); if (entity != null) { Field[] fields = ReflectHelper.getAllFields(entity); for (int i = 0, index = 0; i < fields.length; i++) { Field field = fields[i]; int modifiers = field.getModifiers(); if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers) || Modifier.isFinal(modifiers) || ObjectHelper.isTransientId(clazz, field)) { continue; }//from w w w . ja v a 2 s . c o m Object value = ReflectHelper.getFieldValueWithGetterMethod(entity, entity.getClass(), field.getName()); if (ObjectHelper.isEmpty(value)) { continue; } if (field.getType() == String.class && useLike) { sqlBuffer.append("and " + field.getName() + " like ?" + index++ + " "); valueList.add("%" + value + "%"); } else { sqlBuffer.append("and " + field.getName() + "=?" + index++ + " "); valueList.add(value); } } } return valueList; }
From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java
/** * Read all field into a map./* www. jav a2 s . co m*/ * * @param ret the ret * @param clz the clz * @param bean the bean * @param modifiederMask required Modified.XXX fields set. * @param negModifierMask disalowwed Modified.XXX fields set. */ public static void fetchAllFields(Map<String, Object> ret, Class<?> clz, Object bean, int modifiederMask, int negModifierMask) { if (clz == null) { return; } for (Field f : clz.getDeclaredFields()) { int mod = f.getModifiers(); if ((mod & negModifierMask) != 0) { continue; } if ((mod & modifiederMask) != modifiederMask) { continue; } if (ret.containsKey(f.getName()) == true) { continue; } Object value = readField(bean, f); ret.put(f.getName(), value); } fetchAllFields(ret, clz.getSuperclass(), bean, modifiederMask, negModifierMask); }
From source file:com.github.braully.web.DescriptorHtmlEntity.java
private void addHtmlFilterElement(Field field) { if (elementsFilter == null) { elementsFilter = new ArrayList<>(); }//from w w w. ja v a2 s . c o m final int modifiers = field.getModifiers(); if (!Modifier.isStatic(modifiers) && !hiddenFilterProperties.contains(field.getName())) { elementsFilter.add(buildDescriptorHtmlEntity(field)); } }
From source file:CrossRef.java
/** * Print the fields and methods of one class. *//* www .j a v a 2 s. co m*/ protected void doClass(Class c) { int i, mods; startClass(c); try { Field[] fields = c.getDeclaredFields(); Arrays.sort(fields, new Comparator() { public int compare(Object o1, Object o2) { return ((Field) o1).getName().compareTo(((Field) o2).getName()); } }); for (i = 0; i < fields.length; i++) { Field field = (Field) fields[i]; if (!Modifier.isPrivate(field.getModifiers())) putField(field, c); // else System.err.println("private field ignored: " + field); } Method methods[] = c.getDeclaredMethods(); // Arrays.sort(methods); for (i = 0; i < methods.length; i++) { if (!Modifier.isPrivate(methods[i].getModifiers())) putMethod(methods[i], c); // else System.err.println("pvt: " + methods[i]); } } catch (Exception e) { e.printStackTrace(); } endClass(); }
From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java
/** * Copy all instance fiels from source to target. static fields are not copied. * //from www. j a v a 2 s . c om * @param <T> the generic type * @param targetClass the target class * @param source the source * @param target the target */ public static <T> void copyInstanceProperties(Class<?> targetClass, T source, T target) { if (targetClass == null) { return; } for (Field f : targetClass.getDeclaredFields()) { int mod = f.getModifiers(); if (Modifier.isStatic(mod) == true) { continue; } Object value = readField(source, f.getName()); writeField(target, f, value); } copyInstanceProperties(targetClass.getSuperclass(), source, target); }
From source file:de.ks.flatadocdb.defaults.ReflectionLuceneDocumentExtractor.java
protected boolean filterField(Field f) { boolean noStatic = !Modifier.isStatic(f.getModifiers()); if (noStatic) { Class<?> type = f.getType(); boolean validType = isValidBaseType(type); Type genericType = f.getGenericType(); if (!validType && Collection.class.isAssignableFrom(type) && genericType instanceof ParameterizedType) { Type[] actualTypeArguments = ((ParameterizedType) genericType).getActualTypeArguments(); if (actualTypeArguments.length == 1 && actualTypeArguments[0] instanceof Class) { validType = isValidBaseType((Class<?>) actualTypeArguments[0]); }//from w w w . j av a 2 s. co m } if (!validType && TypeUtils.isArrayType(type)) { Type arrayComponentType = TypeUtils.getArrayComponentType(type); if (arrayComponentType instanceof Class) { validType = isValidBaseType((Class<?>) arrayComponentType); } } return validType; } return false; }