Example usage for java.lang.reflect Field getName

List of usage examples for java.lang.reflect Field getName

Introduction

In this page you can find the example usage for java.lang.reflect Field getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the field represented by this Field object.

Usage

From source file:com.zlfun.framework.excel.ExcelUtils.java

private static <T> T fill(Map<String, String> map, T t) {
    T bean = t;//w ww . j  av  a  2  s .co  m
    try {

        if (t.getClass().getAnnotation(WebParam.class) == null) {

            Field[] fs = t.getClass().getDeclaredFields();
            for (Field f : fs) {

                set(bean, f, map.get(f.getName()));

            }
        } else {
            Field[] fs = t.getClass().getDeclaredFields();
            for (Field f : fs) {
                WebParam param = f.getAnnotation(WebParam.class);
                if (param != null) {
                    String fname;
                    if ("".equals(param.value())) {
                        fname = f.getName();
                    } else {
                        fname = param.value();
                    }

                    set(bean, f, map.get(fname));
                }

            }

            // ?
            Class<?> parent = t.getClass().getSuperclass();
            while (parent != Object.class) {

                if (parent.getAnnotation(ItemField.class) != null) {

                    Field[] pfs = parent.getDeclaredFields();
                    for (Field f : pfs) {
                        WebParam param = f.getAnnotation(WebParam.class);
                        if (param != null) {
                            String fname;
                            if ("".equals(param.value())) {
                                fname = f.getName();
                            } else {
                                fname = param.value();
                            }

                            set(bean, f, map.get(fname));
                        }

                    }

                    parent = parent.getSuperclass();
                } else {
                    break;
                }

            }

        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bean;
}

From source file:com.jk.util.JKObjectUtil.java

/**
 * Gets the instance variables./*  w  ww. j ava2 s  .  c om*/
 *
 * @param <T>
 *            the generic type
 * @param clas
 *            the clas
 * @return the instance variables
 */
public static <T> String getInstanceVariables(Class<T> clas) {
    StringBuffer fieldsString = new StringBuffer();
    Field[] fields = clas.getDeclaredFields();
    int i = 0;
    for (Field field : fields) {
        if (i++ > 0) {
            fieldsString.append(JK.CSV_SEPARATOR);
        }
        fieldsString.append(field.getName());
    }
    return fieldsString.toString();
}

From source file:org.cybercat.automation.annotations.AnnotationBuilder.java

/**
 * @param targetObject//from ww  w . ja  va  2  s  . c  o m
 * @param fields
 * @return
 * @throws AutomationFrameworkException
 * @throws PageObjectException
 */
@SuppressWarnings("unchecked")
private static <T extends AbstractPageObject> T createPageObjectField(Object targetObject, Field field)
        throws AutomationFrameworkException, PageObjectException {
    AutomationMain mainFactory = AutomationMain.getMainFactory();
    PageFactory pageFactory = mainFactory.getPageFactory();
    Class<AbstractPageObject> clazz;
    try {
        clazz = (Class<AbstractPageObject>) field.getType();
    } catch (Exception e) {
        throw new AutomationFrameworkException("Unexpected field type :" + field.getType().getSimpleName()
                + " field name: " + field.getName() + " class: " + targetObject.getClass().getSimpleName()
                + " Thread ID:" + Thread.currentThread().getId()
                + " \n\tThis field must be of the type that extends AbstractPageObject class.", e);
    }
    try {
        T po = (T) pageFactory.createPage(clazz);
        po.setPageFactory(pageFactory);
        field.set(targetObject, po);
        return po;
    } catch (Exception e) {
        throw new AutomationFrameworkException(
                "Set filed exception. Please, save this log and contact the Cybercat project support."
                        + " field name: " + field.getName() + " class: "
                        + targetObject.getClass().getSimpleName() + " Thread ID:"
                        + Thread.currentThread().getId(),
                e);
    }
}

From source file:com.github.maven_nar.NarUtil.java

/**
 * Produces a human-readable string of the given object which has fields
 * annotated with the Maven {@link Parameter} annotation.
 * //  w w  w . j a  v a2 s  . c  o m
 * @param o The object for which a human-readable string is desired.
 * @return A human-readable string, with each {@code @Parameter} field on a
 *         separate line rendered as a key/value pair.
 */
public static String prettyMavenString(final Object o) {
    final StringBuilder sb = new StringBuilder();
    sb.append(o.getClass().getName()).append(":\n");
    for (final Field f : o.getClass().getDeclaredFields()) {
        if (f.getAnnotation(Parameter.class) == null)
            continue;
        sb.append("\t").append(f.getName()).append("=").append(fieldValue(f, o)).append("\n");
    }
    return sb.toString();
}

From source file:ee.ria.xroad.common.message.SoapUtils.java

/**
 * Checks consistency of two SOAP headers.
 * @param h1 the first SOAP header//from w  w  w.jav a2s . c  o m
 * @param h2 the second SOAP header
 */
public static void checkConsistency(SoapHeader h1, SoapHeader h2) {
    for (Field field : SoapHeader.class.getDeclaredFields()) {
        if (field.isAnnotationPresent(CheckConsistency.class)) {
            Object value1 = getFieldValue(field, h1);
            Object value2 = getFieldValue(field, h2);
            if (ObjectUtils.notEqual(value1, value2)) {
                throw new CodedException(X_INCONSISTENT_HEADERS,
                        "Field '%s' does not match in request and response", field.getName());
            }
        }
    }
}

From source file:hu.javaforum.commons.ReflectionHelper.java

/**
 * Returns with the name of the field.//from w ww .j a  v a2  s  .  c  o m
 * Returns:
 * - with annotated name, if the Field has XmlElement annotation
 * - otherwise with the name of the field
 *
 * @param field The Field instance
 * @return Field name as char array
 */
public static char[] getFieldName(final Field field) {
    if (field == null) {
        return new char[0];
    }

    if (XML_ELEMENT_LOADED) {
        final Annotation[] annotations = field.getAnnotations();
        for (Annotation annotation : annotations) {
            if (XmlElement.class.equals(annotation.annotationType())) {
                return ((XmlElement) annotation).name().toCharArray();
            }
        }
    }

    return field.getName().toCharArray();
}

From source file:com.yiji.openapi.sdk.util.Reflections.java

public static Set<String> getSimpleFieldNames(Class<?> pojoClass) {
    Set<String> propertyNames = new HashSet<String>();
    Class<?> clazz = pojoClass;
    do {/*  w w  w .  ja  va  2 s.  c  om*/
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (!Modifier.isStatic(field.getModifiers()) && (field.getType().isPrimitive()
                    || isWrapClass(field.getType()) || field.getType().isAssignableFrom(Timestamp.class)
                    || field.getType().isAssignableFrom(Date.class)
                    || field.getType().isAssignableFrom(String.class)
                    || field.getType().isAssignableFrom(Calendar.class))) {
                propertyNames.add(field.getName());
            }
        }
        clazz = clazz.getSuperclass();
    } while (clazz != null && !clazz.getSimpleName().equalsIgnoreCase("Object"));
    return propertyNames;
}

From source file:edu.usu.sdl.openstorefront.doc.JaxrsProcessor.java

private static void mapParameters(List<APIParamModel> parameterList, Field fields[]) {
    for (Field field : fields) {
        APIParamModel paramModel = new APIParamModel();
        paramModel.setFieldName(field.getName());

        QueryParam queryParam = (QueryParam) field.getAnnotation(QueryParam.class);
        FormParam formParam = (FormParam) field.getAnnotation(FormParam.class);
        MatrixParam matrixParam = (MatrixParam) field.getAnnotation(MatrixParam.class);
        HeaderParam headerParam = (HeaderParam) field.getAnnotation(HeaderParam.class);
        CookieParam cookieParam = (CookieParam) field.getAnnotation(CookieParam.class);
        PathParam pathParam = (PathParam) field.getAnnotation(PathParam.class);
        BeanParam beanParam = (BeanParam) field.getAnnotation(BeanParam.class);

        if (queryParam != null) {
            paramModel.setParameterType(QueryParam.class.getSimpleName());
            paramModel.setParameterName(queryParam.value());
        }/*from w w  w.  j  a  v a  2  s  . com*/
        if (formParam != null) {
            paramModel.setParameterType(FormParam.class.getSimpleName());
            paramModel.setParameterName(formParam.value());
        }
        if (matrixParam != null) {
            paramModel.setParameterType(MatrixParam.class.getSimpleName());
            paramModel.setParameterName(matrixParam.value());
        }
        if (pathParam != null) {
            paramModel.setParameterType(PathParam.class.getSimpleName());
            paramModel.setParameterName(pathParam.value());
        }
        if (headerParam != null) {
            paramModel.setParameterType(HeaderParam.class.getSimpleName());
            paramModel.setParameterName(headerParam.value());
        }
        if (cookieParam != null) {
            paramModel.setParameterType(CookieParam.class.getSimpleName());
            paramModel.setParameterName(cookieParam.value());
        }

        if (beanParam != null) {
            Class fieldClass = field.getDeclaringClass();
            mapParameters(parameterList, fieldClass.getDeclaredFields());
        }

        if (StringUtils.isNotBlank(paramModel.getParameterType())) {

            APIDescription aPIDescription = (APIDescription) field.getAnnotation(APIDescription.class);
            if (aPIDescription != null) {
                paramModel.setParameterDescription(aPIDescription.value());
            }

            ParameterRestrictions restrictions = (ParameterRestrictions) field
                    .getAnnotation(ParameterRestrictions.class);
            if (restrictions != null) {
                paramModel.setRestrictions(restrictions.value());
            }

            RequiredParam requiredParam = (RequiredParam) field.getAnnotation(RequiredParam.class);
            if (requiredParam != null) {
                paramModel.setRequired(true);
            }

            DefaultValue defaultValue = (DefaultValue) field.getAnnotation(DefaultValue.class);
            if (defaultValue != null) {
                paramModel.setDefaultValue(defaultValue.value());
            }

            parameterList.add(paramModel);
        }
    }
}

From source file:com.jilk.ros.rosbridge.implementation.JSON.java

private static JSONObject convertObjectToJSONObject(Object o) {
    JSONObject result = new JSONObject();
    for (Field f : o.getClass().getFields()) {
        Object fieldObject = getFieldObject(f, o);
        if (fieldObject != null) {
            Object resultObject;//from  w  w w .  j ava 2s .  c o m
            if (Indication.isBase64Encoded(f))
                resultObject = convertByteArrayToBase64JSONString(fieldObject);
            else if (Indication.asArray(f))
                resultObject = convertObjectToJSONArray(fieldObject);
            else
                resultObject = convertElementToJSON(fieldObject);
            result.put(f.getName(), resultObject);
        }
    }
    return result;
}

From source file:kina.utils.UtilMongoDB.java

/**
 * Returns the field name as known by the datastore. If the provided field object Field annotation
 * specifies the fieldName property, the value of this property will be returned, otherwise the java field name
 * will be returned.// w  w w.  jav a  2 s . c  o m
 *
 * @param field the Field object associated to the property for which we want to resolve the name.
 * @return the field name.
 */
public static String kinaFieldName(java.lang.reflect.Field field) {

    kina.annotations.Field fieldAnnotation = field.getAnnotation(kina.annotations.Field.class);
    Key genericKeyAnnotation = field.getAnnotation(Key.class);
    String fieldName = null;

    if (fieldAnnotation != null) {
        fieldName = fieldAnnotation.fieldName();
    } else if (genericKeyAnnotation != null) {
        fieldName = genericKeyAnnotation.fieldName();
    }

    if (StringUtils.isNotEmpty(fieldName)) {
        return fieldName;
    } else {
        return field.getName();
    }
}