@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(); }
@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 MethodSpec buildDeploy( String className, AbiDefinition functionDefinition, Class authType, String authName, boolean withGasProvider) { boolean isPayable = functionDefinition.isPayable(); MethodSpec.Builder methodBuilder = getDeployMethodSpec( className, authType, authName, isPayable, withGasProvider); String inputParams = addParameters(methodBuilder, functionDefinition.getInputs()); if (!inputParams.isEmpty()) { return buildDeployWithParams( methodBuilder, className, inputParams, authName, isPayable, withGasProvider); } else { return buildDeployNoParams(methodBuilder, className, authName, isPayable, withGasProvider); } }
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";
private List<MethodSpec> buildFunctionDefinitions( String className, TypeSpec.Builder classBuilder, List<AbiDefinition> functionDefinitions) throws ClassNotFoundException { List<MethodSpec> methodSpecs = new ArrayList<>(); for (AbiDefinition functionDefinition : functionDefinitions) { if (functionDefinition.getType().equals("function")) { MethodSpec ms = buildFunction(functionDefinition); methodSpecs.add(ms); } else if (functionDefinition.getType().equals("event")) { methodSpecs.addAll(buildEventFunctions(functionDefinition, classBuilder)); } } return methodSpecs; }
@Test public void testBuildFuncNameConstants() throws Exception { AbiDefinition functionDefinition = new AbiDefinition( false, Arrays.asList( new AbiDefinition.NamedType("param", "uint8")), "functionName", Collections.emptyList(), "function", true); TypeSpec.Builder builder = TypeSpec.classBuilder("testClass"); builder.addFields(solidityFunctionWrapper .buildFuncNameConstants(Collections.singletonList(functionDefinition))); //CHECKSTYLE:OFF String expected = "class testClass {\n" + " public static final java.lang.String FUNC_FUNCTIONNAME = \"functionName\";\n" + "}\n"; //CHECKSTYLE:ON assertThat(builder.build().toString(), is(expected)); }
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(); }
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 MethodSpec buildDeploy( String className, AbiDefinition functionDefinition, Class authType, String authName, boolean withGasProvider) { boolean isPayable = functionDefinition.isPayable(); MethodSpec.Builder methodBuilder = getDeployMethodSpec( className, authType, authName, isPayable, withGasProvider); String inputParams = addParameters(methodBuilder, functionDefinition.getInputs()); if (!inputParams.isEmpty()) { return buildDeployWithParams( methodBuilder, className, inputParams, authName, isPayable, withGasProvider); } else { return buildDeployNoParams(methodBuilder, className, authName, isPayable, withGasProvider); } }
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";
List<MethodSpec> methodSpecs = new ArrayList<>(); for (AbiDefinition functionDefinition : functionDefinitions) { if (functionDefinition.getType().equals("constructor")) { constructor = true; methodSpecs.add(buildDeploy(
@Test public void testBuildFunctionConstantInvalid() throws Exception { AbiDefinition functionDefinition = new AbiDefinition( true, Arrays.asList( new AbiDefinition.NamedType("param", "uint8")), "functionName", Collections.emptyList(), "type", false); MethodSpec methodSpec = solidityFunctionWrapper.buildFunction(functionDefinition); //CHECKSTYLE:OFF String expected = "public void functionName(java.math.BigInteger param) {\n" + " throw new RuntimeException(\"cannot call constant function with void return type\");\n" + "}\n"; //CHECKSTYLE:ON assertThat(methodSpec.toString(), is(expected)); }
String inputParams) throws ClassNotFoundException { String functionName = functionDefinition.getName();
if (isConstant() != that.isConstant()) { return false; if (isPayable() != that.isPayable()) { return false; if (getInputs() != null ? !getInputs().equals(that.getInputs()) : that.getInputs() != null) { return false; if (getName() != null ? !getName().equals(that.getName()) : that.getName() != null) { return false; if (getOutputs() != null ? !getOutputs().equals(that.getOutputs()) : that.getOutputs() != null) { return false; if (getStateMutability() != null ? !getStateMutability().equals(that.getStateMutability()) : that.getStateMutability() != null) { return false; return getType() != null ? getType().equals(that.getType()) : that.getType() == null;
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)"); } }
private List<MethodSpec> buildFunctionDefinitions( String className, TypeSpec.Builder classBuilder, List<AbiDefinition> functionDefinitions) throws ClassNotFoundException { List<MethodSpec> methodSpecs = new ArrayList<>(); for (AbiDefinition functionDefinition : functionDefinitions) { if (functionDefinition.getType().equals("function")) { MethodSpec ms = buildFunction(functionDefinition); methodSpecs.add(ms); } else if (functionDefinition.getType().equals("event")) { methodSpecs.addAll(buildEventFunctions(functionDefinition, classBuilder)); } } return methodSpecs; }