use of com.cjsoftware.processor.ucs.builder.ProxyQueueBuilder in project ucsFramework by cjsoftware-lib.
the class UcsContractProcessor method processElement.
@Override
public void processElement(TypeElement contractSpec) {
PackageElement packageElement = getProcessorModel().getElementUtils().getPackageOf(contractSpec);
ClassName generatedContractBrokerClassName = ClassName.get(packageElement.getQualifiedName().toString(), String.format(GENERATED_CONTRACT_BROKER_CLASS_NAME_TEMPLATE, sanitiseClassName(contractSpec.getSimpleName().toString())));
String qualifiedPackageName = packageElement.getQualifiedName().toString();
TypeElement uiContract = findDescendentOf(contractSpec, BaseUiContract.class);
ClassName uiProxyQueueClass = null;
if (uiContract != null) {
ProxyQueueBuilder uiProxyQueueBuilder = new ProxyQueueBuilder(getProcessorModel(), qualifiedPackageName, false, true);
uiProxyQueueClass = uiProxyQueueBuilder.buildClass(uiContract);
}
TypeElement coordinatorContract = findDescendentOf(contractSpec, BaseCoordinatorContract.class);
ClassName coordinatorProxyQueueClass = null;
if (coordinatorContract != null) {
ProxyQueueBuilder coordinatorProxyQueueBuilder = new ProxyQueueBuilder(getProcessorModel(), qualifiedPackageName, true, true);
coordinatorProxyQueueClass = coordinatorProxyQueueBuilder.buildClass(coordinatorContract);
}
TypeElement screenNavigationContract = findDescendentOf(contractSpec, BaseScreenNavigationContract.class);
ClassName screenNavigationProxyQueueClass = null;
if (screenNavigationContract != null) {
ProxyQueueBuilder screenNavigationProxyQueueBuilder = new ProxyQueueBuilder(getProcessorModel(), qualifiedPackageName, false, true);
screenNavigationProxyQueueClass = screenNavigationProxyQueueBuilder.buildClass(screenNavigationContract);
}
TypeElement stateManagerContract = findDescendentOf(contractSpec, BaseStateManagerContract.class);
ParameterizedTypeName contractBrokerSuperInterface = ParameterizedTypeName.get(ClassName.get(ContractBroker.class), ClassName.get(uiContract), ClassName.get(screenNavigationContract), ClassName.get(coordinatorContract), ClassName.get(stateManagerContract));
TypeSpec.Builder contractBrokerClass = TypeSpec.classBuilder(generatedContractBrokerClassName).addModifiers(Modifier.PUBLIC).addSuperinterface(contractBrokerSuperInterface);
contractBrokerClass.addField(TypeName.get(CoordinatorBinder.class), PROXY_BINDER_FIELD_NAME, Modifier.PRIVATE);
contractBrokerClass.addField(TypeName.get(coordinatorContract.asType()), COORDINATOR_FIELD_NAME, Modifier.PRIVATE);
contractBrokerClass.addField(TypeName.get(stateManagerContract.asType()), STATEMANAGER_FIELD_NAME, Modifier.PRIVATE);
contractBrokerClass.addField(coordinatorProxyQueueClass, COORDINATOR_PROXY_QUEUE_FIELD_NAME, Modifier.PRIVATE);
contractBrokerClass.addField(uiProxyQueueClass, UI_PROXY_QUEUE_FIELD_NAME, Modifier.PRIVATE);
contractBrokerClass.addField(screenNavigationProxyQueueClass, SCREEN_NAVIGATION_PROXY_QUEUE_FIELD_NAME, Modifier.PRIVATE);
TypeSpec coordinatorBinderProxy = buildBindingProxyClass(uiContract, screenNavigationContract, stateManagerContract);
contractBrokerClass.addType(coordinatorBinderProxy);
contractBrokerClass.addMethod(MethodSpec.constructorBuilder().addAnnotation(ClassName.get("javax.inject", "Inject")).addModifiers(Modifier.PUBLIC).addParameter(ParameterSpec.builder(ClassName.get(Executor.class), "uiExecutor").addAnnotation(AnnotationSpec.builder(ClassName.get("com.cjsoftware.library.platform.android.dagger", "MainLooper")).build()).build()).addParameter(ClassName.get(Executor.class), "backgroundExecutor").addParameter(TypeName.get(coordinatorContract.asType()), COORDINATOR_PARAM_NAME).addParameter(TypeName.get(stateManagerContract.asType()), STATEMANAGER_PARAM_NAME).addStatement("this.$N = $N", COORDINATOR_FIELD_NAME, COORDINATOR_PARAM_NAME).addStatement("this.$N = $N", STATEMANAGER_FIELD_NAME, STATEMANAGER_PARAM_NAME).addStatement("$N = new $T($N)", COORDINATOR_PROXY_QUEUE_FIELD_NAME, coordinatorProxyQueueClass, "backgroundExecutor").addStatement("$N.setProxiedInterface($N)", COORDINATOR_PROXY_QUEUE_FIELD_NAME, COORDINATOR_PARAM_NAME).addStatement("this.$N = new $T($N)", UI_PROXY_QUEUE_FIELD_NAME, uiProxyQueueClass, "uiExecutor").addStatement("this.$N = new $T($N)", SCREEN_NAVIGATION_PROXY_QUEUE_FIELD_NAME, screenNavigationProxyQueueClass, "uiExecutor").addStatement("(($T)this.$N).bindUi($N)", CoordinatorBinder.class, COORDINATOR_FIELD_NAME, UI_PROXY_QUEUE_FIELD_NAME).addStatement("(($T)this.$N).bindScreenNavigation(($T)$N)", CoordinatorBinder.class, COORDINATOR_FIELD_NAME, screenNavigationContract, SCREEN_NAVIGATION_PROXY_QUEUE_FIELD_NAME).addStatement("(($T)this.$N).bindStateManager($N)", CoordinatorBinder.class, COORDINATOR_FIELD_NAME, STATEMANAGER_FIELD_NAME).addStatement("this.$N = new $N()", PROXY_BINDER_FIELD_NAME, coordinatorBinderProxy.name).build());
contractBrokerClass.addMethod(MethodSpec.methodBuilder("getCoordinatorBinder").addAnnotation(Override.class).addAnnotation(getProcessorModel().getNonNullAnnotationClassName()).addModifiers(Modifier.PUBLIC).returns(TypeName.get(CoordinatorBinder.class)).addStatement("return $N", PROXY_BINDER_FIELD_NAME).build());
contractBrokerClass.addMethod(MethodSpec.methodBuilder("getCoordinator").addAnnotation(Override.class).addAnnotation(getProcessorModel().getNonNullAnnotationClassName()).addModifiers(Modifier.PUBLIC).returns(TypeName.get(coordinatorContract.asType())).addStatement("return $N", COORDINATOR_PROXY_QUEUE_FIELD_NAME).build());
contractBrokerClass.addMethod(MethodSpec.methodBuilder("getUi").addAnnotation(Override.class).addAnnotation(getProcessorModel().getNonNullAnnotationClassName()).addModifiers(Modifier.PUBLIC).returns(TypeName.get(uiContract.asType())).addStatement("return this.$N", UI_PROXY_QUEUE_FIELD_NAME).build());
contractBrokerClass.addMethod(MethodSpec.methodBuilder("getScreenNavigation").addAnnotation(Override.class).addAnnotation(getProcessorModel().getNonNullAnnotationClassName()).addModifiers(Modifier.PUBLIC).returns(TypeName.get(screenNavigationContract.asType())).addStatement("return this.$N", SCREEN_NAVIGATION_PROXY_QUEUE_FIELD_NAME).build());
contractBrokerClass.addMethod(MethodSpec.methodBuilder("getStateManager").addAnnotation(Override.class).addAnnotation(getProcessorModel().getNonNullAnnotationClassName()).addModifiers(Modifier.PUBLIC).returns(TypeName.get(stateManagerContract.asType())).addStatement("return this.$N", STATEMANAGER_FIELD_NAME).build());
try {
JavaFile javaFile = JavaFile.builder(packageElement.getQualifiedName().toString(), contractBrokerClass.build()).addFileComment("Generated Code, do not modify.").build();
javaFile.writeTo(getProcessorModel().getFiler());
} catch (IOException e) {
fatalErrorMessage("Exception writing java file: " + e.toString(), contractSpec);
}
}
Aggregations