詹子聪 5 lat temu
rodzic
commit
e511bd5571

+ 1 - 2
app/src/main/java/com/miekir/newmvp/MainActivity.java

@@ -5,7 +5,6 @@ import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 
-import com.miekir.mvp.base.DataResult;
 import com.miekir.mvp.presenter.DataMethod;
 import com.miekir.mvp.presenter.InjectPresenter;
 import com.miekir.mvp.view.BaseMVPActivity;
@@ -38,7 +37,7 @@ public class MainActivity extends BaseMVPActivity {
 
     // todo 检查该注解的参数个数,不是3个就抛出异常
     @DataMethod
-    public void onDataCome1(int code, String msg, TestBean1 data) {
+    public void onDataCome1(String code, String msg, TestBean1 data) {
         Log.e("ttttt", data.getName());
     }
 

+ 19 - 0
mvp/src/main/java/com/miekir/mvp/view/BaseMVPActivity.java

@@ -1,6 +1,7 @@
 package com.miekir.mvp.view;
 
 import android.os.Bundle;
+import android.text.TextUtils;
 
 import androidx.lifecycle.MutableLiveData;
 import androidx.lifecycle.Observer;
@@ -50,10 +51,28 @@ public abstract class BaseMVPActivity extends BaseActivity implements IView {
                 }
             }
 
+            // 参数校验
             if (methods != null && methods.length > 0) {
                 for (Method method : methods) {
                     DataMethod dataMethod = method.getAnnotation(DataMethod.class);
                     if (dataMethod != null) {
+                        int length = method.getParameterTypes().length;
+                        if (length != 3) {
+                            throw new AssertionError("DataMethod参数数量必须为3个");
+                        }
+
+                        String firstType = method.getParameterTypes()[0].getSimpleName();
+                        String firstCorrectType = int.class.getSimpleName();
+                        if (!TextUtils.equals(firstCorrectType, firstType)) {
+                            throw new AssertionError("DataMethod第1个参数必须为" + firstCorrectType);
+                        }
+
+                        String secondType = method.getParameterTypes()[1].getSimpleName();
+                        String secondCorrectType = String.class.getSimpleName();
+                        if (!TextUtils.equals(secondCorrectType, secondType)) {
+                            throw new AssertionError("DataMethod第2个参数必须为" + secondCorrectType);
+                        }
+
                         mDataMethodList.add(method);
                     }
                 }

+ 28 - 0
mvp/src/main/java/com/miekir/mvp/view/BaseMVPFragment.java

@@ -1,6 +1,7 @@
 package com.miekir.mvp.view;
 
 import android.os.Bundle;
+import android.text.TextUtils;
 
 import androidx.annotation.Nullable;
 import androidx.lifecycle.MutableLiveData;
@@ -58,6 +59,33 @@ public abstract class BaseMVPFragment extends BaseFragment implements IView {
                     }
                 }
             }
+
+            // 参数校验
+            if (methods != null && methods.length > 0) {
+                for (Method method : methods) {
+                    DataMethod dataMethod = method.getAnnotation(DataMethod.class);
+                    if (dataMethod != null) {
+                        int length = method.getParameterTypes().length;
+                        if (length != 3) {
+                            throw new AssertionError("DataMethod参数数量必须为3个");
+                        }
+
+                        String firstType = method.getParameterTypes()[0].getSimpleName();
+                        String firstCorrectType = int.class.getSimpleName();
+                        if (!TextUtils.equals(firstCorrectType, firstType)) {
+                            throw new AssertionError("DataMethod第1个参数必须为" + firstCorrectType);
+                        }
+
+                        String secondType = method.getParameterTypes()[1].getSimpleName();
+                        String secondCorrectType = String.class.getSimpleName();
+                        if (!TextUtils.equals(secondCorrectType, secondType)) {
+                            throw new AssertionError("DataMethod第2个参数必须为" + secondCorrectType);
+                        }
+
+                        mDataMethodList.add(method);
+                    }
+                }
+            }
         }
 
         mInjectPresenters = new ArrayList<>();