Search in sources :

Example 46 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project celesta by CourseOrchestra.

the class CursorGenerator method buildSetAutoIncrement.

private MethodSpec buildSetAutoIncrement(Map<String, ? extends ColumnMeta<?>> columns) {
    MethodSpec.Builder builder = MethodSpec.methodBuilder("_setAutoIncrement").addModifiers(Modifier.PROTECTED).addAnnotation(Override.class);
    ParameterSpec param = ParameterSpec.builder(int.class, "val").build();
    builder.addParameter(param);
    columns.entrySet().stream().filter(e -> e.getValue() instanceof IntegerColumn).filter(e -> ((IntegerColumn) e.getValue()).getSequence() != null).findAny().ifPresent(e -> builder.addStatement("this.$N = $N", camelize(e.getKey()), param.name));
    return builder.build();
}
Also used : BasicCursor(ru.curs.celesta.dbutils.BasicCursor) Arrays(java.util.Arrays) Modifier(javax.lang.model.element.Modifier) CelestaException(ru.curs.celesta.CelestaException) ZonedDateTime(java.time.ZonedDateTime) ClassName(com.squareup.javapoet.ClassName) SequenceElement(ru.curs.celesta.score.SequenceElement) Grain(ru.curs.celesta.score.Grain) Parameter(ru.curs.celesta.score.Parameter) ColumnMeta(ru.curs.celesta.score.ColumnMeta) Generated(javax.annotation.Generated) StringColumn(ru.curs.celesta.score.StringColumn) ResultSet(java.sql.ResultSet) Map(java.util.Map) ICelesta(ru.curs.celesta.ICelesta) ZoneOffset(java.time.ZoneOffset) DataGrainElement(ru.curs.celesta.score.DataGrainElement) CelestaGenerated(ru.curs.celesta.dbutils.CelestaGenerated) ParameterSpec(com.squareup.javapoet.ParameterSpec) CallContext(ru.curs.celesta.CallContext) TimeZone(java.util.TimeZone) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) Set(java.util.Set) Column(ru.curs.celesta.score.Column) Collectors(java.util.stream.Collectors) JavaFile(com.squareup.javapoet.JavaFile) Objects(java.util.Objects) List(java.util.List) ViewCursor(ru.curs.celesta.dbutils.ViewCursor) TriggerType(ru.curs.celesta.event.TriggerType) BasicTable(ru.curs.celesta.score.BasicTable) GrainElement(ru.curs.celesta.score.GrainElement) BasicDataAccessor(ru.curs.celesta.dbutils.BasicDataAccessor) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) IntegerColumn(ru.curs.celesta.score.IntegerColumn) MaterializedViewCursor(ru.curs.celesta.dbutils.MaterializedViewCursor) ZonedDateTimeColumn(ru.curs.celesta.score.ZonedDateTimeColumn) FieldSpec(com.squareup.javapoet.FieldSpec) TableElement(ru.curs.celesta.score.TableElement) LocalDateTime(java.time.LocalDateTime) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) Cursor(ru.curs.celesta.dbutils.Cursor) HashMap(java.util.HashMap) View(ru.curs.celesta.score.View) ReadOnlyTable(ru.curs.celesta.score.ReadOnlyTable) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) Sequence(ru.curs.celesta.dbutils.Sequence) BinaryColumn(ru.curs.celesta.score.BinaryColumn) ParameterizedView(ru.curs.celesta.score.ParameterizedView) CodeBlock(com.squareup.javapoet.CodeBlock) LinkedHashSet(java.util.LinkedHashSet) Iterator(java.util.Iterator) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) IOException(java.io.IOException) CursorIterator(ru.curs.celesta.dbutils.CursorIterator) TypeSpec(com.squareup.javapoet.TypeSpec) ParameterizedViewCursor(ru.curs.celesta.dbutils.ParameterizedViewCursor) MaterializedView(ru.curs.celesta.score.MaterializedView) Table(ru.curs.celesta.score.Table) File(java.io.File) Consumer(java.util.function.Consumer) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) VersionedElement(ru.curs.celesta.score.VersionedElement) DateTimeFormatter(java.time.format.DateTimeFormatter) NamedElement(ru.curs.celesta.score.NamedElement) FileResource(ru.curs.celesta.score.io.FileResource) ReadOnlyTableCursor(ru.curs.celesta.dbutils.ReadOnlyTableCursor) Collections(java.util.Collections) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) IntegerColumn(ru.curs.celesta.score.IntegerColumn) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec)

Example 47 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project web3j by web3j.

the class SolidityFunctionWrapper method buildParameterTypes.

private List<ParameterSpec> buildParameterTypes(List<AbiDefinition.NamedType> namedTypes, boolean primitives) throws ClassNotFoundException {
    List<ParameterSpec> result = new ArrayList<>(namedTypes.size());
    for (int i = 0; i < namedTypes.size(); i++) {
        AbiDefinition.NamedType namedType = namedTypes.get(i);
        String name = createValidParamName(namedType.getName(), i);
        String type = namedTypes.get(i).getType();
        if (type.equals("tuple")) {
            result.add(ParameterSpec.builder(structClassNameMap.get(namedType.structIdentifier()), name).build());
        } else if (type.startsWith("tuple") && type.contains("[")) {
            result.add(ParameterSpec.builder(buildStructArrayTypeName(namedType, primitives), name).build());
        } else {
            result.add(ParameterSpec.builder(buildTypeName(type, primitives), name).build());
        }
    }
    return result;
}
Also used : NamedType(org.web3j.protocol.core.methods.response.AbiDefinition.NamedType) ParameterSpec(com.squareup.javapoet.ParameterSpec) AbiDefinition(org.web3j.protocol.core.methods.response.AbiDefinition) ArrayList(java.util.ArrayList) Utf8String(org.web3j.abi.datatypes.Utf8String)

Example 48 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project web3j by web3j.

the class MethodSpecGeneratorTest method testGenerate.

@Test
public void testGenerate() {
    List<ParameterSpec> parameterSpec = Collections.singletonList(ParameterSpec.builder(Web3j.class, "web3j", Modifier.FINAL).build());
    String javaPoetStringFormat1 = "$T $L = $S";
    Object[] replacementValues1 = new Object[] { String.class, "hello ", "Hello how are you" };
    String javaPoetStringFormat2 = "$T $L = $T.build()";
    Object[] replacementValues2 = new Object[] { Web3j.class, "web3j", Web3j.class };
    Map<String, Object[]> statementBody = new LinkedHashMap<>();
    statementBody.put(javaPoetStringFormat1, replacementValues1);
    statementBody.put(javaPoetStringFormat2, replacementValues2);
    MethodSpecGenerator methodSpecGenerator = new MethodSpecGenerator("unitTest", Test.class, Modifier.PUBLIC, parameterSpec, statementBody);
    MethodSpec generatedMethodSpec = methodSpecGenerator.generate();
    assertEquals("@org.junit.jupiter.api.Test\n" + "public void unitTest(final org.web3j.protocol.Web3j web3j) throws java.lang.Exception {\n" + "  java.lang.String hello  = \"Hello how are you\";\n" + "  org.web3j.protocol.Web3j web3j = org.web3j.protocol.Web3j.build();\n" + "}\n", generatedMethodSpec.toString());
}
Also used : Web3j(org.web3j.protocol.Web3j) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 49 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project dialogue by palantir.

the class ServiceImplementationGenerator method clientImpl.

private MethodSpec clientImpl(EndpointDefinition def) {
    List<ParameterSpec> params = def.arguments().stream().map(arg -> ParameterSpec.builder(ArgumentTypes.caseOf(arg.argType()).primitive((javaTypeName, _parameterSerializerMethodName, _isList) -> javaTypeName).rawRequestBody(typeName -> typeName).optional((optionalJavaType, _unused) -> optionalJavaType).mapType(typeName -> typeName).customType(typeName -> typeName), arg.argName().get()).build()).collect(Collectors.toList());
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(def.endpointName().get()).addModifiers(Modifier.PUBLIC).addParameters(params).addAnnotation(Override.class);
    methodBuilder.addCode("$T $L = $T.builder();", Request.Builder.class, REQUEST, Request.class);
    def.arguments().forEach(arg -> methodBuilder.addCode(generateParam(arg)));
    methodBuilder.returns(def.returns().returnType());
    boolean isAsync = def.returns().asyncInnerType().isPresent();
    String executeCode = isAsync ? "$L.clients().call($L, $L.build(), $L);" : "$L.clients().callBlocking($L, $L.build(), $L);";
    CodeBlock execute = CodeBlock.of(executeCode, serviceDefinition.conjureRuntimeArgName(), def.channelFieldName(), REQUEST, def.returns().deserializerFieldName());
    methodBuilder.addCode(!def.returns().isVoid() || isAsync ? "return $L" : "$L", execute);
    return methodBuilder.build();
}
Also used : OptionalType(com.palantir.dialogue.annotations.processor.data.ArgumentType.OptionalType) Modifier(javax.lang.model.element.Modifier) FieldSpec(com.squareup.javapoet.FieldSpec) ClassName(com.squareup.javapoet.ClassName) Serializer(com.palantir.dialogue.Serializer) ParameterEncoderType(com.palantir.dialogue.annotations.processor.data.ParameterEncoderType) Cases(com.palantir.dialogue.annotations.processor.data.ParameterType.Cases) ParameterSerializer(com.palantir.dialogue.annotations.ParameterSerializer) ErrorHandlingVoidDeserializer(com.palantir.dialogue.annotations.ErrorHandlingVoidDeserializer) Request(com.palantir.dialogue.Request) CodeBlock(com.squareup.javapoet.CodeBlock) DefaultParameterSerializer(com.palantir.dialogue.annotations.DefaultParameterSerializer) ParameterSpec(com.squareup.javapoet.ParameterSpec) ParameterTypes(com.palantir.dialogue.annotations.processor.data.ParameterTypes) Deserializer(com.palantir.dialogue.Deserializer) MethodSpec(com.squareup.javapoet.MethodSpec) EncoderType(com.palantir.dialogue.annotations.processor.data.ParameterEncoderType.EncoderType) ServiceDefinition(com.palantir.dialogue.annotations.processor.data.ServiceDefinition) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ReturnType(com.palantir.dialogue.annotations.processor.data.ReturnType) ArgumentTypes(com.palantir.dialogue.annotations.processor.data.ArgumentTypes) TypeSpec(com.squareup.javapoet.TypeSpec) Collectors(java.util.stream.Collectors) ErrorHandlingDeserializerFactory(com.palantir.dialogue.annotations.ErrorHandlingDeserializerFactory) EndpointChannel(com.palantir.dialogue.EndpointChannel) List(java.util.List) ArgumentType(com.palantir.dialogue.annotations.processor.data.ArgumentType) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) TypeMarker(com.palantir.dialogue.TypeMarker) ArgumentDefinition(com.palantir.dialogue.annotations.processor.data.ArgumentDefinition) EndpointDefinition(com.palantir.dialogue.annotations.processor.data.EndpointDefinition) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) Request(com.palantir.dialogue.Request) CodeBlock(com.squareup.javapoet.CodeBlock)

Example 50 with ParameterSpec

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

the class SolidityFunctionWrapper method addParameter.

private MethodSpec.Builder addParameter(MethodSpec.Builder methodBuilder, String type, String name) {
    ParameterSpec parameterSpec = buildParameterType(type, name);
    TypeName typeName = getWrapperType(parameterSpec.type);
    ParameterSpec inputParameter = ParameterSpec.builder(typeName, parameterSpec.name).build();
    methodBuilder.addParameter(inputParameter);
    return methodBuilder;
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ParameterSpec(com.squareup.javapoet.ParameterSpec)

Aggregations

ParameterSpec (com.squareup.javapoet.ParameterSpec)97 MethodSpec (com.squareup.javapoet.MethodSpec)59 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)40 ArrayList (java.util.ArrayList)34 TypeName (com.squareup.javapoet.TypeName)30 ClassName (com.squareup.javapoet.ClassName)27 TypeSpec (com.squareup.javapoet.TypeSpec)26 Map (java.util.Map)26 CodeBlock (com.squareup.javapoet.CodeBlock)23 FieldSpec (com.squareup.javapoet.FieldSpec)22 List (java.util.List)20 HashMap (java.util.HashMap)18 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)17 JavaFile (com.squareup.javapoet.JavaFile)13 Modifier (javax.lang.model.element.Modifier)13 TypeElement (javax.lang.model.element.TypeElement)13 Collectors (java.util.stream.Collectors)12 IOException (java.io.IOException)11 Element (javax.lang.model.element.Element)11 TypeMirror (javax.lang.model.type.TypeMirror)11