Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2008, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
    *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software 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
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.ejb3.common.lang;
 
 import java.util.List;
 
SerializableMethod A Serializable view of an Invoked Method, providing overridden implementations of hashCode, equals, and toString JIRA: EJBTHREE-1269

Author(s):
ALR
Version:
$Revision: $
 
 public class SerializableMethod implements Serializable
 {
    // ------------------------------------------------------------------------------||
    // Class Members ----------------------------------------------------------------||
    // ------------------------------------------------------------------------------||
 
    private static final long serialVersionUID = 1L;
 
    private static final Logger log = Logger.getLogger(SerializableMethod.class);
 
    // ------------------------------------------------------------------------------||
    // Instance Members -------------------------------------------------------------||
    // ------------------------------------------------------------------------------||
 
   
The name of the method
 
    private String name;

   
Fully-qualified declaring class name of the method
 
    private String declaringClassName;

   
Fully-qualified final (child) class name of the method, may have inherited the method
 
    private String actualClassName;

   
Fully-qualfied class name of the return type of the method
 
    private String returnType;

   
Array of fully-qualified class names of arguments, in order
 
    private String[] argumentTypes;
 
    // ------------------------------------------------------------------------------||
    // Constructors -----------------------------------------------------------------||
    // ------------------------------------------------------------------------------||
 
   
Constructor Creates a Serializable Method View, using the declaring class of the specified method as the actual class

Parameters:
method The method this view represents
 
    public SerializableMethod(Method method)
    {
       this(methodmethod.getDeclaringClass());
    }

   
Constructor Creates a Serializable Method View, allowing for an alternate "actual class" defining the method in addition to the declaring class

Parameters:
method The method this view represents
actualClass The class to which this method is associated, may be a child of the class declaring the method
   public SerializableMethod(Method methodClass<?> actualClass)
   {
      // Pass to alternate constructor
      this(methodactualClass != null ? actualClass.getName() : null);
   }

   
Constructor Creates a Serializable Method View, allowing for an alternate "actual class" defining the method in addition to the declaring class

Parameters:
method The method this view represents
actualClass The name of the class to which this method is associated, may be a child of the class declaring the method
   public SerializableMethod(Method methodString actualClassName)
   {
      // Set properties
      this.setName(method.getName());
      this.setDeclaringClassName(method.getDeclaringClass().getName());
      this.setActualClassName(actualClassName);
      this.setReturnType(method.getReturnType().getName());
      Class<?>[] paramTypes = method.getParameterTypes();
      List<StringparamTypesString = new ArrayList<String>();
      if (paramTypes != null)
      {
         for (Class<?> paramType : paramTypes)
         {
            paramTypesString.add(paramType.getName());
         }
      }
      this.setArgumentTypes(paramTypesString.toArray(new String[]
      {}));
   }
   // ------------------------------------------------------------------------------||
   // Overridden Implementations ---------------------------------------------------||
   // ------------------------------------------------------------------------------||
   public boolean equals(Object obj)
   {
      // If not an instance of SerializableMethod
      if (!(obj instanceof SerializableMethod))
      {
         // Different types, we can't be equal
         return false;
      }
      // Cast
      SerializableMethod other = SerializableMethod.class.cast(obj);
      // We're equal if all properties are equal
      return this.getDeclaringClassName().equals(other.getDeclaringClassName())
            && this.getName().equals(other.getName())
            && Arrays.equals(this.getArgumentTypes(), other.getArgumentTypes())
            && this.getActualClassName().equals(other.getActualClassName());
   }
   public int hashCode()
   {
      // toString is unique, use it
      return this.toString().hashCode();
   }
   public String toString()
   {
      // Initialize
      StringBuffer sb = new StringBuffer();
      // Construct
      sb.append(this.getActualClassName());
      sb.append(": ");
      sb.append(this.getDeclaringClassName());
      sb.append('.');
      sb.append(this.getName());
      sb.append('(');
      int count = 0;
      for (String argType : this.getArgumentTypes())
      {
         count++;
         sb.append(argType);
         if (count < this.getArgumentTypes().length)
         {
            sb.append(',');
         }
      }
      sb.append(')');
      // Return
      return sb.toString();
   }
   // ------------------------------------------------------------------------------||
   // Functional Methods -----------------------------------------------------------||
   // ------------------------------------------------------------------------------||

   
Obtains the Method described by this view, using the TCL

Returns:
   public Method toMethod()
   {
      return this.toMethod(SecurityActions.getThreadContextClassLoader());
   }

   
Obtains the Method described by this view, using the ClassLoader specified

Parameters:
cl
Returns:
   public Method toMethod(ClassLoader cl)
   {
      // Load the Class described by the Method
      Class<?> invokingClass = this.getClassType(cl);
      // Load the argument types
      List<ObjectargTypesList = new ArrayList<Object>();
      for (String argTypeName : this.getArgumentTypes())
      {
         Class<?> argType = this.getClassFromName(argTypeNamecl);
         argTypesList.add(argType);
      }
      Class<?>[] argTypes = argTypesList.toArray(new Class<?>[]
      {});
      // Obtain the Method
      String methodName = this.getName();
      Method invokedMethod = null;
      try
      {
         invokedMethod = invokingClass.getMethod(methodNameargTypes);
      }
      catch (NoSuchMethodException nsme)
      {
         throw new RuntimeException("Method " + this + " does not exist in " + invokingClass.getName(), nsme);
      }
      // Return
      return invokedMethod;
   }

   
Obtains the Class described by this view, using the TCL

Returns:
   public Class<?> getClassType()
   {
      return this.getClassType(Thread.currentThread().getContextClassLoader());
   }

   
Obtains the Class described by this view, using the specified ClassLoader

Parameters:
cl
Returns:
   public Class<?> getClassType(ClassLoader cl)
   {
      // Obtain
      Class<?> clazz = this.getClassFromName(this.getDeclaringClassName(), cl);
      // Return 
      return clazz;
   }

   
Returns the class associated with the given name

Parameters:
name
cl
Returns:
   protected Class<?> getClassFromName(String nameClassLoader cl)
   {
      // Perform assertions
      assert cl != null : ClassLoader.class.getSimpleName() + " must be defined.";
      
      // Load the Class described by the Method
      Class<?> clazz = null;
      try
      {
         // use the PrimitiveClassLoadingUtil to avoid any primitive checks here, in this method.
         clazz = PrimitiveClassLoadingUtil.loadClass(namecl);
      }
      catch (ClassNotFoundException cnfe)
      {
         throw new RuntimeException("Specified calling class, " + name + " could not be found for " + clcnfe);
      }
      // Return
      return clazz;
   }
   // ------------------------------------------------------------------------------||
   // Accessors / Mutators ---------------------------------------------------------||
   // ------------------------------------------------------------------------------||
   {
      return ;
   }
   public void setDeclaringClassName(String className)
   {
      this. = className;
   }
   public String getReturnType()
   {
      return ;
   }
   public void setReturnType(String returnType)
   {
      this. = returnType;
   }
   public String[] getArgumentTypes()
   {
      return ;
   }
   public void setArgumentTypes(String[] argumentTypes)
   {
      this. = argumentTypes;
   }
   public String getName()
   {
      return ;
   }
   public void setName(String name)
   {
      this. = name;
   }
   public String getActualClassName()
   {
      return ;
   }
   public void setActualClassName(String actualClassName)
   {
      // If no actual class name is specified
      if (actualClassName == null || actualClassName.trim().length() == 0)
      {
         // Use the declaring class name
         String declaringClassName = this.getDeclaringClassName();
         assert declaringClassName != null && declaringClassName.trim().length() > 0 : "Cannot implicitly set actual class to declaring class as declaring class is not defined";
         actualClassName = declaringClassName;
         .debug("Actual class name has not been explicitly specified, so defaulting to declaring class name: "
               + declaringClassName);
      }
      this. = actualClassName;
   }
New to GrepCode? Check out our FAQ X