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:com.webpagebytes.cms.engine.JSONToFromObjectConverter.java

public org.json.JSONObject JSONFromObject(Object object) {
    org.json.JSONObject json = new org.json.JSONObject();
    if (null == object)
        return json;
    Class<? extends Object> objClass = object.getClass();
    Field[] fields = objClass.getDeclaredFields();
    for (Field field : fields) {
        Object storeAdn = field.getAnnotation(WPBAdminFieldStore.class);
        if (storeAdn == null) {
            storeAdn = field.getAnnotation(WPBAdminFieldKey.class);
            if (storeAdn == null) {
                storeAdn = field.getAnnotation(WPBAdminFieldTextStore.class);
                if (storeAdn == null) {
                    storeAdn = field.getAnnotation(WPBAdminField.class);
                }/*www . j a v a  2  s  .  c o  m*/
            }
        }

        if (storeAdn != null) {
            String fieldName = field.getName();
            try {
                PropertyDescriptor pd = new PropertyDescriptor(fieldName, objClass);
                Object value = pd.getReadMethod().invoke(object);
                String fieldValue = JSONStringFromField(value, field.getType());
                if (fieldValue != null) {
                    json.put(fieldName, fieldValue);
                }
            } catch (Exception e) {
                // do nothing, there is no write method for our field
            }
        }
    }
    return json;
}

From source file:gov.nih.nci.system.webservice.WSQuery.java

private Object copyValue(Object newObject, Object obj, Class objKlass) {
    try {/*from  w  w  w.  j a  v a  2s  .c om*/
        Field[] newObjFields = objKlass.getDeclaredFields();
        for (int i = 0; i < newObjFields.length; i++) {
            Field field = newObjFields[i];
            field.setAccessible(true);
            String fieldName = field.getName();
            if (fieldName.equals("serialVersionUID"))
                continue;

            //            if (field.getType().getName().indexOf("gov.nih.nci") > -1) continue;
            if (field.getType().isPrimitive() || field.getType().getName().startsWith("java.")) {
                String getterMethodName = "get" + fieldName.substring(0, 1).toUpperCase()
                        + fieldName.substring(1);
                String setterMethodName = "set" + fieldName.substring(0, 1).toUpperCase()
                        + fieldName.substring(1);
                log.debug("WSQuery.copyValue:  methodName = " + getterMethodName);
                Method getterMethod = objKlass.getMethod(getterMethodName);
                Object value = getterMethod.invoke(obj);
                Method setterMethod = newObject.getClass().getMethod(setterMethodName,
                        new Class[] { getterMethod.getReturnType() });
                setterMethod.invoke(newObject, new Object[] { value });
            }
        }
        // the superclass
        objKlass = objKlass.getSuperclass();
        while (objKlass != null && !objKlass.equals(Object.class) && !objKlass.isInterface()) {
            copyValue(newObject, obj, objKlass);
            objKlass = objKlass.getSuperclass();
        }

    } catch (Exception e) {
        e.printStackTrace();
        log.error("WSQuery.copyValue: WS Error" + e.getMessage());
        //System.out.println("WSQuery.copyValue: WS Error"+ e);
        return null;
    }
    return newObject;
}

From source file:com.nabla.wapp.server.database.UpdateStatement.java

@SuppressWarnings("unchecked")
private IRecordTable commonConstructor(final Class clazz) {
    if (clazz == null)
        return null;
    final IRecordTable t = (IRecordTable) clazz.getAnnotation(IRecordTable.class);
    for (Field field : clazz.getDeclaredFields()) {
        final IRecordField definition = field.getAnnotation(IRecordField.class);
        if (definition == null)
            continue;
        if (definition.id())
            recordId = createParameter(field);
        else {// w  ww. ja v  a  2  s.  c o m
            if (definition.unique())
                uniqueFieldName = field.getName();
            parameters.add(createParameter(field));
        }
    }
    final IRecordTable tt = commonConstructor(clazz.getSuperclass());
    return (t == null) ? tt : t;
}

From source file:main.okapi.cf.annotations.AnnotationsInfo.java

public JSONObject getInfo() throws ClassNotFoundException, IOException, JSONException {
    JSONObject obj = new JSONObject();
    ArrayList<JSONObject> cl = new ArrayList<JSONObject>();
    Iterable<Class> classes = getClasses(this.topPackage);
    for (Class c : classes) {
        JSONObject forClass = new JSONObject();
        if (c.getAnnotations().length > 0) {

            ArrayList<JSONObject> parameters = new ArrayList<JSONObject>();
            for (Field field : c.getDeclaredFields()) {
                if (field.isAnnotationPresent(HyperParameter.class)) {
                    HyperParameter hp = field.getAnnotation(HyperParameter.class);
                    JSONObject parJSON = new JSONObject();
                    parJSON.put("parameterName", hp.parameterName());
                    parJSON.put("defaultValue", hp.defaultValue());
                    parJSON.put("minimumValue", hp.minimumValue());
                    parJSON.put("maximumValue", hp.maximumValue());
                    parameters.add(parJSON);
                }/*  w ww.  ja v a  2 s  .  c om*/
            }
            JSONObject method = new JSONObject();
            method.put("hyperParameters", parameters);
            method.put("class", c.getCanonicalName());
            cl.add(method);
        }
    }
    obj.put("methods", cl);
    return obj;
}

From source file:net.ceos.project.poi.annotated.annotation.XlsElementTest.java

/**
 * Test initialization of the title attribute over multiples types with
 * specific value./*from w  w  w.  ja  v a  2s. co m*/
 */
@Test
public void checkTitleAttribute() {
    Class<XMenFactory.Cyclops> oC = XMenFactory.Cyclops.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsElement
        if (f.isAnnotationPresent(XlsElement.class)) {

            XlsElement xlsElement = (XlsElement) f.getAnnotation(XlsElement.class);

            if (f.getName().equals("dateAttribute")) {
                assertEquals(xlsElement.title(), "Date value");
            } else if (f.getName().equals("stringAttribute")) {
                assertEquals(xlsElement.title(), "String value");
            } else if (f.getName().equals("integerAttribute")) {
                assertEquals(xlsElement.title(), "Integer value");
            } else if (f.getName().equals("doubleAttribute1")) {
                assertEquals(xlsElement.title(), "Double value 1");
            } else if (f.getName().equals("doubleAttribute2")) {
                assertEquals(xlsElement.title(), "Double value 2");
            } else if (f.getName().equals("sum")) {
                assertEquals(xlsElement.title(), "Sum double 1 & double 2");
            }
        }
    }
}

From source file:in.hatimi.nosh.support.CmdLineManager.java

public CmdLineManager(Class<?> type) {
    this.type = type;
    options = new Options();
    Field[] fields = type.getDeclaredFields();
    for (Field field : fields) {
        Option opt = optionFromField(field);
        if (opt != null) {
            options.addOption(opt);/*from   ww w .java 2 s .  c  o m*/
        }
    }
}

From source file:com.github.tddts.jet.config.spring.postprocessor.MessageAnnotationBeanPostProcessor.java

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

    Pair<Class<?>, Object> typeObjectPair = SpringUtil.checkForDinamicProxy(bean);
    Class<?> type = typeObjectPair.getLeft();
    Object target = typeObjectPair.getRight();

    Method[] methods = type.getDeclaredMethods();
    Field[] fields = type.getDeclaredFields();

    for (Method method : methods) {
        if (checkMethod(method, type))
            processMethod(target, method);
    }/* w ww . j  a v  a2  s  .  com*/

    for (Field field : fields) {
        if (checkField(field, type))
            processField(target, field);
    }

    return bean;
}

From source file:net.ceos.project.poi.annotated.annotation.XlsFreeElementTest.java

/**
 * Test initialization of the comment attribute with specific value.
 */// ww w.j  a v  a  2s  . c o m
@Test
public void checkCommentAttribute() {
    Class<XMenFactory.ProfessorX> oC = XMenFactory.ProfessorX.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsFreeElement
        if (f.isAnnotationPresent(XlsFreeElement.class)) {

            XlsFreeElement xlsFreeElement = (XlsFreeElement) f.getAnnotation(XlsFreeElement.class);

            if (f.getName().equals("stringFreeAttribute3")
                    && StringUtils.isNotBlank(xlsFreeElement.comment())) {
                assertEquals(xlsFreeElement.comment(), "Free element with sample comment");
            }
        }
    }
}

From source file:net.ceos.project.poi.annotated.annotation.XlsFreeElementTest.java

/**
 * Test initialization of the decorator attribute with specific value.
 *///from   w ww .  j av  a  2 s.  c  o  m
@Test
public void checkDecoratorAttribute() {
    Class<XMenFactory.ProfessorX> oC = XMenFactory.ProfessorX.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsFreeElement
        if (f.isAnnotationPresent(XlsFreeElement.class)) {

            XlsFreeElement xlsFreeElement = (XlsFreeElement) f.getAnnotation(XlsFreeElement.class);

            if (f.getName().equals("integerFreeAttribute")
                    && StringUtils.isNotBlank(xlsFreeElement.decorator())) {
                assertEquals(xlsFreeElement.decorator(), "myDecorator");
            }
        }
    }
}

From source file:net.ceos.project.poi.annotated.annotation.XlsFreeElementTest.java

/**
 * Test initialization of the formatMask attribute with specific value.
 */// w  w  w.  j av a 2  s  . c  om
@Test
public void checkFormatMaskAttribute() {
    Class<XMenFactory.ProfessorX> oC = XMenFactory.ProfessorX.class;

    List<Field> fL = Arrays.asList(oC.getDeclaredFields());
    for (Field f : fL) {
        // Process @XlsFreeElement
        if (f.isAnnotationPresent(XlsFreeElement.class)) {

            XlsFreeElement xlsFreeElement = (XlsFreeElement) f.getAnnotation(XlsFreeElement.class);

            if (f.getName().equals("integerFreeAttribute")
                    && StringUtils.isNotBlank(xlsFreeElement.formatMask())) {
                assertEquals(xlsFreeElement.formatMask(), "0.0");
            }
        }
    }
}