<commit_before># ClassInjector

[![CircleCI](https://circleci.com/gh/tmurakami/classinjector.svg?style=shield)](https://circleci.com/gh/tmurakami/classinjector)
[![Release](https://jitpack.io/v/tmurakami/classinjector.svg)](https://jitpack.io/#tmurakami/classinjector)  
![Java](https://img.shields.io/badge/Java-7%2B-blue.svg)
![Android](https://img.shields.io/badge/Android-2.3%2B-blue.svg)

A library that provides the ability to inject classes into a class loader.

## Example

```java
ClassInjector.from(new ClassSource() {
    @Override
    public ClassFile getClassFile(String className) throws IOException {
        // Find bytecode for the given class name from arbitrary sources.
        byte[] bytecode = ...
        // Return the ClassFile object with the given class name and its bytecode.
        // If no bytecode was found, return null.
        return bytecode == null ? null : new JvmClassFile(className, bytecode);
    }
}).into(getClass().getClassLoader());
```

For Android, use DexClassFile instead of JvmClassFile.
```java
// Get a Context object.
final Context context = ...
ClassInjector.from(new ClassSource() {
    @Override
    public ClassFile getClassFile(String className) throws IOException {
        // Find a zip file containing "classes.dex" with the given class name.
        String sourcePath = ...
        // If not found, return null.
        if (sourcePath == null) {
            return null;
        }
        // Construct the path for the optimized dex data.
        File cacheDir = context.getDir("dex_cache", Context.MODE_PRIVATE);
        String optimizedPath = new File(cacheDir, "classes.dex").getCanonicalPath();
        // Open the dex file.<commit_msg>Update README.md [ci skip]<commit_after># ClassInjector

[![CircleCI](https://circleci.com/gh/tmurakami/classinjector.svg?style=shield)](https://circleci.com/gh/tmurakami/classinjector)
[![Release](https://jitpack.io/v/tmurakami/classinjector.svg)](https://jitpack.io/#tmurakami/classinjector)  
![Java](https://img.shields.io/badge/Java-7%2B-blue.svg)
![Android](https://img.shields.io/badge/Android-2.3%2B-blue.svg)

A library that provides the ability to inject classes into a class loader.

## Usage

```java
ClassInjector.from(new ClassSource() {
    @Override
    public ClassFile getClassFile(String className) throws IOException {
        // Find bytecode for the given class name from arbitrary sources.
        byte[] bytecode = ...
        // Return the ClassFile object with the given class name and its bytecode.
        // If no bytecode was found, return null.
        return bytecode == null ? null : new JvmClassFile(className, bytecode);
    }
}).into(getClass().getClassLoader());
```

For Android, use DexClassFile instead of JvmClassFile.
```java
// Get a Context object.
final Context context = ...
ClassInjector.from(new ClassSource() {
    @Override
    public ClassFile getClassFile(String className) throws IOException {
        // Find a zip file containing "classes.dex" with the given class name.
        String sourcePath = ...
        // If not found, return null.
        if (sourcePath == null) {
            return null;
        }
        // Construct the path for the optimized dex data.
        File cacheDir = context.getDir("dex_cache", Context.MODE_PRIVATE);
        String optimizedPath = new File(cacheDir, "classes.dex").getCanonicalPath();
        // Open the dex file.
{'fraction_non_alphanumeric': 0.1233518898329915, 'fraction_numerical': 0.002929973630237328, 'mean_word_length': 3.199261992619926, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4242563', 'n_tokens_mistral': 1008, 'n_tokens_neox': 990, 'n_words': 276}