Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2007, Red Hat Middleware LLC, and individual contributors as indicated
   * by the @authors tag. See the copyright.txt 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;

Useful methods for classes.

Author(s):
Carlo de Wolf
Version:
$Revision: 80988 $
 
 public class ClassHelper
 {
   
Casts an object to the class or interface represented by the targetClass Class object. The ClassCastException thrown is more descriptive than the original.

Parameters:
obj the object to be cast
Returns:
the object after casting, or null if obj is null
Throws:
java.lang.ClassCastException if the object is not null and is not assignable to the type T.
 
    public static <T> T cast(Class<T> targetClassObject obj)
    {
       try
       {
          return targetClass.cast(obj);
       }
       catch(ClassCastException e)
       {
          assert obj != null : "a null can always be cast, it should never throw a ClassCastException";
          throw new ClassCastException("Unable to cast " + obj.getClass() + " to " + targetClass);
       }
    }
   
   
 
    private static String argumentTypesToString(Class<?>[] argTypes)
    {
       StringBuilder buf = new StringBuilder();
       buf.append("(");
       if (argTypes != null)
       {
          for (int i = 0; i < argTypes.lengthi++)
          {
             if (i > 0)
             {
                buf.append(", ");
             }
             Class<?> c = argTypes[i];
             buf.append((c == null) ? "null" : c.getName());
          }
       }
       buf.append(")");
       return buf.toString();
    }
 
    private static Method findPrivateMethod(Class<?> targetString methodNameClass<?>... paramTypes)
    {
       // Top of the world
       if(target == null)
          return null;
       
       // TODO: what is faster? scan or catch exception?
       for(Method method : SecurityActions.getDeclaredMethods(target))
       {
          if(method.getName().equals(methodName))
          {
             if(paramTypes == null)
                return method;
             
             if(Arrays.equals(method.getParameterTypes(), paramTypes))
               return method;
         }
      }
      
      return findPrivateMethod(target.getSuperclass(), methodNameparamTypes);
   }
   
   
Find all methods with a specific name on a class and it's super classes regardless of parameter signature.

Parameters:
cls the class to scan
methodName the name of the methods to find
Returns:
a list of methods found, or empty
   public static List<MethodgetAllMethodsByName(Class<?> clsString methodName)
   {
      List<Methodmethods = new ArrayList<Method>();
      populateWithMethodsByName(methodsclsmethodName);
      return methods;
   }
   
   
Find all methods starting with the specified prefix on the specified class

Parameters:
clazz
methodNamePrefix
Returns:
   public static List<MethodgetAllMethodsByPrefix(Class<?> clazzString methodNamePrefix)
   {
      List<Methodmethods = new ArrayList<Method>();
      ClassHelper.populateWithMethodsByPrefix(methodsclazzmethodNamePrefix);
      return methods;
   }
   
   
Returns the Method with the given attributes of either this class or one of it's super classes. TODO: return type specifics are not considered FIXME: rename method (it must return all modifiers)

Parameters:
cls class to scan
methodName the name of the method
paramTypes the parameter types
Returns:
the Method matching the method name and parameters
Throws:
java.lang.NoSuchMethodException if no method can be found
   public static Method getPrivateMethod(Class<?> clsString methodNameClass<?>... paramTypesthrows NoSuchMethodException
   {
      assert cls != null : "cls is null";
      assert methodName != null : "methodName is null";
      
      Method result = findPrivateMethod(clsmethodNameparamTypes);
      if(result == null)
         throw new NoSuchMethodException(cls.getName() + "." + methodName + argumentTypesToString(paramTypes));
      
      return result;
   }
   
   
Obtains a Class corresponding to the specified type using the specified ClassLoader, throwing a descriptive RuntimeException in the case the Class could not be found

Parameters:
type
cl
Returns:
Author(s):
ALR
   public static final Class<?> getClassFromTypeName(String typeClassLoader cl)
   {
      try
      {
         // Load the class
         return cl.loadClass(type);
      }
      catch (ClassNotFoundException e)
      {
         // Throw descriptive message
         throw new RuntimeException("Specified class " + type + " could not be found by the "
               + ClassLoader.class.getSimpleName() + ", " + cle);
      }
   }
   
   private static void populateWithMethodsByName(List<MethodmethodsClass<?> clsString methodName)
   {
      // Top of the world
      if(cls == null)
         return;
      
      for(Method method : SecurityActions.getDeclaredMethods(cls))
      {
         if(method.getName().equals(methodName))
            methods.add(method);
      }
      
      populateWithMethodsByName(methodscls.getSuperclass(), methodName);
   }
   
   private static void populateWithMethodsByPrefix(List<MethodmethodsClass<?> clazzString methodNamePrefix)
   {
      // Exit Condition
      if (clazz == null)
      {
         return;
      }
      // For all declared methods
      for (Method method : SecurityActions.getDeclaredMethods(clazz))
      {
         if (method.getName().startsWith(methodNamePrefix))
            methods.add(method);
      }
      populateWithMethodsByPrefix(methodsclazz.getSuperclass(), methodNamePrefix);
   }
New to GrepCode? Check out our FAQ X