Search in sources :

Example 16 with TypeSpec

use of com.squareup.javapoet.TypeSpec in project RxBus by ViTess.

the class ProxyBuilder method createFunc1.

/**
     * just add the filter which about param
     *
     * @return
     */
private TypeSpec createFunc1(List<TypeMirror> paramTypes) {
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("call").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(Object.class, "o").returns(Boolean.class);
    methodBuilder.addStatement("return true");
    TypeSpec func1 = TypeSpec.anonymousClassBuilder("").addSuperinterface(ParameterizedTypeName.get(FILTER_FUNC, TypeName.get(Object.class), TypeName.get(Boolean.class))).addMethod(methodBuilder.build()).build();
    return func1;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 17 with TypeSpec

use of com.squareup.javapoet.TypeSpec in project RxBus by ViTess.

the class ProxyBuilder method createProxyAction.

private TypeSpec createProxyAction(MethodBinder binder) {
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("toDo").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(mTargetClassName, "target");
    //        if (binder.getParamTypes().size() > 0) {
    //            TypeName typeName;
    //            TypeMirror paramType = binder.getParamTypes().get(0);
    //            if (paramType.getKind().isPrimitive()) {
    //                typeName = TypeName.get(paramType);
    //                if (!typeName.isBoxedPrimitive())
    //                    typeName = typeName.box();
    //            } else if (paramType.getKind().equals(TypeKind.ARRAY))
    //                typeName = TypeName.get(paramType);
    //            else
    //                typeName = ClassName.get((TypeElement) Util.TypeUtils.asElement(paramType));
    //            methodBuilder.addStatement("target." + binder.getMethodName() + "($T.class.cast(o))", typeName);
    //        } else
    //            methodBuilder.addStatement("target." + binder.getMethodName() + "()");
    TypeMirror paramType = binder.getParamType();
    TypeName typeName;
    if (paramType == null) {
        typeName = DEFAULT_OBJECT;
        methodBuilder.addStatement("target." + binder.getMethodName() + "()");
    } else {
        if (paramType.getKind().isPrimitive()) {
            typeName = TypeName.get(paramType);
            if (!typeName.isBoxedPrimitive())
                typeName = typeName.box();
        } else
            typeName = ClassName.get(paramType);
        methodBuilder.addStatement("target." + binder.getMethodName() + "(o)");
    }
    methodBuilder.addParameter(typeName, "o");
    TypeSpec proxyAction = TypeSpec.anonymousClassBuilder("").addSuperinterface(ParameterizedTypeName.get(PROXY_ACTION, mTargetClassName, typeName)).addMethod(methodBuilder.build()).build();
    return proxyAction;
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) TypeMirror(javax.lang.model.type.TypeMirror) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 18 with TypeSpec

use of com.squareup.javapoet.TypeSpec in project bazel by bazelbuild.

the class JavaCodeGeneratorHelper method targetWithNextHelper.

/**
   * Writes a class file {@code Deps(index).java} to the directory
   * {@code projectPath/com/example/deps(index)}
   *
   * @param callNext if we should call the method from {@code Deps(index+1).java}
   */
static void targetWithNextHelper(int index, boolean callNext, Path projectPath) throws IOException {
    ClassName nextClass = ClassName.get("com.example.deps" + (index + 1), "Deps" + (index + 1));
    MethodSpec callNextMethod = MethodSpec.methodBuilder("CallNext").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(void.class).addStatement("$T.PrintSth()", nextClass).build();
    TypeSpec.Builder klassBuilder = TypeSpec.classBuilder("Deps" + index).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(randomMethod);
    if (callNext) {
        klassBuilder.addMethod(callNextMethod);
    }
    TypeSpec klass = klassBuilder.build();
    writeClassToDir(klass, "com.example.deps" + index, projectPath);
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 19 with TypeSpec

use of com.squareup.javapoet.TypeSpec in project bazel by bazelbuild.

the class JavaCodeGeneratorHelper method writeRandomClassToDir.

static void writeRandomClassToDir(boolean addExtraMethod, String className, String packageName, Path projectPath) throws IOException {
    TypeSpec klass = genRandomClass(addExtraMethod, className);
    writeClassToDir(klass, packageName, projectPath);
}
Also used : TypeSpec(com.squareup.javapoet.TypeSpec)

Example 20 with TypeSpec

use of com.squareup.javapoet.TypeSpec in project bazel by bazelbuild.

the class JavaCodeGeneratorHelper method targetWithNextExtraHelper.

/**
   * Writes a class file {@code Deps(index).java} with extra method {@code printSthElse()}
   * to the directory {@code projectPath/com/example/deps(index)}
   *
   * @param callNext if we should call the method from {@code Deps(index+1).java}
   */
static void targetWithNextExtraHelper(int index, boolean callNext, Path projectPath) throws IOException {
    ClassName nextClass = ClassName.get("com.example.deps" + (index + 1), "Deps" + (index + 1));
    MethodSpec callNextMethod = MethodSpec.methodBuilder("CallNext").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(void.class).addStatement("$T.PrintSth()", nextClass).build();
    TypeSpec.Builder klassBuilder = TypeSpec.classBuilder("Deps" + index).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(randomMethod).addMethod(somethingElseMethod);
    if (callNext) {
        klassBuilder.addMethod(callNextMethod);
    }
    TypeSpec klass = klassBuilder.build();
    writeClassToDir(klass, "com.example.deps" + index, projectPath);
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

TypeSpec (com.squareup.javapoet.TypeSpec)44 ClassName (com.squareup.javapoet.ClassName)26 MethodSpec (com.squareup.javapoet.MethodSpec)20 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)10 TypeName (com.squareup.javapoet.TypeName)8 NotNull (org.jetbrains.annotations.NotNull)8 FieldSpec (com.squareup.javapoet.FieldSpec)7 ArrayList (java.util.ArrayList)6 Type (com.squareup.wire.schema.Type)5 TypeElement (javax.lang.model.element.TypeElement)5 ByteString (okio.ByteString)5 WireField (com.squareup.wire.WireField)4 JavaGenerator (com.squareup.wire.java.JavaGenerator)4 Field (com.squareup.wire.schema.Field)4 MessageType (com.squareup.wire.schema.MessageType)4 ProtoType (com.squareup.wire.schema.ProtoType)4 Schema (com.squareup.wire.schema.Schema)4 CodeBlock (com.squareup.javapoet.CodeBlock)3 EnclosingType (com.squareup.wire.schema.EnclosingType)3 EnumType (com.squareup.wire.schema.EnumType)3