static

in xstream/src/java/com/thoughtworks/xstream/core/JVM.java [89:194]


    static {
        final Exception exception = new RuntimeException();
        exception.fillInStackTrace();
        final StackTraceElement[] stackTrace = exception.getStackTrace();
        isUnnamedModule = !stackTrace[0].toString().contains("xstream@"); // Java 9: getModule() == null

        boolean test = true;
        Object unsafe = null;
        try {
            final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
            final Field unsafeField = unsafeClass.getDeclaredField("theUnsafe");
            unsafeField.setAccessible(true);
            unsafe = unsafeField.get(null);
            final Method allocateInstance = unsafeClass.getDeclaredMethod("allocateInstance", new Class[]{Class.class});
            allocateInstance.setAccessible(true);
            test = allocateInstance.invoke(unsafe, new Object[]{Test.class}) != null;
        } catch (final Exception | Error e) {
            test = false;
        }
        canAllocateWithUnsafe = test;
        test = false;
        Class<? extends ReflectionProvider> type = PureJavaReflectionProvider.class;
        if (canUseSunUnsafeReflectionProvider()) {
            Class<? extends ReflectionProvider> cls = loadClassForName(
                "com.thoughtworks.xstream.converters.reflection.SunUnsafeReflectionProvider");
            if (cls != null) {
                try {
                    final ReflectionProvider provider = DependencyInjectionFactory.newInstance(cls);
                    final Test t = (Test)provider.newInstance(Test.class);
                    try {
                        provider.writeField(t, "o", "object", Test.class);
                        provider.writeField(t, "c", Character.valueOf('c'), Test.class);
                        provider.writeField(t, "b", Byte.valueOf((byte)1), Test.class);
                        provider.writeField(t, "s", Short.valueOf((short)1), Test.class);
                        provider.writeField(t, "i", Integer.valueOf(1), Test.class);
                        provider.writeField(t, "l", Long.valueOf(1), Test.class);
                        provider.writeField(t, "f", Float.valueOf(1), Test.class);
                        provider.writeField(t, "d", Double.valueOf(1), Test.class);
                        provider.writeField(t, "bool", Boolean.TRUE, Test.class);
                        test = true;
                    } catch (final IncompatibleClassChangeError | ObjectAccessException e) {
                        cls = null;
                    }
                    if (cls == null) {
                        cls = loadClassForName(
                            "com.thoughtworks.xstream.converters.reflection.SunLimitedUnsafeReflectionProvider");
                    }
                    type = cls;
                } catch (final ObjectAccessException e) {
                }
            }
        }
        reflectionProviderType = type;
        canWriteWithUnsafe = test;
        final Comparator<Object> comparator = new Comparator<Object>() {
            @Override
            public int compare(final Object o1, final Object o2) {
                throw new RuntimeException();
            }
        };
        final SortedMap<Object, Object> map = new PresortedMap<>(comparator);
        map.put("one", null);
        map.put("two", null);
        try {
            new TreeMap<>(comparator).putAll(map);
            test = true;
        } catch (final RuntimeException e) {
            test = false;
        }
        optimizedTreeMapPutAll = test;
        final SortedSet<Object> set = new PresortedSet<>(comparator);
        set.addAll(map.keySet());
        try {
            new TreeSet<>(comparator).addAll(set);
            test = true;
        } catch (final RuntimeException e) {
            test = false;
        }
        optimizedTreeSetAddAll = test;
        try {
            new SimpleDateFormat("z").parse("UTC");
            test = true;
        } catch (final ParseException | RuntimeException e) {
            test = false;
        }
        canParseUTCDateFormat = test;
        try {
            new SimpleDateFormat("X").parse("Z");
            test = true;
        } catch (final ParseException | RuntimeException e) {
            test = false;
        }
        canParseISO8601TimeZoneInDateFormat = test;
        try {
            @SuppressWarnings("resource")
            final CustomObjectOutputStream stream = new CustomObjectOutputStream(null, null);
            test = stream != null;
        } catch (final RuntimeException | IOException e) {
            test = false;
        }
        canCreateDerivedObjectOutputStream = test;

        isAWTAvailable = loadClassForName("java.awt.Color", false) != null;
        isSwingAvailable = loadClassForName("javax.swing.LookAndFeel", false) != null;
        isSQLAvailable = loadClassForName("java.sql.Date") != null;
    }