Search in sources :

Example 31 with ParameterSpec

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

the class InterceptorProcessor method parseInterceptors.

/**
 * Parse tollgate.
 *
 * 处理 @Interceptor 元素
 *
 * 1. 编译每个 @Interceptor 元素
 * 2. 验证每个 @Interceptor 元素
 * 3. 根据优先级 = key,@Interceptor 元素 为 value,缓存到 TreeMap 内
 * 4. JavaPoet 生成 java class
 *
 * @param elements elements of tollgate.
 * @throws IOException IOException
 */
private void parseInterceptors(Set<? extends Element> elements) throws IOException {
    if (CollectionUtils.isNotEmpty(elements)) {
        logger.info(">>> Found interceptors, size is " + elements.size() + " <<<");
        // Verify and cache, sort incidentally.
        for (Element element : elements) {
            if (verify(element)) {
                // Check the interceptor meta
                logger.info("A interceptor verify over, its " + element.asType());
                Interceptor interceptor = element.getAnnotation(Interceptor.class);
                Element lastInterceptor = interceptors.get(interceptor.priority());
                if (null != lastInterceptor) {
                    // Added, throw exceptions
                    throw new IllegalArgumentException(String.format(Locale.getDefault(), "More than one interceptors use same priority [%d], They are [%s] and [%s].", interceptor.priority(), lastInterceptor.getSimpleName(), element.getSimpleName()));
                }
                interceptors.put(interceptor.priority(), element);
            } else {
                logger.error("A interceptor verify failed, its " + element.asType());
            }
        }
        /**
         * 获取 IInterceptor 的 TypeElement
         * 获取 IInterceptorGroup 的 TypeElement
         */
        // Interface of ARouter.
        TypeElement type_ITollgate = elementUtil.getTypeElement(IINTERCEPTOR);
        TypeElement type_ITollgateGroup = elementUtil.getTypeElement(IINTERCEPTOR_GROUP);
        /**
         *  Build input type, format as :
         *
         *  ```Map<Integer, Class<? extends ITollgate>>```
         *
         *  @Override
         *  public void loadInto(Map<Integer, Class<? extends IInterceptor>> interceptors)
         *  中的参数类型
         *  Map<Integer, Class<? extends IInterceptor>>
         */
        ParameterizedTypeName inputMapTypeOfTollgate = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(Integer.class), ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(ClassName.get(type_ITollgate))));
        /**
         *  @Override
         *  public void loadInto(Map<Integer, Class<? extends IInterceptor>> interceptors)
         *  中的参数
         *  Map<Integer, Class<? extends IInterceptor>> interceptors
         */
        // Build input param name.
        ParameterSpec tollgateParamSpec = ParameterSpec.builder(inputMapTypeOfTollgate, "interceptors").build();
        /**
         * 整个 loadInto 方法
         *  @Override
         *  public void loadInto(Map<Integer, Class<? extends IInterceptor>> interceptors)
         */
        // Build method : 'loadInto'
        MethodSpec.Builder loadIntoMethodOfTollgateBuilder = MethodSpec.methodBuilder(METHOD_LOAD_INTO).addAnnotation(Override.class).addModifiers(PUBLIC).addParameter(tollgateParamSpec);
        // Generate
        if (null != interceptors && interceptors.size() > 0) {
            // Build method body
            for (Map.Entry<Integer, Element> entry : interceptors.entrySet()) {
                loadIntoMethodOfTollgateBuilder.addStatement("interceptors.put(" + entry.getKey() + ", $T.class)", ClassName.get((TypeElement) entry.getValue()));
            }
        }
        /**
         * 生成 java class
         *
         * public class ???$$Interceptors$$app implements IInterceptorGroup
         */
        // Write to disk(Write file even interceptors is empty.)
        JavaFile.builder(PACKAGE_OF_GENERATE_FILE, TypeSpec.classBuilder(NAME_OF_INTERCEPTOR + SEPARATOR + moduleName).addModifiers(PUBLIC).addJavadoc(WARNING_TIPS).addMethod(loadIntoMethodOfTollgateBuilder.build()).addSuperinterface(ClassName.get(type_ITollgateGroup)).build()).build().writeTo(mFiler);
        logger.info(">>> Interceptor group write over. <<<");
    }
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) Interceptor(com.alibaba.android.arouter.facade.annotation.Interceptor) Map(java.util.Map) TreeMap(java.util.TreeMap) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 32 with ParameterSpec

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

the class ComponentGenerator method buildInverseUnsetMethod.

@Nonnull
private static MethodSpec buildInverseUnsetMethod(@Nonnull final InverseDescriptor inverse) throws ProcessorException {
    final ObservableDescriptor observable = inverse.getObservable();
    final String methodName = Multiplicity.ONE == inverse.getMultiplicity() ? getInverseUnsetMethodName(observable.getName()) : getInverseZUnsetMethodName(observable.getName());
    final MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName);
    if (ElementsUtil.areTypesInDifferentPackage(inverse.getTargetType(), inverse.getComponent().getElement())) {
        builder.addModifiers(Modifier.PUBLIC);
    }
    final String otherName = inverse.getOtherName();
    final ParameterSpec parameter = ParameterSpec.builder(TypeName.get(inverse.getTargetType().asType()), otherName, Modifier.FINAL).addAnnotation(GeneratorUtil.NONNULL_CLASSNAME).build();
    builder.addParameter(parameter);
    generateNotDisposedInvariant(builder, methodName);
    builder.addStatement("this.$N.preReportChanged()", observable.getFieldName());
    final CodeBlock.Builder block = CodeBlock.builder();
    block.beginControlFlow("if ( this.$N == $N )", observable.getDataFieldName(), otherName);
    for (final ExecutableElement hook : inverse.getPreInverseRemoveHooks()) {
        block.addStatement("$N( $N )", hook.getSimpleName(), otherName);
    }
    block.addStatement("this.$N = null", observable.getDataFieldName());
    block.addStatement("this.$N.reportChanged()", observable.getFieldName());
    block.endControlFlow();
    builder.addCode(block.build());
    return builder.build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeBlock(com.squareup.javapoet.CodeBlock) Nonnull(javax.annotation.Nonnull)

Example 33 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project AndroidQuick by ddnosh.

the class AnnotatedClass method generateFile.

JavaFile generateFile() {
    // 方法参数类型
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), ClassName.get(TagInfo.class));
    ParameterSpec params = ParameterSpec.builder(parameterizedTypeName, "params").build();
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("load").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(params);
    for (BindTagField item : mFields) {
        String key = item.getTypeName().toString();
        TagInfo.Type type = item.getType();
        String[] value = item.getTag();
        String description = item.getDescription();
        // 添加方法内容
        methodBuilder.addStatement("params.put($S,$T.build($T.$L,$S,$S,$S))", key, ClassName.get(TagInfo.class), ClassName.get(TagInfo.Type.class), type, key, Arrays.toString(value), description);
    }
    // 生成类
    TypeSpec finderClass = TypeSpec.classBuilder("TagService").addSuperinterface(ClassName.get("com.androidwind.annotation.core", "ILoad")).addModifiers(Modifier.PUBLIC).addMethod(methodBuilder.build()).build();
    return JavaFile.builder("com.androidwind.annotation", finderClass).build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) TagInfo(com.androidwind.annotation.annotation.TagInfo) Map(java.util.Map) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 34 with ParameterSpec

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

the class CursorGenerator method buildConstructors.

private static List<MethodSpec> buildConstructors(GrainElement ge, TypeName columnsClassType) {
    List<MethodSpec> results = new ArrayList<>();
    ParameterSpec contextParam = ParameterSpec.builder(CallContext.class, "context").build();
    ParameterSpec fieldsParam = ParameterSpec.builder(ParameterizedTypeName.get(Set.class, String.class), "fields").build();
    ParameterSpec columnsParam = ParameterSpec.builder(ArrayTypeName.of(ParameterizedTypeName.get(ClassName.get(ColumnMeta.class), WildcardTypeName.subtypeOf(Object.class))), "columns").build();
    ParameterSpec parametersParam = ParameterSpec.builder(ParameterizedTypeName.get(Map.class, String.class, Object.class), "parameters").build();
    Supplier<MethodSpec.Builder> msp = () -> MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(contextParam);
    // Common constructor
    MethodSpec.Builder builder = msp.get();
    if (ge instanceof ParameterizedView) {
        builder.addParameter(parametersParam);
        builder.addStatement("super(context, parameters)");
    } else {
        builder.addStatement("super(context)");
    }
    results.add(builder.build());
    if (ge instanceof SequenceElement) {
        return results;
    }
    // Constructor with columns limitation
    builder = msp.get();
    if (ge instanceof ParameterizedView) {
        builder.addParameter(parametersParam);
        builder.addParameter(columnsParam).varargs();
        builder.addStatement("super(context, parameters, columns)");
    } else {
        builder.addParameter(columnsParam).varargs();
        builder.addStatement("super(context, columns)");
    }
    results.add(builder.build());
    // Deprecated constructor with fields limitation
    builder = msp.get();
    if (ge instanceof ParameterizedView) {
        builder.addParameter(fieldsParam);
        builder.addParameter(parametersParam);
        builder.addStatement("super(context, fields, parameters)");
    } else {
        builder.addParameter(fieldsParam);
        builder.addStatement("super(context, fields)");
    }
    results.add(builder.addAnnotation(Deprecated.class).build());
    // ParameterizedView constructors
    if (ge instanceof ParameterizedView) {
        ParameterizedView pv = (ParameterizedView) ge;
        builder = msp.get();
        for (Parameter parameter : pv.getParameters().values()) {
            builder.addParameter(ParameterSpec.builder(parameter.getJavaClass(), parameter.getName()).build());
        }
        String spec = "super (context, paramsMap(" + pv.getParameters().values().stream().map(c -> "$N").collect(Collectors.joining(", ")) + "))";
        builder.addStatement(spec, pv.getParameters().keySet().toArray());
        results.add(builder.build());
        builder = msp.get();
        for (Parameter parameter : pv.getParameters().values()) {
            builder.addParameter(ParameterSpec.builder(parameter.getJavaClass(), parameter.getName()).build());
        }
        builder.addParameter(columnsParam).varargs();
        spec = "super (context, paramsMap(" + pv.getParameters().values().stream().map(c -> "$N").collect(Collectors.joining(", ")) + "), columns)";
        builder.addStatement(spec, pv.getParameters().keySet().toArray());
        results.add(builder.build());
        results.add(getParameterizedViewTypedConstructorHelper(pv));
    }
    return results;
}
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) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) SequenceElement(ru.curs.celesta.score.SequenceElement) ArrayList(java.util.ArrayList) CallContext(ru.curs.celesta.CallContext) Parameter(ru.curs.celesta.score.Parameter) ParameterizedView(ru.curs.celesta.score.ParameterizedView)

Example 35 with ParameterSpec

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

the class CursorGenerator method buildClearBuffer.

private MethodSpec buildClearBuffer(Map<String, ? extends ColumnMeta<?>> columns, Set<Column<?>> pk) {
    ParameterSpec param = ParameterSpec.builder(boolean.class, "withKeys").build();
    MethodSpec.Builder builder = MethodSpec.methodBuilder("_clearBuffer").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(param);
    if (!pk.isEmpty()) {
        builder.beginControlFlow("if ($N)", param.name);
        pk.stream().forEach(c -> builder.addStatement("this.$N = null", camelize(c.getName())));
        builder.endControlFlow();
    }
    columns.entrySet().stream().filter(e -> !pk.contains(e.getValue())).forEach(e -> builder.addStatement("this.$N = null", camelize(e.getKey())));
    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) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec)

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