GithubHelp home page GithubHelp logo

patchlib's Introduction

#PatchLib


In very early stage of development!

Goodbye, Java reflection!

Let's pretend you are using a good library that fits almost all your need, except one thing - some useful fields or classes are private, so you can't access in normal ways.

It's extremely painful to use reflections.

AClass aInstance = someMethod();
Class<?> cls = Class.forName("com.example.ADefaultClass");
cls.setAccessible(true);
Field field = cls.getDeclaredField("aPrivateField");
field.setAccessible(true);
Object fieldObj = field.get(aInstance);
Class<?> fieldCls = Class.forName("com.example.APrivateFieldType");
Method classMethod = fieldCls.getMethod("privateMethod", Object.class);
classMethod.invoke(fieldObj, "Parameter");
// ... Exceptions are even not included in this example!

Hey! since classes in those libraries will be shipped with our binary, why can't we modify it?

With PatchLib, all you need to do is write a rule file in .yml format like this:

com/example/ADefaultClass:
  modifiers: +public # Make public
  fields:
    - aPrivateField:
      modifiers: +public # Make public
com/example/APrivateFieldType:
  modifiers: +public # Make public
  methods:
    - privateMethod:
      modifiers: +public # Make public
        

... and use your desired classes like this:

AClass aInstance = someMethod();
aInstance.aPrivateField.privateMethod("Parameter");

That's it!


There're more advanced usages:

# Let you throw custom exceptions in Retrofit
/retrofit/Converter: # Will match all subclasses if name beginning with a slash
methods:
  convert:
    exceptions: +com/example/CustomException
/retrofit/RequestBuilderAction:
methods:
  perform:
    exceptions: +com/example/CustomException
/retrofit/RequestFactory:
methods:
  create:
    exceptions: +com/example/CustomException
retrofit/OkHttpCall:
methods:
  parseResponse:
    exceptions: +com/example/CustomException
  createRawCall:
    exceptions: +com/example/CustomException
  execute:
    exceptions: +com/example/CustomException
/retrofit/Call:
methods:
  execute:
    exceptions: +com/example/CustomException
/retrofit/CallAdapter:
methods:
  adapt:
    exceptions: +com/example/CustomException
retrofit/MethodHandler:
methods:
  invoke:
    exceptions: +com/example/CustomException

The library not exactly matches your need? Patch it! No more reflections, no more waiting for pull request approval, no more meed to fork repos and merge code changes, just write rules and enjoy!

patchlib's People

Contributors

mariotaku avatar

Watchers

James Cloos avatar Zhang Mingyun 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.