Adding new features to the application using on-the-fly generation

Can I subclass at runtime or at runtime? If so, how is this achieved and what precautions should be taken to prevent the rogue from harming the hack inside the application?

Edit: Name changed from "Class Generation on the fly."

+3
source share
4 answers
+1

,

"javax.tools"

.

    String rogue = "package abc; "+
                 "public class RogueObjectWreakingHavoc extends SomeImportantClass {"+
                 " { System.exit(-1) }"+
                 "}"
    File sourceFile = new File("RogueObjectWreakingHavoc.java");
    FileWriter fileWriter = new FileWriter(sourceFile);
    fileWriter.write(rogue);
    fileWriter.close();

    List<File> files = new ArrayList<File>();
    files.add(sourceFile);

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null,
                                                                          null,
                                                                          null);

    compiler.getTask(null, 
                     fileManager,
                     null,
                     null,
                     null,
                     fileManager.getJavaFileObjectsFromFiles(files))
    .call();
    fileManager.close();

   // load using URLClassLoader...

, , , , , ( , , )

( , Java) " Java"

+1

For other developers who want to add the ability to expand their software, another approach for the behavior of the plugin is the forName()class method (used, for example, JDBC) and Dynamic Class Loading.

http://mindprod.com/jgloss/classforname.html

0
source

Source: https://habr.com/ru/post/1740380/


All Articles