Java源码示例:org.web3j.abi.datatypes.Fixed
示例1
static <T extends NumericType> T decodeNumeric(String input, Class<T> type) {
try {
byte[] inputByteArray = Numeric.hexStringToByteArray(input);
int typeLengthAsBytes = getTypeLengthInBytes(type);
byte[] resultByteArray = new byte[typeLengthAsBytes + 1];
if (Int.class.isAssignableFrom(type) || Fixed.class.isAssignableFrom(type)) {
resultByteArray[0] = inputByteArray[0]; // take MSB as sign bit
}
int valueOffset = Type.MAX_BYTE_LENGTH - typeLengthAsBytes;
System.arraycopy(inputByteArray, valueOffset, resultByteArray, 1, typeLengthAsBytes);
BigInteger numericValue = new BigInteger(resultByteArray);
return type.getConstructor(BigInteger.class).newInstance(numericValue);
} catch (NoSuchMethodException | SecurityException
| InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
throw new UnsupportedOperationException(
"Unable to create instance of " + type.getName(), e);
}
}
示例2
@Test
public void testGetTypeName() throws ClassNotFoundException {
assertThat(Utils.getTypeName(new TypeReference<Uint>(){}), is("uint256"));
assertThat(Utils.getTypeName(new TypeReference<Int>(){}), is("int256"));
assertThat(Utils.getTypeName(new TypeReference<Ufixed>(){}), is("ufixed256"));
assertThat(Utils.getTypeName(new TypeReference<Fixed>(){}), is("fixed256"));
assertThat(Utils.getTypeName(new TypeReference<Uint64>(){}), is("uint64"));
assertThat(Utils.getTypeName(new TypeReference<Int64>(){}), is("int64"));
assertThat(Utils.getTypeName(new TypeReference<Bool>(){}), is("bool"));
assertThat(Utils.getTypeName(new TypeReference<Utf8String>(){}), is("string"));
assertThat(Utils.getTypeName(new TypeReference<DynamicBytes>(){}), is("bytes"));
assertThat(Utils.getTypeName(
new TypeReference.StaticArrayTypeReference<StaticArray<Uint>>(5){}),
is("uint256[5]"));
assertThat(Utils.getTypeName(
new TypeReference<DynamicArray<Uint>>(){}),
is("uint256[]"));
}
示例3
static <T extends NumericType> T decodeNumeric(String input, Class<T> type) {
try {
byte[] inputByteArray = Numeric.hexStringToByteArray(input);
int typeLengthAsBytes = getTypeLengthInBytes(type);
byte[] resultByteArray = new byte[typeLengthAsBytes + 1];
if (Int.class.isAssignableFrom(type) || Fixed.class.isAssignableFrom(type)) {
resultByteArray[0] = inputByteArray[0]; // take MSB as sign bit
}
int valueOffset = Type.MAX_BYTE_LENGTH - typeLengthAsBytes;
System.arraycopy(inputByteArray, valueOffset, resultByteArray, 1, typeLengthAsBytes);
BigInteger numericValue = new BigInteger(resultByteArray);
return type.getConstructor(BigInteger.class).newInstance(numericValue);
} catch (NoSuchMethodException | SecurityException
| InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
throw new UnsupportedOperationException(
"Unable to create instance of " + type.getName(), e);
}
}
示例4
@Test
public void testGetTypeName() throws ClassNotFoundException {
assertThat(Utils.getTypeName(new TypeReference<Uint>(){}), is("uint256"));
assertThat(Utils.getTypeName(new TypeReference<Int>(){}), is("int256"));
assertThat(Utils.getTypeName(new TypeReference<Ufixed>(){}), is("ufixed256"));
assertThat(Utils.getTypeName(new TypeReference<Fixed>(){}), is("fixed256"));
assertThat(Utils.getTypeName(new TypeReference<Uint64>(){}), is("uint64"));
assertThat(Utils.getTypeName(new TypeReference<Int64>(){}), is("int64"));
assertThat(Utils.getTypeName(new TypeReference<Bool>(){}), is("bool"));
assertThat(Utils.getTypeName(new TypeReference<Utf8String>(){}), is("string"));
assertThat(Utils.getTypeName(new TypeReference<DynamicBytes>(){}), is("bytes"));
assertThat(Utils.getTypeName(
new TypeReference.StaticArrayTypeReference<StaticArray<Uint>>(5){}),
is("uint256[5]"));
assertThat(Utils.getTypeName(
new TypeReference<DynamicArray<Uint>>(){}),
is("uint256[]"));
}
示例5
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;
}
}
示例6
@Test
public void testGetTypeName() {
assertEquals(Utils.getTypeName(new TypeReference<Uint>() {}), ("uint256"));
assertEquals(Utils.getTypeName(new TypeReference<Int>() {}), ("int256"));
assertEquals(Utils.getTypeName(new TypeReference<Ufixed>() {}), ("ufixed256"));
assertEquals(Utils.getTypeName(new TypeReference<Fixed>() {}), ("fixed256"));
assertEquals(Utils.getTypeName(new TypeReference<Uint64>() {}), ("uint64"));
assertEquals(Utils.getTypeName(new TypeReference<Int64>() {}), ("int64"));
assertEquals(Utils.getTypeName(new TypeReference<Bool>() {}), ("bool"));
assertEquals(Utils.getTypeName(new TypeReference<Utf8String>() {}), ("string"));
assertEquals(Utils.getTypeName(new TypeReference<DynamicBytes>() {}), ("bytes"));
assertEquals(
Utils.getTypeName(
new TypeReference.StaticArrayTypeReference<StaticArray<Uint>>(5) {}),
("uint256[5]"));
assertEquals(Utils.getTypeName(new TypeReference<DynamicArray<Uint>>() {}), ("uint256[]"));
}
示例7
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;
}
}
示例8
private MethodSpec.Builder generateFixedTypes(MethodSpec.Builder builder, String packageName) {
for (int mBitSize = 8, nBitSize = Type.MAX_BIT_LENGTH - 8;
mBitSize < Type.MAX_BIT_LENGTH && nBitSize > 0;
mBitSize += 8, nBitSize -= 8) {
String suffix = mBitSize + "x" + nBitSize;
builder = addStatement(
builder, packageName, Ufixed.TYPE_NAME + suffix,
Ufixed.class.getSimpleName() + suffix);
builder = addStatement(
builder, packageName, Fixed.TYPE_NAME + suffix,
Fixed.class.getSimpleName() + suffix);
}
return builder;
}
示例9
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;
}
}
示例10
private MethodSpec.Builder generateFixedTypes(MethodSpec.Builder builder, String packageName) {
for (int mBitSize = 8, nBitSize = Type.MAX_BIT_LENGTH - 8;
mBitSize < Type.MAX_BIT_LENGTH && nBitSize > 0;
mBitSize += 8, nBitSize -= 8) {
String suffix = mBitSize + "x" + nBitSize;
builder = addStatement(
builder, packageName, Ufixed.TYPE_NAME + suffix,
Ufixed.class.getSimpleName() + suffix);
builder = addStatement(
builder, packageName, Fixed.TYPE_NAME + suffix,
Fixed.class.getSimpleName() + suffix);
}
return builder;
}
示例11
static <T extends NumericType> T decodeNumeric(String input, Class<T> type) {
try {
byte[] inputByteArray = Numeric.hexStringToByteArray(input);
int typeLengthAsBytes = getTypeLengthInBytes(type);
byte[] resultByteArray = new byte[typeLengthAsBytes + 1];
if (Int.class.isAssignableFrom(type) || Fixed.class.isAssignableFrom(type)) {
resultByteArray[0] = inputByteArray[0]; // take MSB as sign bit
}
int valueOffset = Type.MAX_BYTE_LENGTH - typeLengthAsBytes;
System.arraycopy(inputByteArray, valueOffset, resultByteArray, 1, typeLengthAsBytes);
BigInteger numericValue = new BigInteger(resultByteArray);
return type.getConstructor(BigInteger.class).newInstance(numericValue);
} catch (NoSuchMethodException
| SecurityException
| InstantiationException
| IllegalAccessException
| IllegalArgumentException
| InvocationTargetException e) {
throw new UnsupportedOperationException(
"Unable to create instance of " + type.getName(), e);
}
}