package io.vertx.codegen;
import io.vertx.codegen.annotations.ModuleGen;
import io.vertx.codegen.type.TypeNameTranslator;
import javax.lang.model.element.PackageElement;
import javax.lang.model.util.Elements;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Set;
import java.util.function.BiFunction;
public class ModuleInfo {
private final String packageName;
private final String name;
private final String groupPackage;
public ModuleInfo(String packageName, String name, String groupPackage) {
this.packageName = packageName;
this.name = name;
this.groupPackage = groupPackage;
}
private static final BiFunction<Elements, String, Set<PackageElement>> getPackageElementJava8 = (elts, fqn) -> {
PackageElement elt = elts.getPackageElement(fqn);
return elt != null ? Collections.singleton(elt) : Collections.emptySet();
};
private static final BiFunction<Elements, String, Set<PackageElement>> getPackageElement;
static {
BiFunction<Elements, String, Set<PackageElement>> result = getPackageElementJava8;
try {
Method method = Elements.class.getDeclaredMethod("getAllPackageElements", CharSequence.class);
result = (elts, fqn) -> {
try {
return (Set<PackageElement>) method.invoke(elts, fqn);
} catch (Exception e) {
return getPackageElementJava8.apply(elts, fqn);
}
};
} catch (NoSuchMethodException e) {
}
getPackageElement = result;
}
public static ModuleInfo resolve(Elements elementUtils, PackageElement pkgElt) {
String pkgQN = pkgElt.getQualifiedName().toString();
while (true) {
if (pkgElt != null) {
ModuleGen annotation = pkgElt.getAnnotation(ModuleGen.class);
if (annotation != null) {
return new ModuleInfo(pkgElt.getQualifiedName().toString(), annotation.name(), annotation.groupPackage());
}
}
int pos = pkgQN.lastIndexOf('.');
if (pos == -1) {
break;
} else {
pkgQN = pkgQN.substring(0, pos);
Set<PackageElement> pkgElts = getPackageElement.apply(elementUtils, pkgQN);
pkgElt = pkgElts.isEmpty() ? null : pkgElts.iterator().next();
}
}
return null;
}
public String getGroupPackage() {
return groupPackage;
}
public String getPackageName() {
return packageName;
}
public String translatePackageName(String lang) {
return translateQualifiedName(packageName, lang);
}
public String translateQualifiedName(String qualifiedName, String lang) {
return TypeNameTranslator.hierarchical(lang).translate(this, qualifiedName);
}
public String getName() {
return name;
}
public String getName(Case _case) {
return _case.format(Case.KEBAB.parse(name));
}
}