Search in sources :

Example 1 with NamespaceResolver

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('/', '.'));
        }
    }
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) ValueResolver(io.quarkus.qute.ValueResolver) AnnotationValue(org.jboss.jandex.AnnotationValue) NamespaceResolver(io.quarkus.qute.NamespaceResolver) ClassCreator(io.quarkus.gizmo.ClassCreator) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo)

Example 2 with NamespaceResolver

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;
            }
        });
    }
}
Also used : Locale(java.util.Locale) ArcContainer(io.quarkus.arc.ArcContainer) Resolver(io.quarkus.qute.Resolver) NamespaceResolver(io.quarkus.qute.NamespaceResolver) HashMap(java.util.HashMap) EvalContext(io.quarkus.qute.EvalContext) Variant(io.quarkus.qute.Variant) NamespaceResolver(io.quarkus.qute.NamespaceResolver) HashMap(java.util.HashMap) Map(java.util.Map) CompletionStage(java.util.concurrent.CompletionStage)

Aggregations

NamespaceResolver (io.quarkus.qute.NamespaceResolver)2 ArcContainer (io.quarkus.arc.ArcContainer)1 ClassCreator (io.quarkus.gizmo.ClassCreator)1 EvalContext (io.quarkus.qute.EvalContext)1 Resolver (io.quarkus.qute.Resolver)1 ValueResolver (io.quarkus.qute.ValueResolver)1 Variant (io.quarkus.qute.Variant)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Map (java.util.Map)1 CompletionStage (java.util.concurrent.CompletionStage)1 AnnotationInstance (org.jboss.jandex.AnnotationInstance)1 AnnotationTarget (org.jboss.jandex.AnnotationTarget)1 AnnotationValue (org.jboss.jandex.AnnotationValue)1 ClassInfo (org.jboss.jandex.ClassInfo)1