1 package com.thoughtworks.xstream.core; 2 3 import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider; 4 import com.thoughtworks.xstream.converters.reflection.ReflectionProvider; 5 6 import java.util.HashMap ; 7 import java.util.Map ; 8 9 public class JVM { 10 11 private static Map classCache = new HashMap (); 12 private static ReflectionProvider reflectionProvider; 13 14 public static boolean is14() { 15 return loadClass("java.util.LinkedHashMap") != null; 16 } 17 18 public static Class loadClass(String name) { 19 if (classCache.containsKey(name)) { 20 return (Class ) classCache.get(name); 21 } else { 22 try { 23 Class cls = Class.forName(name); 24 classCache.put(name, cls); 25 return cls; 26 } catch (ClassNotFoundException e) { 27 return null; 28 } 29 } 30 } 31 32 public static synchronized ReflectionProvider bestReflectionProvider() { 33 if (reflectionProvider == null) { 34 try { 35 if (loadClass("sun.misc.Unsafe") != null) { 36 String cls = "com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider"; 37 reflectionProvider = (ReflectionProvider) loadClass(cls).newInstance(); 38 } else { 39 reflectionProvider = new PureJavaReflectionProvider(); 40 } 41 } catch (InstantiationException e) { 42 reflectionProvider = new PureJavaReflectionProvider(); 43 } catch (IllegalAccessException e) { 44 reflectionProvider = new PureJavaReflectionProvider(); 45 } 46 } 47 return reflectionProvider; 48 } 49 50 } 51 | Popular Tags |