Skip to content

Commit

Permalink
忽略配置的内置CallAdapterFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Oct 7, 2022
1 parent 01b66ac commit 1d44a60
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @author 陈添明
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class BodyCallAdapterFactory extends CallAdapter.Factory {
public final class BodyCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory {

public static final BodyCallAdapterFactory INSTANCE = new BodyCallAdapterFactory();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.lianjiatech.retrofit.spring.boot.core;

/**
* 组件内置CallAdapterFactory,标记接口。
* @author 陈添明
* @since 2022/9/12 8:08 下午
*/
public interface InternalCallAdapterFactory {
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* @author 陈添明
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ResponseCallAdapterFactory extends CallAdapter.Factory {
public final class ResponseCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory {

public static final ResponseCallAdapterFactory INSTANCE = new ResponseCallAdapterFactory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
Expand Down Expand Up @@ -165,14 +167,26 @@ private Retrofit createRetrofit() {
.validateEagerly(retrofitClient.validateEagerly())
.client(client);

combineAndCreate(retrofitClient.callAdapterFactories(), retrofitConfigBean.getGlobalCallAdapterFactoryClasses())
.forEach(retrofitBuilder::addCallAdapterFactory);
// 添加配置或者指定的CallAdapterFactory
List<Class<? extends CallAdapter.Factory>> callAdapterFactories = new ArrayList<>(2);
callAdapterFactories.addAll(Arrays.asList(retrofitClient.callAdapterFactories()));
callAdapterFactories.addAll(Arrays.asList(retrofitConfigBean.getGlobalCallAdapterFactoryClasses()));
callAdapterFactories.stream()
// 过滤掉内置的CallAdapterFactory,因为后续会指定add
.filter(adapterFactoryClass -> !InternalCallAdapterFactory.class.isAssignableFrom(adapterFactoryClass))
.forEach(adapterFactoryClass -> retrofitBuilder
.addCallAdapterFactory(AppContextUtils.getBeanOrNew(applicationContext, adapterFactoryClass)));

addReactiveCallAdapterFactory(retrofitBuilder);
retrofitBuilder.addCallAdapterFactory(ResponseCallAdapterFactory.INSTANCE);
retrofitBuilder.addCallAdapterFactory(BodyCallAdapterFactory.INSTANCE);

combineAndCreate(retrofitClient.converterFactories(), retrofitConfigBean.getGlobalConverterFactoryClasses())
.forEach(retrofitBuilder::addConverterFactory);
// 添加配置或者指定的ConverterFactory
List<Class<? extends Converter.Factory>> converterFactories = new ArrayList<>(4);
converterFactories.addAll(Arrays.asList(retrofitClient.converterFactories()));
converterFactories.addAll(Arrays.asList(retrofitConfigBean.getGlobalConverterFactoryClasses()));
converterFactories.forEach(converterFactoryClass -> retrofitBuilder
.addConverterFactory(AppContextUtils.getBeanOrNew(applicationContext, converterFactoryClass)));

return retrofitBuilder.build();
}
Expand Down Expand Up @@ -218,21 +232,6 @@ private boolean reactor3ClassExist() {
}
}

private <E> List<E> combineAndCreate(Class<? extends E>[] clz, Class<? extends E>[] globalClz) {
if (clz.length == 0 && globalClz.length == 0) {
return Collections.emptyList();
}
List<Class<? extends E>> combineClz = new ArrayList<>(clz.length + globalClz.length);
combineClz.addAll(Arrays.asList(clz));
combineClz.addAll(Arrays.asList(globalClz));

List<E> result = new ArrayList<>(combineClz.size());
for (Class<? extends E> aClass : combineClz) {
result.add(AppContextUtils.getBeanOrNew(applicationContext, aClass));
}
return result;
}

@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import com.github.lianjiatech.retrofit.spring.boot.core.InternalCallAdapterFactory;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import reactor.core.publisher.Mono;
Expand All @@ -20,7 +21,7 @@
* @since 2022/6/9 8:53 下午
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MonoCallAdapterFactory extends CallAdapter.Factory {
public class MonoCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory {

public static final MonoCallAdapterFactory INSTANCE = new MonoCallAdapterFactory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import com.github.lianjiatech.retrofit.spring.boot.core.InternalCallAdapterFactory;
import io.reactivex.Completable;
import io.reactivex.CompletableEmitter;
import io.reactivex.annotations.NonNull;
Expand All @@ -20,7 +21,7 @@
* @since 2022/6/10 8:08 上午
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Rxjava2CompletableCallAdapterFactory extends CallAdapter.Factory {
public class Rxjava2CompletableCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory {

public static final Rxjava2CompletableCallAdapterFactory INSTANCE = new Rxjava2CompletableCallAdapterFactory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import com.github.lianjiatech.retrofit.spring.boot.core.InternalCallAdapterFactory;
import io.reactivex.Single;
import io.reactivex.SingleEmitter;
import io.reactivex.annotations.NonNull;
Expand All @@ -21,7 +22,7 @@
* @since 2022/6/10 8:08 上午
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Rxjava2SingleCallAdapterFactory extends CallAdapter.Factory {
public class Rxjava2SingleCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory {

public static final Rxjava2SingleCallAdapterFactory INSTANCE = new Rxjava2SingleCallAdapterFactory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import com.github.lianjiatech.retrofit.spring.boot.core.InternalCallAdapterFactory;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.CompletableEmitter;
Expand All @@ -20,7 +21,7 @@
* @since 2022/6/10 8:08 上午
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Rxjava3CompletableCallAdapterFactory extends CallAdapter.Factory {
public class Rxjava3CompletableCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory {

public static final Rxjava3CompletableCallAdapterFactory INSTANCE = new Rxjava3CompletableCallAdapterFactory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import com.github.lianjiatech.retrofit.spring.boot.core.InternalCallAdapterFactory;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleEmitter;
Expand All @@ -21,7 +22,7 @@
* @since 2022/6/10 8:08 上午
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Rxjava3SingleCallAdapterFactory extends CallAdapter.Factory {
public class Rxjava3SingleCallAdapterFactory extends CallAdapter.Factory implements InternalCallAdapterFactory {

public static final Rxjava3SingleCallAdapterFactory INSTANCE = new Rxjava3SingleCallAdapterFactory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static <T> T getBeanOrNew(ApplicationContext context, Class<T> clz) {
} catch (Exception e1) {
try {
log.warn("Failed to get bean from applicationContext!", e1);
return clz.newInstance();
return clz.getDeclaredConstructor().newInstance();
} catch (Exception e2) {
log.warn("Failed to create instance by reflection.", e2);
try {
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ retrofit:
- retrofit2.converter.jackson.JacksonConverterFactory
# 全局调用适配器工厂(组件扩展的调用适配器工厂已经内置,这里请勿重复配置)
global-call-adapter-factories:

- com.github.lianjiatech.retrofit.spring.boot.core.BodyCallAdapterFactory
# 全局日志打印配置
global-log:
# 启用全局日志打印
Expand Down

0 comments on commit 1d44a60

Please sign in to comment.