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

io.ebean.querybean.generator.FindDbName Maven / Gradle / Ivy

There is a newer version: 15.8.0
Show newest version
package io.ebean.querybean.generator;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;
import java.util.List;
import java.util.Map;
import java.util.Set;

class FindDbName {

  /**
   * Return the value of the DbName annotation or null if it isn't found on the element.
   */
  static String value(TypeElement element, Types typeUtils) {

    AnnotationMirror mirror = findDbNameMirror(element);
    if (mirror != null) {
      return readDbNameValue(mirror);
    }
    final TypeMirror typeMirror = element.getSuperclass();
    if (typeMirror.getKind() == TypeKind.NONE) {
      return null;
    }
    final TypeElement element1 = (TypeElement)typeUtils.asElement(typeMirror);
    return value(element1, typeUtils);
  }

  private static String readDbNameValue(AnnotationMirror mirror) {

    final Map elementValues = mirror.getElementValues();
    final Set> entries = elementValues.entrySet();
    for (Map.Entry entry : entries) {
      if ("value".equals(entry.getKey().getSimpleName().toString())) {
        return (String) entry.getValue().getValue();
      }
    }
    return null;
  }

  private static AnnotationMirror findDbNameMirror(TypeElement element) {
    final List mirrors = element.getAnnotationMirrors();
    for (AnnotationMirror mirror : mirrors) {
      final String name = mirror.getAnnotationType().asElement().toString();
      if (Constants.DBNAME.equals(name)) {
        return mirror;
      }
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy