BuiltIns.java

/*
** Module   : BuiltIns.java
** Abstract : Helper class to simplify access to built-in functions
**
** Copyright (c) 2004-2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- --JPRM-- ----------------------------Description------------------------------
** 001 ECF 20080312  @37510  Created initial version. Helper class to
**                           simplify access to built-in functions.
** 002 CA  20130408          Ensure the public and support functions are always sorted by name
**                           (as Class.getDeclaredMethods returns them in an undetermined order).
** 003 CA  20190722          Do not register a method without a HQLFunction annotation.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
** 
** Additional terms under GNU Affero GPL version 3 section 7:
** 
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
** 
**   0. Attribution Requirement.
** 
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
** 
**   1. No License To Use Trademarks.
** 
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
** 
**   2. No Misrepresentation of Affiliation.
** 
**     You may not represent yourself as Golden Code Development Corporation or FWD.
** 
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
** 
**   3. No Misrepresentation of Source or Origin.
** 
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.persist.pl;

import java.lang.reflect.*;
import java.util.*;

/**
 * Helper class to simplify access to built-in function backing methods.
 * Exposes all standard methods which need to run within the database.
 */
public final class BuiltIns
{
   /** Classes which contain backing methods of public built-in functions */
   private static final Class<?>[] publicFunctionClasses = new Class<?>[]
   {
      Functions.class,
      Operators.class,
   };
   
   /** Classes which contain backing methods of support functions */
   private static final Class<?>[] supportFunctionClasses = new Class<?>[]
   {
      ErrorHandler.class,
   };
   
   /**
    * Private constructor.  All access is via static methods.
    */
   private BuiltIns()
   {
   }
   
   /**
    * Get a list of all public built-in and support function backing methods.
    * 
    * @return  List of backing methods for all functions.
    */
   public static List<Method> getStandardFunctions()
   {
      ArrayList<Method> list = new ArrayList<Method>();
      list.addAll(getPublicFunctions());
      list.addAll(getSupportFunctions());
      list.trimToSize();
      
      return list;
   }
   
   /**
    * Get a list of all public built-in function backing methods.
    * 
    * @return  List of backing methods for built-in functions.
    */
   public static List<Method> getPublicFunctions()
   {
      return getMethodList(publicFunctionClasses);
   }
   
   /**
    * Get a list of all support function backing methods.
    * 
    * @return  List of backing methods for support functions.
    */
   public static List<Method> getSupportFunctions()
   {
      return getMethodList(supportFunctionClasses);
   }
   
   /**
    * Given an array of classes, extract all public, static methods and
    * return them in a list.
    * 
    * @param   classes
    *          Classes from which to extract methods.
    * 
    * @return  List of all public, static methods.
    */
   private static List<Method> getMethodList(Class<?>[] classes)
   {
      ArrayList<Method> list = new ArrayList<Method>();
      for (Class<?> clazz : classes)
      {
         list.addAll(getMethodList(clazz));
      }
      
      list.trimToSize();
      
      // sort the methods by their full signature
      Collections.sort(list, new Comparator<Method>()
      {
         public int compare(Method m1, Method m2)
         {
            return m1.toString().compareTo(m2.toString());
         }
      });
      
      return Collections.unmodifiableList(list);
   }
   
   /**
    * Given a single class, extract all public, static methods and return them
    * in a list.
    * 
    * @param   clazz
    *          Class from which to extract methods.
    * 
    * @return  List of all public, static methods.
    */
   private static List<Method> getMethodList(Class<?> clazz)
   {
      List<Method> list = new ArrayList<Method>();
      Method[] all = clazz.getDeclaredMethods();
      for (Method method : all)
      {
         int mod = method.getModifiers();
         if (Modifier.isPublic(mod) && 
             Modifier.isStatic(mod) && 
             method.isAnnotationPresent(HQLFunction.class))
         {
            list.add(method);
         }
      }
      
      return list;
   }
}