Created
June 16, 2019 20:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to see a usage example:
using JavaPoetAdhoc