use of com.app.annotation.apt.InstanceFactory in project T-MVP by north2016.
the class InstanceProcessor method process.
@Override
public void process(RoundEnvironment roundEnv, AnnotationProcessor mAbstractProcessor) {
String CLASS_NAME = "InstanceFactory";
TypeSpec.Builder tb = classBuilder(CLASS_NAME).addModifiers(PUBLIC, FINAL).addJavadoc("@ 实例化工厂 此类由apt自动生成");
MethodSpec.Builder methodBuilder1 = MethodSpec.methodBuilder("create").addAnnotation(MemoryCache.class).addJavadoc("@此方法由apt自动生成").returns(Object.class).addModifiers(PUBLIC, STATIC).addException(IllegalAccessException.class).addException(InstantiationException.class).addParameter(Class.class, "mClass");
List<ClassName> mList = new ArrayList<>();
CodeBlock.Builder blockBuilder = CodeBlock.builder();
// 括号开始
blockBuilder.beginControlFlow(" switch (mClass.getSimpleName())");
try {
for (TypeElement element : ElementFilter.typesIn(roundEnv.getElementsAnnotatedWith(InstanceFactory.class))) {
mAbstractProcessor.mMessager.printMessage(Diagnostic.Kind.NOTE, "正在处理: " + element.toString());
if (!Utils.isValidClass(mAbstractProcessor.mMessager, element))
return;
ClassName currentType = ClassName.get(element);
if (mList.contains(currentType))
continue;
mList.add(currentType);
// String className = null;
// try {
// Class<?> clazz = element.getAnnotation(InstanceFactory.class).value();
// className = clazz.getCanonicalName();
// } catch (MirroredTypeException mte) {
// DeclaredType classTypeMirror = (DeclaredType) mte.getTypeMirror();
// TypeElement classTypeElement = (TypeElement) classTypeMirror.asElement();
// className = classTypeElement.getQualifiedName().toString();
// } catch (Exception e) {
// }
// if (className != null && !className.equals(InstanceFactory.class.getName())) {
// blockBuilder.addStatement("case $S: return new $T()", currentType.simpleName(), Utils.getType(className));//初始化Repository
// } else {
// 初始化Presenter
blockBuilder.addStatement("case $S: return new $T()", currentType.simpleName(), currentType);
// }
}
blockBuilder.addStatement("default: return mClass.newInstance()");
blockBuilder.endControlFlow();
methodBuilder1.addCode(blockBuilder.build());
tb.addMethod(methodBuilder1.build());
// 生成源代码
JavaFile javaFile = JavaFile.builder(Utils.PackageName, tb.build()).build();
// 在 app module/build/generated/source/apt 生成一份源代码
javaFile.writeTo(mAbstractProcessor.mFiler);
} catch (FilerException e) {
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations