Java Code Examples for org.web3j.utils.Strings#capitaliseFirstLetter()

The following examples show how to use org.web3j.utils.Strings#capitaliseFirstLetter() . 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: RunnerTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void generateJavaCode() {
	String binPath = RunnerTest.class.getClassLoader().getResource(bin).getPath();
	String abiPath = RunnerTest.class.getClassLoader().getResource(abi).getPath();

	String outputPath = tempDirPath;
	// String outputPath = System.getProperty("user.dir").replace("console", "core") + "/src/test/java/";
	String packageName = "com.platon.sdk.contracts";

	String[] params = { "solidity", "generate", binPath, abiPath, "-o", outputPath, "-p", packageName };
	try {
		Runner.main(params);
	} catch (Exception e) {
		logger.error("Failed to generate Java code: " + e.getMessage(), e);
	}

	String sourceFile = tempDirPath + File.separator + packageName.replace('.', File.separatorChar) + File.separator
			+ Strings.capitaliseFirstLetter("HumanStandardToken") + ".java";

	boolean condition = new File(sourceFile).exists();

	Assert.assertTrue("Java wrapper source code generator for Solidity ABI format error.", condition);
}
 
Example 2
Source File: WasmFunctionWrapper.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
public TypeSpec buildStruct(WasmAbiDefinition functionDefinition, Set<String> customTypes) {
	String className = Strings.capitaliseFirstLetter(functionDefinition.getName());
	TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(className).addModifiers(Modifier.PUBLIC, Modifier.STATIC);
	List<FieldSpec> fieldSpecs = new ArrayList<>();

	if (null != functionDefinition.getBaseclass() && !functionDefinition.getBaseclass().isEmpty()) {
		String baseClass = Strings.capitaliseFirstLetter(functionDefinition.getBaseclass().get(0));
		FieldSpec field = FieldSpec.builder(ClassName.get("", baseClass), "baseClass", Modifier.PUBLIC).build();
		fieldSpecs.add(field);
	}
	for (int i = 0; i < functionDefinition.getFields().size(); i++) {
		WasmAbiDefinition.NamedType namedType = functionDefinition.getFields().get(i);
		String name = namedType.getName();
		String type = namedType.getType();
		fieldSpecs.add(FieldSpec.builder(buildTypeName(type, customTypes), name, Modifier.PUBLIC).build());
	}
	typeBuilder.addFields(fieldSpecs);

	return typeBuilder.build();
}
 
Example 3
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
void generateJavaFiles(
        String contractName, String bin, List<AbiDefinition> abi, String destinationDir,
        String basePackageName, Map<String, String> addresses)
        throws IOException, ClassNotFoundException {
    String className = Strings.capitaliseFirstLetter(contractName);

    TypeSpec.Builder classBuilder = createClassBuilder(className, bin);

    classBuilder.addMethod(buildConstructor(Credentials.class, CREDENTIALS));
    classBuilder.addMethod(buildConstructor(TransactionManager.class,
            TRANSACTION_MANAGER));
    classBuilder.addMethods(
            buildFunctionDefinitions(className, classBuilder, abi));
    classBuilder.addMethod(buildLoad(className, Credentials.class, CREDENTIALS));
    classBuilder.addMethod(buildLoad(className, TransactionManager.class,
            TRANSACTION_MANAGER));

    addAddressesSupport(classBuilder, addresses);

    write(basePackageName, classBuilder.build(), destinationDir);
}
 
Example 4
Source File: SolidityFunctionWrapperGenerator.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private void generate() throws IOException, ClassNotFoundException {

		File binaryFile = new File(binaryFileLocation);
		if (!binaryFile.exists()) {
			exitError("Invalid input binary file specified: " + binaryFileLocation);
		}

		byte[] bytes = Files.readBytes(new File(binaryFile.toURI()));
		String binary = new String(bytes);

		File absFile = new File(absFileLocation);
		if (!absFile.exists() || !absFile.canRead()) {
			exitError("Invalid input ABI file specified: " + absFileLocation);
		}
		String fileName = absFile.getName();
		String contractName = getFileNameNoExtension(fileName);
		bytes = Files.readBytes(new File(absFile.toURI()));
		String abi = new String(bytes);

		List<AbiDefinition> functionDefinitions = loadContractDefinition(absFile);

		if (functionDefinitions.isEmpty()) {
			exitError("Unable to parse input ABI file");
		} else {
			String className = Strings.capitaliseFirstLetter(contractName);
			System.out.printf("Generating " + basePackageName + "." + className + " ... ");
			new SolidityFunctionWrapper(useJavaNativeTypes).generateJavaFiles(contractName, binary, abi, destinationDirLocation.toString(),
					basePackageName);
			System.out.println("File written to " + destinationDirLocation.toString() + "\n");
		}
	}
 
Example 5
Source File: SolidityFunctionWrapperGenerator.java    From web3j with Apache License 2.0 5 votes vote down vote up
public final void generate() throws IOException, ClassNotFoundException {
    String binary = Contract.BIN_NOT_PROVIDED;
    if (binFile != null) {
        byte[] bytes = Files.readBytes(binFile);
        binary = new String(bytes);
    }
    List<AbiDefinition> functionDefinitions = loadContractDefinition(abiFile);

    if (functionDefinitions.isEmpty()) {
        exitError("Unable to parse input ABI file");
    } else {
        String className = Strings.capitaliseFirstLetter(contractName);
        System.out.print("Generating " + basePackageName + "." + className + " ... ");

        new SolidityFunctionWrapper(
                        useJavaNativeTypes,
                        useJavaPrimitiveTypes,
                        generateSendTxForCalls,
                        addressLength)
                .generateJavaFiles(
                        contractClass,
                        contractName,
                        binary,
                        functionDefinitions,
                        destinationDirLocation.toString(),
                        basePackageName,
                        null);

        System.out.println("File written to " + destinationDirLocation.toString() + "\n");
    }
}
 
Example 6
Source File: TruffleJsonFunctionWrapperGenerator.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void generate() throws IOException, ClassNotFoundException {

    File truffleJsonFile = new File(jsonFileLocation);
    if (!truffleJsonFile.exists() || !truffleJsonFile.canRead()) {
        exitError("Invalid input json file specified: " + jsonFileLocation);
    }

    String fileName = truffleJsonFile.getName();
    String contractName = getFileNameNoExtension(fileName);

    Contract c = loadContractDefinition(truffleJsonFile);
    if (c == null) {
        exitError("Unable to parse input json file");
    } else {
        String className = Strings.capitaliseFirstLetter(contractName);
        System.out.printf("Generating " + basePackageName + "." + className + " ... ");
        Map<String, String> addresses;
        if (c.networks != null && !c.networks.isEmpty()) {
            addresses = c.networks.entrySet().stream()
                    .filter(e -> (e.getValue() != null && e.getValue().getAddress() != null))
                    .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getAddress()
                    ));
        } else {
            addresses = Collections.EMPTY_MAP;
        }
        new SolidityFunctionWrapper(useJavaNativeTypes)
                .generateJavaFiles(contractName,
                        c.getBytecode(),
                        c.getAbi(),
                        destinationDirLocation.toString(),
                        basePackageName,
                        addresses);
        System.out.println("File written to " + destinationDirLocation.toString() + "\n");
    }
}
 
Example 7
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
MethodSpec buildEventTransactionReceiptFunction(
        String responseClassName,
        String functionName,
        List<NamedTypeName> indexedParameters,
        List<NamedTypeName> nonIndexedParameters) {

    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(
            ClassName.get(List.class), ClassName.get("", responseClassName));

    String generatedFunctionName = "get" + Strings.capitaliseFirstLetter(functionName)
            + "Events";
    MethodSpec.Builder transactionMethodBuilder = MethodSpec
            .methodBuilder(generatedFunctionName)
            .addModifiers(Modifier.PUBLIC)
            .addParameter(TransactionReceipt.class, "transactionReceipt")
            .returns(parameterizedTypeName);

    transactionMethodBuilder.addStatement("$T valueList = extractEventParametersWithLog("
            + buildEventDefinitionName(functionName) + ", "
            + "transactionReceipt)", ParameterizedTypeName.get(List.class,
                    Contract.EventValuesWithLog.class))
            .addStatement("$1T responses = new $1T(valueList.size())",
                    ParameterizedTypeName.get(ClassName.get(ArrayList.class),
                            ClassName.get("", responseClassName)))
            .beginControlFlow("for ($T eventValues : valueList)",
                    Contract.EventValuesWithLog.class)
            .addStatement("$1T typedResponse = new $1T()",
                    ClassName.get("", responseClassName))
            .addCode(buildTypedResponse("typedResponse", indexedParameters,
                    nonIndexedParameters, false))
            .addStatement("responses.add(typedResponse)")
            .endControlFlow();


    transactionMethodBuilder.addStatement("return responses");
    return transactionMethodBuilder.build();
}
 
Example 8
Source File: SolidityFunctionWrapperGenerator.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void generate() throws IOException, ClassNotFoundException {

        File binaryFile = new File(binaryFileLocation);
        if (!binaryFile.exists()) {
            exitError("Invalid input binary file specified: " + binaryFileLocation);
        }

        byte[] bytes = Files.readBytes(new File(binaryFile.toURI()));
        String binary = new String(bytes);

        File absFile = new File(absFileLocation);
        if (!absFile.exists() || !absFile.canRead()) {
            exitError("Invalid input ABI file specified: " + absFileLocation);
        }
        String fileName = absFile.getName();
        String contractName = getFileNameNoExtension(fileName);
        bytes = Files.readBytes(new File(absFile.toURI()));
        String abi = new String(bytes);

        List<AbiDefinition> functionDefinitions = loadContractDefinition(absFile);

        if (functionDefinitions.isEmpty()) {
            exitError("Unable to parse input ABI file");
        } else {
            String className = Strings.capitaliseFirstLetter(contractName);
            System.out.printf("Generating " + basePackageName + "." + className + " ... ");
            new SolidityFunctionWrapper(useJavaNativeTypes).generateJavaFiles(
                    contractName, binary, abi, destinationDirLocation.toString(), basePackageName);
            System.out.println("File written to " + destinationDirLocation.toString() + "\n");
        }
    }
 
Example 9
Source File: WasmFunctionWrapperGenerator.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private void generate() throws IOException, ClassNotFoundException {

		File binaryFile = new File(binaryFileLocation);
		if (!binaryFile.exists()) {
			exitError("Invalid input binary file specified: " + binaryFileLocation);
		}

		byte[] bytes = Files.readBytes(new File(binaryFile.toURI()));
		// String binary = new String(bytes);
		String binary = Numeric.toHexString(bytes);

		File absFile = new File(absFileLocation);
		if (!absFile.exists() || !absFile.canRead()) {
			exitError("Invalid input ABI file specified: " + absFileLocation);
		}
		String fileName = absFile.getName();
		String contractName = getFileNameNoExtension(getFileNameNoExtension(fileName));
		bytes = Files.readBytes(new File(absFile.toURI()));
		String abi = new String(bytes);

		List<WasmAbiDefinition> functionDefinitions = loadContractDefinition(absFile);

		if (functionDefinitions.isEmpty()) {
			exitError("Unable to parse input ABI file");
		} else {
			String className = Strings.capitaliseFirstLetter(contractName);

			System.out.println("Generating " + basePackageName + "." + className + " ... ");

			new WasmFunctionWrapper().generateJavaFiles(contractName, binary, abi, destinationDirLocation.toString(), basePackageName);

			System.out.println("File written to " + destinationDirLocation.toString() + "\n");
		}
	}
 
Example 10
Source File: TruffleJsonFunctionWrapperGenerator.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void generate() throws IOException, ClassNotFoundException {

    File truffleJsonFile = new File(jsonFileLocation);
    if (!truffleJsonFile.exists() || !truffleJsonFile.canRead()) {
        exitError("Invalid input json file specified: " + jsonFileLocation);
    }

    String fileName = truffleJsonFile.getName();
    String contractName = getFileNameNoExtension(fileName);

    Contract c = loadContractDefinition(truffleJsonFile);
    if (c == null) {
        exitError("Unable to parse input json file");
    } else {
        String className = Strings.capitaliseFirstLetter(contractName);
        System.out.printf("Generating " + basePackageName + "." + className + " ... ");
        Map<String, String> addresses;
        if (c.networks != null && !c.networks.isEmpty()) {
            addresses = c.networks.entrySet().stream()
                    .filter(e -> (e.getValue() != null && e.getValue().getAddress() != null))
                    .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getAddress()
                    ));
        } else {
            addresses = Collections.EMPTY_MAP;
        }
        new SolidityFunctionWrapper(useJavaNativeTypes)
                .generateJavaFiles(contractName,
                        c.getBytecode(),
                        c.getAbi(),
                        destinationDirLocation.toString(),
                        basePackageName,
                        addresses);
        System.out.println("File written to " + destinationDirLocation.toString() + "\n");
    }
}
 
Example 11
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
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<NamedTypeName> parameters = new ArrayList<>();
    List<NamedTypeName> indexedParameters = new ArrayList<>();
    List<NamedTypeName> nonIndexedParameters = new ArrayList<>();

    for (AbiDefinition.NamedType namedType : inputs) {
        NamedTypeName parameter = new NamedTypeName(
                namedType.getName(),
                buildTypeName(namedType.getType()),
                namedType.isIndexed()
        );
        if (namedType.isIndexed()) {
            indexedParameters.add(parameter);
        } else {
            nonIndexedParameters.add(parameter);
        }
        parameters.add(parameter);
    }

    classBuilder.addField(createEventDefinition(functionName, parameters));

    classBuilder.addType(buildEventResponseObject(responseClassName, indexedParameters,
            nonIndexedParameters));

    List<MethodSpec> methods = new ArrayList<>();
    methods.add(buildEventTransactionReceiptFunction(responseClassName,
            functionName, indexedParameters, nonIndexedParameters));

    methods.add(buildEventObservableFunction(responseClassName, functionName,
            indexedParameters, nonIndexedParameters));
    methods.add(buildDefaultEventObservableFunction(responseClassName,
            functionName));
    return methods;
}
 
Example 12
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
MethodSpec buildEventTransactionReceiptFunction(
        String responseClassName,
        String functionName,
        List<NamedTypeName> indexedParameters,
        List<NamedTypeName> nonIndexedParameters) {

    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(
            ClassName.get(List.class), ClassName.get("", responseClassName));

    String generatedFunctionName = "get" + Strings.capitaliseFirstLetter(functionName)
            + "Events";
    MethodSpec.Builder transactionMethodBuilder = MethodSpec
            .methodBuilder(generatedFunctionName)
            .addModifiers(Modifier.PUBLIC)
            .addParameter(TransactionReceipt.class, "transactionReceipt")
            .returns(parameterizedTypeName);

    transactionMethodBuilder.addStatement("$T valueList = extractEventParametersWithLog("
            + buildEventDefinitionName(functionName) + ", "
            + "transactionReceipt)", ParameterizedTypeName.get(List.class,
            Contract.EventValuesWithLog.class))
            .addStatement("$1T responses = new $1T(valueList.size())",
                    ParameterizedTypeName.get(ClassName.get(ArrayList.class),
                            ClassName.get("", responseClassName)))
            .beginControlFlow("for ($T eventValues : valueList)",
                    Contract.EventValuesWithLog.class)
            .addStatement("$1T typedResponse = new $1T()",
                    ClassName.get("", responseClassName))
            .addCode(buildTypedResponse("typedResponse", indexedParameters,
                    nonIndexedParameters, false))
            .addStatement("responses.add(typedResponse)")
            .endControlFlow();


    transactionMethodBuilder.addStatement("return responses");
    return transactionMethodBuilder.build();
}
 
Example 13
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
void generateJavaFiles(
        String contractName, String bin, List<AbiDefinition> abi, String destinationDir,
        String basePackageName, Map<String, String> addresses)
        throws IOException, ClassNotFoundException {
    String className = Strings.capitaliseFirstLetter(contractName);

    TypeSpec.Builder classBuilder = createClassBuilder(className, bin);

    classBuilder.addMethod(buildConstructor(Credentials.class, CREDENTIALS, false));
    classBuilder.addMethod(buildConstructor(Credentials.class, CREDENTIALS, true));
    classBuilder.addMethod(buildConstructor(TransactionManager.class,
            TRANSACTION_MANAGER, false));
    classBuilder.addMethod(buildConstructor(TransactionManager.class,
            TRANSACTION_MANAGER, true));
    classBuilder.addFields(buildFuncNameConstants(abi));
    classBuilder.addMethods(
            buildFunctionDefinitions(className, classBuilder, abi));
    classBuilder.addMethod(buildLoad(className, Credentials.class, CREDENTIALS, false));
    classBuilder.addMethod(buildLoad(className, TransactionManager.class,
            TRANSACTION_MANAGER, false));
    classBuilder.addMethod(buildLoad(className, Credentials.class, CREDENTIALS, true));
    classBuilder.addMethod(buildLoad(className, TransactionManager.class,
            TRANSACTION_MANAGER, true));

    addAddressesSupport(classBuilder, addresses);

    write(basePackageName, classBuilder.build(), destinationDir);
}
 
Example 14
Source File: WasmFunctionWrapper.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
List<MethodSpec> buildEventFunctions(WasmAbiDefinition functionDefinition, TypeSpec.Builder classBuilder, Set<String> customTypes) {
	String functionName = functionDefinition.getName();
	String responseClassName = Strings.capitaliseFirstLetter(functionName) + "EventResponse";

	List<NamedType> inputs = functionDefinition.getInput();
	int topic = functionDefinition.getTopic();
	List<NamedTypeName> indexedParameters = new ArrayList<>();
	List<NamedTypeName> nonIndexedParameters = new ArrayList<>();
	for (int i = 0; i < inputs.size(); i++) {
		NamedType namedType = inputs.get(i);
		String name = namedType.getName();
		String type = namedType.getType();

		TypeName typeName = buildTypeName(type, customTypes);
		boolean indexed = i < topic ? true : false;
		NamedTypeName parameter = new NamedTypeName(name, typeName, indexed);
		if (indexed) {
			indexedParameters.add(parameter);
		} else {
			nonIndexedParameters.add(parameter);
		}
	}

	classBuilder.addField(createEventDefinition(functionName, indexedParameters, nonIndexedParameters));
	classBuilder.addType(buildEventResponseObject(responseClassName, indexedParameters, nonIndexedParameters));

	List<MethodSpec> methods = new ArrayList<>();
	methods.add(buildEventTransactionReceiptFunction(responseClassName, functionName, indexedParameters, nonIndexedParameters));
	methods.add(buildEventObservableFunction(responseClassName, functionName, indexedParameters, nonIndexedParameters));
	methods.add(buildDefaultEventObservableFunction(responseClassName, functionName));
	return methods;
}
 
Example 15
Source File: TruffleJsonFunctionWrapperGenerator.java    From web3j with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void generate() throws IOException, ClassNotFoundException {

    File truffleJsonFile = new File(jsonFileLocation);
    if (!truffleJsonFile.exists() || !truffleJsonFile.canRead()) {
        exitError("Invalid input json file specified: " + jsonFileLocation);
    }

    String fileName = truffleJsonFile.getName();
    String contractName = getFileNameNoExtension(fileName);

    Contract c = loadContractDefinition(truffleJsonFile);
    if (c == null) {
        exitError("Unable to parse input json file");
    } else {
        String className = Strings.capitaliseFirstLetter(contractName);
        System.out.printf("Generating " + basePackageName + "." + className + " ... ");
        Map<String, String> addresses;
        if (c.networks != null && !c.networks.isEmpty()) {
            addresses =
                    c.networks.entrySet().stream()
                            .filter(
                                    e ->
                                            (e.getValue() != null
                                                    && e.getValue().getAddress() != null))
                            .collect(
                                    Collectors.toMap(
                                            Map.Entry::getKey, e -> e.getValue().getAddress()));
        } else {
            addresses = Collections.EMPTY_MAP;
        }
        new SolidityFunctionWrapper(useJavaNativeTypes, Address.DEFAULT_LENGTH)
                .generateJavaFiles(
                        contractName,
                        c.getBytecode(),
                        c.getAbi(),
                        destinationDirLocation.toString(),
                        basePackageName,
                        addresses);
        System.out.println("File written to " + destinationDirLocation.toString() + "\n");
    }
}
 
Example 16
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
void 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<NamedTypeName> indexedParameters = new ArrayList<>();
    List<NamedTypeName> nonIndexedParameters = new ArrayList<>();

    for (AbiDefinition.NamedType namedType : inputs) {

        if (namedType.isIndexed()) {
            indexedParameters.add(
                    new org.web3j.codegen.SolidityFunctionWrapper.NamedTypeName(
                            namedType.getName(),
                            buildTypeName(namedType.getType())));
        } else {
            nonIndexedParameters.add(
                    new org.web3j.codegen.SolidityFunctionWrapper.NamedTypeName(
                            namedType.getName(),
                            buildTypeName(namedType.getType())));
        }
    }

    classBuilder.addField(createEventDefinition(functionName,
            indexedParameters, nonIndexedParameters));

    classBuilder.addType(buildEventResponseObject(responseClassName, indexedParameters,
            nonIndexedParameters));

    classBuilder.addMethod(buildEventTransactionReceiptFunction(responseClassName,
            functionName, indexedParameters, nonIndexedParameters));

    classBuilder.addMethod(buildEventObservableFunction(responseClassName, functionName,
            indexedParameters, nonIndexedParameters));

    classBuilder.addMethod(buildDefaultEventObservableFunction(responseClassName,
            functionName));
}
 
Example 17
Source File: WasmFunctionWrapper.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
void generateJavaFiles(String contractName, String bin, List<WasmAbiDefinition> abi, String destinationDir, String basePackageName)
		throws IOException, ClassNotFoundException {
	String className = Strings.capitaliseFirstLetter(contractName);

	TypeSpec.Builder classBuilder = createClassBuilder(className, bin);

	classBuilder.addMethod(buildConstructor(Credentials.class, CREDENTIALS));
	classBuilder.addMethod(buildConstructor(TransactionManager.class, TRANSACTION_MANAGER));

	classBuilder.addFields(buildFuncNameConstants(abi));

	classBuilder.addMethods(buildFunctionDefinitions(className, classBuilder, abi));

	classBuilder.addMethod(buildLoad(className, Credentials.class, CREDENTIALS));
	classBuilder.addMethod(buildLoad(className, TransactionManager.class, TRANSACTION_MANAGER));

	write(basePackageName, classBuilder.build(), destinationDir);
}
 
Example 18
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 4 votes vote down vote up
public void generateJavaFiles(
        Class<? extends Contract> contractClass,
        String contractName,
        String bin,
        List<AbiDefinition> abi,
        String destinationDir,
        String basePackageName,
        Map<String, String> addresses)
        throws IOException, ClassNotFoundException {

    if (!java.lang.reflect.Modifier.isAbstract(contractClass.getModifiers())) {
        throw new IllegalArgumentException("Contract base class must be abstract");
    }

    String className = Strings.capitaliseFirstLetter(contractName);
    TypeSpec.Builder classBuilder = createClassBuilder(contractClass, className, bin);

    classBuilder.addAnnotation(
            AnnotationSpec.builder(SuppressWarnings.class)
                    .addMember("value", "\"rawtypes\"")
                    .build());

    classBuilder.addMethod(buildConstructor(Credentials.class, CREDENTIALS, false));
    classBuilder.addMethod(buildConstructor(Credentials.class, CREDENTIALS, true));
    classBuilder.addMethod(
            buildConstructor(TransactionManager.class, TRANSACTION_MANAGER, false));
    classBuilder.addMethod(
            buildConstructor(TransactionManager.class, TRANSACTION_MANAGER, true));
    classBuilder.addFields(buildFuncNameConstants(abi));
    classBuilder.addTypes(buildStructTypes(abi));
    classBuilder.addMethods(buildFunctionDefinitions(className, classBuilder, abi));
    classBuilder.addMethod(buildLoad(className, Credentials.class, CREDENTIALS, false));
    classBuilder.addMethod(
            buildLoad(className, TransactionManager.class, TRANSACTION_MANAGER, false));
    classBuilder.addMethod(buildLoad(className, Credentials.class, CREDENTIALS, true));
    classBuilder.addMethod(
            buildLoad(className, TransactionManager.class, TRANSACTION_MANAGER, true));
    if (!bin.equals(Contract.BIN_NOT_PROVIDED)) {
        classBuilder.addMethods(buildDeployMethods(className, classBuilder, abi));
    }

    addAddressesSupport(classBuilder, addresses);

    write(basePackageName, classBuilder.build(), destinationDir);
}
 
Example 19
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 4 votes vote down vote up
MethodSpec buildEventTransactionReceiptFunction(
        String responseClassName,
        String functionName,
        List<NamedTypeName> indexedParameters,
        List<NamedTypeName> nonIndexedParameters) {

    ParameterizedTypeName parameterizedTypeName =
            ParameterizedTypeName.get(
                    ClassName.get(List.class), ClassName.get("", responseClassName));

    String generatedFunctionName =
            "get" + Strings.capitaliseFirstLetter(functionName) + "Events";
    MethodSpec.Builder transactionMethodBuilder =
            MethodSpec.methodBuilder(generatedFunctionName)
                    .addModifiers(Modifier.PUBLIC)
                    .addParameter(TransactionReceipt.class, "transactionReceipt")
                    .returns(parameterizedTypeName);

    transactionMethodBuilder
            .addStatement(
                    "$T valueList = extractEventParametersWithLog("
                            + buildEventDefinitionName(functionName)
                            + ", "
                            + "transactionReceipt)",
                    ParameterizedTypeName.get(List.class, Contract.EventValuesWithLog.class))
            .addStatement(
                    "$1T responses = new $1T(valueList.size())",
                    ParameterizedTypeName.get(
                            ClassName.get(ArrayList.class),
                            ClassName.get("", responseClassName)))
            .beginControlFlow(
                    "for ($T eventValues : valueList)", Contract.EventValuesWithLog.class)
            .addStatement("$1T typedResponse = new $1T()", ClassName.get("", responseClassName))
            .addCode(
                    buildTypedResponse(
                            "typedResponse", indexedParameters, nonIndexedParameters, false))
            .addStatement("responses.add(typedResponse)")
            .endControlFlow();

    transactionMethodBuilder.addStatement("return responses");
    return transactionMethodBuilder.build();
}
 
Example 20
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 4 votes vote down vote up
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<NamedTypeName> parameters = new ArrayList<>();
    List<NamedTypeName> indexedParameters = new ArrayList<>();
    List<NamedTypeName> nonIndexedParameters = new ArrayList<>();

    for (AbiDefinition.NamedType namedType : inputs) {
        final TypeName typeName;
        if (namedType.getType().equals("tuple")) {
            typeName = structClassNameMap.get(namedType.structIdentifier());
        } else {
            typeName = buildTypeName(namedType.getType(), useJavaPrimitiveTypes);
        }
        NamedTypeName parameter = new NamedTypeName(namedType, typeName);
        if (namedType.isIndexed()) {
            indexedParameters.add(parameter);
        } else {
            nonIndexedParameters.add(parameter);
        }
        parameters.add(parameter);
    }

    classBuilder.addField(createEventDefinition(functionName, parameters));

    classBuilder.addType(
            buildEventResponseObject(
                    responseClassName, indexedParameters, nonIndexedParameters));

    List<MethodSpec> methods = new ArrayList<>();
    methods.add(
            buildEventTransactionReceiptFunction(
                    responseClassName, functionName, indexedParameters, nonIndexedParameters));

    methods.add(
            buildEventFlowableFunction(
                    responseClassName, functionName, indexedParameters, nonIndexedParameters));
    methods.add(buildDefaultEventFlowableFunction(responseClassName, functionName));
    return methods;
}