java——JavaPoet
Posted gaoquanquan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java——JavaPoet相关的知识,希望对你有一定的参考价值。
public static void main(String [] args){ ClassName activity = ClassName.get("android.app", "Activity"); TypeSpec.Builder mainActivityBuilder = TypeSpec.classBuilder("MainActivity") .addModifiers(Modifier.PUBLIC) .superclass(activity); ClassName override = ClassName.get("java.lang", "Override"); ClassName bundle = ClassName.get("android.os", "Bundle"); ClassName nullable = ClassName.get("android.support.annotation", "Nullable"); ParameterSpec savedInstanceState = ParameterSpec.builder(bundle, "savedInstanceState") .addAnnotation(nullable) .build(); MethodSpec onCreate = MethodSpec.methodBuilder("onCreate") .addAnnotation(override) .addModifiers(Modifier.PROTECTED) .addParameter(savedInstanceState) .addStatement("super.onCreate(savedInstanceState)") .addStatement("setContentView(R.layout.activity_main)") .build(); MethodSpec hello = MethodSpec.methodBuilder("hello") .addModifiers(Modifier.PUBLIC) .addStatement("System.out.println("hello world")") .build(); TypeSpec mainActivity = mainActivityBuilder.addMethod(onCreate) .addMethod(hello) .build(); JavaFile file = JavaFile.builder("com.test", mainActivity).build(); File f = null; try { file.writeTo(System.out); f = new File("C:\JavaWorkspace\annotationTest\poetTest"); file.writeTo(f); } catch (IOException e) { e.printStackTrace(); } }
很神奇,还不太会用,只能照葫芦画瓢
https://blog.csdn.net/l540675759/article/details/82931785
以上是关于java——JavaPoet的主要内容,如果未能解决你的问题,请参考以下文章
java 注解处理器(AbstractProcessor) 获取到 指定注解的属性值 javapoet 如何使用这个值生成类?