Example usage for java.lang Class getDeclaredFields

List of usage examples for java.lang Class getDeclaredFields

Introduction

In this page you can find the example usage for java.lang Class getDeclaredFields.

Prototype

@CallerSensitive
public Field[] getDeclaredFields() throws SecurityException 

Source Link

Document

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.

Usage

From source file:be.fedict.eid.applet.service.AppletServiceServlet.java

public static void injectInitParams(ServletConfig config, MessageHandler<?> messageHandler)
        throws ServletException, IllegalArgumentException, IllegalAccessException {
    Class<?> messageHandlerClass = messageHandler.getClass();
    Field[] fields = messageHandlerClass.getDeclaredFields();
    for (Field field : fields) {
        InitParam initParamAnnotation = field.getAnnotation(InitParam.class);
        if (null == initParamAnnotation) {
            continue;
        }/*  w  w w  .  java2s . c om*/
        String initParamName = initParamAnnotation.value();
        Class<?> fieldType = field.getType();
        field.setAccessible(true);
        if (ServiceLocator.class.equals(fieldType)) {
            /*
             * We always inject a service locator.
             */
            ServiceLocator<Object> fieldValue = new ServiceLocator<Object>(initParamName, config);
            field.set(messageHandler, fieldValue);
            continue;
        }
        String initParamValue = config.getInitParameter(initParamName);
        if (initParamAnnotation.required() && null == initParamValue) {
            throw new ServletException("missing required init-param: " + initParamName + " for message handler:"
                    + messageHandlerClass.getName());
        }
        if (null == initParamValue) {
            continue;
        }
        if (Boolean.TYPE.equals(fieldType)) {
            LOG.debug("injecting boolean: " + initParamValue);
            Boolean fieldValue = Boolean.parseBoolean(initParamValue);
            field.set(messageHandler, fieldValue);
            continue;
        }
        if (String.class.equals(fieldType)) {
            field.set(messageHandler, initParamValue);
            continue;
        }
        if (InetAddress.class.equals(fieldType)) {
            InetAddress inetAddress;
            try {
                inetAddress = InetAddress.getByName(initParamValue);
            } catch (UnknownHostException e) {
                throw new ServletException("unknown host: " + initParamValue);
            }
            field.set(messageHandler, inetAddress);
            continue;
        }
        if (Long.class.equals(fieldType)) {
            Long fieldValue = Long.parseLong(initParamValue);
            field.set(messageHandler, fieldValue);
            continue;
        }
        throw new ServletException("unsupported init-param field type: " + fieldType.getName());
    }
}

From source file:com.ghy.common.util.reflection.ReflectionUtils.java

/**
 * ?string//  w  w w.  ja  v a  2  s  . c  om
 * @param object
 * @param map
 * @throws Exception 
 * @throws NoSuchMethodException 
 * @throws SecurityException 
 */
public static void fromObtoMap(Object object, Map<String, String> map)
        throws SecurityException, NoSuchMethodException, Exception {
    if (object != null) {//if (object!=null )  ----begin
        // 
        Class<?> clz = object.getClass();
        // ?Field
        Field[] fields = clz.getDeclaredFields();
        for (Field field : fields) {// --for() begin
            // String
            if (field.getGenericType().toString().equals("class java.lang.String")) { // type???"class "????
                // gettet
                String methodName = field.getName();
                Method m = (Method) object.getClass().getMethod("get" + getMethodName(methodName));
                String val = (String) m.invoke(object);// getter?
                if (val != null) {
                    map.put(methodName, val);
                }
            }
        }
    }
}

From source file:com.buaa.cfs.utils.ReflectionUtils.java

/**
 * Gets all the declared fields of a class including fields declared in superclasses.
 *///from   w w  w  . j a  va 2  s .  c o m
public static List<Field> getDeclaredFieldsIncludingInherited(Class<?> clazz) {
    List<Field> fields = new ArrayList<Field>();
    while (clazz != null) {
        for (Field field : clazz.getDeclaredFields()) {
            fields.add(field);
        }
        clazz = clazz.getSuperclass();
    }

    return fields;
}

From source file:com.palantir.ptoss.util.Reflections.java

/**
 * Gets all fields from a given class that are assignable from the target class.
 * @param klass type to query for fields.
 * @param targetClass interface or class that fields must be assignable from to be
 * returned.//from   ww  w  .j a  v  a2  s. c  o m
 * @return all fields in <code>klass</code> that are assignable from
 * <code>targetClass</code>
 * @see Class#isAssignableFrom(Class)
 * @see Class#getDeclaredFields()
 */
public static List<Field> getFieldsOfType(Class<?> klass, Class<?> targetClass) {
    List<Field> fields = Lists.newArrayList();
    for (Field f : klass.getDeclaredFields()) {
        if (targetClass.isAssignableFrom(f.getType())) {
            fields.add(f);
        }
    }
    return fields;
}

From source file:org.mayocat.application.AbstractService.java

private static List<Field> getAllFields(Class<?> type) {
    List<Field> fields = new ArrayList<Field>();

    fields.addAll(Arrays.asList(type.getDeclaredFields()));

    if (type.getSuperclass() != null) {
        fields.addAll(getAllFields(type.getSuperclass()));
    }/*from w  w w  .j a v  a  2s.c  o  m*/

    return fields;
}

From source file:com.fengduo.bee.commons.core.lang.BeanUtils.java

/**
 * ?field,//from  w  w  w.  j  a v  a 2  s . com
 * 
 * @param fields
 * @param type
 * @return
 */
public static Field[] getAllFields(Collection<Field> fields, Class<?> type) {
    if (Argument.isEmpty(fields)) {
        fields = new HashSet<Field>();
    }
    for (Field field : type.getDeclaredFields()) {
        fields.add(field);
    }
    if (type.getSuperclass() != null) {
        fields.addAll(Arrays.asList(getAllFields(fields, type.getSuperclass())));
    }
    return fields.toArray(new Field[fields.size()]);
}

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

private static void processIntegrationService(Object targetObject, Class<? extends Object> clazz)
        throws AutomationFrameworkException {
    if (clazz == null)
        return;/*  w ww . j  a  v  a  2s  .c o m*/
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        if (field.getAnnotation(CCProperty.class) != null) {
            processPropertyField(targetObject, field);
        }
    }
    processIntegrationService(targetObject, clazz.getSuperclass());
}

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

@SuppressWarnings("unchecked")
private static final <T extends AbstractPageObject> void processCCPageFragment(T entity,
        Class<? extends AbstractPageObject> clazz) throws AutomationFrameworkException {
    if (clazz == null)
        return;/* ww w  . j a  v a 2  s .  c  o  m*/
    Field[] fields = clazz.getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        fields[i].setAccessible(true);
        if (fields[i].getAnnotation(CCPageFragment.class) != null) {
            entity.addPageFragment(createPageObjectField(entity, fields[i]));
        }
    }
    processCCPageFragment(entity, (Class<? extends AbstractPageObject>) clazz.getSuperclass());
}

From source file:com.fengduo.bee.commons.core.lang.BeanUtils.java

public static LinkedList<Field> _getAllFields(LinkedList<Field> fields, Class<?> type) {
    if (Argument.isEmpty(fields)) {
        fields = new LinkedList<Field>();
    }/* w  ww  . j ava  2s  .  co  m*/
    for (Field field : type.getDeclaredFields()) {
        fields.add(field);
    }
    if (type.getSuperclass() != null) {
        fields.addAll(_getAllFields(fields, type.getSuperclass()));
    }
    return fields;
}

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

private static <T extends Object> void processCCFeatureForObject(T entity, Class<? extends Object> clazz)
        throws AutomationFrameworkException {
    if (clazz == null)
        return;//  www.  j  a va  2  s .co m
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        if (field.getAnnotation(CCFeature.class) != null) {
            processCCFeatureField(entity, field);
        } else if (field.getAnnotation(CCIntegrationService.class) != null) {
            createIntegrationService(field, entity);
        } else if (field.getAnnotation(CCProperty.class) != null) {
            processPropertyField(entity, field);
        }
    }
    processCCFeatureForObject(entity, (Class<? extends Object>) clazz.getSuperclass());
}