Search in sources :

Example 96 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project microservice_framework by CJSCommonPlatform.

the class JaxRsImplementationGenerator method methodBodyForMultipartPost.

/**
 * Supplier that produces code specific to a Multipart POST httpAction type.
 *
 * @param resourceMethodName name of the resource method
 * @param actionType         the HTTP action type for this method
 * @param bodyMimeType       the mime type to retrieve the file parts from
 * @param responseStrategy   the response strategy to pass to the ResponseStrategyFactory
 * @return the supplier that returns the {@link CodeBlock}
 */
private Supplier<CodeBlock> methodBodyForMultipartPost(final String resourceMethodName, final ActionType actionType, final MimeType bodyMimeType, final String responseStrategy) {
    final TypeName stringClassType = TypeName.get(String.class);
    final ParameterizedTypeName listType = ParameterizedTypeName.get(ClassName.get(List.class), stringClassType);
    final TypeName arrayClassType = TypeName.get(ArrayList.class);
    return () -> CodeBlock.builder().addStatement("final $T $L = new $T<>()", listType, PART_DEFINITIONS_VARIABLE, arrayClassType).add(putAllFileFormPartsInPartDefinitionsBuilder(bodyMimeType)).addStatement(REST_PROCESSOR_MULTIPART_METHOD_STATEMENT, responseStrategy, INTERCEPTOR_CHAIN_PROCESSOR_FIELD, ACTION_MAPPER_FIELD, resourceMethodName, actionType.toString(), VALID_PARAMETER_COLLECTION_BUILDER_VARIABLE, FILE_INPUT_DETAILS_FACTORY_FIELD, MULTIPART_FORM_DATA_INPUT, PART_DEFINITIONS_VARIABLE).build();
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 97 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project microservice_framework by CJSCommonPlatform.

the class JaxRsApplicationCodeGenerator method generateGetClassesMethod.

/**
 * Generate the getClasses method that returns the set of implemented resource classes.
 *
 * @param implementationNames the collection of implementation class names
 * @return the {@link MethodSpec} that represents the getClasses method
 */
private MethodSpec generateGetClassesMethod(final Collection<String> implementationNames) {
    final ParameterizedTypeName wildcardClassType = ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(Object.class));
    final ParameterizedTypeName classSetType = ParameterizedTypeName.get(ClassName.get(Set.class), wildcardClassType);
    return MethodSpec.methodBuilder("getClasses").addModifiers(PUBLIC).addAnnotation(Override.class).addCode(CodeBlock.builder().addStatement("$T classes = $L.providers()", classSetType, COMMON_PROVIDERS_FIELD).add(statementsToAddClassToSetForEach(implementationNames)).addStatement("return classes").build()).returns(classSetType).build();
}
Also used : Set(java.util.Set) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 98 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project web3sdk by FISCO-BCOS.

the class SolidityFunctionWrapper method buildTupleResultContainer0.

private void buildTupleResultContainer0(MethodSpec.Builder methodBuilder, ParameterizedTypeName tupleType, List<TypeName> outputParameterTypes) throws ClassNotFoundException {
    List<TypeName> typeArguments = tupleType.typeArguments;
    CodeBlock.Builder codeBuilder = CodeBlock.builder();
    String resultStringSimple = "\n($T) results.get($L)";
    if (useNativeJavaTypes) {
        resultStringSimple += ".getValue()";
    }
    String resultStringNativeList = "\nconvertToNative(($T) results.get($L).getValue())";
    int size = typeArguments.size();
    ClassName classList = ClassName.get(List.class);
    for (int i = 0; i < size; i++) {
        TypeName param = outputParameterTypes.get(i);
        TypeName convertTo = typeArguments.get(i);
        String resultString = resultStringSimple;
        // elements of arrays to native java types too
        if (useNativeJavaTypes && param instanceof ParameterizedTypeName) {
            ParameterizedTypeName oldContainer = (ParameterizedTypeName) param;
            ParameterizedTypeName newContainer = (ParameterizedTypeName) convertTo;
            if (newContainer.rawType.compareTo(classList) == 0 && newContainer.typeArguments.size() == 1) {
                convertTo = ParameterizedTypeName.get(classList, oldContainer.typeArguments.get(0));
                resultString = resultStringNativeList;
            }
        }
        codeBuilder.add(resultString, convertTo, i);
        codeBuilder.add(i < size - 1 ? ", " : "\n");
    }
    methodBuilder.addStatement("return new $T(\n$L)", tupleType, codeBuilder.build());
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) CodeBlock(com.squareup.javapoet.CodeBlock) ClassName(com.squareup.javapoet.ClassName) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 99 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project web3sdk by FISCO-BCOS.

the class SolidityFunctionWrapper method buildFunctionWithInputDecoder.

private MethodSpec buildFunctionWithInputDecoder(AbiDefinition functionDefinition, boolean isOverLoad) throws ClassNotFoundException {
    String functionName = getInputOutputFunctionName(functionDefinition, isOverLoad);
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("get" + Strings.capitaliseFirstLetter(functionName) + "Input").addModifiers(Modifier.PUBLIC).addParameter(TransactionReceipt.class, "transactionReceipt");
    List<TypeName> returnTypes = buildReturnTypes(buildTypeNames(functionDefinition.getInputs()));
    ParameterizedTypeName parameterizedTupleType = ParameterizedTypeName.get(ClassName.get("org.fisco.bcos.web3j.tuples.generated", "Tuple" + returnTypes.size()), returnTypes.toArray(new TypeName[returnTypes.size()]));
    methodBuilder.returns(parameterizedTupleType);
    methodBuilder.addStatement("String data = transactionReceipt.getInput().substring(10)");
    buildVariableLengthReturnFunctionConstructor(methodBuilder, functionDefinition.getName(), "", buildTypeNames(functionDefinition.getInputs()));
    methodBuilder.addStatement("$T<Type> results = $T.decode(data, function.getOutputParameters());", List.class, FunctionReturnDecoder.class);
    buildTupleResultContainer0(methodBuilder, parameterizedTupleType, buildTypeNames(functionDefinition.getInputs()));
    return methodBuilder.build();
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 100 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project web3sdk by FISCO-BCOS.

the class SolidityFunctionWrapper method createMappedParameterTypes.

private String createMappedParameterTypes(AbiDefinition.NamedType namedType) {
    String name = namedType.getName();
    String type = namedType.getType();
    AbiDefinition.NamedType.Type innerType = new AbiDefinition.NamedType.Type(type);
    ParameterSpec parameterSpec = ParameterSpec.builder(buildTypeName(type), name).build();
    if (parameterSpec.type instanceof ParameterizedTypeName) {
        List<TypeName> typeNames = ((ParameterizedTypeName) parameterSpec.type).typeArguments;
        if (typeNames.size() != 1) {
            throw new UnsupportedOperationException("Only a single parameterized type is supported");
        } else {
            String parameterSpecType = parameterSpec.type.toString();
            TypeName typeName = typeNames.get(0);
            String typeMapInput = typeName + ".class";
            if (typeName instanceof ParameterizedTypeName) {
                List<TypeName> typeArguments = ((ParameterizedTypeName) typeName).typeArguments;
                if (typeArguments.size() != 1) {
                    throw new UnsupportedOperationException("Only a single parameterized type is supported");
                }
                TypeName innerTypeName = typeArguments.get(0);
                parameterSpecType = ((ParameterizedTypeName) parameterSpec.type).rawType.toString();
                typeMapInput = ((ParameterizedTypeName) typeName).rawType + ".class, " + innerTypeName + ".class";
            }
            if (innerType.dynamicArray()) {
                // dynamic array
                return parameterSpec.name + ".isEmpty()?org.fisco.bcos.web3j.abi.datatypes.DynamicArray.empty" + "(\"" + type + "\"):" + "new " + parameterSpecType + "(\n" + "        org.fisco.bcos.web3j.abi.Utils.typeMap(" + parameterSpec.name + ", " + typeMapInput + "))";
            } else {
                // static array
                return "new " + parameterSpecType + "(\n" + "        org.fisco.bcos.web3j.abi.Utils.typeMap(" + parameterSpec.name + ", " + typeMapInput + "))";
            }
        }
    } else {
        return "new " + parameterSpec.type + "(" + parameterSpec.name + ")";
    }
}
Also used : Type(org.fisco.bcos.web3j.abi.datatypes.Type) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) EncryptType(org.fisco.bcos.web3j.crypto.EncryptType) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) ParameterSpec(com.squareup.javapoet.ParameterSpec) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Aggregations

ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)135 TypeName (com.squareup.javapoet.TypeName)90 ClassName (com.squareup.javapoet.ClassName)53 MethodSpec (com.squareup.javapoet.MethodSpec)47 TypeSpec (com.squareup.javapoet.TypeSpec)29 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)29 TypeElement (javax.lang.model.element.TypeElement)23 ArrayList (java.util.ArrayList)19 TypeMirror (javax.lang.model.type.TypeMirror)19 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)15 HashSet (java.util.HashSet)14 Map (java.util.Map)13 List (java.util.List)12 Set (java.util.Set)12 FieldSpec (com.squareup.javapoet.FieldSpec)11 ParameterSpec (com.squareup.javapoet.ParameterSpec)11 TypeVariableName (com.squareup.javapoet.TypeVariableName)10 Element (javax.lang.model.element.Element)10 PackageElement (javax.lang.model.element.PackageElement)10 Builder (com.squareup.javapoet.TypeSpec.Builder)9