Last active
February 5, 2017 02:06
-
-
Save igor-brishkoski/a2ce90b586439946782a14688b32b9ce to your computer and use it in GitHub Desktop.
Code Generation
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
private void writeSourceFile(TypeElement originatingType) { | |
//get Log class from android.util package | |
//This will make sure the Log class is properly imported into our class | |
ClassName logClassName = ClassName.get("android.util", "Log"); | |
//get the current annotated class name | |
TypeVariableName typeVariableName = TypeVariableName.get(originatingType.getSimpleName().toString()); | |
//create static void method named log | |
MethodSpec log = MethodSpec.methodBuilder(METHOD_LOG) | |
.addModifiers(Modifier.PUBLIC, Modifier.STATIC) | |
.returns(void.class) | |
//Parameter variable based on the annotated class | |
.addParameter(typeVariableName, KEY_PARAM_NAME) | |
//add a Lod.d("ClassName", String.format(class fields)); | |
.addStatement("$T.d($S, $L)", logClassName, originatingType.getSimpleName().toString(), generateFormater(originatingType)) | |
.build(); | |
//create a class to wrap our method | |
//the class name will be the annotated class name + _Log | |
TypeSpec loggerClass = TypeSpec.classBuilder(originatingType.getSimpleName().toString() + CLASS_SUFFIX) | |
.addModifiers(Modifier.PUBLIC, Modifier.FINAL) | |
//add the log statetemnt from above | |
.addMethod(log) | |
.build(); | |
//create the file | |
JavaFile javaFile = JavaFile.builder(originatingType.getEnclosingElement().toString(), loggerClass) | |
.build(); | |
try { | |
javaFile.writeTo(filer); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment