Search in sources :

Example 16 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project EasyRouter by Xiasm.

the class ExtraProcessor method generateAutoWired.

private void generateAutoWired() throws IOException {
    TypeMirror typeActivity = elementUtils.getTypeElement(Constant.ACTIVITY).asType();
    TypeElement iExtra = elementUtils.getTypeElement(Constant.IEXTRA);
    if (!Utils.isEmpty(parentAndChild)) {
        // 参数 Object target
        ParameterSpec objectParamSpec = ParameterSpec.builder(TypeName.OBJECT, "target").build();
        for (Map.Entry<TypeElement, List<Element>> entry : parentAndChild.entrySet()) {
            TypeElement rawClassElement = entry.getKey();
            if (!typeUtils.isSubtype(rawClassElement.asType(), typeActivity)) {
                throw new RuntimeException("just support activity filed: " + rawClassElement);
            }
            // 封装的函数生成类
            LoadExtraBuilder loadExtra = new LoadExtraBuilder(objectParamSpec);
            loadExtra.setElementUtils(elementUtils);
            loadExtra.setTypeUtils(typeUtils);
            ClassName className = ClassName.get(rawClassElement);
            loadExtra.injectTarget(className);
            // 遍历属性
            for (int i = 0; i < entry.getValue().size(); i++) {
                Element element = entry.getValue().get(i);
                loadExtra.buildStatement(element);
            }
            // 生成java类名
            String extraClassName = rawClassElement.getSimpleName() + Constant.NAME_OF_EXTRA;
            // 生成 XX$$Autowired
            JavaFile.builder(className.packageName(), TypeSpec.classBuilder(extraClassName).addSuperinterface(ClassName.get(iExtra)).addModifiers(PUBLIC).addMethod(loadExtra.build()).build()).build().writeTo(filerUtils);
            log.i("Generated Extra: " + className.packageName() + "." + extraClassName);
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ParameterSpec(com.squareup.javapoet.ParameterSpec) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ClassName(com.squareup.javapoet.ClassName) ArrayList(java.util.ArrayList) List(java.util.List) LoadExtraBuilder(com.xsm.easy.compiler.utils.LoadExtraBuilder) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project EasyRouter by Xiasm.

the class RouterProcessor method generatedRoot.

/**
 * 生成Root类  作用:记录<分组,对应的Group类>
 * @param iRouteRoot
 * @param iRouteGroup
 */
private void generatedRoot(TypeElement iRouteRoot, TypeElement iRouteGroup) {
    // 创建参数类型 Map<String,Class<? extends IRouteGroup>> routes>
    // Wildcard 通配符
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(ClassName.get(iRouteGroup))));
    // 参数 Map<String,Class<? extends IRouteGroup>> routes> routes
    ParameterSpec parameter = ParameterSpec.builder(parameterizedTypeName, "routes").build();
    // 函数 public void loadInfo(Map<String,Class<? extends IRouteGroup>> routes> routes)
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(Constant.METHOD_LOAD_INTO).addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(parameter);
    // 函数体
    for (Map.Entry<String, String> entry : rootMap.entrySet()) {
        methodBuilder.addStatement("routes.put($S, $T.class)", entry.getKey(), ClassName.get(Constant.PACKAGE_OF_GENERATE_FILE, entry.getValue()));
    }
    // 生成$Root$类
    String className = Constant.NAME_OF_ROOT + moduleName;
    TypeSpec typeSpec = TypeSpec.classBuilder(className).addSuperinterface(ClassName.get(iRouteRoot)).addModifiers(Modifier.PUBLIC).addMethod(methodBuilder.build()).build();
    try {
        JavaFile.builder(Constant.PACKAGE_OF_GENERATE_FILE, typeSpec).build().writeTo(filerUtils);
        log.i("Generated RouteRoot:" + Constant.PACKAGE_OF_GENERATE_FILE + "." + className);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 18 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project android_packages_apps_Dialer by LineageOS.

the class ComponentGeneratingStep method cloneParameters.

private List<ParameterSpec> cloneParameters(List<? extends VariableElement> variableElementsList) {
    List<ParameterSpec> list = new ArrayList<>();
    for (VariableElement variableElement : variableElementsList) {
        ParameterSpec.Builder builder = ParameterSpec.builder(TypeName.get(variableElement.asType()), variableElement.getSimpleName().toString()).addModifiers(variableElement.getModifiers());
        list.add(builder.build());
    }
    return list;
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement)

Example 19 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project kripton by xcesco.

the class SelectPaginatedResultHelper method buildSpecializedPagedResultClass.

/**
 * Build paginated result class handler.
 *
 * @param classBuilder
 *            the class builder
 * @param method
 *            the method
 * @return name of generated class
 */
public static String buildSpecializedPagedResultClass(TypeSpec.Builder classBuilder, SQLiteModelMethod method) {
    // TypeName entityTypeName =
    // TypeUtility.typeName(method.getParent().getEntityClassName());
    TypeName entityTypeName = TypeUtility.typeName(method.getEntity().getName());
    String pagedResultName = "PaginatedResult" + (pagedResultCounter++);
    TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(pagedResultName).addModifiers(Modifier.PUBLIC).superclass(TypeUtility.parameterizedTypeName(TypeUtility.className(PagedResultImpl.class), entityTypeName));
    // add fields and define constructor
    MethodSpec.Builder setupBuilder = MethodSpec.constructorBuilder();
    MethodSpec.Builder executeBuilder = MethodSpec.methodBuilder("execute").addModifiers(Modifier.PUBLIC).returns(TypeUtility.parameterizedTypeName(TypeUtility.className(List.class), entityTypeName));
    executeBuilder.addComment("Executor builder - BEGIN");
    // method for execution with pageRequest
    // @formatter:off
    ClassName dataSourceClassName = BindDataSourceBuilder.generateDataSourceName(method.getParent().getParent());
    MethodSpec.Builder executeWithPageRequestBuilder = MethodSpec.methodBuilder("execute").addModifiers(Modifier.PUBLIC).addParameter(PageRequest.class, "pageRequest").returns(TypeUtility.parameterizedTypeName(TypeUtility.className(List.class), entityTypeName));
    executeWithPageRequestBuilder.addComment("Executor with pageRequet - BEGIN");
    executeWithPageRequestBuilder.addCode("return $T.getInstance().executeBatch(daoFactory -> daoFactory.get$L().$L" + SelectPaginatedResultHelper.WITH_PAGE_REQUEST_PREFIX + "(", dataSourceClassName, method.getParentName(), method.getName());
    ClassName daoFactoryClassName = BindDaoFactoryBuilder.generateDaoFactoryClassName(method.getParent().getParent());
    MethodSpec.Builder executeWithDaoFactortBuilder = MethodSpec.methodBuilder("execute").addParameter(daoFactoryClassName, "daoFactory").addModifiers(Modifier.PUBLIC).returns(TypeUtility.parameterizedTypeName(TypeUtility.className(List.class), entityTypeName));
    executeWithDaoFactortBuilder.addCode("return daoFactory.get$L().$L(", method.getParentName(), method.getName());
    if (!method.isPagedLiveData()) {
        executeBuilder.addCode("list=$T.this.$L(", TypeUtility.typeName(method.getParent().getElement(), BindDaoBuilder.SUFFIX), method.getName());
    }
    // we have always a first parameter
    String separator = "";
    ParameterSpec parameterSpec;
    for (Pair<String, TypeName> item : method.getParameters()) {
        if (method.hasDynamicPageSizeConditions() && method.dynamicPageSizeName.equals(item.value0)) {
            setupBuilder.addStatement("this.pageSize=$L", item.value0);
        } else {
            // field
            typeBuilder.addField(item.value1, item.value0);
            setupBuilder.addStatement("this.$L=$L", item.value0, item.value0);
        }
        // construtor
        parameterSpec = ParameterSpec.builder(item.value1, item.value0).build();
        setupBuilder.addParameter(parameterSpec);
        // execute
        if (method.dynamicPageSizeName != null && method.dynamicPageSizeName.equals(item.value0)) {
            if (!method.isPagedLiveData()) {
                executeBuilder.addCode(separator + "this.pageSize");
            }
            executeWithDaoFactortBuilder.addCode(separator + "this.pageSize");
            executeWithPageRequestBuilder.addCode(separator + "this.pageSize");
        } else {
            if (!method.isPagedLiveData()) {
                executeBuilder.addCode(separator + item.value0);
            }
            executeWithDaoFactortBuilder.addCode(separator + item.value0);
            executeWithPageRequestBuilder.addCode(separator + item.value0);
        }
        separator = ", ";
    }
    if (method.isPagedLiveData()) {
        // if method is not paged live data, add complete execute method
        executeBuilder.addComment("paged result is used in live data, so this method must be empty");
        executeBuilder.addStatement("return null");
    } else {
        executeBuilder.addCode(separator + "this);\n");
        executeBuilder.addStatement("return list");
    }
    executeBuilder.addComment("Executor builder - END");
    typeBuilder.addMethod(executeBuilder.build());
    if (!method.hasDynamicPageSizeConditions()) {
        ModelAnnotation annotation = method.getAnnotation(BindSqlSelect.class);
        int pageSize = annotation.getAttributeAsInt(AnnotationAttributeType.PAGE_SIZE);
        // in case pageSize is not specified (only for liveData)
        if (pageSize == 0) {
            pageSize = 20;
        }
        setupBuilder.addStatement("this.pageSize=$L", pageSize);
    }
    typeBuilder.addMethod(setupBuilder.build());
    executeWithDaoFactortBuilder.addCode(separator + "this);\n");
    executeWithPageRequestBuilder.addCode(separator + "pageRequest));\n");
    executeWithPageRequestBuilder.addComment("Executor with pageRequet - END");
    // add methods to type builder
    typeBuilder.addMethod(executeWithDaoFactortBuilder.build());
    typeBuilder.addMethod(executeWithPageRequestBuilder.build());
    classBuilder.addType(typeBuilder.build());
    return pagedResultName;
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) PageRequest(com.abubusoft.kripton.android.PageRequest) ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ClassName(com.squareup.javapoet.ClassName) Builder(com.squareup.javapoet.MethodSpec.Builder) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 20 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project kripton by xcesco.

the class SqlInsertBuilder method generate.

/**
 * Generate.
 *
 * @param classBuilder
 *            the class builder
 * @param method
 *            the method
 */
public static void generate(TypeSpec.Builder classBuilder, SQLiteModelMethod method) {
    InsertType insertResultType = detectInsertType(method);
    // if true, field must be associate to ben attributes
    TypeName returnType = method.getReturnClass();
    AssertKripton.failWithInvalidMethodSignException(insertResultType == null, method);
    // generate method code
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getName()).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC);
    ParameterSpec parameterSpec;
    for (Pair<String, TypeName> item : method.getParameters()) {
        parameterSpec = ParameterSpec.builder(item.value1, item.value0).build();
        methodBuilder.addParameter(parameterSpec);
    }
    methodBuilder.returns(returnType);
    // fail if we use jql to INSERT_BEAN with operation of INSERT-FOR-SELECT
    // AssertKripton.failWithInvalidMethodSignException(insertResultType ==
    // InsertType.INSERT_BEAN && method.jql.containsSelectOperation, method,
    // "INSERT-FROM-SELECT SQL can not be used with method sign");
    // generate inner code
    methodBuilder.addComment("Specialized Insert - $L - BEGIN", insertResultType.getClass().getSimpleName());
    insertResultType.generate(classBuilder, methodBuilder, method, returnType);
    methodBuilder.addComment("Specialized Insert - $L - END", insertResultType.getClass().getSimpleName());
    MethodSpec methodSpec = methodBuilder.build();
    classBuilder.addMethod(methodSpec);
    if (method.contentProviderEntryPathEnabled) {
        // we need to generate insert for content provider to
        generateInsertForContentProvider(classBuilder, method, insertResultType);
    }
}
Also used : TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec)

Aggregations

ParameterSpec (com.squareup.javapoet.ParameterSpec)96 MethodSpec (com.squareup.javapoet.MethodSpec)60 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)42 ArrayList (java.util.ArrayList)35 TypeName (com.squareup.javapoet.TypeName)32 ClassName (com.squareup.javapoet.ClassName)30 TypeSpec (com.squareup.javapoet.TypeSpec)27 Map (java.util.Map)26 CodeBlock (com.squareup.javapoet.CodeBlock)23 List (java.util.List)21 FieldSpec (com.squareup.javapoet.FieldSpec)20 HashMap (java.util.HashMap)18 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)17 TypeElement (javax.lang.model.element.TypeElement)14 JavaFile (com.squareup.javapoet.JavaFile)13 Modifier (javax.lang.model.element.Modifier)13 Collectors (java.util.stream.Collectors)12 Element (javax.lang.model.element.Element)12 IOException (java.io.IOException)11 TypeMirror (javax.lang.model.type.TypeMirror)11