Skip to content

tankist88/object2source

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

95 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

object2source

Build Status Codecov Codacy Badge Maven Central

A library for generating the source code that creates an instance of the object that is submitted to the input. This means that using this library, you can reverse engineer objects from the JVM memory. The object2source can generate data provider methods for a variety of standard Java types. If you want to generate a method to get an instance of an arbitrary custom class, you can use the extension system. Now you can use extensions to generate instances of classes from the libraries of Google guava and YodaTime.

Links to the repository with extensions:

  1. https://github.com/tankist88/guava-extension
  2. https://github.com/tankist88/jodatime-extension

Example of usage

TestObj testObj = new TestObj();
ProviderResult pr = (new SourceGenerator()).createDataProviderMethod(testObj);

Print result:

for (ProviderInfo pi : pr.getProviders()) {
    System.out.println(pi.getMethodBody());
}

Example output:

private static TestObj getTestObj_240650537() {
    TestObj testObj = new TestObj();
    testObj.setNum(0);
    testObj.setCh(' ');
    testObj.setArr(getArray_483422889());
    testObj.setTestObjArr(getArray_1277181601());
    return testObj;
}

private static int[] getArray_483422889() {
    int[] array = new int[10];
    array[0] = 0;
    array[1] = 0;
    array[2] = 0;
    array[3] = 0;
    array[4] = 0;
    array[5] = 0;
    array[6] = 0;
    array[7] = 0;
    array[8] = 0;
    array[9] = 0;
    return array;
}

private static TestObj[] getArray_1277181601() {
    TestObj[] array = new TestObj[10];
    array[0] = null;
    array[1] = null;
    array[2] = null;
    array[3] = null;
    array[4] = null;
    array[5] = null;
    array[6] = null;
    array[7] = null;
    array[8] = null;
    array[9] = null;
    return array;
}

Register extensions

SourceGenerator sg = new SourceGenerator();
Extension ext = (Extension) Class.forName("org.example.MyCustomTypeExtension").newInstance();
sg.registerExtension(ext);

Create custom extension

To create custom extension you need create class that implements Extension interface.

Installation

mvn clean install

Contacts

About

Converting java objects to source code that create them

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages