List of usage examples for java.lang.reflect Field getName
public String getName()
From source file:com.nridge.core.base.field.data.DataBeanBag.java
private static DataField reflectField(Object anObject, Field aField) throws NoSuchFieldException, IllegalAccessException { DataField dataField;//from w ww . j av a2 s. co m String fieldName, fieldValue, fieldTitle; com.nridge.core.base.field.Field.Type fieldType; fieldName = aField.getName(); fieldTitle = com.nridge.core.base.field.Field.nameToTitle(fieldName); Class<?> classFieldType = aField.getType(); Object fieldObject = aField.get(anObject); if ((StringUtils.endsWith(classFieldType.getName(), "Integer")) || (StringUtils.endsWith(classFieldType.getName(), "int"))) { fieldType = com.nridge.core.base.field.Field.Type.Integer; fieldValue = fieldObject.toString(); dataField = new DataField(fieldType, fieldName, fieldTitle, fieldValue); } else if ((StringUtils.endsWith(classFieldType.getName(), "Float")) || (StringUtils.endsWith(classFieldType.getName(), "float"))) { fieldType = com.nridge.core.base.field.Field.Type.Float; fieldValue = fieldObject.toString(); dataField = new DataField(fieldType, fieldName, fieldTitle, fieldValue); } else if ((StringUtils.endsWith(classFieldType.getName(), "Double")) || (StringUtils.endsWith(classFieldType.getName(), "double"))) { fieldType = com.nridge.core.base.field.Field.Type.Double; fieldValue = fieldObject.toString(); dataField = new DataField(fieldType, fieldName, fieldTitle, fieldValue); } else if ((StringUtils.endsWith(classFieldType.getName(), "Boolean")) || (StringUtils.endsWith(classFieldType.getName(), "boolean"))) { fieldType = com.nridge.core.base.field.Field.Type.Boolean; fieldValue = fieldObject.toString(); dataField = new DataField(fieldType, fieldName, fieldTitle, fieldValue); } else if (StringUtils.endsWith(classFieldType.getName(), "Date")) { fieldType = com.nridge.core.base.field.Field.Type.Date; Date fieldDate = (Date) fieldObject; fieldValue = com.nridge.core.base.field.Field.dateValueFormatted(fieldDate, null); dataField = new DataField(fieldType, fieldName, fieldTitle, fieldValue); } else { fieldType = com.nridge.core.base.field.Field.Type.Text; if (fieldObject instanceof String[]) { String[] fieldValues = (String[]) fieldObject; dataField = new DataField(fieldType, fieldName, fieldTitle); dataField.setMultiValueFlag(true); dataField.setValues(fieldValues); } else if (fieldObject instanceof Collection) { ArrayList<String> fieldValues = (ArrayList<String>) fieldObject; dataField = new DataField(fieldType, fieldName, fieldTitle); dataField.setMultiValueFlag(true); dataField.setValues(fieldValues); } else { fieldValue = fieldObject.toString(); dataField = new DataField(fieldType, fieldName, fieldTitle, fieldValue); } } return dataField; }
From source file:com.stratio.deep.es.utils.UtilES.java
/** * converts from JSONObject to an entity class with deep's anotations * * @param classEntity the entity name.// w w w.j ava 2 s .c o m * @param jsonObject the instance of the JSONObject to convert. * @param <T> return type. * @return the provided JSONObject converted to an instance of T. * @throws IllegalAccessException * @throws InstantiationException * @throws java.lang.reflect.InvocationTargetException */ public static <T> T getObjectFromJson(Class<T> classEntity, LinkedMapWritable jsonObject) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { T t = classEntity.newInstance(); Field[] fields = AnnotationUtils.filterDeepFields(classEntity); Object insert; for (Field field : fields) { Method method = Utils.findSetter(field.getName(), classEntity, field.getType()); Class<?> classField = field.getType(); String key = AnnotationUtils.deepFieldName(field); Text text = new org.apache.hadoop.io.Text(key); Writable currentJson = jsonObject.get(text); if (currentJson != null) { if (Iterable.class.isAssignableFrom(classField)) { Type type = field.getGenericType(); insert = subDocumentListCase(type, (ArrayWritable) currentJson); method.invoke(t, (insert)); } else if (IDeepType.class.isAssignableFrom(classField)) { insert = getObjectFromJson(classField, (LinkedMapWritable) currentJson); method.invoke(t, (insert)); } else { insert = currentJson; try { method.invoke(t, getObjectFromWritable((Writable) insert)); } catch (Exception e) { LOG.error("impossible to convert field " + t + " :" + field + " error: " + e.getMessage()); method.invoke(t, Utils.castNumberType(getObjectFromWritable((Writable) insert), t.getClass())); } } } } return t; }
From source file:br.gov.frameworkdemoiselle.util.contrib.Strings.java
public static String toString(Object object) { StringBuffer result = new StringBuffer(); Object fieldValue;// www. j a v a 2s. c om if (object != null) { result.append(object.getClass().getSimpleName()); result.append(" ["); boolean first = true; for (Field field : Reflections.getNonStaticDeclaredFields(object.getClass())) { if (!field.isAnnotationPresent(Ignore.class)) { if (first) { first = false; } else { result.append(", "); } result.append(field.getName()); result.append("="); fieldValue = Reflections.getFieldValue(field, object); result.append(fieldValue != null && fieldValue.getClass().isArray() ? Arrays.toString((Object[]) fieldValue) : fieldValue); } } result.append("]"); } return result.toString(); }
From source file:cz.cuni.mff.d3s.spl.example.newton.app.Main.java
private static void inspectClass(Object obj) { if (!INSPECT) { return;//from www . jav a 2 s . com } System.out.printf("Inspecting %s:\n", obj); Class<?> klass = obj.getClass(); System.out.printf(" Class: %s\n", klass.getName()); for (Field f : klass.getDeclaredFields()) { Object value; boolean accessible = f.isAccessible(); try { f.setAccessible(true); value = f.get(obj); } catch (IllegalArgumentException | IllegalAccessException e) { value = String.format("<failed to read: %s>", e.getMessage()); } f.setAccessible(accessible); System.out.printf(" Field %s: %s\n", f.getName(), value); } for (Method m : klass.getDeclaredMethods()) { System.out.printf(" Method %s\n", m.getName()); } System.out.printf("-------\n"); System.out.flush(); }
From source file:com.github.gekoh.yagen.util.FieldInfo.java
private static void addAttributeOverrides(Map<String, String> overrides, String path, Class type) { for (Field field : type.getDeclaredFields()) { String curPath = path + field.getName() + "."; Column column;//from w w w .j a v a 2 s . c om if (field.isAnnotationPresent(AttributeOverride.class)) { addAttributeOverride(overrides, addNamePrefixToAttributeOverride( formatAnnotation(field.getAnnotation(AttributeOverride.class)), curPath)); } else if (field.isAnnotationPresent(AttributeOverrides.class)) { for (AttributeOverride attributeOverride : field.getAnnotation(AttributeOverrides.class).value()) { addAttributeOverride(overrides, addNamePrefixToAttributeOverride(formatAnnotation(attributeOverride), curPath)); } } else if (((column = field.getAnnotation(Column.class)) != null && (!column.nullable() || column.unique())) || (field.isAnnotationPresent(Basic.class) && !field.getAnnotation(Basic.class).optional())) { String columnName = column != null ? column.name() : field.getName(); int length = column != null ? column.length() : 255; String override = "@javax.persistence.AttributeOverride(name=\"" + path + field.getName() + "\", column=" + "@javax.persistence.Column(name=\"" + columnName + "\", length=" + length + ", nullable=true, unique=false))"; addAttributeOverride(overrides, override); } if (field.isAnnotationPresent(Embedded.class)) { addAttributeOverrides(overrides, curPath, field.getType()); } } }
From source file:com.screenslicer.webapp.ScreenSlicerClient.java
@Path("configure") @POST/*from www. j av a2 s . c o m*/ @Produces("application/json") @Consumes("application/json") public static final Response configure(String reqString) { try { if (reqString != null) { final String reqDecoded = Crypto.decode(reqString, CommonUtil.ip()); if (reqDecoded != null) { final Map<String, Object> args = CommonUtil.gson.fromJson(reqDecoded, CommonUtil.objectType); final Request request = CommonUtil.gson.fromJson(reqDecoded, Request.class); Field[] fields = request.getClass().getFields(); for (Field field : fields) { args.remove(field.getName()); } Map<String, Object> conf = customApp.configure(request, args); if (conf != null) { return Response.ok( Crypto.encode(CommonUtil.gson.toJson(conf, CommonUtil.objectType), CommonUtil.ip())) .build(); } } } } catch (Exception e) { Log.exception(e); } return null; }
From source file:br.com.renatoccosta.regexrenamer.element.base.ElementFactory.java
public static void setParameter(Element element, Field field, Object value) throws InvalidParameterException { try {/*from w w w. j av a 2 s . com*/ //for security reasons, only parameters can be set with this method if (field.getAnnotation(Parameter.class) != null) { if (value instanceof String) { value = utilsBean.getConvertUtils().convert((String) value, field.getType()); } PropertyUtils.setProperty(element, field.getName(), value); } else { throw createInvalidParameterException(field.getName(), null); } } catch (Exception ex) { throw createInvalidParameterException(field.getName(), ex); } }
From source file:cn.wanghaomiao.seimi.utils.StructValidator.java
public static boolean validateAnno(Object object) { for (Field field : object.getClass().getDeclaredFields()) { NotNull notNullCheck = field.getAnnotation(NotNull.class); if (notNullCheck != null) { try { Object val = FieldUtils.readField(field, object, true); if (StringUtils.isBlank(String.valueOf(val))) { logger.error("Field={}.{} can not be null!", object.getClass().getSimpleName(), field.getName()); return false; }/* w w w . j av a 2s. co m*/ } catch (IllegalAccessException e) { logger.error(e.getMessage(), e); } } } return true; }
From source file:com.autobizlogic.abl.util.BeanUtil.java
private static Field getFieldFromClass(Class<?> cls, String fieldName, Class<?> argClass, boolean onlyProtectedAndHigher) { Field[] allFields = cls.getDeclaredFields(); for (Field field : allFields) { if (!field.getName().equals(fieldName)) continue; int modifiers = field.getModifiers(); if (onlyProtectedAndHigher && Modifier.isPrivate(modifiers)) continue; if (argClass != null) { Class<?> genericType = getGenericType(field.getType()); if (!genericType.isAssignableFrom(argClass)) { String extraInfo = ""; // If the two classes have the same name, it's probably a classloader problem, // so we generate more informative output to help debug if (field.getType().getName().equals(argClass.getName())) { extraInfo = ". It looks like the two classes have the same name, so this is " + "probably a classloader issue. The bean field's class comes from " + field.getType().getClassLoader() + ", the other class comes from " + argClass.getClassLoader(); }// ww w .j ava2 s .co m throw new RuntimeException("Bean field " + fieldName + " of class " + cls.getName() + " is of the wrong type (" + field.getType().getName() + ") for the given argument, " + "which is of type " + argClass.getName() + extraInfo); } } return field; } return null; }
From source file:IntrospectionUtil.java
public static boolean containsSameFieldName(Field field, Class c, boolean checkPackage) { if (checkPackage) { if (!c.getPackage().equals(field.getDeclaringClass().getPackage())) return false; }/*w ww .j a v a2 s.c o m*/ boolean sameName = false; Field[] fields = c.getDeclaredFields(); for (int i = 0; i < fields.length && !sameName; i++) { if (fields[i].getName().equals(field.getName())) sameName = true; } return sameName; }