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

io.dinject.javalin.generator.BeanReader Maven / Gradle / Ivy

There is a newer version: 1.22
Show newest version
package io.dinject.javalin.generator;

import io.dinject.controller.Path;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import static io.dinject.javalin.generator.Constants.JAVALIN_ROLES;

/**
 * Reads the type information for the Controller (bean).
 */
class BeanReader {

  private final TypeElement beanType;

  private final List roles;

  private final List methods = new ArrayList<>();

  private final Set staticImportTypes = new TreeSet<>();

  private final Set importTypes = new TreeSet<>();

  BeanReader(TypeElement beanType) {
    this.beanType = beanType;
    this.roles = Util.findRoles(beanType);

    importTypes.add(Constants.GENERATED);
    importTypes.add(Constants.SINGLETON);
    importTypes.add(Constants.API_BUILDER);
    importTypes.add(Constants.WEB_ROUTES);
    importTypes.add(beanType.getQualifiedName().toString());
  }

  TypeElement getBeanType() {
    return beanType;
  }

  void read() {
    if (!roles.isEmpty()) {
      addStaticImportType(JAVALIN_ROLES);
      for (String role : roles) {
        addStaticImportType(role);
      }
    }

    for (Element element : beanType.getEnclosedElements()) {
      if (element.getKind() == ElementKind.METHOD) {
        readMethod(element);
      }
    }
  }


  private void readMethod(Element element) {

    ExecutableElement methodElement = (ExecutableElement) element;
    MethodReader methodReader = new MethodReader(this, methodElement);
    methodReader.read();
    methods.add(methodReader);
  }

  List getRoles() {
    return roles;
  }

  List getMethods() {
    return methods;
  }

  String getPath() {
    Path path = beanType.getAnnotation(Path.class);
    if (path == null) {
      return null;
    }
    return Util.trimTrailingSlash(path.value());
  }

  void addImportType(String rawType) {
    importTypes.add(rawType);
  }

  void addStaticImportType(String rawType) {
    staticImportTypes.add(rawType);
  }

  Set getStaticImportTypes() {
    return staticImportTypes;
  }

  Set getImportTypes() {
    return importTypes;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy