Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save edefazio/d53c1ed91ff0eb9e3939a7acd7f76dcd to your computer and use it in GitHub Desktop.
Save edefazio/d53c1ed91ff0eb9e3939a7acd7f76dcd to your computer and use it in GitHub Desktop.
Integrate JavaPoet with AdHoc to make compiling loading and using dynamic JavaPoet models easy
package com.squareup.javapoet.adhoc;
import com.squareup.javapoet.JavaFile;
import run.adhoc.*;
/**
* Adapts JavaPoet's {@link JavaFile} code model to the {@link javax.tools.JavaCompiler}
* infrastructure through Adhocs {@link CodeModelFile}. We want to reduce the
* friction for compiling / loading and using the Java source code models
*
* @author Eric
*/
public class JavaPoetAdhoc implements AdhocFactory<JavaFile>{
public static final JavaPoetAdhoc INSTANCE = new JavaPoetAdhoc();
@Override
public CodeModelFile<JavaFile> file(JavaFile javaPoetModel) {
return new CodeModelFile<>( javaPoetModel,
(JavaFile jf)->{
if( jf.packageName != null && jf.packageName.length() > 0){
return jf.packageName + "." + jf.typeSpec.name;
}
return jf.typeSpec.name;
},
jf-> jf.toString(),
jf->0L);
}
}
@edefazio
Copy link
Author

to see a usage example:
using JavaPoetAdhoc

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