Skip to content

Instantly share code, notes, and snippets.

@edefazio
Last active January 22, 2017 00:45
Show Gist options
  • Save edefazio/b491989cd6ef72ad7ea2bc0005895c81 to your computer and use it in GitHub Desktop.
Save edefazio/b491989cd6ef72ad7ea2bc0005895c81 to your computer and use it in GitHub Desktop.
example varcode building a class model
package usecase.model;
import java.io.Serializable;
import varcode.java.Java;
import varcode.java.adhoc.Export;
import varcode.java.model._class;
public class ModelClass
{
static _class _ModelOfClass = _class.of(
"package usecase.model;",
"import java.util.UUID;",
"import java.io.Serializable",
"/** Class Javadoc comment */",
"@Deprecated",
"public class ModelOfClass<K,V> implements Serializable" )
.staticBlock( "System.out.println( \"In Static Block\" );" )
.field( "private static final long serialVersionUID = 42L;" )
.property(
"/** Field JavaDoc */",
"@Deprecated",
"public final int memberField;" )
.method(
"/** Method Javadoc*/",
"@Deprecated",
"public String getId( String prefix )",
"return prefix + UUID.randomUUID().toString();" )
.mainMethod( "System.out.println( \"Hello World\");" )
.constructor(
"/** constructor Javadoc */",
"@Deprecated",
"public ModelOfClass( int fieldValue )",
"this.memberField = fieldValue;" );
public static void main( String[] args )
{
Export.dir("C:\\MyApp\\src\\main\\java\\").toFile(_ModelOfClass );
//create an instance, pass 100 in as argument
Object instance = _ModelOfClass.instance( 100 );
Java.callMain( instance ); //prints "Hello World"
Java.call( instance, "getId", "somePrefix" );
//export the compiled "usecase.model.ModelOfClass.class" file
Export.dir( "C:\\MyApp\\target\\classes\\" ).toFile( instance.getClass() );
}
}
@edefazio
Copy link
Author

contents of the exported file : "C:/MyApp/src/main/java/usecase/ModelOfClass.java"

package usecase.model;

import java.io.Serializable;
import java.util.UUID;;


/**
 *  Class Javadoc comment 
 */
@Deprecated
public class ModelOfClass<K, V> 
    implements Serializable
{
    private static final long serialVersionUID = 42L;
    /**
     *  Field JavaDoc 
     */
    @Deprecated
    public final int memberField;
    /**
     *  constructor Javadoc 
     */
    @Deprecated
    public ModelOfClass( int fieldValue )
    {
        this.memberField = fieldValue;
    }

    public static void main( String[] args )
    {
        System.out.println( "Hello World");
    }
    public int getMemberField(  )
    {
        return this.memberField;
    }
    /**
     *  Method Javadoc
     */
    @Deprecated
    public String getId( String prefix )
    {
        return prefix + UUID.randomUUID().toString();
    }
    static
    {
        System.out.println( "In Static Block" );
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment