Example usage for java.lang Class getTypeName

List of usage examples for java.lang Class getTypeName

Introduction

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

Prototype

public String getTypeName() 

Source Link

Document

Return an informative string for the name of this type.

Usage

From source file:io.klerch.alexa.state.model.AlexaStateModel.java

/**
 * Returns the key used to save the model in the session attributes. This method takes an id
 * thus will return the key for a specific instance of the model as many of them can exist in your session.
 * @param modelClass The type of an AlexaStateModel.
 * @param id the key for a specific instance of the model
 * @param <TModel> The model type derived from AlexaStateModel.
 * @return key used to save the model in the session attributes
 *///from  ww  w  .j a v  a2  s .  com
public static <TModel extends AlexaStateModel> String getAttributeKey(final Class<TModel> modelClass,
        final String id) {
    return modelClass.getTypeName() + (id != null && !id.isEmpty() ? AttributeKeySeparator + id : "");
}

From source file:springfox.documentation.schema.DefaultTypeIdProvider.java

@Override
public String nameFor(Class<?> type) {
    return type.getTypeName();
}

From source file:com.arvato.thoroughly.service.tmall.impl.APIServiceImpl.java

public String getTmallDomainFieldByClassType(final Class<?> classType, final String prefix) {
    if (fields.containsKey(classType.getTypeName())) {
        return fields.get(classType.getTypeName());
    } else {//ww  w  .  j ava  2 s .  c  o  m
        return startSearch(classType, prefix);
    }

}

From source file:com.arvato.thoroughly.service.tmall.impl.APIServiceImpl.java

private String startSearch(final Class<?> classType, String prefix) {
    final Field[] objectFields = classType.getDeclaredFields();

    final StringBuffer sb = new StringBuffer();
    String fieldsSeparatedByCommas = null;

    prefix = prefix == null ? "" : prefix + ".";

    for (final Field item : objectFields) {
        if ("serialVersionUID".equals(item.getName())) {
            continue;
        }//from  w w  w  .j  a v  a2 s  . c om
        final ApiListField apiListField = item.getAnnotation(ApiListField.class);
        if (apiListField != null) {
            continue;
        }
        final ApiField annotation = item.getAnnotation(ApiField.class);

        sb.append(prefix + annotation.value() + ",");
    }
    if (StringUtils.isNotEmpty(sb)) {
        fieldsSeparatedByCommas = sb.toString().substring(0, sb.toString().length() - 1);

        fields.put(classType.getTypeName(), fieldsSeparatedByCommas);
    }
    return fieldsSeparatedByCommas;
}

From source file:com.haulmont.cuba.web.gui.components.WebDataGrid.java

@Override
public <T extends Renderer> T createRenderer(Class<T> type) {
    Class<? extends Renderer> rendererClass = rendererClasses.get(type);
    if (rendererClass == null) {
        throw new IllegalStateException(
                String.format("Can't find renderer class for '%s'", type.getTypeName()));
    }/*from w  w  w.  j  a v a2 s.co  m*/

    try {
        return type.cast(rendererClass.newInstance());
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException(
                String.format("Error creating the '%s' renderer instance", type.getTypeName()), e);
    }
}

From source file:ca.oson.json.Oson.java

public ObjectMapper getJackson() {
    if (jackson == null) {
        jackson = new ObjectMapper();
        prefixProcessing();/*from  w  w  w  .j  a v  a  2  s .co m*/

        // do not fail for funny reason
        jackson.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        jackson.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

        switch (getDefaultType()) {
        case ALWAYS:
            jackson.setSerializationInclusion(Include.ALWAYS);
            break;
        case NON_NULL:
            jackson.setSerializationInclusion(Include.NON_NULL);
            break;
        case NON_EMPTY:
            jackson.setSerializationInclusion(Include.NON_EMPTY);
            break;
        case DEFAULT:
            jackson.setSerializationInclusion(Include.USE_DEFAULTS);
            break;
        default:
            jackson.setSerializationInclusion(Include.NON_NULL);
            break;
        }

        if (getPrettyPrinting() && getIndentation() > 0) {
            jackson.enable(SerializationFeature.INDENT_OUTPUT);
            jackson.configure(SerializationFeature.INDENT_OUTPUT, true);
        }

        if (getOrderByKeyAndProperties()) {
            jackson.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
        }

        Set<FieldMapper> mappers = getFieldMappers();
        if (mappers != null) {
            //defines how names of JSON properties ("external names") are derived from names of POJO methods and fields ("internal names"),
            // in cases where they are not auto-detected and no explicit annotations exist for naming
            // config - Configuration in used: either SerializationConfig or DeserializationConfig,
            // depending on whether method is called during serialization or deserialization
            jackson.setPropertyNamingStrategy(new PropertyNamingStrategy() {
                @Override
                public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
                    String name = defaultName;
                    if (name == null) {
                        name = field.getName();
                    }
                    name = name.trim();
                    if (name.isEmpty()) {
                        return super.nameForField(config, field, defaultName);
                    }

                    String lname = name.toLowerCase();

                    if (config instanceof SerializationConfig || !(config instanceof DeserializationConfig)) {
                        for (FieldMapper fieldMapper : mappers) {
                            String java = fieldMapper.java;

                            if (java != null && lname.equals(java.toLowerCase())) {
                                Class<?> type = fieldMapper.getType();
                                String fullName = field.getFullName(); // ca.oson.json.Artist#age;
                                int index = fullName.indexOf('#');
                                if (index > -1) {
                                    fullName = fullName.substring(0, index);
                                }

                                if (type != null) {
                                    try {
                                        // mapper.getType().getTypeName() is the same as mapper.getType().getName()
                                        if (type.getTypeName().equals(fullName)) {
                                            return fieldMapper.json;
                                        }

                                    } catch (SecurityException e) {
                                        // e.printStackTrace();
                                    }

                                } else { // just by name
                                    return fieldMapper.json;
                                }
                            }
                        }

                    }

                    if (config instanceof DeserializationConfig || !(config instanceof SerializationConfig)) {
                        for (FieldMapper fieldMapper : mappers) {
                            String json = fieldMapper.json;

                            if (json != null && lname.equals(json.toLowerCase())) {
                                Class<?> type = fieldMapper.getType();
                                String fullName = field.getFullName(); // ca.oson.json.Artist#age;
                                int index = fullName.indexOf('#');
                                if (index > -1) {
                                    fullName = fullName.substring(0, index);
                                }

                                if (type != null) {
                                    try {
                                        // mapper.getType().getTypeName() is the same as mapper.getType().getName()
                                        if (type.getTypeName().equals(fullName)) {
                                            return fieldMapper.java;
                                        }

                                    } catch (SecurityException e) {
                                        // e.printStackTrace();
                                    }

                                } else { // just by name
                                    return fieldMapper.java;
                                }
                            }
                        }

                    }

                    return super.nameForField(config, field, defaultName);
                }

                @Override
                public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method,
                        String defaultName) {
                    String name = defaultName;
                    if (name == null) {
                        name = method.getName().substring(3);
                    }
                    name = name.trim();
                    if (name.isEmpty()) {
                        return super.nameForGetterMethod(config, method, defaultName);
                    }

                    String lname = name.toLowerCase();

                    if (config instanceof SerializationConfig || !(config instanceof DeserializationConfig)) {
                        for (FieldMapper fieldMapper : mappers) {
                            String java = fieldMapper.java;

                            if (java != null && lname.equals(java.toLowerCase())) {
                                Class<?> type = fieldMapper.getType();
                                String fullName = method.getFullName(); // java.util.Date#getTime(0 params)
                                int index = fullName.indexOf('#');
                                if (index > -1) {
                                    fullName = fullName.substring(0, index);
                                }

                                if (type != null) {
                                    try {
                                        // mapper.getType().getTypeName() is the same as mapper.getType().getName()
                                        if (type.getTypeName().equals(fullName)) {
                                            return fieldMapper.json;
                                        }

                                    } catch (SecurityException e) {
                                        // e.printStackTrace();
                                    }

                                } else { // just by name
                                    return fieldMapper.json;
                                }
                            }
                        }

                    }

                    if (config instanceof DeserializationConfig || !(config instanceof SerializationConfig)) {
                        for (FieldMapper fieldMapper : mappers) {
                            String json = fieldMapper.json;

                            if (json != null && lname.equals(json.toLowerCase())) {
                                Class<?> type = fieldMapper.getType();
                                String fullName = method.getFullName(); // java.util.Date#getTime(0 params)
                                int index = fullName.indexOf('#');
                                if (index > -1) {
                                    fullName = fullName.substring(0, index);
                                }

                                if (type != null) {
                                    try {
                                        // mapper.getType().getTypeName() is the same as mapper.getType().getName()
                                        if (type.getTypeName().equals(fullName)) {
                                            return fieldMapper.java;
                                        }

                                    } catch (SecurityException e) {
                                        // e.printStackTrace();
                                    }

                                } else { // just by name
                                    return fieldMapper.java;
                                }
                            }
                        }

                    }

                    return super.nameForGetterMethod(config, method, defaultName);
                }

                /*
                 * (non-Javadoc)
                 * @see com.fasterxml.jackson.databind.PropertyNamingStrategy#nameForSetterMethod(com.fasterxml.jackson.databind.cfg.MapperConfig, com.fasterxml.jackson.databind.introspect.AnnotatedMethod, java.lang.String)
                 *
                 * Method called to find external name (name used in JSON) for given logical POJO property, as defined by given setter method; typically called when building a deserializer (but not necessarily only then).
                 */
                @Override
                public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method,
                        String defaultName) {
                    String name = defaultName;
                    if (name == null) {
                        name = method.getName().substring(3);
                    }
                    name = name.trim();
                    if (name.isEmpty()) {
                        return super.nameForSetterMethod(config, method, defaultName);
                    }

                    String lname = name.toLowerCase();

                    if (config instanceof SerializationConfig || !(config instanceof DeserializationConfig)) {
                        for (FieldMapper fieldMapper : mappers) {
                            String java = fieldMapper.java;

                            if (java != null && lname.equals(java.toLowerCase())) {
                                Class<?> type = fieldMapper.getType();
                                String fullName = method.getFullName(); // java.util.Date#getTime(0 params)
                                int index = fullName.indexOf('#');
                                if (index > -1) {
                                    fullName = fullName.substring(0, index);
                                }

                                if (type != null) {
                                    try {
                                        // mapper.getType().getTypeName() is the same as mapper.getType().getName()
                                        if (type.getTypeName().equals(fullName)) {
                                            return fieldMapper.json;
                                        }

                                    } catch (SecurityException e) {
                                        // e.printStackTrace();
                                    }

                                } else { // just by name
                                    return fieldMapper.json;
                                }
                            }
                        }

                    }

                    if (config instanceof DeserializationConfig || !(config instanceof SerializationConfig)) {
                        for (FieldMapper fieldMapper : mappers) {
                            String json = fieldMapper.json;

                            if (json != null && lname.equals(json.toLowerCase())) {
                                Class<?> type = fieldMapper.getType();
                                String fullName = method.getFullName(); // java.util.Date#getTime(0 params)
                                int index = fullName.indexOf('#');
                                if (index > -1) {
                                    fullName = fullName.substring(0, index);
                                }

                                if (type != null) {
                                    try {
                                        // mapper.getType().getTypeName() is the same as mapper.getType().getName()
                                        if (type.getTypeName().equals(fullName)) {
                                            return fieldMapper.java;
                                        }

                                    } catch (SecurityException e) {
                                        // e.printStackTrace();
                                    }

                                } else { // just by name
                                    return fieldMapper.java;
                                }
                            }
                        }

                    }

                    return super.nameForSetterMethod(config, method, defaultName);
                }

                /*
                 * (non-Javadoc)
                 * @see com.fasterxml.jackson.databind.PropertyNamingStrategy#nameForConstructorParameter(com.fasterxml.jackson.databind.cfg.MapperConfig, com.fasterxml.jackson.databind.introspect.AnnotatedParameter, java.lang.String)
                 *
                 * find external name (name used in JSON) for given logical POJO property, as defined by given setter method; typically called when building a deserializer (but not necessarily only then).
                 */
                @Override
                public String nameForConstructorParameter(MapperConfig<?> config, AnnotatedParameter ctorParam,
                        String defaultName) {
                    String name = defaultName;
                    if (name == null) {
                        name = ctorParam.getName();
                    }
                    name = name.trim();
                    if (name.isEmpty()) {
                        return super.nameForConstructorParameter(config, ctorParam, defaultName);
                    }

                    String lname = name.toLowerCase();

                    if (config instanceof SerializationConfig || !(config instanceof DeserializationConfig)) {
                        for (FieldMapper fieldMapper : mappers) {
                            String java = fieldMapper.java;

                            if (java != null && lname.equals(java.toLowerCase())) {
                                Class<?> type = fieldMapper.getType();
                                String fullName = ctorParam.getName(); // java.util.Date#
                                int index = fullName.indexOf('#');
                                if (index > -1) {
                                    fullName = fullName.substring(0, index);
                                }

                                if (type != null) {
                                    try {
                                        // mapper.getType().getTypeName() is the same as mapper.getType().getName()
                                        if (type.getTypeName().equals(fullName)) {
                                            return fieldMapper.json;
                                        }

                                    } catch (SecurityException e) {
                                        // e.printStackTrace();
                                    }

                                } else { // just by name
                                    String json = fieldMapper.json;

                                    if (json == null) {
                                        // do nothing?

                                    } else {
                                        return json;
                                    }
                                }
                            }
                        }

                    }

                    if (config instanceof DeserializationConfig || !(config instanceof SerializationConfig)) {
                        for (FieldMapper fieldMapper : mappers) {
                            String json = fieldMapper.json;

                            if (json != null && lname.equals(json.toLowerCase())) {
                                Class<?> type = fieldMapper.getType();
                                String fullName = ctorParam.getName(); // java.util.Date#
                                int index = fullName.indexOf('#');
                                if (index > -1) {
                                    fullName = fullName.substring(0, index);
                                }

                                if (type != null) {
                                    try {
                                        // mapper.getType().getTypeName() is the same as mapper.getType().getName()
                                        if (type.getTypeName().equals(fullName)) {
                                            return fieldMapper.java;
                                        }

                                    } catch (SecurityException e) {
                                        // e.printStackTrace();
                                    }

                                } else { // just by name
                                    return fieldMapper.java;
                                }
                            }
                        }

                    }

                    return super.nameForConstructorParameter(config, ctorParam, defaultName);
                }

            });
        }

        jackson.setDateFormat(getDateFormat());
    }

    return jackson;
}