Example usage for java.lang.reflect Field toGenericString

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

Introduction

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

Prototype

public String toGenericString() 

Source Link

Document

Returns a string describing this Field , including its generic type.

Usage

From source file:EnumSpy.java

private static void print(List<Field> lst, String s) {
    for (Field f : lst) {
        out.format(fmt, s, f.toGenericString(), synthetic(f));
    }/*from w  ww .j  av a2 s.c  om*/
}

From source file:truco.plugin.utils.Utils.java

public static void test(Player p) {
    try {/*  w w w  .  j a  v a2s . c  o  m*/
        String currentVersion;
        Object propertyManager;
        Object obj = Bukkit.getServer().getClass().getDeclaredMethod("getServer").invoke(Bukkit.getServer());
        propertyManager = obj.getClass().getDeclaredMethod("getPropertyManager").invoke(obj);
        currentVersion = propertyManager.getClass().getPackage().getName();
        Method handle = p.getClass().getMethod("getHandle");
        Class c = Class.forName(currentVersion + ".Entity");
        for (Field f : c.getDeclaredFields()) {
            System.out.println(f.getName() + "-" + f.toGenericString());
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:org.evosuite.setup.TestClusterUtils.java

/**
 * Get the set of fields defined in this class and its superclasses
 *
 * @param clazz/*from   w w w .  j a  va2s. c o m*/
 * @return
 */
public static Set<Field> getFields(Class<?> clazz) {
    // TODO: Helper not necessary here!
    Map<String, Field> helper = new TreeMap<>();

    Set<Field> fields = new LinkedHashSet<>();
    if (clazz.getSuperclass() != null) {
        for (Field f : getFields(clazz.getSuperclass())) {
            helper.put(f.toGenericString(), f);
        }

    }
    for (Class<?> in : Reflection.getInterfaces(clazz)) {
        for (Field f : getFields(in)) {
            helper.put(f.toGenericString(), f);
        }
    }

    for (Field f : Reflection.getDeclaredFields(clazz)) {
        helper.put(f.toGenericString(), f);
    }
    fields.addAll(helper.values());

    return fields;
}

From source file:fr.cls.atoll.motu.processor.wps.StringList.java

public static void testGetFields() {
    List<Field> list = getAllFields(new LinkedList<Field>(), InputDescriptionType.class);
    for (Field field : list) {
        System.out.println(field.toGenericString());
    }/*w  w  w .j  a va 2s  . co  m*/
}

From source file:org.evosuite.setup.TestClusterGenerator.java

/**
 * Get the set of fields defined in this class and its superclasses
 * /*from w  w w.j  ava  2 s .  co  m*/
 * @param clazz
 * @return
 */
public static Set<Field> getFields(Class<?> clazz) {
    // TODO: Helper not necessary here!
    Map<String, Field> helper = new TreeMap<String, Field>();

    Set<Field> fields = new LinkedHashSet<Field>();
    if (clazz.getSuperclass() != null) {
        for (Field f : getFields(clazz.getSuperclass())) {
            helper.put(f.toGenericString(), f);
        }

    }
    for (Class<?> in : clazz.getInterfaces()) {
        for (Field f : getFields(in)) {
            helper.put(f.toGenericString(), f);
        }
    }

    try {
        for (Field f : clazz.getDeclaredFields()) {
            helper.put(f.toGenericString(), f);
        }
    } catch (NoClassDefFoundError e) {
        // TODO: What shall we do?
        logger.info("Error while trying to load fields of class {}: {}", clazz.getName(), e);
    }
    fields.addAll(helper.values());

    return fields;
}

From source file:com.sdl.odata.renderer.json.writer.JsonWriter.java

private void marshallStructuralProperty(Object object, StructuralProperty property)
        throws ODataRenderException, IOException, NoSuchFieldException, IllegalAccessException {
    String propertyName = property.getName();

    // Get the property value through reflection
    Object propertyValue;// www  .ja  va  2s  .  c  o m
    Field field = property.getJavaField();
    try {
        field.setAccessible(true);
        propertyValue = field.get(object);
    } catch (IllegalAccessException e) {
        LOG.error("Error getting field value of field: " + field.toGenericString());
        throw new ODataRenderException("Error getting field value of field: " + field.toGenericString());
    }

    // Collection properties and non-nullable properties should not be null
    if (propertyValue == null) {
        if (property.isCollection()) {
            throw new ODataRenderException("Collection property has null value: " + property);
        } else if (!property.isNullable()) {
            throw new ODataRenderException("Non-nullable property has null value: " + property);
        }
    }

    // Check if the property is a collection
    if (property.isCollection()) {
        // Get an iterator for the array or collection
        Iterator<?> iterator;
        if (propertyValue.getClass().isArray()) {
            iterator = Arrays.asList((Object[]) propertyValue).iterator();
        } else if (Collection.class.isAssignableFrom(propertyValue.getClass())) {
            iterator = ((Collection<?>) propertyValue).iterator();
        } else {
            throw new UnsupportedOperationException("Unsupported collection type: "
                    + propertyValue.getClass().getName() + " for property: " + propertyName);
        }

        // Get the OData type of the elements of the collection
        Type elementType = entityDataModel.getType(property.getElementTypeName());
        if (elementType == null) {
            throw new ODataRenderException("OData type not found for elements of property: " + property);
        }

        LOG.debug("Start collection property: {}", propertyName);
        if (((Collection) propertyValue).isEmpty()) {
            jsonGenerator.writeArrayFieldStart(propertyName);
            jsonGenerator.writeEndArray();
        } else {
            while (iterator.hasNext()) {
                Object element = iterator.next();
                if (element instanceof Number | element instanceof String | element.getClass().isEnum()) {
                    marshallToArray(propertyName, element, iterator);
                } else {
                    marshallCollection(propertyName, iterator, element, elementType);
                }
            }
        }
        LOG.debug("End collection property: {}", propertyName);
    } else {
        // Single value (non-collection) property
        LOG.debug("Start property: {}", propertyName);

        // Get the OData type of the property
        Type propertyType = entityDataModel.getType(property.getTypeName());
        if (propertyType == null) {
            throw new ODataRenderException("OData type not found for property: " + property);
        }

        jsonGenerator.writeFieldName(propertyName);
        if (propertyType.getMetaType().equals(COMPLEX) && propertyValue != null) {
            jsonGenerator.writeStartObject();
        }
        marshall(propertyValue, propertyType);
        if (propertyType.getMetaType().equals(COMPLEX) && propertyValue != null) {
            jsonGenerator.writeEndObject();
        }
        LOG.debug("End property: {}", propertyName);
    }
}

From source file:br.gov.frameworkdemoiselle.internal.implementation.ConfigurationLoader.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private void validateValue(Field field, Object value) {
    ValidatorFactory dfv = Validation.buildDefaultValidatorFactory();
    Validator validator = dfv.getValidator();

    Set violations = validator.validateProperty(this.object, field.getName());

    StringBuffer message = new StringBuffer();

    if (!violations.isEmpty()) {
        for (Iterator iter = violations.iterator(); iter.hasNext();) {
            ConstraintViolation violation = (ConstraintViolation) iter.next();
            message.append(field.toGenericString() + " " + violation.getMessage() + "\n");
        }/*from  w ww  .ja v a2  s .  c om*/

        throw new ConfigurationException(message.toString(), new ConstraintViolationException(violations));
    }
}

From source file:org.demoiselle.internal.implementation.ConfigurationLoader.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private void validateValue(Field field, Object value) {
    ValidatorFactory dfv = Validation.buildDefaultValidatorFactory();
    Validator validator = dfv.getValidator();

    Set violations = validator.validateProperty(this.object, field.getName());

    StringBuilder message = new StringBuilder();

    if (!violations.isEmpty()) {
        for (Iterator iter = violations.iterator(); iter.hasNext();) {
            ConstraintViolation violation = (ConstraintViolation) iter.next();
            message.append(field.toGenericString() + " " + violation.getMessage() + "\n");
        }/*from   ww w  . j  a  va  2s. c o  m*/

        throw new ConfigurationException(message.toString(), new ConstraintViolationException(violations));
    }
}

From source file:br.gov.frameworkdemoiselle.internal.implementation.ConfigurationLoader.java

private ConfigurationValueExtractor getValueExtractor(Field field) {
    Collection<ConfigurationValueExtractor> candidates = new HashSet<ConfigurationValueExtractor>();
    ConfigurationBootstrap bootstrap = Beans.getReference(ConfigurationBootstrap.class);

    for (Class<? extends ConfigurationValueExtractor> extractorClass : bootstrap.getCache()) {
        ConfigurationValueExtractor extractor = Beans.getReference(extractorClass);

        if (extractor.isSupported(field)) {
            candidates.add(extractor);//from w ww . j  a v  a2  s.c o m
        }
    }

    ConfigurationValueExtractor elected = StrategySelector.selectInstance(ConfigurationValueExtractor.class,
            candidates);

    if (elected == null) {
        throw new ConfigurationException(getBundle().getString("configuration-extractor-not-found",
                field.toGenericString(), ConfigurationValueExtractor.class.getName()),
                new ClassNotFoundException());
    }

    return elected;
}

From source file:org.demoiselle.internal.implementation.ConfigurationLoader.java

private ConfigurationValueExtractor getValueExtractor(Field field) {
    Collection<ConfigurationValueExtractor> candidates = new HashSet<ConfigurationValueExtractor>();
    ConfigurationBootstrap bootstrap = CDI.current().select(ConfigurationBootstrap.class).get();

    for (Class<? extends ConfigurationValueExtractor> extractorClass : bootstrap.getCache()) {
        ConfigurationValueExtractor extractor = CDI.current().select(extractorClass).get();

        if (extractor.isSupported(field)) {
            candidates.add(extractor);/*from ww w .  j  a v a  2  s.c  om*/
        }
    }

    ConfigurationValueExtractor elected = StrategySelector.selectReference(ConfigurationValueExtractor.class,
            candidates);

    if (elected == null) {
        throw new ConfigurationException(getBundle().getString("configuration-extractor-not-found",
                field.toGenericString(), ConfigurationValueExtractor.class.getName()),
                new ClassNotFoundException());
    }

    return elected;
}