Skip to content

Commit

Permalink
完善注释说明
Browse files Browse the repository at this point in the history
  • Loading branch information
woilsy committed Jun 21, 2023
1 parent 199dc3e commit ec7506a
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 19 deletions.
6 changes: 6 additions & 0 deletions mock/src/main/java/com/woilsy/mock/annotations/Mock.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 用于标记字段来Mock想要的数据,比如<br/>
* &#064;Mock("18")<br/>
* int age;<br/>
* 来表示将age字段的值Mock为18
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Mock {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Mock一个boolean的范围,可以用来做随机。<br/>
* 比如:@MockBooleanRange(true, false)可以表示为50%的true或者false
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MockBooleanRange {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 将网络请求接口中的某个函数进行排除,配合{@link MockObj}使用
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MockExclude {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 包含网络请求接口中的某个函数,配合{@link MockObj}使用
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MockInclude {
Expand Down
3 changes: 3 additions & 0 deletions mock/src/main/java/com/woilsy/mock/annotations/MockObj.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 用于标记某个网络请求接口类为Mock对象,配合{@link MockExclude}和{@link MockInclude}实现对类中请求函数的控制。
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MockObj {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* 用于执行HttpServer
*/
public class MockServerExecutor {

private HttpServer httpServer;
Expand Down
43 changes: 31 additions & 12 deletions mock/src/main/java/com/woilsy/mock/interceptor/MockInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,55 @@ public Response intercept(Chain chain) throws IOException {
return chain.proceed(request);
}

/**
* 将动态url类型的数据手动插入
*
* @param params 请求参数
* @param method 请求函数
* @param mockObj 请求接口对象
* @param methodName 请求类型,比如POST/GET,作为key的一部分,用于去重。
*/
private void putDynamicUrlData(List<?> params, Method method, MockObj mockObj, String methodName) {
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (int i = 0; i < parameterAnnotations.length; i++) {
Annotation[] annotations = parameterAnnotations[i];
for (Annotation annotation : annotations) {
//Url注解本身作用不大 主要是通过它的索引去获取实际的参数值
if (annotation instanceof Url) {
String url = params.get(i).toString();
String key = url + methodName;
if (!dynamicUrls.contains(key) && URLUtil.isNetworkUrl(url)) {
dynamicUrls.add(key);
//根据规则 手动插入数据
if (mockObj != null) {
MockPriority mockPriority = MockDataParse.getMockPriority(mockObj.value(), method);
String encodedPath = Uri.parse(url).getEncodedPath();
if (mockPriority != null) {
HttpInfo httpInfo = new HttpInfo(
HttpMethod.valueOf(methodName.toUpperCase()),
encodedPath,
MockPriority.DEFAULT
);
MockDataParse.putMethodData(method, httpInfo);
}
}
insertMethodData(method, mockObj, methodName, url);
}
break;
}
}
}
}

/**
* 将该请求函数进行解析后插入其类型
*/
private static void insertMethodData(Method method, MockObj mockObj, String methodName, String url) {
if (mockObj != null) {
MockPriority mockPriority = MockDataParse.getMockPriority(mockObj.value(), method);
String encodedPath = Uri.parse(url).getEncodedPath();
if (mockPriority != null) {
HttpInfo httpInfo = new HttpInfo(
HttpMethod.valueOf(methodName.toUpperCase()),
encodedPath,
MockPriority.DEFAULT
);
MockDataParse.putMethodData(method, httpInfo);
}
}
}

/**
* 得到新的Request进行请求
*/
private Response getMockResponse(Chain chain, Request request, HttpUrl httpUrl) throws IOException {
HttpUrl httpUrl1 = httpUrl
.newBuilder()
Expand Down
4 changes: 4 additions & 0 deletions mock/src/main/java/com/woilsy/mock/parse/MockDataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.woilsy.mock.entity.HttpData;
import com.woilsy.mock.entity.HttpInfo;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;
import java.util.Collection;
Expand Down Expand Up @@ -66,16 +67,19 @@ public void clear() {
httpDataMap.clear();
}

@NotNull
@Override
public Set<HttpInfo> keySet() {
return httpDataMap.keySet();
}

@NotNull
@Override
public Collection<HttpData> values() {
return httpDataMap.values();
}

@NotNull
@Override
public Set<Entry<HttpInfo, HttpData>> entrySet() {
return httpDataMap.entrySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public Object generateType(Type type, Field typeField, Object parent) {
* 查找一个有用的类型
*/
private Type findValidType(WildcardType wt) {
//先找lowerBounds
Type[] lowerBounds = wt.getLowerBounds();
Type[] upperBounds = wt.getUpperBounds();
Type lowerType = null;
int invalidCount = 0;
for (Type lowerBound : lowerBounds) {
Expand All @@ -43,10 +43,11 @@ private Type findValidType(WildcardType wt) {
lowerType = lowerBound;
}
}
//如果上方不满足条件
//如果不满足条件 那么尝试去upperBounds中找
if (lowerType == null || invalidCount > 0) {
invalidCount = 0;
Type upperType = null;
Type[] upperBounds = wt.getUpperBounds();
for (Type upperBound : upperBounds) {
if (upperBound == Object.class) {
invalidCount++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import com.woilsy.mock.Mocker;
import com.woilsy.mock.options.MockOptions;

/**
* 用于初始化Mocker,初始化还需要添加拦截器才会完成。
*/
public class MockInitProvider extends ContentProvider {

@Override
Expand Down
2 changes: 1 addition & 1 deletion mock/src/main/java/com/woilsy/mock/server/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse re
if (data == null) {//部分情况没有mock数据,例如定义返回类型为ResponseBody且没有为其填充数据
int code = 404;
AsyncHttpServerResponse httpServerResponse = response.code(404);
String message = "没有找到" + path + "的mock数据," + "错误原因:如果定义返回类型为非Bean类,那么需要定义DataSource";
String message = "没有找到" + path + "的mock数据,请在Logcat查看Mock日志";
String sbJson = "{" + "\"code\"" + ":" + code + ",\"message\"" + ":\"" + message + "\"" + "}";
httpServerResponse.send(sbJson);
} else {
Expand Down
6 changes: 3 additions & 3 deletions mock/src/main/java/com/woilsy/mock/utils/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static Type getSuspendFunctionReturnType(Method method) {
if (rawType == Continuation.class) {
Type[] actualTypeArguments = params.getActualTypeArguments();
if (actualTypeArguments.length > 0 && actualTypeArguments[0] instanceof WildcardType) {
return (WildcardType) actualTypeArguments[0];
return actualTypeArguments[0];
}
}
} catch (NoClassDefFoundError ignored) {
Expand Down Expand Up @@ -120,7 +120,7 @@ public static Object newClassInstance(Class<?> cls) {
}
}
} catch (Exception e) {//使用不安全的方式创建
LogUtil.e("()->构造器创建失败,尝试使用Unsafe创建:");
LogUtil.e("构造器创建失败,尝试使用Unsafe创建");
}
return unsafeCreate(cls);
}
Expand All @@ -132,7 +132,7 @@ public static Object unsafeCreate(Class<?> cls) {
try {
return ClassUtils.allocateInstance(cls);
} catch (Exception e2) {
LogUtil.e("()->尝试使用Unsafe创建失败:", e2);
LogUtil.e("尝试使用Unsafe创建失败:", e2);
return null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion mock/src/main/java/com/woilsy/mock/utils/NetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static String getIp(Context context) {
return ipAdd == 0 ? null : intToIp(ipAdd);
}
} catch (Exception e) {
Log.e("NetUtil", "getIp error");
Log.e("NetUtil", "getIp error", e);
}
return null;
}
Expand Down

0 comments on commit ec7506a

Please sign in to comment.