<commit_before>/*
 * Copyright (c) 2005-2006 Digital Bazaar, Inc.  All rights reserved.
 */
package com.db.common;

import com.db.common.logging.Logger;
import com.db.common.logging.LoggerManager;

import java.lang.reflect.Method;

/**
 * This object is used to invoke a method, optionally, report a message back
 * when the method completes. The method can be invoked in another thread
 * or on the current one.
 * 
 * @author <NAME>
 */
public class MethodInvoker extends Thread
{
   /**
    * The object with the method to invoke.
    */
   protected Object mAgent;
   
   /**
    * The name of the method to invoke.
    */
   protected String mMethodName;
   
   /**
    * The method parameters.
    */
   protected Object[] mParams;
   
   /**
    * A custom lock object to synchronize on.
    */
   protected Object mLockObject;
   
   /**
    * True if the lock object should remain locked until the method invoked
    * message has been handled, false if it should unlock immediately after
    * the method has been invoked.
    */
   protected boolean mLockMessageHandling;
   
   /**
    * A MethodInvokedMessage with information about the invoked method.
    */
   protected MethodInvokedMessage mMessage;
   
   /**
    * A MethodInvoker that calls a callback method once the method completes.
    */
   protected MethodInvoker mCallbackMethodInvoker;
   
   /**
    * The MethodInvokerListener to receive the MethodInvokedMessage once
    * the method completes.
    */
   protected MethodInvokerListener mListener;
   
   /**
    * Creates a new MethodInvoker thread.
    * 
    * @param agent the object that the method will be invoked on.
    * @param methodName the name of the method to invoke.
    * @param params the parameters for the method.
    */
   public MethodInvoker(Object agent, String methodName, Object[] params)
   {
      mAgent = agent;
      mMethodName = methodName;
      mParams = params;
      
      if(mParams == null)
      {
         mParams = new Object[0];
      }
      
      mLockObject = null;
      mLockMessageHandling = false;
      
      mMessage = null;
      mListener = null;
      mCallbackMethodInvoker = null;
   }
   
   /**
    * Finds the method to invoke.
    * 
    * @param agent the object to invoke the method on.
    * @param methodName the name of the method to invoke.
    * @param params the parameters for the method.
    * @return the method to invoke or null if no such method exists.
    */
   protected Method findMethod(Object agent, String methodName, Object[] params)
   {
      Method rval = null;
      
      // get the methods for the agent, if the agent is a class obtain
      // then directly, if not, then get the class of the agent and
      // then obtain them
      Method[] methods = new Method[0];
      if(agent instanceof Class)
      {
         methods = ((Class)agent).getMethods();
      }
      else
      {
         methods = agent.getClass().getMethods();
      }
      
      for(int i = 0; i < methods.length; i++)
      {
         Method m = methods[i];
         if(m.getName().equals(methodName))
         {
            Class[] types = m.getParameterTypes();
            int numParams = types.length;
            if(params.length == numParams)
            {
               boolean typesMatch = true;
               for(int n = 0; n < numParams; n++)
               {
                  // ensure the parameter is the appropriate type
                  if(!types[n].isInstance(params[n]))
                  {
                     // assume failure unless a primitive is being used
                     typesMatch = false;
                     
                     if(types[n].isPrimitive())
                     {
                        // determine if the passed type matches the primitive
                        String paramClass =
                           params[n].getClass().getName().toLowerCase();
                        String primitive =
                           "java.lang." + types[n].getName();
                        
                        typesMatch = paramClass.equals(primitive);
                     }
                     
                     // break out if types do not match
                     if(!typesMatch)
                     {
                        break;
                     }
                  }
               }
               
               // if the parameter types match, proceed
               if(typesMatch)
               {
                  // method found
                  rval = m;
                  break;
               }
            }
         }<commit_msg>Updated documentation for better clarity in MethodInvoker.<commit_after>/*
 * Copyright (c) 2005-2006 Digital Bazaar, Inc.  All rights reserved.
 */
package com.db.common;

import com.db.common.logging.Logger;
import com.db.common.logging.LoggerManager;

import java.lang.reflect.Method;

/**
 * This object is used to invoke a method, optionally, report a message back
 * when the method completes. The method can be invoked in another thread
 * or on the current one.
 * 
 * @author <NAME>
 */
public class MethodInvoker extends Thread
{
   /**
    * The object with the method to invoke.
    */
   protected Object mAgent;
   
   /**
    * The name of the method to invoke.
    */
   protected String mMethodName;
   
   /**
    * The method parameters.
    */
   protected Object[] mParams;
   
   /**
    * A custom lock object to synchronize on.
    */
   protected Object mLockObject;
   
   /**
    * True if the lock object should remain locked until the method invoked
    * message has been handled, false if it should unlock immediately after
    * the method has been invoked.
    */
   protected boolean mLockMessageHandling;
   
   /**
    * A MethodInvokedMessage with information about the invoked method.
    */
   protected MethodInvokedMessage mMessage;
   
   /**
    * A MethodInvoker that calls a callback method once the method completes.
    */
   protected MethodInvoker mCallbackMethodInvoker;
   
   /**
    * The MethodInvokerListener to receive the MethodInvokedMessage once
    * the method completes.
    */
   protected MethodInvokerListener mListener;
   
   /**
    * Creates a new MethodInvoker thread.
    * 
    * @param agent the object that the method will be invoked on.
    * @param methodName the name of the method to invoke.
    * @param params the parameters for the method.
    */
   public MethodInvoker(Object agent, String methodName, Object[] params)
   {
      mAgent = agent;
      mMethodName = methodName;
      mParams = params;
      
      if(mParams == null)
      {
         mParams = new Object[0];
      }
      
      mLockObject = null;
      mLockMessageHandling = false;
      
      mMessage = null;
      mListener = null;
      mCallbackMethodInvoker = null;
   }
   
   /**
    * Finds the method to invoke.
    * 
    * @param agent the object to invoke the method on.
    * @param methodName the name of the method to invoke.
    * @param params the parameters for the method.
    * @return the method to invoke or null if no such method exists.
    */
   protected Method findMethod(Object agent, String methodName, Object[] params)
   {
      Method rval = null;
      
      // get the methods for the agent, if the agent is a class obtain
      // then directly, if not, then get the class of the agent and
      // then obtain them
      Method[] methods = new Method[0];
      if(agent instanceof Class)
      {
         methods = ((Class)agent).getMethods();
      }
      else
      {
         methods = agent.getClass().getMethods();
      }
      
      for(int i = 0; i < methods.length; i++)
      {
         Method m = methods[i];
         if(m.getName().equals(methodName))
         {
            Class[] types = m.getParameterTypes();
            int numParams = types.length;
            if(params.length == numParams)
            {
               boolean typesMatch = true;
               for(int n = 0; n < numParams; n++)
               {
                  // ensure the parameter is the appropriate type
                  if(!types[n].isInstance(params[n]))
                  {
                     // assume failure unless a primitive is being used
                     typesMatch = false;
                     
                     if(types[n].isPrimitive())
                     {
                        // determine if the param type matches the primitive
                        String paramClass =
                           params[n].getClass().getName().toLowerCase();
                        String primitive =
                           "java.lang." + types[n].getName();
                        
                        typesMatch = paramClass.equals(primitive);
                     }
                     
                     // break out if types do not match
                     if(!typesMatch)
                     {
                        break;
                     }
                  }
               }
               
               // if the parameter types match, proceed
               if(typesMatch)
               {
                  // method found
                  rval = m;
                  break;
               }
            }
         }
{'fraction_non_alphanumeric': 0.07956503014642549, 'fraction_numerical': 0.002583979328165375, 'mean_word_length': 1.5064759848893685, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2972737', 'n_tokens_mistral': 2341, 'n_tokens_neox': 2267, 'n_words': 800}