BeanUtils.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.utils.beans;

  21. import java.lang.reflect.Method;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.openspcoop2.utils.LoggerWrapperFactory;
  25. import org.slf4j.Logger;

  26. /****
  27.  *
  28.  * Implementa funzionalita' di gestione dei bean.
  29.  *
  30.  * @author Pintori Giuliano (pintori@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  *
  34.  */
  35. public class BeanUtils {

  36.     private static Logger log =  LoggerWrapperFactory.getLogger(BeanUtils.class);

  37.     /*****
  38.      *
  39.      * Implementa la funzione di copia del valore delle properties dell'oggetto
  40.      * sorgente in quello destinazione.
  41.      *
  42.      * E' possibile specificare un elenco di metodi setter che si vogliono non
  43.      * invocare.
  44.      *
  45.      * @param oggettoDestinazione
  46.      * @param oggettoOriginale
  47.      * @param metodiEsclusi
  48.      */
  49.     public static void copy(Object oggettoDestinazione,
  50.             Object oggettoOriginale, List<BlackListElement> metodiEsclusi) {
  51.         copy(BeanUtils.log, oggettoDestinazione, oggettoOriginale, metodiEsclusi);
  52.     }
  53.     public static void copy(Object oggettoDestinazione,
  54.             Object oggettoOriginale) {
  55.         copy(BeanUtils.log, oggettoDestinazione, oggettoOriginale, null);
  56.     }
  57.     public static void copy(Logger logParam, Object oggettoDestinazione,
  58.             Object oggettoOriginale) {
  59.         copy(logParam, oggettoDestinazione, oggettoOriginale, null);
  60.     }
  61.     public static void copy(Logger logParam, Object oggettoDestinazione,
  62.             Object oggettoOriginale, List<BlackListElement> metodiEsclusi) {
  63.         copy(logParam, oggettoDestinazione, oggettoOriginale, metodiEsclusi, false);
  64.     }
  65.     public static void copy(Logger logParam, Object oggettoDestinazione,
  66.             Object oggettoOriginale, List<BlackListElement> metodiEsclusi,
  67.             boolean throwRuntimeException) {

  68.         //check esistenza oggetti da copiare.
  69.         if (oggettoDestinazione == null || oggettoOriginale == null) {
  70.             logParam.debug("Parametri non validi.");
  71.             return;
  72.         }

  73.         // elenco dei metodi passati null, lo inizializzo.
  74.         if (metodiEsclusi == null) {
  75.             metodiEsclusi = new ArrayList<BlackListElement>(0);
  76.         }

  77.         logParam.debug(" Copia delle properties dell'oggetto di classe["
  78.                 + oggettoOriginale.getClass().getName()
  79.                 + "] all'interno dell'oggetto ["
  80.                 + oggettoDestinazione.getClass().getName() + "]");

  81.         // 1. prelevo l'oggetto Class dei due oggetti da manipolare.
  82.         Class<?> oggettoOriginaleClass = oggettoOriginale.getClass();
  83.         Class<?> oggettoDestinazioneClass = oggettoDestinazione.getClass();
  84.         try {
  85.             // 2. scorro i metodi a disposizione nell'oggetto destinazione.
  86.             for (Method oggettoDestinazioneMethod : oggettoDestinazioneClass
  87.                     .getMethods()) {
  88.                 String setterName = oggettoDestinazioneMethod.getName();

  89.                 //logParam.debug("Metodo analizzato: " + setterName);

  90.                 // il metodo da invocare e' un setter, con un solo parametro.
  91.                 if (setterName.startsWith("set")
  92.                         && oggettoDestinazioneMethod.getParameterTypes().length == 1) {
  93.                     // controllo di corrispondenza del tipo di parametro
  94.                     BlackListElement ble = new BlackListElement(setterName,
  95.                             oggettoDestinazioneMethod.getParameterTypes());
  96.                     if (!metodiEsclusi.contains(ble)) {
  97.                         String getPrefix = "get";

  98.                         String name = setterName.substring(setterName
  99.                                         .lastIndexOf("set") + 3);
  100.                         try {
  101.                        
  102.                             // caso particolare: i getter che restituiscono boolean
  103.                             // o Boolean hanno il nome che inizia per 'is'
  104.                             if (oggettoDestinazioneMethod.getParameterTypes()[0]
  105.                                     .equals(Boolean.class)
  106.                                     || oggettoDestinazioneMethod
  107.                                     .getParameterTypes()[0]
  108.                                             .equals(boolean.class)) {
  109.                                 getPrefix = "is";
  110.                             }
  111.    
  112.                             // calcolo il nome del metodo getter corrispondente nell'oggetto origine;
  113.                             String getterName = getPrefix+ name;
  114.                             //      logParam.debug("Nome Getter: " + getterName);
  115.    
  116.                             // prelevo il metodo getter.
  117.                             Method oggettoOriginaleMethod = oggettoOriginaleClass
  118.                                     .getMethod(getterName);
  119.    
  120.                             // invoco il getter nell'oggetto origine per ottenere il valore da settare nell'oggetto destinazione.
  121.                             if (oggettoOriginaleMethod != null) {
  122.                                 Object retObj = oggettoOriginaleMethod.invoke(
  123.                                         oggettoOriginale);
  124.    
  125.                                 // prelevo il valore e lo utilizzo come parametro del metodo setter.
  126.                                 if (retObj != null) {
  127.                                     oggettoDestinazioneMethod.invoke(
  128.                                             oggettoDestinazione, retObj);
  129.                                 }
  130.                             }
  131.                         }catch(Throwable e) {
  132.                             throw new Exception("[field '"+name+"'] "+e.getMessage(),e);
  133.                         }
  134.            
  135.                     }
  136.                 }
  137.             }
  138.         }catch (Throwable e) {
  139.             logParam.error(e.getMessage(), e);
  140.             if(throwRuntimeException) {
  141.                 throw new RuntimeException(e.getMessage(),e);
  142.             }
  143.         }
  144.     }
  145. }