Search in sources :

Example 56 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project glide by bumptech.

the class RequestBuilderGenerator method generateGeneratedRequestOptionEquivalent.

/**
 * Generates a particular method with  an equivalent name and arguments to the given method
 * from the generated {@code 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)) && // We need to combine warnings below.
            !input.type.equals(TypeName.get(SuppressWarnings.class));
        }
    }).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");
    AnnotationSpec suppressWarnings = buildSuppressWarnings(requestOptionMethod);
    if (suppressWarnings != null) {
        result.addAnnotation(suppressWarnings);
    }
    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)

Example 57 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project glide by bumptech.

the class ProcessorUtil method dedupedParameters.

private static List<ParameterSpec> dedupedParameters(List<ParameterSpec> parameters) {
    boolean hasDupes = false;
    Set<String> names = new HashSet<>();
    for (ParameterSpec parameter : parameters) {
        String name = parameter.name;
        if (names.contains(name)) {
            hasDupes = true;
        } else {
            names.add(name);
        }
    }
    if (hasDupes) {
        List<ParameterSpec> copy = parameters;
        parameters = new ArrayList<>();
        for (int i = 0; i < copy.size(); i++) {
            ParameterSpec parameter = copy.get(i);
            parameters.add(ParameterSpec.builder(parameter.type, parameter.name + i).addModifiers(parameter.modifiers).addAnnotations(parameter.annotations).build());
        }
    }
    return parameters;
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 58 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 = elementUtils.getTypeElement(IINTERCEPTOR);
        TypeElement type_ITollgateGroup = elementUtils.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 59 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)

Example 60 with ParameterSpec

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

the class RouterProcessor method generatedGroup.

private void generatedGroup(TypeElement iRouteGroup) {
    // 创建参数类型 Map<String, RouteMeta>
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), ClassName.get(RouteMeta.class));
    ParameterSpec altas = ParameterSpec.builder(parameterizedTypeName, "atlas").build();
    for (Map.Entry<String, List<RouteMeta>> entry : groupMap.entrySet()) {
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(Constant.METHOD_LOAD_INTO).addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(altas);
        String groupName = entry.getKey();
        List<RouteMeta> groupData = entry.getValue();
        for (RouteMeta routeMeta : groupData) {
            // 函数体的添加
            methodBuilder.addStatement("atlas.put($S,$T.build($T.$L,$T.class,$S,$S))", routeMeta.getPath(), ClassName.get(RouteMeta.class), ClassName.get(RouteMeta.Type.class), routeMeta.getType(), ClassName.get(((TypeElement) routeMeta.getElement())), routeMeta.getPath(), routeMeta.getGroup());
        }
        String groupClassName = Constant.NAME_OF_GROUP + groupName;
        TypeSpec typeSpec = TypeSpec.classBuilder(groupClassName).addSuperinterface(ClassName.get(iRouteGroup)).addModifiers(Modifier.PUBLIC).addMethod(methodBuilder.build()).build();
        JavaFile javaFile = JavaFile.builder(Constant.PACKAGE_OF_GENERATE_FILE, typeSpec).build();
        try {
            javaFile.writeTo(filerUtils);
        } catch (IOException e) {
            e.printStackTrace();
        }
        rootMap.put(groupName, groupClassName);
    }
}
Also used : RouteMeta(com.xsm.easy.annotation.modle.RouteMeta) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) IOException(java.io.IOException) JavaFile(com.squareup.javapoet.JavaFile) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ParameterSpec (com.squareup.javapoet.ParameterSpec)97 MethodSpec (com.squareup.javapoet.MethodSpec)59 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)40 ArrayList (java.util.ArrayList)34 TypeName (com.squareup.javapoet.TypeName)30 ClassName (com.squareup.javapoet.ClassName)27 TypeSpec (com.squareup.javapoet.TypeSpec)26 Map (java.util.Map)26 CodeBlock (com.squareup.javapoet.CodeBlock)23 FieldSpec (com.squareup.javapoet.FieldSpec)22 List (java.util.List)20 HashMap (java.util.HashMap)18 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)17 JavaFile (com.squareup.javapoet.JavaFile)13 Modifier (javax.lang.model.element.Modifier)13 TypeElement (javax.lang.model.element.TypeElement)13 Collectors (java.util.stream.Collectors)12 IOException (java.io.IOException)11 Element (javax.lang.model.element.Element)11 TypeMirror (javax.lang.model.type.TypeMirror)11