GithubHelp home page GithubHelp logo

clazz-loaderz's Introduction

internal JetBrains project

Experiments with Java class loaders

License

Apache 2.0

Load classes from resources

This project contains of 4 modules.

resource-clazz-loader this module contains main code that is used to load classes

packed here goes simplified code intended to be used from user's code all classes are renamed to fit version there is factory-name.txt file containing actual name

packed-impl generated code, intermediate package used to make IDEA compile renamed classes you should avoid any changed in sources under the module

packed-test this is a black-box tests module. There is only dependency to create artifact

Installation

  • Take the .jar from packed artifact output and copy it's contents into the main .jar of your application/library

  • Copy and rename factory-name.txt file and put in into your app's main .jar

  • Add URLs of your .jar files to load in the file. One pre line

  • Add those .jar files to the main .jar resources

  • Use the following code in your app to make listed .jar files be loaded in a classloader

        private ClassLoader classLoaderFromResources(@NotNull ClassLoader parent) {
          try {
            final Scanner scanner = new Scanner(parent.getResourceAsStream("renamed-libraries-list"), "utf-8");
            return (ClassLoader)parent.loadClass(scanner.nextLine()).getMethod("scan", ClassLoader.class, Scanner.class).invoke(null, parent, scanner);
          } catch (Exception e) {
            throw new RuntimeException("Failed to load classloader. " + e.getMessage(), e);
          }
        }
    

Misc

You may use the following Ant pre/post-action in IDEA artifact to generate classpath for your lib:

        <pathconvert pathsep="&#xA;" property="classpath">
          <fileset dir="${artifact.output.path}" includes="**/*.jar"/>
          <chainedmapper>
            <flattenmapper />
            <globmapper from="*" to="PREFIX/*"/>
          </chainedmapper>
        </pathconvert>
        <echo append="true" file="${artifact.output.path}/classpath">&#xA;${classpath}&#xA;</echo>

See hint for more details

Limitations

  • There is a custom (non URLClassLoader) implementation of classloader
  • This classloader prefers local classes to classes from parent classloader
  • Some libraries may fail to resolve patterns on resources (i.e. SpringFramework)
  • Only .jar are supported in classpath

clazz-loaderz's People

Contributors

jonnyzzz avatar

Watchers

 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.