Iterable<FieldSpec> buildFuncNameConstants(List<AbiDefinition> functionDefinitions) { List<FieldSpec> fields = new ArrayList<>(); Set<String> fieldNames = new HashSet<>(); fieldNames.add(Contract.FUNC_DEPLOY); for (AbiDefinition functionDefinition : functionDefinitions) { if (functionDefinition.getType().equals("function")) { String funcName = functionDefinition.getName(); if (!fieldNames.contains(funcName)) { FieldSpec field = FieldSpec.builder(String.class, funcNameToConst(funcName), Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) .initializer("$S", funcName) .build(); fields.add(field); fieldNames.add(funcName); } } } return fields; }
@Override public int hashCode() { int result = (isConstant() ? 1 : 0); result = 31 * result + (getInputs() != null ? getInputs().hashCode() : 0); result = 31 * result + (getName() != null ? getName().hashCode() : 0); result = 31 * result + (getOutputs() != null ? getOutputs().hashCode() : 0); result = 31 * result + (getType() != null ? getType().hashCode() : 0); result = 31 * result + (isPayable() ? 1 : 0); result = 31 * result + (getStateMutability() != null ? getStateMutability().hashCode() : 0); return result; }
private void buildTransactionFunction( AbiDefinition functionDefinition, MethodSpec.Builder methodBuilder, String inputParams) throws ClassNotFoundException { if (functionDefinition.hasOutputs()) { //CHECKSTYLE:OFF reporter.report(String.format( "Definition of the function %s 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", functionDefinition.getName())); //CHECKSTYLE:ON } if (functionDefinition.isPayable()) { methodBuilder.addParameter(BigInteger.class, WEI_VALUE); } String functionName = functionDefinition.getName(); methodBuilder.returns(buildRemoteCall(TypeName.get(TransactionReceipt.class))); methodBuilder.addStatement("final $T function = new $T(\n$N, \n$T.<$T>asList($L), \n$T" + ".<$T<?>>emptyList())", Function.class, Function.class, funcNameToConst(functionName), Arrays.class, Type.class, inputParams, Collections.class, TypeReference.class); if (functionDefinition.isPayable()) { methodBuilder.addStatement( "return executeRemoteCallTransaction(function, $N)", WEI_VALUE); } else { methodBuilder.addStatement("return executeRemoteCallTransaction(function)"); } }
MethodSpec buildFunction( AbiDefinition functionDefinition) throws ClassNotFoundException { String functionName = functionDefinition.getName(); // If the solidity function name is a reserved word // in the current java version prepend it with "_" if (!SourceVersion.isName(functionName)) { functionName = "_" + functionName; } 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(); }
String inputParams) throws ClassNotFoundException { String functionName = functionDefinition.getName();
List<MethodSpec> buildEventFunctions( AbiDefinition functionDefinition, TypeSpec.Builder classBuilder) throws ClassNotFoundException { String functionName = functionDefinition.getName(); List<AbiDefinition.NamedType> inputs = functionDefinition.getInputs(); String responseClassName = Strings.capitaliseFirstLetter(functionName) + "EventResponse";
@Test public void testParseConvertLib() throws Exception { Contract mc = parseContractJson(contractBaseDir, "MetaCoin", "ConvertLib"); assertEquals("Unexpected contract name", "ConvertLib", mc.getContractName()); assertEquals("Unexpected number of functions", 1, mc.abi.size()); AbiDefinition abi = mc.abi.get(0); assertEquals("Unexpected function name", "convert", abi.getName()); assertTrue("Expected function to be 'constant'", abi.isConstant()); assertFalse("Expected function to not be 'payable'", abi.isPayable()); assertEquals("Expected abi to represent a function", "function", abi.getType()); assertEquals("Expected the 'pure' for the state mutability setting", "pure", abi.getStateMutability()); } }
Iterable<FieldSpec> buildFuncNameConstants(List<AbiDefinition> functionDefinitions) { List<FieldSpec> fields = new ArrayList<>(); Set<String> fieldNames = new HashSet<>(); fieldNames.add(Contract.FUNC_DEPLOY); for (AbiDefinition functionDefinition : functionDefinitions) { if (functionDefinition.getType().equals("function")) { String funcName = functionDefinition.getName(); if (!fieldNames.contains(funcName)) { FieldSpec field = FieldSpec.builder(String.class, funcNameToConst(funcName), Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) .initializer("$S", funcName) .build(); fields.add(field); fieldNames.add(funcName); } } } return fields; }
private void buildTransactionFunction( AbiDefinition functionDefinition, MethodSpec.Builder methodBuilder, String inputParams) throws ClassNotFoundException { if (functionDefinition.hasOutputs()) { //CHECKSTYLE:OFF reporter.report(String.format( "Definition of the function %s 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", functionDefinition.getName())); //CHECKSTYLE:ON } if (functionDefinition.isPayable()) { methodBuilder.addParameter(BigInteger.class, WEI_VALUE); } String functionName = functionDefinition.getName(); methodBuilder.returns(buildRemoteCall(TypeName.get(TransactionReceipt.class))); methodBuilder.addStatement("final $T function = new $T(\n$N, \n$T.<$T>asList($L), \n$T" + ".<$T<?>>emptyList())", Function.class, Function.class, funcNameToConst(functionName), Arrays.class, Type.class, inputParams, Collections.class, TypeReference.class); if (functionDefinition.isPayable()) { methodBuilder.addStatement( "return executeRemoteCallTransaction(function, $N)", WEI_VALUE); } else { methodBuilder.addStatement("return executeRemoteCallTransaction(function)"); } }
String inputParams) throws ClassNotFoundException { String functionName = functionDefinition.getName();
MethodSpec buildFunction( AbiDefinition functionDefinition) throws ClassNotFoundException { String functionName = functionDefinition.getName(); // If the solidity function name is a reserved word // in the current java version prepend it with "_" if (!SourceVersion.isName(functionName)) { functionName = "_" + functionName; } 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(); }
List<MethodSpec> buildEventFunctions( AbiDefinition functionDefinition, TypeSpec.Builder classBuilder) throws ClassNotFoundException { String functionName = functionDefinition.getName(); List<AbiDefinition.NamedType> inputs = functionDefinition.getInputs(); String responseClassName = Strings.capitaliseFirstLetter(functionName) + "EventResponse";