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
  * 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.managed.plugins.factory;
Common untility methods used by the factory plugins.

Author(s):
Scott.Stark@jboss.org
Version:
$Revision:$
public class Utility
{
   
Get the instance factory for a class

Parameters:
clazz the class
instanceFactories - the registered mapping of classes to InstanceClassFactorys
defaultInstanceFactory - the default InstanceClassFactory to use if no class match is found.
Returns:
the InstanceClassFactory
   @SuppressWarnings("unchecked")
   public static InstanceClassFactory getInstanceClassFactory(Class<?> clazz,
         Map<Class<?>, InstanceClassFactory<?>> instanceFactories,
         InstanceClassFactory defaultInstanceFactory)
   {
      synchronized (instanceFactories)
      {
         Class<?> c = clazz;
         InstanceClassFactory factory = instanceFactories.get(c);
         while(factory == null && c != Object.class)
         {
            // First check for interfaces
            Class<?>[] ifaces = c.getInterfaces();
            if(ifaces != null)
            {
               for(Class<?> i : ifaces)
               {
                  factory = instanceFactories.get(i);
                  if(factory != null)
                     break;
               }
            }
            // Next look at superclasses
            if(factory == null)
            {
               c = c.getSuperclass();
               factory = instanceFactories.get(c);
            }
         }
         if (factory != null)
            return factory;
      }
      InstanceClassFactory factory = defaultInstanceFactory;
      return factory;
   }
New to GrepCode? Check out our FAQ X