All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.growingio.sdk.annotation.compiler.AppModuleGenerator Maven / Gradle / Ivy

There is a newer version: 4.3.1
Show newest version
/*
 * Copyright (C) 2020 Beijing Yishu Technology Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.growingio.sdk.annotation.compiler;

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeSpec;

import java.util.Set;

import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;

import static com.growingio.sdk.annotation.compiler.ProcessUtils.GENERATED_APP_MODULE_IMPL_SIMPLE_NAME;
import static com.growingio.sdk.annotation.compiler.ProcessUtils.GENERATED_ROOT_MODULE_SIMPLE_NAME;
import static com.growingio.sdk.annotation.compiler.ProcessUtils.GIO_LOG_TAG;
import static com.growingio.sdk.annotation.compiler.ProcessUtils.GIO_TRACKER_REGISTRY_NAME;
import static com.growingio.sdk.annotation.compiler.ProcessUtils.GIO_TRACKER_REGISTRY_PACKAGE_NAME;
import static com.growingio.sdk.annotation.compiler.ProcessUtils.GROWINGIO_MODULE_PACKAGE_NAME;


/**
 * 

The output file generated by this class with a AppModule looks like this: * *

 * 
 *  {@literal @SuppressWarnings("deprecation")}
 * final class GeneratedGioModuleImpl extends GeneratedGioModule {
 *  private final GrowingAppModule appModule;
 *
 *  public GeneratedGioModuleImpl(Context context) {
 *      Log.d("GIO", "Discovered GIOModule from annotation: com.growingio.android.okhttp3.OkhttpLibraryGioModule");
 *      appModule = new GrowingAppModule();
 *  }
 *
 *  {@literal @Override}
 *  public void registerComponents(Context context, TrackerRegistry registry) {
 *      new OkhttpLibraryGioModule().registerComponents(context,registry);
 *      appModule.registerComponents(context,registry);
 *  }
 * }
 * 
 * 
* * @author cpacm 4/29/21 */ final class AppModuleGenerator { private final ProcessUtils processUtils; AppModuleGenerator(ProcessUtils processUtils) { this.processUtils = processUtils; } void generate(TypeElement appModule, Set gioModules) { ClassName appModuleClassName = ClassName.get(appModule); // constructor MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC) .addParameter( ParameterSpec.builder(ClassName.get("android.content", "Context"), "context") .build()); ClassName androidLogName = ClassName.get("android.util", "Log"); for (String moduleName : gioModules) { constructorBuilder.addStatement( "$T.d($S, $S)", androidLogName, GIO_LOG_TAG, "Discovered GIOModule from annotation: " + moduleName); } constructorBuilder.addStatement("appModule = new $T()", appModule); MethodSpec constructor = constructorBuilder.build(); //register component MethodSpec.Builder registerComponents = MethodSpec.methodBuilder("registerComponents") .addModifiers(Modifier.PUBLIC) .addAnnotation(Override.class) .addParameter( ParameterSpec.builder(ClassName.get("android.content", "Context"), "context") //.addAnnotation(processorUtil.nonNull()) .build()) .addParameter( ParameterSpec.builder(ClassName.get(GIO_TRACKER_REGISTRY_PACKAGE_NAME, GIO_TRACKER_REGISTRY_NAME), "registry") .build()); for (String module : gioModules) { ClassName moduleClassName = ClassName.bestGuess(module); registerComponents.addStatement( "new $T().registerComponents(context,registry)", moduleClassName); } registerComponents.addStatement("appModule.registerComponents(context,registry)"); MethodSpec registerMethod = registerComponents.build(); TypeSpec.Builder builder = TypeSpec.classBuilder(GENERATED_APP_MODULE_IMPL_SIMPLE_NAME) .addModifiers(Modifier.FINAL) .addAnnotation( AnnotationSpec.builder(SuppressWarnings.class) .addMember("value", "$S", "deprecation") .build()) .superclass( ClassName.get( GROWINGIO_MODULE_PACKAGE_NAME, GENERATED_ROOT_MODULE_SIMPLE_NAME)) .addField(appModuleClassName, "appModule", Modifier.PRIVATE, Modifier.FINAL) .addMethod(constructor) .addMethod(registerMethod); TypeSpec generatedGIOModule = builder.build(); writeGioModule(generatedGIOModule); } private void writeGioModule(TypeSpec appModule) { processUtils.writeClass(GROWINGIO_MODULE_PACKAGE_NAME, appModule); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy