Java: Load class from string

后端 未结 4 2018
粉色の甜心
粉色の甜心 2020-12-18 16:09

I know this has probably something to do with class loaders, however I couldn\'t find an example (it might be I\'m google-ing for the wrong keywords.

I am trying to

相关标签:
4条回答
  • 2020-12-18 16:34

    Use Java Compiler API. Here is a blog post that shows you how to do it.

    You can use temporary files for this, as this requires input/output file, or you can create custom implementation of JavaFileObject that reads source from string. From the javadoc:

       /**
        * A file object used to represent source coming from a string.
        */
       public class JavaSourceFromString extends SimpleJavaFileObject {
           /**
            * The source code of this "file".
            */
           final String code;
    
           /**
            * Constructs a new JavaSourceFromString.
            * @param name the name of the compilation unit represented by this file object
            * @param code the source code for the compilation unit represented by this file object
            */
           JavaSourceFromString(String name, String code) {
               super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension),
                     Kind.SOURCE);
               this.code = code;
           }
    
           @Override
           public CharSequence getCharContent(boolean ignoreEncodingErrors) {
               return code;
           }
       }
    

    Once you have the output file (which is a compiled .class file), you can load it using URLClassLoader as follows:

        ClassLoader loader = new URLClassLoader(new URL[] {myClassFile.toURL());
        Class myClass = loader.loadClass("my.package.MyClass");
    

    and then instantiate it, using:

        myClass.newInstance();
    

    or using a Constructor.

    0 讨论(0)
  • 2020-12-18 16:39

    You can use Rhino and JavaScript in JDK 7. That might be a good way to do it.

    invokedynamic is coming....

    If you want to stick with Java, you need something to parse the source and turn it into byte code - something like cglib.

    0 讨论(0)
  • 2020-12-18 16:46

    At first you need to compile your code, for example using compiler API: ( http://www.accordess.com/wpblog/an-overview-of-java-compilation-api-jsr-199/, http://docs.oracle.com/javase/6/docs/api/javax/tools/package-summary.html). And after it load compiled class with ClassLoader ( http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ClassLoader.html )

    0 讨论(0)
  • 2020-12-18 16:47

    You could compile it using JavaCompiler but I suggest you to use Groovy for this run-time class creation. It would be much easier.

    0 讨论(0)
提交回复
热议问题