Java Code Examples for com.jstarcraft.core.common.reflection.TypeUtility#getRawType()

The following examples show how to use com.jstarcraft.core.common.reflection.TypeUtility#getRawType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArraySortConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<IndexableField> convert(LuceneContext context, String path, Field field, LuceneSort annotation, Type type, Object data) {
    Collection<IndexableField> indexables = new LinkedList<>();
    Class<?> componentClass = null;
    Type componentType = null;
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
        componentType = genericArrayType.getGenericComponentType();
        componentClass = TypeUtility.getRawType(componentType, null);
    } else {
        Class<?> clazz = TypeUtility.getRawType(type, null);
        componentType = clazz.getComponentType();
        componentClass = clazz.getComponentType();
    }
    throw new StorageException();
}
 
Example 2
Source File: ObjectConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public void writeValueTo(CsvWriter context, Type type, Object instance) throws Exception {
    // TODO 处理null
    CSVPrinter out = context.getOutputStream();
    if (instance == null) {
        out.print(StringUtility.EMPTY);
        return;
    }
    // 处理对象类型
    Class<?> clazz = TypeUtility.getRawType(type, null);
    ClassDefinition definition = context.getClassDefinition(clazz);
    int length = definition.getProperties().length;
    out.print(length);
    for (PropertyDefinition property : definition.getProperties()) {
        CsvConverter converter = context.getCsvConverter(property.getSpecification());
        Object value = property.getValue(instance);
        converter.writeValueTo(context, property.getType(), value);
    }
}
 
Example 3
Source File: MapConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public void writeValueTo(CsvWriter context, Type type, Map<Object, Object> value) throws Exception {
    CSVPrinter out = context.getOutputStream();
    if (value == null) {
        out.print(StringUtility.EMPTY);
        return;
    }
    // 兼容UniMi
    type = TypeUtility.refineType(type, Map.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Map<Object, Object> map = Map.class.cast(value);
    out.print(map.size());
    Class<?> keyClazz = TypeUtility.getRawType(types[0], null);
    CsvConverter keyConverter = context.getCsvConverter(Specification.getSpecification(keyClazz));
    Class<?> valueClazz = TypeUtility.getRawType(types[1], null);
    CsvConverter valueConverter = context.getCsvConverter(Specification.getSpecification(valueClazz));
    for (Entry<Object, Object> keyValue : map.entrySet()) {
        Object key = keyValue.getKey();
        keyConverter.writeValueTo(context, types[0], key);
        Object element = keyValue.getValue();
        valueConverter.writeValueTo(context, types[1], element);
    }
    return;
}
 
Example 4
Source File: ObjectIndexConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<IndexableField> convert(LuceneContext context, String path, Field field, LuceneIndex annotation, Type type, Object data) {
    Collection<IndexableField> indexables = new LinkedList<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);

    try {
        // TODO 此处需要代码重构
        for (Entry<Field, IndexConverter> keyValue : context.getIndexKeyValues(clazz).entrySet()) {
            // TODO 此处代码可以优反射次数.
            field = keyValue.getKey();
            IndexConverter converter = keyValue.getValue();
            annotation = field.getAnnotation(LuceneIndex.class);
            String name = field.getName();
            for (IndexableField indexable : converter.convert(context, path + "." + name, field, annotation, field.getGenericType(), field.get(data))) {
                indexables.add(indexable);
            }
        }
        return indexables;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 5
Source File: ObjectConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public Object readValueFrom(CsvReader context, Type type) throws Exception {
    // TODO 处理null
    Iterator<String> in = context.getInputStream();
    String check = in.next();
    if (StringUtility.isEmpty(check)) {
        return null;
    }
    int length = Integer.valueOf(check);
    // 处理对象类型
    Class<?> clazz = TypeUtility.getRawType(type, null);
    ClassDefinition definition = context.getClassDefinition(clazz);
    Object instance = definition.getInstance();
    for (PropertyDefinition property : definition.getProperties()) {
        CsvConverter converter = context.getCsvConverter(property.getSpecification());
        Object value = converter.readValueFrom(context, property.getType());
        property.setValue(instance, value);
    }
    return instance;
}
 
Example 6
Source File: ArrayStoreConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public NavigableMap<String, IndexableField> encode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, Object instance) {
    NavigableMap<String, IndexableField> indexables = new TreeMap<>();
    Class<?> componentClass = null;
    Type componentType = null;
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
        componentType = genericArrayType.getGenericComponentType();
        componentClass = TypeUtility.getRawType(componentType, null);
    } else {
        Class<?> clazz = TypeUtility.getRawType(type, null);
        componentType = clazz.getComponentType();
        componentClass = clazz.getComponentType();
    }
    Specification specification = Specification.getSpecification(componentClass);
    StoreConverter converter = context.getStoreConverter(specification);
    int size = Array.getLength(instance);
    IndexableField indexable = new StoredField(path + ".size", size);
    indexables.put(path + ".size", indexable);
    for (int index = 0; index < size; index++) {
        Object element = Array.get(instance, index);
        indexables.putAll(converter.encode(context, path + "[" + index + "]", field, annotation, componentType, element));
    }
    return indexables;
}
 
Example 7
Source File: CollectionConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<Object> readValueFrom(CsvReader context, Type type) throws Exception {
    // TODO 处理null
    Iterator<String> in = context.getInputStream();
    String check = in.next();
    if (StringUtility.isEmpty(check)) {
        return null;
    }
    int length = Integer.valueOf(check);
    Class<?> clazz = TypeUtility.getRawType(type, null);
    // 兼容UniMi
    type = TypeUtility.refineType(type, Collection.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    ClassDefinition definition = context.getClassDefinition(clazz);
    Collection<Object> collection = (Collection) definition.getInstance();
    Class<?> elementClazz = TypeUtility.getRawType(types[0], null);
    CsvConverter converter = context.getCsvConverter(Specification.getSpecification(elementClazz));
    for (int index = 0; index < length; index++) {
        Object element = converter.readValueFrom(context, types[0]);
        collection.add(element);
    }
    return collection;
}
 
Example 8
Source File: ObjectStoreConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public NavigableMap<String, IndexableField> encode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, Object instance) {
    NavigableMap<String, IndexableField> indexables = new TreeMap<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);

    try {
        // TODO 此处需要代码重构
        for (Entry<Field, StoreConverter> keyValue : context.getStoreKeyValues(clazz).entrySet()) {
            // TODO 此处代码可以优反射次数.
            field = keyValue.getKey();
            StoreConverter converter = keyValue.getValue();
            annotation = field.getAnnotation(LuceneStore.class);
            String name = field.getName();
            type = field.getGenericType();
            Object data = field.get(instance);
            for (IndexableField indexable : converter.encode(context, path + "." + name, field, annotation, type, data).values()) {
                indexables.put(path + "." + name, indexable);
            }
        }
        return indexables;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 9
Source File: EnumerationConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object readValueFrom(CsvReader context, Type type) throws Exception {
    Iterator<String> in = context.getInputStream();
    String element = in.next();
    if (StringUtility.isEmpty(element)) {
        return null;
    }
    int index = Integer.valueOf(element);
    Class<?> clazz = TypeUtility.getRawType(type, null);
    return clazz.getEnumConstants()[index];
}
 
Example 10
Source File: NumberSortConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<IndexableField> convert(LuceneContext context, String path, Field field, LuceneSort annotation, Type type, Object data) {
    Collection<IndexableField> indexables = new LinkedList<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);
    clazz = ClassUtility.primitiveToWrapper(clazz);
    if (Byte.class.isAssignableFrom(clazz)) {
        indexables.add(new NumericDocValuesField(path, (byte) data));
        return indexables;
    }
    if (Short.class.isAssignableFrom(clazz)) {
        indexables.add(new NumericDocValuesField(path, (short) data));
        return indexables;
    }
    if (Integer.class.isAssignableFrom(clazz)) {
        indexables.add(new NumericDocValuesField(path, (int) data));
        return indexables;
    }
    if (Long.class.isAssignableFrom(clazz)) {
        indexables.add(new NumericDocValuesField(path, (long) data));
        return indexables;
    }
    if (Float.class.isAssignableFrom(clazz)) {
        indexables.add(new FloatDocValuesField(path, (float) data));
        return indexables;
    }
    if (Double.class.isAssignableFrom(clazz)) {
        indexables.add(new DoubleDocValuesField(path, (double) data));
        return indexables;
    }
    throw new StorageException();
}
 
Example 11
Source File: MapConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Map<Object, Object> readValueFrom(CsvReader context, Type type) throws Exception {
    // TODO 处理null
    Iterator<String> in = context.getInputStream();
    String check = in.next();
    if (StringUtility.isEmpty(check)) {
        return null;
    }
    int length = Integer.valueOf(check);
    Class<?> clazz = TypeUtility.getRawType(type, null);
    // 兼容UniMi
    type = TypeUtility.refineType(type, Map.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    ClassDefinition definition = context.getClassDefinition(clazz);
    Map<Object, Object> map = (Map) definition.getInstance();
    Class<?> keyClazz = TypeUtility.getRawType(types[0], null);
    CsvConverter keyConverter = context.getCsvConverter(Specification.getSpecification(keyClazz));
    Class<?> valueClazz = TypeUtility.getRawType(types[1], null);
    CsvConverter valueConverter = context.getCsvConverter(Specification.getSpecification(valueClazz));
    for (int index = 0; index < length; index++) {
        Object key = keyConverter.readValueFrom(context, types[0]);
        Object element = valueConverter.readValueFrom(context, types[1]);
        map.put(key, element);
    }
    return map;
}
 
Example 12
Source File: CollectionStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, NavigableMap<String, IndexableField> indexables) {
    String from = path;
    char character = path.charAt(path.length() - 1);
    character++;
    String to = path.substring(0, path.length() - 1) + character;
    indexables = indexables.subMap(from, true, to, false);
    Class<?> clazz = TypeUtility.getRawType(type, null);
    // 兼容UniMi
    type = TypeUtility.refineType(type, Collection.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Type elementType = types[0];
    Class<?> elementClazz = TypeUtility.getRawType(elementType, null);

    try {
        // TODO 此处需要代码重构
        Collection<Object> collection = (Collection) context.getInstance(clazz);
        Specification specification = Specification.getSpecification(elementClazz);
        StoreConverter converter = context.getStoreConverter(specification);

        IndexableField indexable = indexables.get(path + ".size");
        int size = indexable.numericValue().intValue();
        for (int index = 0; index < size; index++) {
            Object element = converter.decode(context, path + "[" + index + "]", field, annotation, elementType, indexables);
            collection.add(element);
        }
        return collection;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 13
Source File: ArrayStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, NavigableMap<String, IndexableField> indexables) {
    String from = path;
    char character = path.charAt(path.length() - 1);
    character++;
    String to = path.substring(0, path.length() - 1) + character;
    indexables = indexables.subMap(from, true, to, false);
    Class<?> componentClass = null;
    Type componentType = null;
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
        componentType = genericArrayType.getGenericComponentType();
        componentClass = TypeUtility.getRawType(componentType, null);
    } else {
        Class<?> clazz = TypeUtility.getRawType(type, null);
        componentType = clazz.getComponentType();
        componentClass = clazz.getComponentType();
    }
    Specification specification = Specification.getSpecification(componentClass);
    StoreConverter converter = context.getStoreConverter(specification);
    IndexableField indexable = indexables.get(path + ".size");
    int size = indexable.numericValue().intValue();
    Object array = Array.newInstance(componentClass, size);
    for (int index = 0; index < size; index++) {
        Object element = converter.decode(context, path + "[" + index + "]", field, annotation, componentType, indexables);
        Array.set(array, index, element);
    }
    return array;
}
 
Example 14
Source File: ObjectStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, NavigableMap<String, IndexableField> indexables) {
    String from = path;
    char character = path.charAt(path.length() - 1);
    character++;
    String to = path.substring(0, path.length() - 1) + character;
    indexables = indexables.subMap(from, true, to, false);
    Class<?> clazz = TypeUtility.getRawType(type, null);

    try {
        // TODO 此处需要代码重构
        Object instance = context.getInstance(clazz);
        for (Entry<Field, StoreConverter> keyValue : context.getStoreKeyValues(clazz).entrySet()) {
            // TODO 此处代码可以优反射次数.
            field = keyValue.getKey();
            StoreConverter converter = keyValue.getValue();
            annotation = field.getAnnotation(LuceneStore.class);
            String name = field.getName();
            type = field.getGenericType();
            Object data = converter.decode(context, path + "." + name, field, annotation, type, indexables);
            field.set(instance, data);
        }
        return instance;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 15
Source File: ArrayConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public void writeValueTo(CsvWriter context, Type type, Object value) throws Exception {
    // TODO 处理null
    CSVPrinter out = context.getOutputStream();
    if (value == null) {
        out.print(StringUtility.EMPTY);
        return;
    }
    Class<?> componentClass = null;
    Type componentType = null;
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
        componentType = genericArrayType.getGenericComponentType();
        componentClass = TypeUtility.getRawType(componentType, null);
    } else {
        Class<?> clazz = TypeUtility.getRawType(type, null);
        componentType = clazz.getComponentType();
        componentClass = clazz.getComponentType();
    }
    int length = Array.getLength(value);
    out.print(length);
    Specification specification = Specification.getSpecification(componentClass);
    CsvConverter converter = context.getCsvConverter(specification);
    for (int index = 0; index < length; index++) {
        Object element = Array.get(value, index);
        converter.writeValueTo(context, componentType, element);
    }
}
 
Example 16
Source File: EnumerationStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, NavigableMap<String, IndexableField> indexables) {
    String from = path;
    char character = path.charAt(path.length() - 1);
    character++;
    String to = path.substring(0, path.length() - 1) + character;
    indexables = indexables.subMap(from, true, to, false);
    IndexableField indexable = indexables.firstEntry().getValue();
    Class clazz = TypeUtility.getRawType(type, null);
    return Enum.valueOf(clazz, indexable.stringValue());
}
 
Example 17
Source File: MapStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public NavigableMap<String, IndexableField> encode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, Object instance) {
    NavigableMap<String, IndexableField> indexables = new TreeMap<>();
    // 兼容UniMi
    type = TypeUtility.refineType(type, Map.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Type keyType = types[0];
    Class<?> keyClazz = TypeUtility.getRawType(keyType, null);
    Type valueType = types[1];
    Class<?> valueClazz = TypeUtility.getRawType(valueType, null);

    try {
        // TODO 此处需要代码重构
        Map<Object, Object> map = Map.class.cast(instance);
        Specification keySpecification = Specification.getSpecification(keyClazz);
        StoreConverter keyConverter = context.getStoreConverter(keySpecification);
        Specification valueSpecification = Specification.getSpecification(valueClazz);
        StoreConverter valueConverter = context.getStoreConverter(valueSpecification);

        int size = map.size();
        IndexableField indexable = new StoredField(path + ".size", size);
        indexables.put(path + ".size", indexable);
        int index = 0;
        for (Entry<Object, Object> keyValue : map.entrySet()) {
            Object key = keyValue.getKey();
            indexables.putAll(keyConverter.encode(context, path + "[" + index + "_key]", field, annotation, keyType, key));
            Object value = keyValue.getValue();
            indexables.putAll(valueConverter.encode(context, path + "[" + index + "_value]", field, annotation, valueType, value));
            index++;
        }
        return indexables;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 18
Source File: MapStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, NavigableMap<String, IndexableField> indexables) {
    String from = path;
    char character = path.charAt(path.length() - 1);
    character++;
    String to = path.substring(0, path.length() - 1) + character;
    indexables = indexables.subMap(from, true, to, false);
    Class<?> clazz = TypeUtility.getRawType(type, null);
    // 兼容UniMi
    type = TypeUtility.refineType(type, Map.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Type keyType = types[0];
    Class<?> keyClazz = TypeUtility.getRawType(keyType, null);
    Type valueType = types[1];
    Class<?> valueClazz = TypeUtility.getRawType(valueType, null);

    try {
        // TODO 此处需要代码重构
        Map<Object, Object> map = (Map) context.getInstance(clazz);
        Specification keySpecification = Specification.getSpecification(keyClazz);
        StoreConverter keyConverter = context.getStoreConverter(keySpecification);
        Specification valueSpecification = Specification.getSpecification(valueClazz);
        StoreConverter valueConverter = context.getStoreConverter(valueSpecification);

        IndexableField indexable = indexables.get(path + ".size");
        int size = indexable.numericValue().intValue();
        for (int index = 0; index < size; index++) {
            Object key = keyConverter.decode(context, path + "[" + index + "_key]", field, annotation, keyType, indexables);
            Object value = valueConverter.decode(context, path + "[" + index + "_value]", field, annotation, valueType, indexables);
            map.put(key, value);
        }
        return map;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 19
Source File: ArrayConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object readValueFrom(CsvReader context, Type type) throws Exception {
    // TODO 处理null
    Iterator<String> in = context.getInputStream();
    String check = in.next();
    if (StringUtility.isEmpty(check)) {
        return null;
    }
    int length = Integer.valueOf(check);
    Class<?> componentClass = null;
    Type componentType = null;
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
        componentType = genericArrayType.getGenericComponentType();
        componentClass = TypeUtility.getRawType(componentType, null);
    } else {
        Class<?> clazz = TypeUtility.getRawType(type, null);
        componentType = clazz.getComponentType();
        componentClass = clazz.getComponentType();
    }
    Object array = Array.newInstance(componentClass, length);
    Specification specification = Specification.getSpecification(componentClass);
    CsvConverter converter = context.getCsvConverter(specification);
    for (int index = 0; index < length; index++) {
        Object element = converter.readValueFrom(context, componentType);
        Array.set(array, index, element);
    }
    return array;
}
 
Example 20
Source File: BooleanSortConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<IndexableField> convert(LuceneContext context, String path, Field field, LuceneSort annotation, Type type, Object data) {
    Collection<IndexableField> indexables = new LinkedList<>();
    Class<?> clazz = TypeUtility.getRawType(type, null);
    clazz = ClassUtility.primitiveToWrapper(clazz);
    if (AtomicBoolean.class.isAssignableFrom(clazz)) {
        indexables.add(new NumericDocValuesField(path, AtomicBoolean.class.cast(data).get() ? 1L : 0L));
        return indexables;
    }
    if (Boolean.class.isAssignableFrom(clazz)) {
        indexables.add(new NumericDocValuesField(path, Boolean.class.cast(data) ? 1L : 0L));
        return indexables;
    }
    throw new StorageException();
}