List of usage examples for java.lang.reflect Field getName
public String getName()
From source file:com.alading.library.util.db.util.sql.TAUpdateSqlBuilder.java
/** * ,?//ww w. ja v a2 s . c o m * * @return * @throws TADBException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static TAArrayList getFieldsAndValue(Object entity) throws TADBException, IllegalArgumentException, IllegalAccessException { // TODO Auto-generated method stub TAArrayList arrayList = new TAArrayList(); if (entity == null) { throw new TADBException("?"); } Class<?> clazz = entity.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (!TADBUtils.isTransient(field)) { if (TADBUtils.isBaseDateType(field)) { TAPrimaryKey annotation = field.getAnnotation(TAPrimaryKey.class); if (annotation == null || !annotation.autoIncrement()) { String columnName = TADBUtils.getColumnByField(field); field.setAccessible(true); arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(), field.get(entity) == null ? null : field.get(entity).toString()); } } } } return arrayList; }
From source file:com.witness.utils.db.util.sql.TAUpdateSqlBuilder.java
/** * ,?//w w w . j ava2 s.co m * * @return * @throws TADBException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static TAArrayList getFieldsAndValue(Object entity) throws TADBException, IllegalArgumentException, IllegalAccessException { // TODO Auto-generated method stub TAArrayList arrayList = new TAArrayList(); if (entity == null) { throw new TADBException("?"); } Class<?> clazz = entity.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (!TADBUtils.isTransient(field)) { if (TADBUtils.isBaseDateType(field)) { DBPrimaryKey annotation = field.getAnnotation(DBPrimaryKey.class); if (annotation == null || !annotation.autoIncrement()) { String columnName = TADBUtils.getColumnByField(field); field.setAccessible(true); arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(), field.get(entity) == null ? null : field.get(entity).toString()); } } } } return arrayList; }
From source file:com.google.code.siren4j.util.ReflectionUtils.java
/** * Replaces field tokens with the actual value in the fields. The field types must be simple property types * * @param str//w ww . java 2s . c o m * @param fields info * @param parentMode * @param obj * @return * @throws Siren4JException */ public static String replaceFieldTokens(Object obj, String str, List<ReflectedInfo> fields, boolean parentMode) throws Siren4JException { Map<String, Field> index = new HashMap<String, Field>(); if (StringUtils.isBlank(str)) { return str; } if (fields != null) { for (ReflectedInfo info : fields) { Field f = info.getField(); if (f != null) { index.put(f.getName(), f); } } } try { for (String key : ReflectionUtils.getTokenKeys(str)) { if ((!parentMode && !key.startsWith("parent.")) || (parentMode && key.startsWith("parent."))) { String fieldname = key.startsWith("parent.") ? key.substring(7) : key; if (index.containsKey(fieldname)) { Field f = index.get(fieldname); if (f.getType().isEnum() || ArrayUtils.contains(propertyTypes, f.getType())) { String replacement = ""; Object theObject = f.get(obj); if (f.getType().isEnum()) { replacement = theObject == null ? "" : ((Enum) theObject).name(); } else { replacement = theObject == null ? "" : theObject.toString(); } str = str.replaceAll("\\{" + key + "\\}", Matcher.quoteReplacement("" + replacement)); } } } } } catch (Exception e) { throw new Siren4JException(e); } return str; }
From source file:ea.compoment.db.util.sql.InsertSqlBuilder.java
/** * ,?/* www . ja v a 2 s.c o m*/ * * @return * @throws DBException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static NVArrayList getFieldsAndValue(Object entity) throws DBException, IllegalArgumentException, IllegalAccessException { NVArrayList arrayList = new NVArrayList(); if (entity == null) { throw new DBException("?"); } Class<?> clazz = entity.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (!DBAnnoUtils.isTransient(field)) { if (DBAnnoUtils.isBaseDateType(field)) { PrimaryKey annotation = field.getAnnotation(PrimaryKey.class); if (annotation != null && annotation.autoIncrement()) { } else { String columnName = DBAnnoUtils.getColumnByField(field); field.setAccessible(true); arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(), field.get(entity) == null ? null : field.get(entity).toString()); } } } } return arrayList; }
From source file:com.google.code.simplestuff.bean.SimpleBean.java
/** * Returns true if the two {@link Field} object collections are equals, * basing the comparison only on the name and not on the class of the * fields./*w ww . j av a2 s . com*/ * * @param firstAnnotatedFields The first collection to compare * @param secondAnnotatedFields The second collection to compare * @param paranoidComparison If a double comparison has to be done * (comparing first and second and then second and first. * @return If the two collection contain the same fields by name. */ private static boolean compareAnnotatedFieldsByName(Collection<Field> firstAnnotatedFields, Collection<Field> secondAnnotatedFields, boolean paranoidComparison) { // TODO: Probably this code can be improved in performances. BTW // consider that it could be possible that parent and child have the // same private property and this will result in two identical (from the // business point of view) fields in the collection. This also means we // cannot speed up performances first comparing the collections sizes. for (Field firstField : firstAnnotatedFields) { boolean fieldFound = false; for (Field secondField : secondAnnotatedFields) { if (firstField.getName().equals(secondField.getName())) { fieldFound = true; // Field found we can exit the second inner cycle. break; } } // If we are the current field of the first collection wasn't found // in the second one... we can directly return false if (!fieldFound) { return false; } } // If the paranoidComparison is true, we repeat the same operation // comparing the second collection with the first one. The // paranoicComparison prevent tricky cases in which for example for // the first bean parent a child have the same annotated field (same // name) and for the second one we have the same number of annotated // fields of the first one. In this case neither comparing size of // the second collection and successful matches would help. if (paranoidComparison) { compareAnnotatedFieldsByName(secondAnnotatedFields, firstAnnotatedFields, false); } // If we are here all the fields of the first collection were // successfully found in the second collection and vice versa. return true; }
From source file:com.alading.library.util.db.util.sql.TAInsertSqlBuilder.java
/** * ,?// w w w .j av a2 s . co m * * @return * @throws TADBException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static TAArrayList getFieldsAndValue(Object entity) throws TADBException, IllegalArgumentException, IllegalAccessException { // TODO Auto-generated method stub TAArrayList arrayList = new TAArrayList(); if (entity == null) { throw new TADBException("?"); } Class<?> clazz = entity.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (!TADBUtils.isTransient(field)) { if (TADBUtils.isBaseDateType(field)) { TAPrimaryKey annotation = field.getAnnotation(TAPrimaryKey.class); if (annotation != null && annotation.autoIncrement()) { } else { String columnName = TADBUtils.getColumnByField(field); field.setAccessible(true); arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(), field.get(entity) == null ? null : field.get(entity).toString()); } } } } return arrayList; }
From source file:com.witness.utils.db.util.sql.TAInsertSqlBuilder.java
/** * ,?// ww w . ja v a2 s . c o m * * @return * @throws TADBException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static TAArrayList getFieldsAndValue(Object entity) throws TADBException, IllegalArgumentException, IllegalAccessException { // TODO Auto-generated method stub TAArrayList arrayList = new TAArrayList(); if (entity == null) { throw new TADBException("?"); } Class<?> clazz = entity.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (!TADBUtils.isTransient(field)) { if (TADBUtils.isBaseDateType(field)) { DBPrimaryKey annotation = field.getAnnotation(DBPrimaryKey.class); if (annotation != null && annotation.autoIncrement()) { } else { String columnName = TADBUtils.getColumnByField(field); field.setAccessible(true); arrayList.add((columnName != null && !columnName.equals("")) ? columnName : field.getName(), field.get(entity) == null ? null : field.get(entity).toString()); } } } } return arrayList; }
From source file:edu.usu.sdl.openstorefront.doc.EntityProcessor.java
private static void addFields(Class entity, EntityDocModel docModel) { if (entity != null) { Field[] fields = entity.getDeclaredFields(); for (Field field : fields) { //Skip static field if ((Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) == false) { EntityFieldModel fieldModel = new EntityFieldModel(); fieldModel.setName(field.getName()); fieldModel.setType(field.getType().getSimpleName()); fieldModel.setOriginClass(entity.getSimpleName()); fieldModel.setEmbeddedType(ReflectionUtil.isComplexClass(field.getType())); if (ReflectionUtil.isCollectionClass(field.getType())) { DataType dataType = (DataType) field.getAnnotation(DataType.class); if (dataType != null) { String typeClass = dataType.value().getSimpleName(); if (StringUtils.isNotBlank(dataType.actualClassName())) { typeClass = dataType.actualClassName(); }/*from w ww .j ava2 s .c o m*/ fieldModel.setGenericType(typeClass); } } APIDescription description = (APIDescription) field.getAnnotation(APIDescription.class); if (description != null) { fieldModel.setDescription(description.value()); } for (Annotation annotation : field.getAnnotations()) { if (annotation.annotationType().getName().equals(APIDescription.class.getName()) == false) { EntityConstraintModel entityConstraintModel = new EntityConstraintModel(); entityConstraintModel.setName(annotation.annotationType().getSimpleName()); APIDescription annotationDescription = (APIDescription) annotation.annotationType() .getAnnotation(APIDescription.class); if (annotationDescription != null) { entityConstraintModel.setDescription(annotationDescription.value()); } //rules Object annObj = field.getAnnotation(annotation.annotationType()); if (annObj instanceof NotNull) { entityConstraintModel.setRules("Field is required"); } else if (annObj instanceof PK) { PK pk = (PK) annObj; entityConstraintModel.setRules("<b>Generated:</b> " + pk.generated()); fieldModel.setPrimaryKey(true); } else if (annObj instanceof FK) { FK fk = (FK) annObj; StringBuilder sb = new StringBuilder(); sb.append("<b>Foreign Key:</b> ").append(fk.value().getSimpleName()); sb.append(" (<b>Enforce</b>: ").append(fk.enforce()); sb.append(" <b>Soft reference</b>: ").append(fk.softReference()); if (StringUtils.isNotBlank(fk.referencedField())) { sb.append(" <b>Reference Field</b>: ").append(fk.referencedField()); } sb.append(" )"); entityConstraintModel.setRules(sb.toString()); } else if (annObj instanceof ConsumeField) { entityConstraintModel.setRules(""); } else if (annObj instanceof Size) { Size size = (Size) annObj; entityConstraintModel .setRules("<b>Min:</b> " + size.min() + " <b>Max:</b> " + size.max()); } else if (annObj instanceof Pattern) { Pattern pattern = (Pattern) annObj; entityConstraintModel.setRules("<b>Pattern:</b> " + pattern.regexp()); } else if (annObj instanceof Sanitize) { Sanitize sanitize = (Sanitize) annObj; entityConstraintModel .setRules("<b>Sanitize:</b> " + sanitize.value().getSimpleName()); } else if (annObj instanceof Unique) { Unique unique = (Unique) annObj; entityConstraintModel.setRules("<b>Handler:</b> " + unique.value().getSimpleName()); } else if (annObj instanceof ValidValueType) { ValidValueType validValueType = (ValidValueType) annObj; StringBuilder sb = new StringBuilder(); if (validValueType.value().length > 0) { sb.append(" <b>Values:</b> ").append(Arrays.toString(validValueType.value())); } if (validValueType.lookupClass().length > 0) { sb.append(" <b>Lookups:</b> "); for (Class lookupClass : validValueType.lookupClass()) { sb.append(lookupClass.getSimpleName()).append(" "); } } if (validValueType.enumClass().length > 0) { sb.append(" <b>Enumerations:</b> "); for (Class enumClass : validValueType.enumClass()) { sb.append(enumClass.getSimpleName()).append(" ("); sb.append(Arrays.toString(enumClass.getEnumConstants())).append(")"); } } entityConstraintModel.setRules(sb.toString()); } else if (annObj instanceof Min) { Min min = (Min) annObj; entityConstraintModel.setRules("<b>Min value:</b> " + min.value()); } else if (annObj instanceof Max) { Max max = (Max) annObj; entityConstraintModel.setRules("<b>Max value:</b> " + max.value()); } else if (annObj instanceof Version) { entityConstraintModel.setRules("Entity version; For Multi-Version control"); } else if (annObj instanceof DataType) { DataType dataType = (DataType) annObj; String typeClass = dataType.value().getSimpleName(); if (StringUtils.isNotBlank(dataType.actualClassName())) { typeClass = dataType.actualClassName(); } entityConstraintModel.setRules("<b>Type:</b> " + typeClass); } else { entityConstraintModel.setRules(annotation.toString()); } //Annotations that have related classes if (annObj instanceof DataType) { DataType dataType = (DataType) annObj; entityConstraintModel.getRelatedClasses().add(dataType.value().getSimpleName()); } if (annObj instanceof FK) { FK fk = (FK) annObj; entityConstraintModel.getRelatedClasses().add(fk.value().getSimpleName()); } if (annObj instanceof ValidValueType) { ValidValueType validValueType = (ValidValueType) annObj; for (Class lookupClass : validValueType.lookupClass()) { entityConstraintModel.getRelatedClasses().add(lookupClass.getSimpleName()); } StringBuilder sb = new StringBuilder(); for (Class enumClass : validValueType.enumClass()) { sb.append("<br>"); sb.append(enumClass.getSimpleName()).append(": ("); sb.append(Arrays.toString(enumClass.getEnumConstants())).append(")"); } entityConstraintModel .setRules(entityConstraintModel.getRules() + " " + sb.toString()); } fieldModel.getConstraints().add(entityConstraintModel); } } docModel.getFieldModels().add(fieldModel); } } addFields(entity.getSuperclass(), docModel); } }
From source file:com.crowsofwar.gorecore.config.ConfigLoader.java
public static void save(Object obj, String path) { try {/*from w w w .j a va 2 s . c o m*/ Map<String, Object> map = new HashMap<>(); Field[] fields = obj.getClass().getDeclaredFields(); for (Field field : fields) { if (field.getAnnotation(Load.class) != null) { field.setAccessible(true); map.put(field.getName(), field.get(obj)); } } ConfigLoader loader = new ConfigLoader(path, obj, map, false); loader.usedValues.putAll(map); loader.save(); } catch (Exception e) { GoreCore.LOGGER.error("Error saving config @ " + path, e); } }
From source file:org.cybercat.automation.annotations.AnnotationBuilder.java
/** * @param entity/*from w ww .j a v a2s. c om*/ * @param fields * @param i * @return * @throws AutomationFrameworkException */ @SuppressWarnings("unchecked") private static <T> Class<AbstractFeature> processCCFeatureField(T entity, Field field) throws AutomationFrameworkException { Class<AbstractFeature> clazz; try { clazz = (Class<AbstractFeature>) field.getType(); } catch (Exception e) { throw new AutomationFrameworkException("Unexpected field type :" + field.getType().getSimpleName() + " field name: " + field.getName() + " class: " + entity.getClass().getSimpleName() + " Thread ID:" + Thread.currentThread().getId() + " \n\tThis field must be of the type that extends AbstractPageObject class.", e); } try { field.set(entity, createFeature(versionControlPreprocessor(clazz))); } catch (Exception e) { throw new AutomationFrameworkException( "Set filed exception. Please, save this log and contact the Cybercat project support." + " field name: " + field.getName() + " class: " + entity.getClass().getSimpleName() + " Thread ID:" + Thread.currentThread().getId(), e); } return clazz; }