All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fisco.bcos.sdk.abi.Utils Maven / Gradle / Ivy

The newest version!
package org.fisco.bcos.sdk.abi;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.fisco.bcos.sdk.abi.datatypes.DynamicArray;
import org.fisco.bcos.sdk.abi.datatypes.DynamicBytes;
import org.fisco.bcos.sdk.abi.datatypes.Fixed;
import org.fisco.bcos.sdk.abi.datatypes.Int;
import org.fisco.bcos.sdk.abi.datatypes.StaticArray;
import org.fisco.bcos.sdk.abi.datatypes.Type;
import org.fisco.bcos.sdk.abi.datatypes.Ufixed;
import org.fisco.bcos.sdk.abi.datatypes.Uint;
import org.fisco.bcos.sdk.abi.datatypes.Utf8String;

/** Utility functions. */
public class Utils {
    private Utils() {}

    public static  String getTypeName(TypeReference typeReference) {
        return getTypeName(typeReference.getType());
    }

    public static  String getTypeName(java.lang.reflect.Type type) {
        try {

            Class cls = Utils.getClassType(type);
            if (type instanceof ParameterizedType) { // array
                return getParameterizedTypeName(type);
            } else { // simple type
                return getSimpleTypeName(cls);
            }

        } catch (ClassNotFoundException e) {
            throw new UnsupportedOperationException("Invalid class reference provided", e);
        }
    }

    private static  String getParameterizedTypeName(
            java.lang.reflect.Type type) {

        try {
            Class cls = Utils.getClassType(type);

            if (DynamicArray.class.isAssignableFrom(cls)) {
                return getTypeName(((ParameterizedType) type).getActualTypeArguments()[0]) + "[]";
            } else if (StaticArray.class.isAssignableFrom(cls)) {

                int length =
                        Integer.parseInt(
                                cls.getSimpleName()
                                        .substring(StaticArray.class.getSimpleName().length()));

                return getTypeName(((ParameterizedType) type).getActualTypeArguments()[0])
                        + "["
                        + length
                        + "]";

            } else {
                throw new UnsupportedOperationException("Invalid type provided " + cls.getName());
            }
        } catch (ClassNotFoundException e) {
            throw new UnsupportedOperationException("Invalid class reference provided", e);
        }
    }

    static String getSimpleTypeName(Class type) {
        String simpleName = type.getSimpleName().toLowerCase();

        if (type.equals(Uint.class)
                || type.equals(Int.class)
                || type.equals(Ufixed.class)
                || type.equals(Fixed.class)) {
            return simpleName + "256";
        } else if (type.equals(Utf8String.class)) {
            return "string";
        } else if (type.equals(DynamicBytes.class)) {
            return "bytes";
        } else {
            return simpleName;
        }
    }

    @SuppressWarnings("rawtypes")
    public static  boolean dynamicType(java.lang.reflect.Type type)
            throws ClassNotFoundException {

        Class cls = Utils.getClassType(type);
        // dynamic type
        if (Utf8String.class.isAssignableFrom(cls)
                || DynamicBytes.class.isAssignableFrom(cls)
                || DynamicArray.class.isAssignableFrom(cls)) {
            return true;
        }

        // not static type
        if (!StaticArray.class.isAssignableFrom(cls)) {
            return false;
        }

        // unpack static array for checking if dynamic type
        java.lang.reflect.Type[] types = ((ParameterizedType) type).getActualTypeArguments();
        return dynamicType(types[0]);
    }

    public static int getLength(List parameters) {
        int count = 0;
        for (Type type : parameters) {
            count += type.offset();
        }
        return count;
    }

    public static  int getOffset(java.lang.reflect.Type type)
            throws ClassNotFoundException {

        if (Utils.dynamicType(type)) {
            return 1;
        }

        Class cls = Utils.getClassType(type);
        if (StaticArray.class.isAssignableFrom(cls)) {
            int length =
                    Integer.parseInt(
                            cls.getSimpleName()
                                    .substring(StaticArray.class.getSimpleName().length()));
            java.lang.reflect.Type[] types = ((ParameterizedType) type).getActualTypeArguments();
            return getOffset(types[0]) * length;
        } else {
            return 1;
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    public static  Class getClassType(java.lang.reflect.Type type)
            throws ClassNotFoundException {
        if (type instanceof ParameterizedType) {
            return (Class) ((ParameterizedType) type).getRawType();
        } else {
            return (Class) Class.forName(type.getTypeName());
        }
    }

    @SuppressWarnings("unchecked")
    private static  Class getParameterizedTypeFromArray(
            java.lang.reflect.Type type) throws ClassNotFoundException {

        java.lang.reflect.Type[] types = ((ParameterizedType) type).getActualTypeArguments();

        return Utils.getClassType(types[0]);
    }

    @SuppressWarnings("unchecked")
    public static List> convert(List> input) {
        List> result = new ArrayList<>(input.size());
        result.addAll(
                input.stream()
                        .map(typeReference -> (TypeReference) typeReference)
                        .collect(Collectors.toList()));
        return result;
    }

    public static , E extends Type> List typeMap(
            List> input, Class outerDestType, Class innerType) {
        List result = new ArrayList<>();
        try {
            Constructor constructor = outerDestType.getDeclaredConstructor(List.class);
            for (List ts : input) {
                E e = constructor.newInstance(typeMap(ts, innerType));
                result.add(e);
            }
        } catch (NoSuchMethodException
                | IllegalAccessException
                | InstantiationException
                | InvocationTargetException e) {
            throw new TypeMappingException(e);
        }
        return result;
    }

    public static > List typeMap(List input, Class destType)
            throws TypeMappingException {

        List result = new ArrayList(input.size());

        if (!input.isEmpty()) {
            try {
                Constructor constructor =
                        destType.getDeclaredConstructor(input.get(0).getClass());
                for (T value : input) {
                    result.add(constructor.newInstance(value));
                }
            } catch (NoSuchMethodException
                    | IllegalAccessException
                    | InvocationTargetException
                    | InstantiationException e) {
                throw new TypeMappingException(e);
            }
        }
        return result;
    }

    @SuppressWarnings("rawtypes")
    public static List typeMapWithoutGenericType(List input, Class destType)
            throws TypeMappingException {
        List result = new ArrayList(input.size());
        if (!input.isEmpty()) {
            try {
                Constructor constructor = destType.getDeclaredConstructor(input.get(0).getClass());
                for (Object value : input) {
                    result.add(constructor.newInstance(value));
                }
            } catch (NoSuchMethodException
                    | IllegalAccessException
                    | InvocationTargetException
                    | InstantiationException e) {
                throw new TypeMappingException(e);
            }
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy