1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| public class TestJavaPoet { public static void main(String[] args) { TypeSpec clazz = clazz(builtinTypeField(), arrayTypeField(), refTypeField(), typeField(), parameterizedTypeField(), wildcardTypeField(), constructor(), method(code())); JavaFile javaFile = JavaFile.builder("com.huangyuanlove.view_inject_compiler.test", clazz).build();
System.out.println(javaFile.toString()); }
private static TypeSpec clazz(FieldSpec builtinTypeField, FieldSpec arrayTypeField, FieldSpec refTypeField, FieldSpec typeField, FieldSpec parameterizedTypeField, FieldSpec wildcardTypeField, MethodSpec constructor, MethodSpec methodSpec) { return TypeSpec.classBuilder("Clazz") .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) .addTypeVariable(TypeVariableName.get("T"))
.superclass(String.class) .addSuperinterface(Serializable.class) .addSuperinterface(ParameterizedTypeName.get(Comparable.class, String.class)) .addSuperinterface(ParameterizedTypeName.get(ClassName.get(Map.class), TypeVariableName.get("T"), WildcardTypeName.subtypeOf(String.class)))
.addStaticBlock(CodeBlock.builder().build()) .addInitializerBlock(CodeBlock.builder().build())
.addField(builtinTypeField) .addField(arrayTypeField) .addField(refTypeField) .addField(typeField) .addField(parameterizedTypeField) .addField(wildcardTypeField)
.addMethod(constructor) .addMethod(methodSpec)
.addType(TypeSpec.classBuilder("InnerClass").build())
.build(); }
private static FieldSpec builtinTypeField() { return FieldSpec.builder(int.class, "mInt", Modifier.PRIVATE).build(); }
private static FieldSpec arrayTypeField() { return FieldSpec.builder(int[].class, "mArr", Modifier.PRIVATE).build(); }
private static FieldSpec refTypeField() { return FieldSpec.builder(File.class, "mRef", Modifier.PRIVATE).build(); }
private static FieldSpec typeField() { return FieldSpec.builder(TypeVariableName.get("T"), "mT", Modifier.PRIVATE).build(); }
private static FieldSpec parameterizedTypeField() { return FieldSpec.builder(ParameterizedTypeName.get(List.class, String.class), "mParameterizedField", Modifier.PRIVATE) .build(); }
private static FieldSpec wildcardTypeField() { return FieldSpec.builder(ParameterizedTypeName.get(ClassName.get(List.class), WildcardTypeName.subtypeOf(String.class)), "mWildcardField", Modifier.PRIVATE) .build(); }
private static MethodSpec constructor() { return MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC) .build(); }
private static MethodSpec method(CodeBlock codeBlock) { return MethodSpec.methodBuilder("method") .addAnnotation(Override.class) .addTypeVariable(TypeVariableName.get("T")) .addModifiers(Modifier.PUBLIC) .returns(int.class) .addParameter(String.class, "string") .addParameter(TypeVariableName.get("T"), "t") .addParameter(ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(Integer.class), WildcardTypeName.subtypeOf(TypeVariableName.get("T"))), "map") .addException(IOException.class) .addException(RuntimeException.class) .addCode(codeBlock) .build(); }
private static CodeBlock code() { return CodeBlock.builder() .addStatement("int foo = 1") .addStatement("$T bar = $S", String.class, "a string")
.addStatement("$T obj = new $T(5)", Object.class, ParameterizedTypeName.get(ClassName.get(HashMap.class), ClassName.get(Integer.class), WildcardTypeName.subtypeOf(TypeVariableName.get("T"))))
.addStatement("baz($L)", TypeSpec.anonymousClassBuilder("$T param", String.class) .superclass(Runnable.class) .addMethod(MethodSpec.methodBuilder("run") .addAnnotation(Override.class) .returns(TypeName.VOID) .build()) .build())
.beginControlFlow("for (int i = 0; i < 5; i++)") .endControlFlow()
.beginControlFlow("while (false)") .endControlFlow()
.beginControlFlow("do") .endControlFlow("while (false)")
.beginControlFlow("if (false)") .nextControlFlow("else if (false)") .nextControlFlow("else") .endControlFlow()
.beginControlFlow("try") .nextControlFlow("catch ($T e)", Exception.class) .addStatement("e.printStackTrace()") .nextControlFlow("finally") .endControlFlow()
.addStatement("return 0") .build(); }
|