一、前言
关于drools-core-5.2.0.Final.jar工具类,对资源路径转换convertClassToResourcePath、文件管理合并路径拼接relative、基于class名称的对象实例化instantiateObject、根据class获取包路径getPackage等。
二、源码说明
package org.drools.core.util;@b@@b@import java.io.File;@b@import java.util.ArrayList;@b@import java.util.Collections;@b@import java.util.HashMap;@b@import java.util.List;@b@import java.util.Map;@b@@b@public final class ClassUtils@b@{@b@ private static Map classes = Collections.synchronizedMap(new HashMap());@b@ private static final String STAR = "*";@b@@b@ public static String convertResourceToClassName(String pResourceName)@b@ {@b@ return stripExtension(pResourceName).replace('/', '.');@b@ }@b@@b@ public static String convertClassToResourcePath(String pName)@b@ {@b@ return pName.replace('.', '/') + ".class";@b@ }@b@@b@ public static String stripExtension(String pResourceName)@b@ {@b@ int i = pResourceName.lastIndexOf(46);@b@ String withoutExtension = pResourceName.substring(0, i);@b@@b@ return withoutExtension;@b@ }@b@@b@ public static String toJavaCasing(String pName) {@b@ char[] name = pName.toLowerCase().toCharArray();@b@ name[0] = Character.toUpperCase(name[0]);@b@ return new String(name);@b@ }@b@@b@ public static String clazzName(File base, File file)@b@ {@b@ int rootLength = base.getAbsolutePath().length();@b@ String absFileName = file.getAbsolutePath();@b@ int p = absFileName.lastIndexOf(46);@b@ String relFileName = absFileName.substring(rootLength + 1, p);@b@@b@ String clazzName = relFileName.replace(File.separatorChar, '.');@b@@b@ return clazzName;@b@ }@b@@b@ public static String relative(File base, File file)@b@ {@b@ int rootLength = base.getAbsolutePath().length();@b@ String absFileName = file.getAbsolutePath();@b@ String relFileName = absFileName.substring(rootLength + 1);@b@ return relFileName;@b@ }@b@@b@ public static String canonicalName(Class clazz) {@b@ StringBuilder name = new StringBuilder();@b@@b@ if (clazz.isArray()) {@b@ name.append(canonicalName(clazz.getComponentType()));@b@ name.append("[]");@b@ } else if (clazz.getDeclaringClass() == null) {@b@ name.append(clazz.getName());@b@ } else {@b@ name.append(canonicalName(clazz.getDeclaringClass()));@b@ name.append(".");@b@ name.append(clazz.getName().substring(clazz.getDeclaringClass().getName().length() + 1));@b@ }@b@@b@ return name.toString();@b@ }@b@@b@ public static Object instantiateObject(String className) {@b@ return instantiateObject(className, null);@b@ }@b@@b@ public static Object instantiateObject(String className, ClassLoader classLoader)@b@ {@b@ Class cls = (Class)classes.get(className);@b@ if (cls == null) {@b@ try {@b@ cls = Class.forName(className);@b@ }@b@ catch (Exception e)@b@ {@b@ }@b@@b@ if ((cls == null) && (classLoader != null))@b@ try {@b@ cls = classLoader.loadClass(className);@b@ }@b@ catch (Exception e)@b@ {@b@ }@b@@b@ if (cls == null)@b@ try {@b@ cls = ClassUtils.class.getClassLoader().loadClass(className);@b@ }@b@ catch (Exception e)@b@ {@b@ }@b@@b@ if (cls == null)@b@ try {@b@ cls = Thread.currentThread().getContextClassLoader().loadClass(className);@b@ }@b@ catch (Exception e)@b@ {@b@ }@b@@b@ if (cls == null)@b@ try {@b@ cls = ClassLoader.getSystemClassLoader().loadClass(className);@b@ }@b@ catch (Exception e)@b@ {@b@ }@b@@b@ if (cls != null) {@b@ classes.put(className, cls);@b@ }@b@ else@b@ throw new RuntimeException("Unable to load class '" + className + "'");@b@@b@ }@b@@b@ Object object = null;@b@ try {@b@ object = cls.newInstance();@b@ } catch (Throwable e) {@b@ throw new RuntimeException("Unable to instantiate object for class '" + className + "'", e);@b@ }@b@@b@ return object;@b@ }@b@@b@ public static void addImportStylePatterns(Map<String, Object> patterns, String str)@b@ {@b@ if ((str == null) || ("".equals(str.trim()))) {@b@ return;@b@ }@b@@b@ String[] items = str.split(" ");@b@ for (int i = 0; i < items.length; ++i) {@b@ List list;@b@ String qualifiedNamespace = items[i].substring(0, items[i].lastIndexOf(46)).trim();@b@@b@ String name = items[i].substring(items[i].lastIndexOf(46) + 1).trim();@b@ Object object = patterns.get(qualifiedNamespace);@b@ if (object == null) {@b@ if ("*".equals(name)) {@b@ patterns.put(qualifiedNamespace, "*");@b@ }@b@ else@b@ {@b@ list = new ArrayList();@b@ list.add(name);@b@ patterns.put(qualifiedNamespace, list);@b@ }@b@ }@b@ else if (name.equals("*"))@b@ {@b@ patterns.put(qualifiedNamespace, "*");@b@ }@b@ else@b@ {@b@ list = (List)object;@b@ if (!(list.contains(object)))@b@ list.add(name);@b@ }@b@ }@b@ }@b@@b@ public static boolean isMatched(Map<String, Object> patterns, String className)@b@ {@b@ String qualifiedNamespace = className.substring(0, className.lastIndexOf(46)).trim();@b@@b@ String name = className.substring(className.lastIndexOf(46) + 1).trim();@b@ Object object = patterns.get(qualifiedNamespace);@b@ if (object == null)@b@ return true;@b@ if ("*".equals(object))@b@ return false;@b@ if (patterns.containsKey("*"))@b@ {@b@ return true;@b@ }@b@ List list = (List)object;@b@ return (!(list.contains(name)));@b@ }@b@@b@ public static String getPackage(Class<?> cls)@b@ {@b@ Package pkg = (cls.isArray()) ? cls.getComponentType().getPackage() : cls.getPackage();@b@ if (pkg == null) {@b@ int dotPos = cls.getName().lastIndexOf(46);@b@ if (dotPos > 0) {@b@ return cls.getName().substring(0, dotPos - 1);@b@ }@b@@b@ return "";@b@ }@b@@b@ return pkg.getName();@b@ }@b@}