Content of file ViewBuilder.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>ViewBuilder.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">NorthernWind :: Core :: Default Implementation</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.impl.ui</a> &gt; <span class="el_source">ViewBuilder.java</span></div><h1>ViewBuilder.java</h1><pre class="source lang-java linenums">/*
 * #%L
 * *********************************************************************************************************************
 *
 * NorthernWind - lightweight CMS
 * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
 * %%
 * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
 * %%
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * *********************************************************************************************************************
 *
 *
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.northernwind.frontend.impl.ui;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Configurable;
import it.tidalwave.util.Id;
import it.tidalwave.northernwind.core.model.HttpStatusException;
import it.tidalwave.northernwind.core.model.Site;
import it.tidalwave.northernwind.core.model.SiteNode;
import it.tidalwave.northernwind.core.model.SiteProvider;
import it.tidalwave.northernwind.frontend.ui.ViewController;
import it.tidalwave.northernwind.frontend.ui.ViewFactory.ViewAndController;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * A builder which creates a View - ViewController pair.
 *
 * @stereotype  Factory
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L57">@Configurable @Slf4j @ToString(exclude = &quot;beanFactory&quot;)</span>
/* package */ class ViewBuilder
  {
    @Inject
    private BeanFactory beanFactory;

    @Nonnull
    /* package */ final Constructor&lt;?&gt; viewConstructor;

    @Nonnull
    /* package */ final Constructor&lt;? extends ViewController&gt; viewControllerConstructor;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public ViewBuilder (@Nonnull final Class&lt;?&gt; viewClass,
                        @Nonnull final Class&lt;? extends ViewController&gt; viewControllerClass)
      throws
      IllegalArgumentException, SecurityException
<span class="pc bpc" id="L77" title="9 of 18 branches missed.">      {</span>
<span class="fc" id="L78">        viewConstructor = viewClass.getConstructors()[0];</span>
<span class="fc" id="L79">        viewControllerConstructor = (Constructor&lt;ViewController&gt;)viewControllerClass.getConstructors()[0];</span>
<span class="pc bpc" id="L80" title="2 of 4 branches missed.">      }</span>

    /*******************************************************************************************************************
     *
     * Creates a new View - ViewController pair. They are first instantiated, and then dependency injection by means
     * of constructor parameters occur. Injected fields are: the id, the {@link SiteNode}, any service declared in
     * the Spring context, including {@link Site}; furthermore, a reference of the View is injected in the Controller.
     *
     * @param   id        the view id
     * @param   siteNode  the {@link SiteNode} the view will be built for
     * @return            the created view
     *
     ******************************************************************************************************************/
    @Nonnull
    public ViewAndController createViewAndController (@Nonnull final Id id, @Nonnull final SiteNode siteNode)
      throws HttpStatusException
      {
<span class="fc" id="L97">        log.debug(&quot;createViewAndController({}, {})&quot;, id, siteNode);</span>

        try
          {
<span class="fc" id="L101">            final var site = siteNode.getSite();</span>
<span class="fc" id="L102">            final var view = viewConstructor.newInstance(</span>
<span class="fc" id="L103">                    computeConstructorArguments(site, viewConstructor, id, siteNode));</span>
<span class="fc" id="L104">            final var controller = viewControllerConstructor.newInstance(</span>
<span class="fc" id="L105">                    computeConstructorArguments(site, viewControllerConstructor, id, siteNode, view));</span>
<span class="fc" id="L106">            controller.initialize();</span>
<span class="fc" id="L107">            return new ViewAndController(view, controller);</span>
          }
<span class="nc" id="L109">        catch (InvocationTargetException e)</span>
          {
            // FIXME: cumbersome
cumbersome
<span class="nc bnc" id="L112" title="All 4 branches missed."> if ((e.getCause() instanceof BeanCreationException) &amp;&amp; (e.getCause().getCause() instanceof HttpStatusException))</span> { <span class="nc" id="L114"> throw (HttpStatusException)e.getCause().getCause();</span> } <span class="nc" id="L117"> throw new RuntimeException(e);</span> } <span class="nc" id="L119"> catch (Exception e)</span> { <span class="nc" id="L121"> throw new RuntimeException(e);</span> } } /******************************************************************************************************************* * * Computes the argument values for calling the given constructor. They are taken from the current * {@link BeanFactory}, with {@code overridingArgs} eventually overriding them. * * @param site the site * @param constructor the constructor * @param overridingArgs the overriding arguments * @return the arguments to pass to the constructor * ******************************************************************************************************************/ @Nonnull private Object[] computeConstructorArguments (@Nonnull final Site site, @Nonnull final Constructor&lt;?&gt; constructor, @Nonnull final Object ... overridingArgs) { <span class="fc" id="L141"> final List&lt;Object&gt; result = new ArrayList&lt;&gt;();</span> <span class="fc bfc" id="L143" title="All 2 branches covered."> x: for (final var argumentType : constructor.getParameterTypes())</span> { <span class="fc bfc" id="L145" title="All 2 branches covered."> for (final var overridingArg : overridingArgs)</span> { <span class="fc bfc" id="L147" title="All 2 branches covered."> if (argumentType.isAssignableFrom(overridingArg.getClass()))</span> { <span class="fc" id="L149"> result.add(overridingArg);</span> <span class="fc" id="L150"> continue x;</span> } } <span class="fc bfc" id="L154" title="All 2 branches covered."> if (Site.class.isAssignableFrom(argumentType))</span> { <span class="fc" id="L156"> result.add(beanFactory.getBean(SiteProvider.class).getSite());</span> } <span class="pc bpc" id="L158" title="1 of 2 branches missed."> else if (BeanFactory.class.isAssignableFrom(argumentType))</span> { <span class="nc" id="L160"> result.add(beanFactory);</span> } else { <span class="fc" id="L164"> result.add(beanFactory.getBean(argumentType));</span> } } <span class="fc" id="L168"> return result.toArray();</span> } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.9.202303310957</span></div></body></html>