MethodUtil.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.util.method;

  35. import org.apache.commons.lang3.StringUtils;

  36. import java.lang.reflect.InvocationTargetException;
  37. import java.lang.reflect.Method;

  38. /**
  39.  *
  40.  * MethodUtils
  41.  *
  42.  */
  43. public final class MethodUtil
  44. {
  45.     private static final String PREFIX_GET = "get";
  46.     private static final String PREFIX_SET = "set";

  47.     /**
  48.      * Instantiates a new method utils.
  49.      */
  50.     private MethodUtil( )
  51.     {
  52.     }

  53.     /**
  54.      * Sets the attribute. <br>
  55.      * <strong>Warning :</strong> This method does not handle setter that :
  56.      * <ul>
  57.      * <li>has no parameter or has more than one parameter</li>
  58.      * <li>has array parameter (ie : String[] or int[] ...)</li>
  59.      * </ul>
  60.      *
  61.      * @param <A>
  62.      *            the generic type of the instance
  63.      * @param <B>
  64.      *            the generic type of the value to set
  65.      * @param instance
  66.      *            the instance to set
  67.      * @param strAttributeName
  68.      *            the attribute name
  69.      * @param value
  70.      *            the value of the attribute to set
  71.      * @throws SecurityException
  72.      *             the security exception
  73.      * @throws NoSuchMethodException
  74.      *             the no such method exception
  75.      * @throws IllegalArgumentException
  76.      *             the illegal argument exception
  77.      * @throws IllegalAccessException
  78.      *             the illegal access exception
  79.      * @throws InvocationTargetException
  80.      *             the invocation target exception
  81.      */
  82.     public static <A, B> void set( A instance, String strAttributeName, B value )
  83.             throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
  84.     {
  85.         if ( StringUtils.isNotBlank( strAttributeName ) && ( instance != null ) && ( value != null ) )
  86.         {
  87.             Method methodSetter = getSetter( instance, strAttributeName, value.getClass( ) );

  88.             if ( methodSetter != null )
  89.             {
  90.                 methodSetter.invoke( instance, value );
  91.             }
  92.             else
  93.             {
  94.                 throw new NoSuchMethodException( );
  95.             }
  96.         }
  97.         else
  98.         {
  99.             throw new IllegalArgumentException( "One on the parameters is null/blank." );
  100.         }
  101.     }

  102.     /**
  103.      * Gets the method.
  104.      *
  105.      * @param <A>
  106.      *            the generic type of the instance
  107.      * @param strMethodPrefix
  108.      *            the str method prefix
  109.      * @param instance
  110.      *            the instance
  111.      * @param strAttributeName
  112.      *            the str attribute name
  113.      * @param clazz
  114.      *            the clazz
  115.      * @return the method
  116.      * @throws SecurityException
  117.      *             the security exception
  118.      * @throws NoSuchMethodException
  119.      *             the no such method exception
  120.      */
  121.     public static <A> Method getMethod( String strMethodPrefix, A instance, String strAttributeName, Class<?> clazz ) throws NoSuchMethodException
  122.     {
  123.         String strFirstLetter = strAttributeName.substring( 0, 1 ).toUpperCase( );

  124.         String strMethodName = strMethodPrefix + strFirstLetter + strAttributeName.substring( 1, strAttributeName.length( ) );

  125.         try
  126.         {
  127.             return instance.getClass( ).getMethod( strMethodName, clazz );
  128.         }
  129.         catch( NoSuchMethodException e )
  130.         {
  131.             return getPrimitiveMethod( strMethodName, instance, clazz );
  132.         }
  133.     }

  134.     /**
  135.      * Gets the primitive method.
  136.      *
  137.      * @param <A>
  138.      *            the generic type of the instance
  139.      * @param strMethodName
  140.      *            the str method name
  141.      * @param instance
  142.      *            the instance
  143.      * @param clazz
  144.      *            the clazz
  145.      * @return the primitive method
  146.      * @throws SecurityException
  147.      *             the security exception
  148.      * @throws NoSuchMethodException
  149.      *             the no such method exception
  150.      */
  151.     public static <A> Method getPrimitiveMethod( String strMethodName, A instance, Class<?> clazz ) throws NoSuchMethodException
  152.     {
  153.         if ( clazz.equals( Integer.class ) )
  154.         {
  155.             return instance.getClass( ).getMethod( strMethodName, int.class );
  156.         }
  157.         else
  158.             if ( clazz.equals( Long.class ) )
  159.             {
  160.                 return instance.getClass( ).getMethod( strMethodName, long.class );
  161.             }
  162.             else
  163.                 if ( clazz.equals( Double.class ) )
  164.                 {
  165.                     return instance.getClass( ).getMethod( strMethodName, double.class );
  166.                 }
  167.                 else
  168.                     if ( clazz.equals( Short.class ) )
  169.                     {
  170.                         return instance.getClass( ).getMethod( strMethodName, short.class );
  171.                     }
  172.                     else
  173.                         if ( clazz.equals( Byte.class ) )
  174.                         {
  175.                             return instance.getClass( ).getMethod( strMethodName, byte.class );
  176.                         }
  177.                         else
  178.                             if ( clazz.equals( Float.class ) )
  179.                             {
  180.                                 return instance.getClass( ).getMethod( strMethodName, float.class );
  181.                             }
  182.                             else
  183.                                 if ( clazz.equals( Character.class ) )
  184.                                 {
  185.                                     return instance.getClass( ).getMethod( strMethodName, char.class );
  186.                                 }
  187.                                 else
  188.                                     if ( clazz.equals( Boolean.class ) )
  189.                                     {
  190.                                         return instance.getClass( ).getMethod( strMethodName, boolean.class );
  191.                                     }

  192.         throw new NoSuchMethodException( );
  193.     }

  194.     /**
  195.      * Gets the setter.
  196.      *
  197.      * @param <A>
  198.      *            the generic type
  199.      * @param instance
  200.      *            the instance
  201.      * @param strAttributeName
  202.      *            the str attribute name
  203.      * @param clazz
  204.      *            the clazz
  205.      * @return the setter
  206.      * @throws SecurityException
  207.      *             the security exception
  208.      * @throws NoSuchMethodException
  209.      *             the no such method exception
  210.      */
  211.     public static <A> Method getSetter( A instance, String strAttributeName, Class<?> clazz ) throws NoSuchMethodException
  212.     {
  213.         return getMethod( PREFIX_SET, instance, strAttributeName, clazz );
  214.     }

  215.     /**
  216.      * Gets the setter.
  217.      *
  218.      * @param <A>
  219.      *            the generic type of the instance
  220.      * @param instance
  221.      *            the instance
  222.      * @param strAttributeName
  223.      *            the str attribute name
  224.      * @param clazz
  225.      *            the clazz
  226.      * @return the setter
  227.      * @throws SecurityException
  228.      *             the security exception
  229.      * @throws NoSuchMethodException
  230.      *             the no such method exception
  231.      */
  232.     public static <A> Method getGetter( A instance, String strAttributeName, Class<?> clazz ) throws NoSuchMethodException
  233.     {
  234.         return getMethod( PREFIX_GET, instance, strAttributeName, clazz );
  235.     }
  236. }