Example usage for java.lang.reflect Field getType

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

Introduction

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

Prototype

public Class<?> getType() 

Source Link

Document

Returns a Class object that identifies the declared type for the field represented by this Field object.

Usage

From source file:be.fedict.eid.applet.service.impl.tlv.TlvParser.java

private static <T> T parseThrowing(byte[] file, Class<T> tlvClass) throws InstantiationException,
        IllegalAccessException, DataConvertorException, UnsupportedEncodingException {
    Field[] fields = tlvClass.getDeclaredFields();
    Map<Integer, Field> tlvFields = new HashMap<Integer, Field>();
    for (Field field : fields) {
        TlvField tlvFieldAnnotation = field.getAnnotation(TlvField.class);
        if (null == tlvFieldAnnotation) {
            continue;
        }/*from   w  w w .j a  v a 2  s .co  m*/
        int tagId = tlvFieldAnnotation.value();
        if (tlvFields.containsKey(new Integer(tagId))) {
            throw new IllegalArgumentException("TLV field duplicate: " + tagId);
        }
        tlvFields.put(new Integer(tagId), field);
    }
    T tlvObject = tlvClass.newInstance();

    int idx = 0;
    while (idx < file.length - 1) {
        byte tag = file[idx];
        idx++;
        byte lengthByte = file[idx];
        int length = lengthByte & 0x7f;
        while ((lengthByte & 0x80) == 0x80) {
            idx++;
            lengthByte = file[idx];
            length = (length << 7) + (lengthByte & 0x7f);
        }
        idx++;
        if (0 == tag) {
            idx += length;
            continue;
        }
        if (tlvFields.containsKey(new Integer(tag))) {
            Field tlvField = tlvFields.get(new Integer(tag));
            Class<?> tlvType = tlvField.getType();
            ConvertData convertDataAnnotation = tlvField.getAnnotation(ConvertData.class);
            byte[] tlvValue = copy(file, idx, length);
            Object fieldValue;
            if (null != convertDataAnnotation) {
                Class<? extends DataConvertor<?>> dataConvertorClass = convertDataAnnotation.value();
                DataConvertor<?> dataConvertor = dataConvertorClass.newInstance();
                fieldValue = dataConvertor.convert(tlvValue);
            } else if (String.class == tlvType) {
                fieldValue = new String(tlvValue, "UTF-8");
            } else if (Boolean.TYPE == tlvType) {
                fieldValue = true;
            } else if (tlvType.isArray() && Byte.TYPE == tlvType.getComponentType()) {
                fieldValue = tlvValue;
            } else {
                throw new IllegalArgumentException("unsupported field type: " + tlvType.getName());
            }
            LOG.debug("setting field: " + tlvField.getName());
            if (null != tlvField.get(tlvObject) && false == tlvField.getType().isPrimitive()) {
                throw new RuntimeException("field was already set: " + tlvField.getName());
            }
            tlvField.setAccessible(true);
            tlvField.set(tlvObject, fieldValue);
        } else {
            LOG.debug("unknown tag: " + (tag & 0xff) + ", length: " + length);
        }
        idx += length;
    }
    return tlvObject;
}

From source file:hu.petabyte.redflags.engine.util.MappingUtils.java

public static List<String> listAllProperties(Class<?> clazz, String rootName) {
    List<String> p = new ArrayList<String>();
    for (Field f : clazz.getDeclaredFields()) {
        String n = f.getName();//from  w  ww .ja  v  a 2s .  c  om
        Class<?> c = f.getType();
        if (null != rootName && !rootName.isEmpty()) {
            n = String.format("%s.%s", rootName, n);
        }
        p.add(n);
        if (!c.getName().startsWith("java.")) {
            p.addAll(listAllProperties(c, n));
        }
    }
    return p;
}

From source file:com.google.cloud.runtimes.builder.Application.java

private static void doForEachOverrideSetting(List<Field> configFields, Consumer<String> actionBooleanSetting,
        Consumer<String> actionStringSetting) {
    for (Field field : configFields) {
        String name = OverrideableSetting.getSettingName(field);
        if ((field.getType().equals(boolean.class) || field.getType().equals(Boolean.class))) {
            actionBooleanSetting.accept(name);
        } else {/*from  w  w w.  java 2 s .  c om*/
            actionStringSetting.accept(name);
        }
    }
}

From source file:Debug.java

public static Debug getDebugLevel(Class cls) throws NoSuchFieldException {
    try {/*from w w  w  . java2s.  com*/
        Field fld = cls.getField("debug");
        if (fld.getType() != Debug.class || !Modifier.isStatic(fld.getModifiers()))
            throw new NoSuchFieldException();
        return (Debug) fld.get(null);
    } catch (IllegalArgumentException e) {
        throw new NoSuchFieldException();
    } catch (IllegalAccessException e) {
        throw new NoSuchFieldException();
    } catch (SecurityException e) {
        throw new NoSuchFieldException();
    }
}

From source file:Debug.java

public static void setDebugLevel(Class cls, Debug level) throws NoSuchFieldException {
    try {/*from  www  .ja v a 2s. c o m*/
        Field fld = cls.getField("debug");
        if (fld.getType() != Debug.class || !Modifier.isStatic(fld.getModifiers()))
            throw new NoSuchFieldException();
        fld.set(null, level);
    } catch (IllegalArgumentException e) {
        throw new NoSuchFieldException();
    } catch (IllegalAccessException e) {
        throw new NoSuchFieldException();
    } catch (SecurityException e) {
        throw new NoSuchFieldException();
    }
}

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 w w  .  ja va  2  s  .c o  m
        }
    }
}

From source file:org.granite.grails.integration.GrailsExternalizer.java

public static boolean isIgnored(Field field) {
    if (EVENTS.contains(field.getName()))
        return true;
    if (field.getName().equals("errors") && field.getType().getName().equals(ERRORS))
        return true;
    return false;
}

From source file:ColorUtils.java

private static boolean isConstantColorField(Field field) {
    return Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())
            && Color.class == field.getType();
}

From source file:com.shazam.shazamcrest.matcher.GsonProvider.java

private static void markSetAndMapFields(final GsonBuilder gsonBuilder) {
    gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() {
        @Override/*from  w ww .  j  av a 2 s  . c  o  m*/
        public String translateName(Field f) {
            if (Set.class.isAssignableFrom(f.getType()) || Map.class.isAssignableFrom(f.getType())) {
                return MARKER + f.getName();
            }
            return f.getName();
        }
    });
}

From source file:com.impetus.kundera.graph.ObjectGraphUtils.java

/**
 * /*from  w w  w.j a v a2  s . c  o m*/
 * @param id
 * @param idField
 * @return
 */
private static boolean isIdSet(Object id, Field idField) {
    // return true, if it is non blank and not zero in case of numeric
    // value.

    if (id != null) {
        return !(NumericUtils.checkIfZero(id.toString(), idField.getType())
                || (StringUtils.isNumeric(id.toString()) && StringUtils.isBlank(id.toString())));
    }
    return false;
}