标签:int addm ref oncreate creat activity world details div
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
标签:int addm ref oncreate creat activity world details div
原文地址:https://www.cnblogs.com/gaoquanquan/p/10294401.html