SolidityFunctionWrapper extends Generator { public MethodSpec buildFunction( AbiDefinition functionDefinition) throws ClassNotFoundException { String functionName = functionDefinition.getName(); MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(functionName) .addModifiers(Modifier.PUBLIC); String inputParams = addParameters(methodBuilder, functionDefinition.getInputs()); List<TypeName> outputParameterTypes = buildTypeNames(functionDefinition.getOutputs()); if (functionDefinition.isConstant()) { buildConstantFunction( functionDefinition, methodBuilder, outputParameterTypes, inputParams); } else { buildTransactionFunction( functionDefinition, methodBuilder, inputParams); } return methodBuilder.build(); } SolidityFunctionWrapper(boolean useNativeJavaTypes);  SolidityFunctionWrapper(boolean useNativeJavaTypes, GenerationReporter reporter); @SuppressWarnings("unchecked") void generateJavaFiles(
            String contractName, String bin, String abi, String destinationDir,
            String basePackageName); Iterable<FieldSpec> buildFuncNameConstants(List<AbiDefinition> functionDefinitions); static TypeName getNativeType(TypeName typeName); static TypeName getNativeType(ParameterizedTypeName parameterizedTypeName); static TypeName getEventNativeType(TypeName typeName); static String createValidParamName(String name, int idx); static List<TypeName> buildTypeNames(List<AbiDefinition.NamedType> namedTypes); MethodSpec buildFunction(
            AbiDefinition functionDefinition); MethodSpec buildEventFunction(
            String responseClassName,
            String functionName,
            List<NamedTypeName> indexedParameters,
            List<NamedTypeName> nonIndexedParameters); List<MethodSpec> buildEventFunctions(
            AbiDefinition functionDefinition,
            TypeSpec.Builder classBuilder); CodeBlock buildTypedResponse(
            String objectName,
            List<com.peersafe.codegen.SolidityFunctionWrapper.NamedTypeName> indexedParameters,
            List<com.peersafe.codegen.SolidityFunctionWrapper.NamedTypeName> nonIndexedParameters,
            boolean observable); static TypeName buildTypeName(String typeDeclaration);  }
@Test public void testBuildFunctionTransaction() throws Exception { AbiDefinition functionDefinition = new AbiDefinition( false, Arrays.asList( new AbiDefinition.NamedType("param", "uint8")), "functionName", Collections.emptyList(), "type", false); MethodSpec methodSpec = solidityFunctionWrapper.buildFunction(functionDefinition); String expected = "public org.web3j.protocol.core.RemoteCall<org.web3j.protocol.core.methods.response.TransactionReceipt> functionName(java.math.BigInteger param) {\n" + " final com.peersafe.abi.datatypes.Function function = new com.peersafe.abi.datatypes.Function(\n" + " FUNC_FUNCTIONNAME, \n" + " java.util.Arrays.<com.peersafe.abi.datatypes.Type>asList(new com.peersafe.abi.datatypes.generated.Uint8(param)), \n" + " java.util.Collections.<com.peersafe.abi.TypeReference<?>>emptyList());\n" + " return executeRemoteCallTransaction(function);\n" + "}\n"; assertThat(methodSpec.toString(), is(expected)); }
@Test public void testBuildingFunctionTransactionThatReturnsValueReportsWarning() throws Exception { AbiDefinition functionDefinition = new AbiDefinition( false, Arrays.asList( new AbiDefinition.NamedType("param", "uint8")), "functionName", Arrays.asList( new AbiDefinition.NamedType("result", "uint8")), "type", false); solidityFunctionWrapper.buildFunction(functionDefinition); verify(generationReporter).report( "Definition of the function functionName returns a value but is not defined as a view function. " + "Please ensure it contains the view modifier if you want to read the return value"); }
@Test public void testBuildPayableFunctionTransaction() throws Exception { AbiDefinition functionDefinition = new AbiDefinition( false, Arrays.asList( new AbiDefinition.NamedType("param", "uint8")), "functionName", Collections.emptyList(), "type", true); MethodSpec methodSpec = solidityFunctionWrapper.buildFunction(functionDefinition); String expected = "public org.web3j.protocol.core.RemoteCall<org.web3j.protocol.core.methods.response.TransactionReceipt> functionName(java.math.BigInteger param, java.math.BigInteger weiValue) {\n" + " final com.peersafe.abi.datatypes.Function function = new com.peersafe.abi.datatypes.Function(\n" + " FUNC_FUNCTIONNAME, \n" + " java.util.Arrays.<com.peersafe.abi.datatypes.Type>asList(new com.peersafe.abi.datatypes.generated.Uint8(param)), \n" + " java.util.Collections.<com.peersafe.abi.TypeReference<?>>emptyList());\n" + " return executeRemoteCallTransaction(function, weiValue);\n" + "}\n"; assertThat(methodSpec.toString(), is(expected)); }
@Test public void testBuildFunctionConstantSingleValueReturn() throws Exception { AbiDefinition functionDefinition = new AbiDefinition( true, Arrays.asList( new AbiDefinition.NamedType("param", "uint8")), "functionName", Arrays.asList( new AbiDefinition.NamedType("result", "int8")), "type", false); MethodSpec methodSpec = solidityFunctionWrapper.buildFunction(functionDefinition); String expected = "public org.web3j.protocol.core.RemoteCall<java.math.BigInteger> functionName(java.math.BigInteger param) {\n" + " final com.peersafe.abi.datatypes.Function function = new com.peersafe.abi.datatypes.Function(FUNC_FUNCTIONNAME, \n" + " java.util.Arrays.<com.peersafe.abi.datatypes.Type>asList(new com.peersafe.abi.datatypes.generated.Uint8(param)), \n" + " java.util.Arrays.<com.peersafe.abi.TypeReference<?>>asList(new com.peersafe.abi.TypeReference<com.peersafe.abi.datatypes.generated.Int8>() {}));\n" + " return executeRemoteCallSingleValueReturn(function, java.math.BigInteger.class);\n" + "}\n"; assertThat(methodSpec.toString(), is(expected)); }
@Test public void testBuildFunctionConstantSingleValueRawListReturn() throws Exception { AbiDefinition functionDefinition = new AbiDefinition( true, Arrays.asList( new AbiDefinition.NamedType("param", "uint8")), "functionName", Arrays.asList( new AbiDefinition.NamedType("result", "address[]")), "type", false); MethodSpec methodSpec = solidityFunctionWrapper.buildFunction(functionDefinition); String expected = "public org.web3j.protocol.core.RemoteCall<java.util.List> functionName(java.math.BigInteger param) {\n" + " final com.peersafe.abi.datatypes.Function function = new com.peersafe.abi.datatypes.Function(FUNC_FUNCTIONNAME, \n" + " java.util.Arrays.<com.peersafe.abi.datatypes.Type>asList(new com.peersafe.abi.datatypes.generated.Uint8(param)), \n" + " java.util.Arrays.<com.peersafe.abi.TypeReference<?>>asList(new com.peersafe.abi.TypeReference<com.peersafe.abi.datatypes.DynamicArray<com.peersafe.abi.datatypes.Address>>() {}));\n" + " return new org.web3j.protocol.core.RemoteCall<java.util.List>(\n" + " new java.util.concurrent.Callable<java.util.List>() {\n" + " @java.lang.Override\n" + " @java.lang.SuppressWarnings(\"unchecked\")\n" + " public java.util.List call() throws java.lang.Exception {\n" + " java.util.List<com.peersafe.abi.datatypes.Type> result = (java.util.List<com.peersafe.abi.datatypes.Type>) executeCallSingleValueReturn(function, java.util.List.class);\n" + " return convertToNative(result);\n" + " }\n" + " });\n" + "}\n"; assertThat(methodSpec.toString(), is(expected)); }
@Test(expected = RuntimeException.class) public void testBuildFunctionConstantInvalid() throws Exception { AbiDefinition functionDefinition = new AbiDefinition( true, Arrays.asList( new AbiDefinition.NamedType("param", "uint8")), "functionName", Collections.emptyList(), "type", false); solidityFunctionWrapper.buildFunction(functionDefinition); }
@Test public void testBuildFunctionConstantMultipleValueReturn() throws Exception { AbiDefinition functionDefinition = new AbiDefinition( true, Arrays.asList( new AbiDefinition.NamedType("param1", "uint8"), new AbiDefinition.NamedType("param2", "uint32")), "functionName", Arrays.asList( new AbiDefinition.NamedType("result1", "int8"), new AbiDefinition.NamedType("result2", "int32")), "type", false); MethodSpec methodSpec = solidityFunctionWrapper.buildFunction(functionDefinition); String expected = "public org.web3j.protocol.core.RemoteCall<org.web3j.tuples.generated.Tuple2<java.math.BigInteger, java.math.BigInteger>> functionName(java.math.BigInteger param1, java.math.BigInteger param2) {\n" + " final com.peersafe.abi.datatypes.Function function = new com.peersafe.abi.datatypes.Function(FUNC_FUNCTIONNAME, \n" + " java.util.Arrays.<com.peersafe.abi.datatypes.Type>asList(new com.peersafe.abi.datatypes.generated.Uint8(param1), \n" + " new com.peersafe.abi.datatypes.generated.Uint32(param2)), \n" + " java.util.Arrays.<com.peersafe.abi.TypeReference<?>>asList(new com.peersafe.abi.TypeReference<com.peersafe.abi.datatypes.generated.Int8>() {}, new com.peersafe.abi.TypeReference<com.peersafe.abi.datatypes.generated.Int32>() {}));\n" + " return new org.web3j.protocol.core.RemoteCall<org.web3j.tuples.generated.Tuple2<java.math.BigInteger, java.math.BigInteger>>(\n" + " new java.util.concurrent.Callable<org.web3j.tuples.generated.Tuple2<java.math.BigInteger, java.math.BigInteger>>() {\n" + " @java.lang.Override\n" + " public org.web3j.tuples.generated.Tuple2<java.math.BigInteger, java.math.BigInteger> call() throws java.lang.Exception {\n" + " java.util.List<com.peersafe.abi.datatypes.Type> results = executeCallMultipleValueReturn(function);\n" + " return new org.web3j.tuples.generated.Tuple2<java.math.BigInteger, java.math.BigInteger>(\n" + " (java.math.BigInteger) results.get(0).getValue(), \n" + " (java.math.BigInteger) results.get(1).getValue());\n" + " }\n" + " });\n" + "}\n"; assertThat(methodSpec.toString(), is(expected)); }