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:jfix.util.Reflections.java

/**
 * Initializes all declared static string fields in given class with name of
 * fields./*from   w ww  .  ja  v  a  2s  .  com*/
 */
public static void init(Class<?> clazz) {
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        try {
            field.set(null, field.getName());
        } catch (Exception e) {
        }
    }
}

From source file:com.seer.datacruncher.jpa.dao.MongoDbDao.java

/**
 * Gets table field name by class field.
 * Example: fieldId = 'idApplication', return = 'id_application'
 *
 * @param fieldId/*w ww  .  ja va  2  s .  com*/
 * @return
 */
private static String getFieldName(String fieldId) {
    for (Field field : DatastreamEntity.class.getDeclaredFields()) {
        if (field.getName() == fieldId) {
            return field.getAnnotation(Column.class).name();
        }
    }
    return null;
}

From source file:com.abiquo.model.util.ModelTransformer.java

public static <T> void transform(final Class sourceClass, final Class<T> targetClass, final Object source,
        final T target) throws Exception {
    Field[] transportFields = sourceClass.getDeclaredFields();
    Class superClass = sourceClass.getSuperclass();
    while (!superClass.getSimpleName().equalsIgnoreCase("SingleResourceTransportDto")) {
        transportFields = (Field[]) ArrayUtils.addAll(transportFields, superClass.getDeclaredFields());
        superClass = superClass.getSuperclass();
    }//from  w  w w  . ja  va2 s.co  m

    for (Field field : transportFields) {

        int modifiers = field.getModifiers();
        if (!Modifier.isTransient(modifiers) && !Modifier.isStatic(modifiers)) {
            String name = field.getName();
            try {
                if (fieldExist(name, targetClass) && fieldExist(name, source.getClass())
                        || getterExist(name, source.getClass())
                                && setterExist(name, targetClass, field.getType())) {
                    Object value = getter(name, source.getClass()).invoke(source, new Object[0]);

                    if (setterExist(name, targetClass, field.getType())) {
                        setter(name, targetClass, field.getType()).invoke(target, new Object[] { value });
                    }
                }
            } catch (InvocationTargetException e) {
                // Ignore invalid field
            }
        }

    }

}

From source file:com.test.edusys.common.utils.reflection.ReflectionUtils.java

/**
 * //  w  w  w  .  j a  v a2 s . c  o  m
 */
public static List<String> getShowLogFieldName(final Class clazz) {
    List<String> lS = new ArrayList<String>();
    Field[] fields = clazz.getDeclaredFields();
    for (Field f : fields) {
        String filedName = f.getName();
        Annotation[] annotation = f.getAnnotations();
        for (Annotation annotation2 : annotation) {
            if (annotation2.annotationType() == ShowLog.class)
                lS.add(filedName);
        }
    }
    return lS;
}

From source file:gumga.framework.presentation.api.CSVGeneratorAPI.java

public static String classToCsvTitle(Class clazz) {
    StringBuilder sb = new StringBuilder();
    for (Field f : getAllAtributes(clazz)) {
        sb.append(f.getName());
        sb.append(CSV_SEPARATOR);// w ww.j av  a2s.  co  m
    }
    sb.deleteCharAt(sb.length() - 1);
    sb.append(CSV_LINE_DELIMITER);
    return sb.toString();
}

From source file:com.test.edusys.common.utils.reflection.ReflectionUtils.java

/**
 * /*from  ww  w. ja  va 2  s  .  c  o  m*/
 */
public static String getKeyFieldName(final Class clazz) {
    Field[] fields = clazz.getDeclaredFields();
    for (Field f : fields) {
        String filedName = f.getName();
        Annotation[] annotation = f.getAnnotations();
        for (Annotation annotation2 : annotation) {
            Class annotationType = annotation2.annotationType();
            if (annotationType.getName().equals("org.nutz.dao.entity.annotation.Id"))
                return filedName;
            if (annotationType.getName().equals("org.nutz.dao.entity.annotation.Name"))
                return filedName;
        }
    }
    return null;
}

From source file:org.apache.aries.blueprint.plugin.model.Property.java

public static Property create(Matcher matcher, Field field) {
    Value value = field.getAnnotation(Value.class);
    if (needsInject(field)) {
        Bean matching = matcher.getMatching(field);
        String ref = (matching == null) ? getRefName(field) : matching.id;
        return new Property(field.getName(), ref, null);
    } else if (value != null) {
        return new Property(field.getName(), null, cleanValue(value.value()));
    } else {//from   ww w  . j a  va2  s .c om
        // Field is not a property
        return null;
    }
}

From source file:index.IndexUtils.java

/**
 * ??//from   w  w  w. j a  va 2  s . c om
 * @param doc
 * @param entityPath
 * @return
 * @throws ClassNotFoundException
 * @throws java.beans.IntrospectionException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws java.lang.reflect.InvocationTargetException
 * @throws InstantiationException
 * @throws NoSuchMethodException 
 */
public static Object getIndexResult(Document doc, String entityPath)
        throws ClassNotFoundException, IntrospectionException, IllegalAccessException, IllegalArgumentException,
        InvocationTargetException, InstantiationException, NoSuchMethodException {
    // ??
    Class c2 = Class.forName(entityPath);
    // 
    Object obj = c2.getConstructor(new Class[] {}).newInstance();
    // 
    Field[] fields = c2.getDeclaredFields();
    // ??
    for (Field field : fields) {
        // ??
        PropertyDescriptor pd = new PropertyDescriptor(field.getName(), c2);
        // setget
        Method method = pd.getWriteMethod();
        // 
        method.invoke(obj, new Object[] { doc.get(field.getName()) });
    }
    return obj;
}

From source file:HTMLColors.java

/** Initialiase colors map */
private static void initColorsMap() {
    Field[] fields = HTMLColors.class.getFields();
    for (Field field : fields) {
        if (field.getType().isAssignableFrom(Color.class)) {
            addColor(field.getName());
        }//from w ww .  j a  va2 s  .co m
    }
}

From source file:gr.abiss.calipso.uischema.serializer.UiSchemaSerializer.java

private static String getFormFieldConfig(Class domainClass, PropertyDescriptor descriptor, String fieldName) {
    String formSchemaJson = null;
    Field field = null;//  w  w  w  . ja v  a2 s. c o m
    StringBuffer formConfig = new StringBuffer();
    String key = domainClass.getName() + "#" + fieldName;
    String cached = CONFIG_CACHE.get(key);
    if (StringUtils.isNotBlank(cached)) {
        formConfig.append(cached);
    } else {
        Class tmpClass = domainClass;
        do {
            for (Field tmpField : tmpClass.getDeclaredFields()) {
                String candidateName = tmpField.getName();
                if (candidateName.equals(fieldName)) {
                    field = tmpField;
                    FormSchemas formSchemasAnnotation = null;
                    if (field.isAnnotationPresent(FormSchemas.class)) {
                        formSchemasAnnotation = field.getAnnotation(FormSchemas.class);
                        gr.abiss.calipso.uischema.annotation.FormSchemaEntry[] formSchemas = formSchemasAnnotation
                                .value();
                        LOGGER.info("getFormFieldConfig, formSchemas: " + formSchemas);
                        if (formSchemas != null) {
                            for (int i = 0; i < formSchemas.length; i++) {
                                if (i > 0) {
                                    formConfig.append(comma);
                                }
                                gr.abiss.calipso.uischema.annotation.FormSchemaEntry formSchemaAnnotation = formSchemas[i];
                                LOGGER.info(
                                        "getFormFieldConfig, formSchemaAnnotation: " + formSchemaAnnotation);
                                appendFormFieldSchema(formConfig, formSchemaAnnotation.state(),
                                        formSchemaAnnotation.json());
                            }
                        }
                        //formConfig = formSchemasAnnotation.json();
                    } else {
                        appendFormFieldSchema(formConfig,
                                gr.abiss.calipso.uischema.annotation.FormSchemaEntry.STATE_DEFAULT,
                                gr.abiss.calipso.uischema.annotation.FormSchemaEntry.TYPE_STRING);
                    }
                    break;
                }
            }
            tmpClass = tmpClass.getSuperclass();
        } while (tmpClass != null && field == null);
        formSchemaJson = formConfig.toString();
        CONFIG_CACHE.put(key, formSchemaJson);
    }

    return formSchemaJson;
}