org.fisco.bcos.web3j.abi.datatypes.StaticArray Java Examples

The following examples show how to use org.fisco.bcos.web3j.abi.datatypes.StaticArray. 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: Utils.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
public static <T extends Type> boolean dynamicType(java.lang.reflect.Type type)
        throws ClassNotFoundException {

    Class<T> 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]);
}
 
Example #2
Source File: Utils.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
public static <T extends Type> int getOffset(java.lang.reflect.Type type)
        throws ClassNotFoundException {

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

    Class<T> 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;
    }
}
 
Example #3
Source File: ContractAbiUtil.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
private static Class<?> getStaticArrayTypeReferenceClass(String type) {
    try {
        return Class.forName("org.fisco.bcos.web3j.abi.datatypes.generated.StaticArray" + type);
    } catch (ClassNotFoundException e) {
        // Unfortunately we can't encode it's length as a type if it's > 32.
        return StaticArray.class;
    }
}
 
Example #4
Source File: CallContract.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public <S extends Type, T> List<List<T>> convertListList(List<StaticArray<S>> arrs) {
    List<List<T>> out = new ArrayList<List<T>>();
    for (StaticArray<S> arr : arrs) {
        List<T> temp = convertList(arr.getValue());
        out.add(temp);
    }
    return out;
}
 
Example #5
Source File: FunctionReturnDecoder.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
private static List<Type> build(String input, List<TypeReference<Type>> outputParameters) {
    List<Type> results = new ArrayList<>(outputParameters.size());

    int offset = 0;
    for (TypeReference<?> typeReference : outputParameters) {
        try {
            @SuppressWarnings("unchecked")
            Class<Type> cls = (Class<Type>) typeReference.getClassType();

            int hexStringDataOffset = getDataOffset(input, offset, typeReference.getType());

            Type result;
            if (DynamicArray.class.isAssignableFrom(cls)) {
                result =
                        TypeDecoder.decodeDynamicArray(
                                input, hexStringDataOffset, typeReference.getType());
            } else if (StaticArray.class.isAssignableFrom(cls)) {
                int length =
                        Integer.parseInt(
                                cls.getSimpleName()
                                        .substring(StaticArray.class.getSimpleName().length()));
                result =
                        TypeDecoder.decodeStaticArray(
                                input, hexStringDataOffset, typeReference.getType(), length);
            } else {
                result = TypeDecoder.decode(input, hexStringDataOffset, cls);
            }

            results.add(result);

            offset +=
                    Utils.getOffset(typeReference.getType())
                            * TypeDecoder.MAX_BYTE_LENGTH_FOR_HEX_STRING;

        } catch (ClassNotFoundException e) {
            throw new UnsupportedOperationException("Invalid class reference provided", e);
        }
    }
    return results;
}
 
Example #6
Source File: TypeDecoder.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T extends Type> T instantiateStaticArray(
        java.lang.reflect.Type type, List<T> elements) {
    try {

        Class<T> cls = Utils.getClassType(type);
        return cls.getConstructor(List.class).newInstance(elements);

    } catch (ReflectiveOperationException e) {
        // noinspection unchecked
        return (T) new StaticArray<>(elements);
    }
}
 
Example #7
Source File: Utils.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
private static <T extends Type, U extends Type> 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);
    }
}
 
Example #8
Source File: SolidityFunctionWrapper.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
private static Class<?> getStaticArrayTypeReferenceClass(String type) {
    try {
        return Class.forName("org.fisco.bcos.web3j.abi.datatypes.generated.StaticArray" + type);
    } catch (ClassNotFoundException e) {
        // Unfortunately we can't encode it's length as a type if it's > 32.
        return StaticArray.class;
    }
}
 
Example #9
Source File: BytesUtils.java    From WeBASE-Collect-Bee with Apache License 2.0 4 votes vote down vote up
public static String staticArrayBytes32ToString(Object obj) {
    StaticArray<Bytes32> sa = (StaticArray<Bytes32>) obj;
    List<Bytes32> list = sa.getValue();
    return bytes32ListTypeToString(list);
}
 
Example #10
Source File: AddressUtils.java    From WeBASE-Collect-Bee with Apache License 2.0 4 votes vote down vote up
public static String staticArrayToString(Object obj) {
    @SuppressWarnings("unchecked")
    StaticArray<Address> sa = (StaticArray<Address>) obj;
    List<Address> list = sa.getValue();
    return JacksonUtils.toJson(list);
}
 
Example #11
Source File: CallContractTest.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
private static void testSyncCallContract(CallContract callContract, String address) {
    CallResult contractResult;

    contractResult =
            callContract.call(
                    address,
                    "getStringOld",
                    new Utf8String("hello world"),
                    new Int256(10086),
                    new Bool(true));

    List<TypeReference<?>> referencesList =
            Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {});
    List<Type> returnList1 =
            FunctionReturnDecoder.decode(
                    contractResult.getOutput(), Utils.convert(referencesList));
    System.out.println("call getStringOld: " + (String) returnList1.get(0).getValue());

    TransactionReceipt receipt;
    receipt =
            callContract.sendTransaction(
                    gasPrice,
                    gasLimit,
                    address,
                    "setAndget",
                    new Utf8String("hello world"),
                    new Int256(10086));
    referencesList =
            Arrays.<TypeReference<?>>asList(
                    new TypeReference<Utf8String>() {}, new TypeReference<Int256>() {});
    List<Type> returnList2 =
            FunctionReturnDecoder.decode(receipt.getOutput(), Utils.convert(referencesList));
    System.out.println(
            "call setAndget: "
                    + (String) returnList2.get(0).getValue()
                    + ", "
                    + (BigInteger) returnList2.get(1).getValue());

    receipt =
            callContract.sendTransaction(
                    address, "setAndget", new Utf8String("hello world"), new Int256(10086));
    referencesList =
            Arrays.<TypeReference<?>>asList(
                    new TypeReference<Utf8String>() {}, new TypeReference<Int256>() {});
    List<Type> returnList3 =
            FunctionReturnDecoder.decode(receipt.getOutput(), Utils.convert(referencesList));
    System.out.println(
            "default call setAndget: "
                    + (String) returnList3.get(0).getValue()
                    + ", "
                    + (BigInteger) returnList3.get(1).getValue());

    contractResult =
            callContract.call(
                    address,
                    "getArray",
                    new StaticArray2(
                            typeMap(
                                    Arrays.asList(
                                            BigInteger.valueOf(-1), BigInteger.valueOf(2)),
                                    Int16.class)),
                    new DynamicArray(
                            typeMap(
                                    Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2)),
                                    Uint16.class)));
    List<Type> returnList4 =
            callContract.decode(
                    contractResult.getOutput(),
                    new TypeReference<StaticArray2<Int16>>() {},
                    new TypeReference<DynamicArray<Int16>>() {});
    System.out.println(
            "call getArray: "
                    + callContract.convertList((List<Type>) returnList4.get(0).getValue())
                    + ", "
                    + callContract.convertList((List<Type>) returnList4.get(1).getValue()));

    List<List<BigInteger>> dyadicArray = new ArrayList<List<BigInteger>>();
    dyadicArray.add(Arrays.asList(BigInteger.valueOf(-1), BigInteger.valueOf(2)));
    dyadicArray.add(Arrays.asList(BigInteger.valueOf(-1), BigInteger.valueOf(992)));
    byte[] bytes = new byte[] {'a', 'b'};
    contractResult =
            callContract.call(
                    address,
                    "newTest",
                    new StaticArray2(typeMap(dyadicArray, StaticArray2.class, Int256.class)),
                    new DynamicBytes(bytes));
    List<Type> returnList5 =
            callContract.decode(
                    contractResult.getOutput(),
                    new TypeReference<StaticArray2<StaticArray2<Int256>>>() {},
                    new TypeReference<DynamicBytes>() {});
    System.out.println(
            "call newTest: "
                    + callContract.convertListList(
                            (List<StaticArray<Int256>>) returnList5.get(0).getValue())
                    + ", "
                    + new String((byte[]) returnList5.get(1).getValue())
                    + ", "
                    + dyadicArray);
}