KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > core > JVM


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 JavaDoc;
7 import java.util.Map JavaDoc;
8
9 public class JVM {
10
11     private static Map JavaDoc classCache = new HashMap JavaDoc();
12     private static ReflectionProvider reflectionProvider;
13
14     public static boolean is14() {
15         return loadClass("java.util.LinkedHashMap") != null;
16     }
17
18     public static Class JavaDoc loadClass(String JavaDoc name) {
19         if (classCache.containsKey(name)) {
20             return (Class JavaDoc) classCache.get(name);
21         } else {
22             try {
23                 Class JavaDoc cls = Class.forName(name);
24                 classCache.put(name, cls);
25                 return cls;
26             } catch (ClassNotFoundException JavaDoc 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 JavaDoc cls = "com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider";
37                     reflectionProvider = (ReflectionProvider) loadClass(cls).newInstance();
38                 } else {
39                     reflectionProvider = new PureJavaReflectionProvider();
40                 }
41             } catch (InstantiationException JavaDoc e) {
42                 reflectionProvider = new PureJavaReflectionProvider();
43             } catch (IllegalAccessException JavaDoc e) {
44                 reflectionProvider = new PureJavaReflectionProvider();
45             }
46         }
47         return reflectionProvider;
48     }
49
50 }
51
Popular Tags