Search in sources :

Example 6 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project epoxy by airbnb.

the class ClassToGenerateInfo method buildParamList.

private List<ParameterSpec> buildParamList(List<? extends VariableElement> params) {
    List<ParameterSpec> result = new ArrayList<>();
    for (VariableElement param : params) {
        Builder builder = ParameterSpec.builder(TypeName.get(param.asType()), param.getSimpleName().toString());
        for (AnnotationMirror annotation : param.getAnnotationMirrors()) {
            builder.addAnnotation(AnnotationSpec.get(annotation));
        }
        result.add(builder.build());
    }
    return result;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ParameterSpec(com.squareup.javapoet.ParameterSpec) Builder(com.squareup.javapoet.ParameterSpec.Builder) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement)

Example 7 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project epoxy by airbnb.

the class EpoxyProcessor method generateParams.

private void generateParams(StringBuilder statementBuilder, List<ParameterSpec> params) {
    boolean first = true;
    for (ParameterSpec param : params) {
        if (!first) {
            statementBuilder.append(", ");
        }
        first = false;
        statementBuilder.append(param.name);
    }
    statementBuilder.append(")");
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec)

Example 8 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project ARouter by alibaba.

the class InterceptorProcessor method parseInterceptors.

/**
     * Parse tollgate.
     *
     * @param elements elements of tollgate.
     */
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());
            }
        }
        // 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>>```
             */
        ParameterizedTypeName inputMapTypeOfTollgate = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(Integer.class), ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(ClassName.get(type_ITollgate))));
        // Build input param name.
        ParameterSpec tollgateParamSpec = ParameterSpec.builder(inputMapTypeOfTollgate, "interceptors").build();
        // 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()));
            }
        }
        // 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 9 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project RxCache by VictorAlbertos.

the class BrewJavaFile method getActionProvider.

private MethodSpec getActionProvider(ClassName providersClassName, ProvidersClass.Method method) {
    String methodName = method.name;
    TypeName list = ClassName.get(method.enclosingTypeObservable);
    TypeName enclosingTypeList = ClassName.get(method.enclosingTypeObservable.getTypeArguments().get(0));
    String enclosingTypeListName = enclosingTypeList.toString();
    ParameterizedTypeName action = ParameterizedTypeName.get(ClassName.get(ActionsList.class), enclosingTypeList);
    ParameterizedTypeName evict = ParameterizedTypeName.get(ClassName.get(ActionsList.Evict.class), enclosingTypeList);
    ParameterizedTypeName arrayList = ParameterizedTypeName.get(ClassName.get(ArrayList.class), enclosingTypeList);
    ParameterizedTypeName observable = ParameterizedTypeName.get(ClassName.get(Observable.class), list);
    ParameterSpec rxProvidersInstance = ParameterSpec.builder(providersClassName, "proxy").addModifiers(Modifier.FINAL).build();
    MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName).addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(rxProvidersInstance).returns(action);
    if (method.hasDynamicKey) {
        builder.addParameter(DynamicKey.class, "dynamicKey", Modifier.FINAL);
    } else if (method.hasDynamicKeyGroup) {
        builder.addParameter(DynamicKeyGroup.class, "dynamicKeyGroup", Modifier.FINAL);
    }
    builder.beginControlFlow("$T evict = new $T()", evict, evict).beginControlFlow("@Override public $T call($T elements)", observable, observable);
    if (method.hasDynamicKey) {
        setReturnEvictForEvictDynamicKey(builder, methodName);
    } else if (method.hasDynamicKeyGroup) {
        setReturnEvictForEvictDynamicKeyGroup(builder, methodName);
    } else {
        setReturnEvictForEvictProvider(builder, methodName);
    }
    builder.endControlFlow().endControlFlow(";");
    if (method.hasDynamicKey) {
        setCacheForEvictDynamicKey(builder, observable, arrayList, methodName, enclosingTypeListName);
    } else if (method.hasDynamicKeyGroup) {
        setCacheForEvictDynamicKeyGroup(builder, observable, arrayList, methodName, enclosingTypeListName);
    } else {
        setCacheForEvictProvider(builder, observable, arrayList, methodName, enclosingTypeListName);
    }
    builder.addStatement("return ActionsList.with(evict, oCache)");
    return builder.build();
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Aggregations

ParameterSpec (com.squareup.javapoet.ParameterSpec)9 MethodSpec (com.squareup.javapoet.MethodSpec)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 TypeElement (javax.lang.model.element.TypeElement)4 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)3 HashMap (java.util.HashMap)3 Element (javax.lang.model.element.Element)3 Autowired (com.alibaba.android.arouter.facade.annotation.Autowired)2 ClassName (com.squareup.javapoet.ClassName)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 VariableElement (javax.lang.model.element.VariableElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 Interceptor (com.alibaba.android.arouter.facade.annotation.Interceptor)1 Route (com.alibaba.android.arouter.facade.annotation.Route)1 RouteMeta (com.alibaba.android.arouter.facade.model.RouteMeta)1 ImmutableList (com.google.common.collect.ImmutableList)1 CodeBlock (com.squareup.javapoet.CodeBlock)1