# InvocationStub.java

    /*
    ** Module   : InvocationStub.java
    ** Abstract : common invocation handler logic
    **
    ** Copyright (c) 2011-2022, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- -----------------------Description------------------------
    ** 001 GES 20111012 First version.
    ** 002 GES 20111025 Unwrap and rethrow the real cause of an exception when it
    **                  is wrapped in an InvocationTargetException. This is done
    **                  for us in the normal remote object implementation but in the
    **                  local implementation, this code must do the job. Otherwise
    **                  code that does not expect to see the ITE will fail.
    ** 003 ECF 20150715 Replace StringBuffer with StringBuilder.
    ** 004 TJD 20220504 Upgrade do Java 11 minor changes
    */
    /*
    ** 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.net;

    import java.lang.reflect.*;

    /**
     * Provides common logic for dynamic proxy implementations. Subclasses only
     * need to implement the {@link #invokeCore} method.
     */
    public abstract class InvocationStub
    implements InvocationHandler
    {
       /**
        * This method acts as an interceptor which is called every time a
        * method of the proxy object is invoked.
        *
        * @param    proxy
        *           Proxy object upon which the intercepted method is invoked.
        * @param    method
        *           Method to be invoked through reflection.
        * @param    args
        *           Arguments, if any, to <code>method</code>.
        *
        * @return   Result of the mapped call.
        *
        * @throws   Throwable
        *           if an exception/error occurs upon the invocation target.
        */
       public Object invoke(Object proxy, Method method, Object[] args)
       throws Throwable
       {
          Object rc = null;

          // reflection will tell us the class (or interface) in which this
          // method was defined
          Class<?> cls = method.getDeclaringClass();

          // if the method was one defined by Object instead of one of the
          // interfaces we represent, then the local instance must be invoked
          // since this is not a remoted API
          if (cls.equals(Object.class))
          {
             String name = method.getName();

             // 3 special cases exist that must be implemented "in-line" since
             // they are not remote AND since trying to invoke these on the
             // local object will dispatch these calls recursively back into
             // this method (infinite recursion) --> this is a quirk of the
             // dynamic proxy environment

             if ("equals".equals(name))
             {
                return (proxy == args[0]) ? Boolean.TRUE : Boolean.FALSE;
             }
             else if ("toString".equals(name))
             {
                StringBuilder sb = new StringBuilder(proxy.getClass().getName());
                sb.append('@');
                sb.append(Integer.toHexString(proxy.hashCode()));

                return sb.toString();
             }
             else if ("hashCode".equals(name))
             {
                return Integer.valueOf(System.identityHashCode(proxy));
             }
             else
             {
                // invoke the local Object method
                return method.invoke(proxy, args);
             }
          }

          try
          {
             rc = invokeCore(proxy, method, args);
          }

          catch (IllegalStateException ise)
          {
             throw new SilentUnwindException(ise);
          }

          // unwrap these here because local redirectors don't have the network
          // dispatching code to do it for them; not needed for normal remote
          // operation (but it should not hurt)
          catch (InvocationTargetException ite)
          {
             Throwable cause = ite.getCause();

             if (cause instanceof Exception)
                throw (Exception) cause;
          }

          return rc;
       }

       /**
        * Core interceptor logic which is called from the main try block in the
        * {@link #invoke} method.
        *
        * @param    proxy
        *           Proxy object upon which the intercepted method is invoked.
        * @param    method
        *           Method to be invoked through reflection.
        * @param    args
        *           Arguments, if any, to <code>method</code>.
        *
        * @return   Result of the mapped call.
        *
        * @throws   Throwable
        *           if an exception/error occurs upon the invocation target,
        *           or if any problem occurs during pre- or post-processing.
        */
       public abstract Object invokeCore(Object proxy, Method method, Object[] args)
       throws Throwable;
    }