Search in sources :

Example 96 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project atlasdb by palantir.

the class TableFactoryRenderer method getV2TableMethod.

private MethodSpec getV2TableMethod(String name, TableDefinition tableDefinition) {
    String tableName = getV2TableName(name);
    TypeName tableType = ClassName.get(packageName, tableName);
    MethodSpec.Builder tableGetterMethodBuilder = MethodSpec.methodBuilder("get" + tableName).addModifiers(Modifier.PUBLIC).addParameter(Transaction.class, "t").returns(tableType).addStatement("return $T.of(t, namespace)", tableType);
    return tableGetterMethodBuilder.build();
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) MethodSpec(com.squareup.javapoet.MethodSpec)

Example 97 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project atlasdb by palantir.

the class TableFactoryRenderer method getConstructors.

private List<MethodSpec> getConstructors() {
    List<MethodSpec> results = new ArrayList<>();
    TypeName functionOfTransactionAndTriggersType = ParameterizedTypeName.get(ClassName.get(Function.class), WildcardTypeName.supertypeOf(Transaction.class), sharedTriggersType);
    TypeName sharedTriggersListType = ParameterizedTypeName.get(ClassName.get(List.class), functionOfTransactionAndTriggersType);
    results.add(factoryBaseBuilder().addParameter(ParameterizedTypeName.get(ClassName.get(List.class), functionOfTransactionAndTriggersType), "sharedTriggers").addParameter(Namespace.class, "namespace").addStatement("return new $T($L, $L)", tableFactoryType, "sharedTriggers", "namespace").build());
    results.add(factoryBaseBuilder().addParameter(sharedTriggersListType, "sharedTriggers").addStatement("return new $T($L, $L)", tableFactoryType, "sharedTriggers", "defaultNamespace").build());
    results.add(factoryBaseBuilder().addParameter(Namespace.class, "namespace").addStatement("return of($T.<$T>of(), $L)", ImmutableList.class, functionOfTransactionAndTriggersType, "namespace").build());
    results.add(factoryBaseBuilder().addStatement("return of($T.<$T>of(), $L)", ImmutableList.class, functionOfTransactionAndTriggersType, "defaultNamespace").build());
    results.add(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).addParameter(sharedTriggersListType, "sharedTriggers").addParameter(Namespace.class, "namespace").addStatement("this.$L = $L", "sharedTriggers", "sharedTriggers").addStatement("this.$L = $L", "namespace", "namespace").build());
    return results;
}
Also used : Function(com.google.common.base.Function) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) Transaction(com.palantir.atlasdb.transaction.api.Transaction) MethodSpec(com.squareup.javapoet.MethodSpec) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Namespace(com.palantir.atlasdb.keyvalue.api.Namespace)

Example 98 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project AndroidLife by CaMnter.

the class SaveTestProcessor method process.

/**
 * {@inheritDoc}
 */
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    MethodSpec main = MethodSpec.methodBuilder("main").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(void.class).addParameter(String[].class, "args").addStatement("$T.out.println($S)", System.class, "Save you from anything").build();
    TypeSpec saveTest = TypeSpec.classBuilder("SaveTest").addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(main).build();
    JavaFile javaFile = JavaFile.builder("com.camnter.annotation.processor.output", saveTest).build();
    try {
        javaFile.writeTo(this.processingEnv.getFiler());
    } catch (IOException e) {
    // e.printStackTrace();
    }
    return false;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 99 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project AndroidLife by CaMnter.

the class RouterManagerClass method getSmartRouterMethod.

/**
 * public static SmartRouter getSmartRouter(@NonNull final String host) {
 * -   return new SmartRouter(host);
 * }
 *
 * @return List<MethodSpec>
 */
public List<MethodSpec> getSmartRouterMethod() {
    final List<MethodSpec> getSmartRouterMethods = new ArrayList<>();
    for (RouterClass routerClass : this.routerClassHashMap.values()) {
        final TypeName routerTypeName = ClassName.get(routerClass.getPackageName(), routerClass.getSimpleName() + Const.SMART_ROUTER_SUFFIX);
        final MethodSpec.Builder getSmartRouterMethodBuilder = MethodSpec.methodBuilder("get" + routerClass.getSimpleName() + "SmartRouter").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(routerTypeName).addParameter(createNonNullParameter(ClassName.get(String.class), "host", Modifier.FINAL)).addCode("    return new $T(host);\n", routerTypeName);
        getSmartRouterMethods.add(getSmartRouterMethodBuilder.build());
    }
    return getSmartRouterMethods;
}
Also used : TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ArrayList(java.util.ArrayList)

Example 100 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project Rocket by mozilla-tw.

the class RequestBuilderGenerator method generateGeneratedRequestOptionEquivalent.

/**
 * Generates a particular method with  an equivalent name and arguments to the given method
 * from the generated {@link com.bumptech.glide.request.BaseRequestBuilder} subclass.
 */
private MethodSpec generateGeneratedRequestOptionEquivalent(MethodSpec requestOptionMethod) {
    CodeBlock callRequestOptionsMethod = CodeBlock.builder().add(".$N(", requestOptionMethod.name).add(FluentIterable.from(requestOptionMethod.parameters).transform(new Function<ParameterSpec, String>() {

        @Override
        public String apply(ParameterSpec input) {
            return input.name;
        }
    }).join(Joiner.on(", "))).add(");\n").build();
    MethodSpec.Builder result = MethodSpec.methodBuilder(requestOptionMethod.name).addJavadoc(processorUtil.generateSeeMethodJavadoc(requestOptionsClassName, requestOptionMethod)).addModifiers(Modifier.PUBLIC).varargs(requestOptionMethod.varargs).addAnnotations(FluentIterable.from(requestOptionMethod.annotations).filter(new Predicate<AnnotationSpec>() {

        @Override
        public boolean apply(AnnotationSpec input) {
            return !input.type.equals(TypeName.get(Override.class)) && // non-final to allow for mocking.
            !input.type.equals(TypeName.get(SafeVarargs.class)) && // autoClone() in RequestBuilder.
            !input.type.equals(CHECK_RESULT_CLASS_NAME);
        }
    }).toList()).addTypeVariables(requestOptionMethod.typeVariables).addParameters(requestOptionMethod.parameters).returns(generatedRequestBuilderOfTranscodeType).beginControlFlow("if (getMutableOptions() instanceof $T)", requestOptionsClassName).addCode("this.requestOptions = (($T) getMutableOptions())", requestOptionsClassName).addCode(callRequestOptionsMethod).nextControlFlow("else").addCode(CodeBlock.of("this.requestOptions = new $T().apply(this.requestOptions)", requestOptionsClassName)).addCode(callRequestOptionsMethod).endControlFlow().addStatement("return this");
    if (requestOptionMethod.annotations.contains(AnnotationSpec.builder(SafeVarargs.class).build())) {
        result.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").addMember("value", "$S", "varargs").build());
    }
    return result.build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) CodeBlock(com.squareup.javapoet.CodeBlock) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) Predicate(com.google.common.base.Predicate)

Aggregations

MethodSpec (com.squareup.javapoet.MethodSpec)155 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)43 TypeName (com.squareup.javapoet.TypeName)42 ArrayList (java.util.ArrayList)38 ClassName (com.squareup.javapoet.ClassName)34 TypeSpec (com.squareup.javapoet.TypeSpec)31 ParameterSpec (com.squareup.javapoet.ParameterSpec)24 CodeBlock (com.squareup.javapoet.CodeBlock)23 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)20 Nonnull (javax.annotation.Nonnull)17 TypeMirror (javax.lang.model.type.TypeMirror)15 WireField (com.squareup.wire.WireField)14 Field (com.squareup.wire.schema.Field)14 TypeElement (javax.lang.model.element.TypeElement)14 ByteString (okio.ByteString)13 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)12 List (java.util.List)11 VariableElement (javax.lang.model.element.VariableElement)11 Utf8String (org.fisco.bcos.web3j.abi.datatypes.Utf8String)11 FieldSpec (com.squareup.javapoet.FieldSpec)10