use of io.quarkus.qute.NamespaceResolver in project quarkus by quarkusio.
the class ValueResolverGenerator method generate.
private void generate(DotName className, int priority) {
ClassInfo clazz = nameToClass.get(className);
String clazzName = className.toString();
boolean ignoreSuperclasses = false;
String namespace = null;
AnnotationInstance templateData = nameToTemplateData.get(className);
if (templateData == null) {
// @TemplateData declared on the class
for (AnnotationInstance annotation : clazz.classAnnotations()) {
if (annotation.name().equals(TEMPLATE_DATA)) {
AnnotationValue targetValue = annotation.value(TARGET);
if (targetValue == null || targetValue.asClass().name().equals(className)) {
templateData = annotation;
}
}
}
}
if (templateData != null) {
AnnotationValue ignoreSuperclassesValue = templateData.value(IGNORE_SUPERCLASSES);
if (ignoreSuperclassesValue != null) {
ignoreSuperclasses = ignoreSuperclassesValue.asBoolean();
}
AnnotationValue namespaceValue = templateData.value(NAMESPACE);
if (namespaceValue != null) {
namespace = namespaceValue.asString().trim();
} else {
namespace = TemplateData.UNDERSCORED_FQCN;
}
if (namespace.isBlank()) {
namespace = null;
}
if (namespace != null) {
if (namespace.equals(TemplateData.UNDERSCORED_FQCN)) {
namespace = underscoredFullyQualifiedName(clazzName);
} else if (namespace.equals(TemplateData.SIMPLENAME)) {
namespace = simpleName(clazz);
}
}
}
Predicate<AnnotationTarget> filters = initFilters(templateData);
LOGGER.debugf("Analyzing %s", clazzName);
String baseName;
if (clazz.enclosingClass() != null) {
baseName = simpleName(clazz.enclosingClass()) + NESTED_SEPARATOR + simpleName(clazz);
} else {
baseName = simpleName(clazz);
}
String targetPackage = packageName(clazz.name());
String generatedName = generatedNameFromTarget(targetPackage, baseName, SUFFIX);
ClassCreator valueResolver = ClassCreator.builder().classOutput(classOutput).className(generatedName).interfaces(ValueResolver.class).build();
implementGetPriority(valueResolver, priority);
implementAppliesTo(valueResolver, clazz);
boolean hasMembers = implementResolve(valueResolver, clazzName, clazz, filters.and(not(ValueResolverGenerator::staticsFilter)), ignoreSuperclasses);
if (hasMembers) {
// Do not generate the resolver if no relevant members are found
valueResolver.close();
generatedTypes.add(generatedName.replace('/', '.'));
}
if (namespace != null) {
// Generate a namespace resolver to access static members
generatedName = generatedNameFromTarget(targetPackage, baseName, NAMESPACE_SUFFIX);
ClassCreator namespaceResolver = ClassCreator.builder().classOutput(classOutput).className(generatedName).interfaces(NamespaceResolver.class).build();
implementGetNamespace(namespaceResolver, namespace);
boolean hasStatics = implementNamespaceResolve(namespaceResolver, clazzName, clazz, filters.and(ValueResolverGenerator::staticsFilter));
if (hasStatics) {
// Do not generate the resolver if no statics are found
namespaceResolver.close();
generatedTypes.add(generatedName.replace('/', '.'));
}
}
}
use of io.quarkus.qute.NamespaceResolver in project quarkus by quarkusio.
the class MessageBundles method setupNamespaceResolvers.
static void setupNamespaceResolvers(@Observes EngineBuilder builder, BundleContext context) {
// Avoid injecting "Instance<Object> instance" which prevents unused beans removal
ArcContainer container = Arc.container();
// For every bundle register a new resolver
for (Entry<String, Map<String, Class<?>>> entry : context.getBundleInterfaces().entrySet()) {
final String bundle = entry.getKey();
final Map<String, Resolver> interfaces = new HashMap<>();
Resolver resolver = null;
for (Entry<String, Class<?>> locEntry : entry.getValue().entrySet()) {
if (locEntry.getKey().equals(DEFAULT_LOCALE)) {
resolver = (Resolver) container.select(locEntry.getValue(), Default.Literal.INSTANCE).get();
continue;
}
Instance<?> found = container.select(locEntry.getValue(), new Localized.Literal(locEntry.getKey()));
if (found.isUnsatisfied()) {
throw new IllegalStateException(Qute.fmt("Bean not found for localized interface [{e.value}] and locale [{e.key}]").data("e", locEntry).render());
}
if (found.isAmbiguous()) {
throw new IllegalStateException(Qute.fmt("Multiple beans found for localized interface [{e.value}] and locale [{e.key}]").data("e", locEntry).render());
}
interfaces.put(locEntry.getKey(), (Resolver) found.get());
}
final Resolver defaultResolver = resolver;
builder.addNamespaceResolver(new NamespaceResolver() {
@Override
public CompletionStage<Object> resolve(EvalContext context) {
Object locale = context.getAttribute(ATTRIBUTE_LOCALE);
if (locale == null) {
Object selectedVariant = context.getAttribute(TemplateInstance.SELECTED_VARIANT);
if (selectedVariant != null) {
locale = ((Variant) selectedVariant).getLocale();
}
if (locale == null) {
return defaultResolver.resolve(context);
}
}
// First try the exact match
Resolver localeResolver = interfaces.get(locale instanceof Locale ? ((Locale) locale).toLanguageTag() : locale.toString());
if (localeResolver == null && locale instanceof Locale) {
// Next try the language
localeResolver = interfaces.get(((Locale) locale).getLanguage());
}
return localeResolver != null ? localeResolver.resolve(context) : defaultResolver.resolve(context);
}
@Override
public String getNamespace() {
return bundle;
}
});
}
}
Aggregations