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

toothpick.compiler.common.generators.CodeGenerator Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
package toothpick.compiler.common.generators;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import toothpick.compiler.common.generators.targets.ParamInjectionTarget;

/**
 * Common base interface for all code generators.
 */
public abstract class CodeGenerator {

  protected static final String LINE_SEPARATOR = System.getProperty("line.separator");

  /**
   * Creates all java code.
   *
   * @returns a string containing the java code generated by this {@link CodeGenerator}.
   */
  public abstract String brewJava();

  public CodeBlock getInvokeScopeGetMethodWithNameCodeBlock(ParamInjectionTarget memberInjectionTarget) {
    final String scopeGetMethodName;
    final String injectionName;
    if (memberInjectionTarget.name == null) {
      injectionName = "";
    } else {
      injectionName = ", \"" + memberInjectionTarget.name.toString() + "\"";
    }
    final ClassName className;
    switch (memberInjectionTarget.kind) {
      case INSTANCE:
        scopeGetMethodName = "getInstance";
        className = ClassName.get(memberInjectionTarget.memberClass);
        break;
      case PROVIDER:
        scopeGetMethodName = "getProvider";
        className = ClassName.get(memberInjectionTarget.kindParamClass);
        break;
      case LAZY:
        scopeGetMethodName = "getLazy";
        className = ClassName.get(memberInjectionTarget.kindParamClass);
        break;
      default:
        throw new IllegalStateException("The kind can't be null.");
    }
    return CodeBlock.builder().add("$L($T.class$L)", scopeGetMethodName, className, injectionName).build();
  }

  /**
   * @returns the FQN of the code generated by this {@link CodeGenerator}.
   */
  public abstract String getFqcn();

  public static String getGeneratedFQNClassName(TypeElement typeElement) {
    return getGeneratedPackageName(typeElement) + "." + getGeneratedSimpleClassName(typeElement);
  }

  public static String getGeneratedSimpleClassName(TypeElement typeElement) {
    String result = typeElement.getSimpleName().toString();
    //deals with inner classes
    while (typeElement.getEnclosingElement().getKind() != ElementKind.PACKAGE) {
      result = typeElement.getEnclosingElement().getSimpleName().toString() + "$" + result;
      typeElement = (TypeElement) typeElement.getEnclosingElement();
    }
    return result;
  }

  public static String getSimpleClassName(ClassName className) {
    String result = "";
    java.util.List simpleNames = className.simpleNames();
    for (int i = 0; i < simpleNames.size(); i++) {
      String name = simpleNames.get(i);
      result += name;
      if (i != simpleNames.size() - 1) {
        result += ".";
      }
    }
    return result;
  }

  public static String getGeneratedPackageName(TypeElement typeElement) {
    //deals with inner classes
    while (typeElement.getEnclosingElement().getKind() != ElementKind.PACKAGE) {
      typeElement = (TypeElement) typeElement.getEnclosingElement();
    }
    return typeElement.getEnclosingElement().toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy