Package org.jboss.util

Source Code of org.jboss.util.ClassLoading

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., 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.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.jboss.classloading.plugins.PackageInfo;
import org.jboss.classloading.plugins.WrappingClassLoader;
import org.jboss.logging.Logger;

// TODO Review

/**
* Reflection utility.
*
* @author <a href="adrian@jboss.com">Adrian Brock</a>
* @version $Revision: 1.2 $
*/
public class ClassLoading
{
   // Constants -----------------------------------------------------

   /** The log */
   private static final Logger log = Logger.getLogger(ClassLoading.class);
  
   /** The primitive classes */
   private static final Map primitives;

   // Attributes ----------------------------------------------------
  
   // Static --------------------------------------------------------

   static
   {
      primitives = new HashMap();
      primitives.put("byte", Byte.TYPE);
      primitives.put("boolean", Boolean.TYPE);
      primitives.put("char", Character.TYPE);
      primitives.put("short", Short.TYPE);
      primitives.put("int", Integer.TYPE);
      primitives.put("long", Long.TYPE);
      primitives.put("float", Float.TYPE);
      primitives.put("double", Double.TYPE);
   }

   public static Class loadClass(ClassLoader cl, String name) throws Exception
   {
      return loadClass(false, null, cl, name);
   }
  
   public static Class loadClass(boolean trace, String ctx, ClassLoader cl, String name) throws Exception
   {
      if (trace)
         log.trace("Loading " + ctx + " class=" + name + " classloader=" + cl);
      Class clazz = (Class) primitives.get(name);
      try
      {
         if (clazz == null)
            clazz = cl.loadClass(name);
      }
      catch (ClassNotFoundException e)
      {
         clazz = Class.forName(name);
      }
      if (trace)
         log.trace("Loaded " + ctx + " " + clazz + " classloader=" + clazz.getClassLoader());
      return clazz;
   }

   public static Set getClassLoaderSpecificPackages(ClassLoader cl)
   {
      Set packages = getClassLoaderPackages(cl);
      packages.removeAll(getSystemPackages());
      return packages;
   }

   private static Set systemPackages;
  
   public static Set getSystemPackages()
   {
      if (systemPackages == null)
         systemPackages = getClassLoaderPackages(ClassLoader.getSystemClassLoader());
      return systemPackages;
   }

   public static Set getClassLoaderPackages(ClassLoader cl)
   {
      PackageInfo info;
      if (cl instanceof PackageInfo)
         info = (PackageInfo) cl;
      else
         info = new WrappingClassLoader(cl);
      Package[] pkgs = info.getPackages();
      HashSet packages = new HashSet();
      for (int i = 0; i < pkgs.length; ++i)
         packages.add(pkgs[i].getName());
     
      return packages;
   }
  
   public static String getPackageName(String className)
   {
      int dot = className.lastIndexOf('/');
      if (dot != -1)
         return className.substring(0, dot);
      else
         return "";
   }
  
   // Constructors --------------------------------------------------
  
   // Public --------------------------------------------------------
  
   // Package protected ---------------------------------------------

   // Protected -----------------------------------------------------
  
   // Private -------------------------------------------------------
  
   // Inner classes -------------------------------------------------
}
TOP

Related Classes of org.jboss.util.ClassLoading

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.