GithubHelp home page GithubHelp logo

youngshingjun / jpaspect Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zhiyongzou/dynamicoc

0.0 1.0 0.0 79 KB

JPAspect fix bugs dynamically by JSON

License: MIT License

Ruby 0.73% Objective-C 99.27%

jpaspect's Introduction

JPAspect

CocoaPods Version License Build Status HitCount

JPAspect 一款轻量级、无侵入和无审核风险的 iOS 热修复框架。JPAspect 通过下发指定规则的 JSON 即可轻松实现线上 Bug 修复。

功能

  • 方法替换为空实现
  • 方法参数修改
  • 方法返回值修改
  • 方法调用前后插入自定义代码
    • 支持任意 OC 方法调用
    • 支持赋值语句
    • 支持 if 语句:==、!=、>、>=、<、<=、||、&&
    • 支持 super 调用
    • 支持自定义局部变量
    • 支持 return 语句

注意

  • JPAspect 不支持 block、struct、enum、循环语句和 C/C++ 函数
  • JPAspect 主要在对目标方法的基础上进行修改,从而实现 bug 修复
  • JPAspect 在方法重写和自定义调用存在一定的局限性。但是用来修复常见 bug 已经足够

示例

数组越界异常

调用 [self outOfBoundsException:index] 时,由于传入 index >= self.testList.count ,导致数组越界异常。

@implementation ViewController

- (void)outOfBoundsException:(NSUInteger)index
{
    // fix code
//    if (index >= self.testList.count) {
//        return;
//    }
    
    [self.testList objectAtIndex:index];
}

@end

JSON 脚本

示例方法存在数组越界崩溃,通过分析,只需增加越界判断保护即可(示例中的注释代码)。JSON 脚本修复:只需要在 outOfBoundsException: 调用前增加数组越界保护逻辑即可,具体修复脚本如下所示:

// 该脚本等于 fix code
{
    "AspectDefineClass" : [],
    "Aspects": [
        {
            "className": "ViewController",
            "selName": "outOfBoundsException:",
            "hookType": 1,
            "argumentNames": ["index"],
            "customMessages" : [
                {
                    "message" : "self.testList.count",
                    "localInstanceKey" : "listCount"
                },
                {
                    "message" : "return",
                    "messageType" : 1,
                    "invokeCondition": {
                        "condition" : "index>=listCount",
                        "operator" : ">="
                    }
                }
            ]
        }
    ]
}

安装

CocoaPods

pod 'JPAspect'

Manually

JPAspect 文件夹拷贝到工程即可

使用

示例一

  1. #import "JPAspect+PatchLoad.h"
  2. 获取本地 JSON 修复配置
  3. 调用 + (void)loadJsonPatchWithPath:(NSString *)filePath
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSString *patchPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"json"];
    [JPAspect loadJsonPatchWithPath:patchPath];
    
    return YES;
}

@end

示例二

  1. #import "JPAspect.h"
  2. 获取本地 JSON 修复配置
  3. 获取 AspectDefineClass 设置使用到的 Class + (void)setupAspectDefineClass:(NSArray<NSString *> *)classList
  4. 获取 Aspects 然后调用 + (void)hookSelectorWithAspectDictionary:(NSDictionary *)aspectDictionary
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSDictionary *patchDic = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:@".../xxx.json"] options:NSJSONReadingMutableContainers error:NULL];
    
    [JPAspect setupAspectDefineClass:[patchDic objectForKey:@"AspectDefineClass"]];

    NSArray<NSDictionary *> *patchs = [patchDic objectForKey:@"Aspects"];
    [patchs enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull patch, NSUInteger idx, BOOL * _Nonnull stop) {
        [JPAspect hookSelectorWithAspectDictionary:patch];
    }];
    
    return YES;
}

@end

其他

  • JPAspect 详细使用文档: Wiki
  • 单元测试用例:JsonPatchDemoTests
  • 如果在使用JPAspect时遇到问题,请发起issue或联系本人

jpaspect's People

Contributors

zhiyongzou avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.