Java Code Examples for com.thoughtworks.xstream.core.JVM#isVersion()

The following examples show how to use com.thoughtworks.xstream.core.JVM#isVersion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ToAttributedValueConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new ToAttributedValueConverter instance.
 * 
 * @param type the type that is handled by this converter instance
 * @param mapper the mapper in use
 * @param reflectionProvider the reflection provider in use
 * @param lookup the converter lookup in use
 * @param valueFieldName the field defining the tag's value (may be null)
 * @param valueDefinedIn the type defining the field
 */
public ToAttributedValueConverter(
    final Class type, final Mapper mapper, final ReflectionProvider reflectionProvider,
    final ConverterLookup lookup, final String valueFieldName, Class valueDefinedIn) {
    this.type = type;
    this.mapper = mapper;
    this.reflectionProvider = reflectionProvider;
    this.lookup = lookup;

    if (valueFieldName == null) {
        valueField = null;
    } else {
        Field field = null;
        try {
            field = (valueDefinedIn != null ? valueDefinedIn : type)
                .getDeclaredField(valueFieldName);
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
        } catch (NoSuchFieldException e) {
            throw new IllegalArgumentException(e.getMessage() + ": " + valueFieldName);
        }
        this.valueField = field;
    }
    enumMapper = JVM.isVersion(5) ? UseAttributeForEnumMapper.createEnumMapper(mapper) : null;
}
 
Example 2
Source File: PureJavaReflectionProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void validateFieldAccess(Field field) {
    if (Modifier.isFinal(field.getModifiers())) {
        if (JVM.isVersion(5)) {
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
        } else {
            throw new ObjectAccessException("Invalid final field "
                    + field.getDeclaringClass().getName() + "." + field.getName());
        }
    }
}
 
Example 3
Source File: XStream.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Mapper buildMapper() {
    Mapper mapper = new DefaultMapper(classLoaderReference);
    if (useXStream11XmlFriendlyMapper()) {
        mapper = new XStream11XmlFriendlyMapper(mapper);
    }
    mapper = new DynamicProxyMapper(mapper);
    mapper = new PackageAliasingMapper(mapper);
    mapper = new ClassAliasingMapper(mapper);
    mapper = new ElementIgnoringMapper(mapper);
    mapper = new FieldAliasingMapper(mapper);
    mapper = new AttributeAliasingMapper(mapper);
    mapper = new SystemAttributeAliasingMapper(mapper);
    mapper = new ImplicitCollectionMapper(mapper, reflectionProvider);
    mapper = new OuterClassMapper(mapper);
    mapper = new ArrayMapper(mapper);
    mapper = new DefaultImplementationsMapper(mapper);
    mapper = new AttributeMapper(mapper, converterLookup, reflectionProvider);
    if (JVM.isVersion(5)) {
        mapper = buildMapperDynamically(
            "com.thoughtworks.xstream.mapper.EnumMapper", new Class[]{Mapper.class},
            new Object[]{mapper});
    }
    mapper = new LocalConversionMapper(mapper);
    mapper = new ImmutableTypesMapper(mapper);
    if (JVM.isVersion(8)) {
        mapper = buildMapperDynamically("com.thoughtworks.xstream.mapper.LambdaMapper", new Class[]{Mapper.class},
            new Object[]{mapper});
    }
    mapper = new SecurityMapper(mapper);
    if (JVM.isVersion(5)) {
        mapper = buildMapperDynamically(ANNOTATION_MAPPER_TYPE, new Class[]{
            Mapper.class, ConverterRegistry.class, ConverterLookup.class,
            ClassLoaderReference.class, ReflectionProvider.class}, new Object[]{
            mapper, converterRegistry, converterLookup, classLoaderReference,
            reflectionProvider});
    }
    mapper = wrapMapper((MapperWrapper)mapper);
    mapper = new CachingMapper(mapper);
    return mapper;
}
 
Example 4
Source File: NamedMapConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs a NamedMapConverter with attribute support handling an explicit Map type.
 * 
 * @param type the Map type this instance will handle
 * @param mapper the mapper
 * @param entryName the name of the entry elements
 * @param keyName the name of the key elements
 * @param keyType the base type of key elements
 * @param valueName the name of the value elements
 * @param valueType the base type of value elements
 * @param keyAsAttribute flag to write key as attribute of entry element
 * @param valueAsAttribute flag to write value as attribute of entry element
 * @param lookup used to lookup SingleValueConverter for attributes
 * @since 1.4.5
 */
public NamedMapConverter(
    Class type, Mapper mapper, String entryName, String keyName, Class keyType,
    String valueName, Class valueType, boolean keyAsAttribute, boolean valueAsAttribute,
    ConverterLookup lookup) {
    super(mapper, type);
    this.entryName = entryName != null && entryName.length() == 0 ? null : entryName;
    this.keyName = keyName != null && keyName.length() == 0 ? null : keyName;
    this.keyType = keyType;
    this.valueName = valueName != null && valueName.length() == 0 ? null : valueName;
    this.valueType = valueType;
    this.keyAsAttribute = keyAsAttribute;
    this.valueAsAttribute = valueAsAttribute;
    this.lookup = lookup;
    enumMapper = JVM.isVersion(5) ? UseAttributeForEnumMapper.createEnumMapper(mapper) : null;

    if (keyType == null || valueType == null) {
        throw new IllegalArgumentException("Class types of key and value are mandatory");
    }
    if (entryName == null) {
        if (keyAsAttribute || valueAsAttribute) {
            throw new IllegalArgumentException(
                "Cannot write attributes to map entry, if map entry must be omitted");
        }
        if (valueName == null) {
            throw new IllegalArgumentException(
                "Cannot write value as text of entry, if entry must be omitted");
        }
    }
    if (keyName == null) {
        throw new IllegalArgumentException("Cannot write key without name");
    }
    if (valueName == null) {
        if (valueAsAttribute) {
            throw new IllegalArgumentException(
                "Cannot write value as attribute without name");
        } else if (!keyAsAttribute) {
            throw new IllegalArgumentException(
                "Cannot write value as text of entry, if key is also child element");
        }
    }
    if (keyAsAttribute && valueAsAttribute && keyName.equals(valueName)) {
        throw new IllegalArgumentException(
            "Cannot write key and value with same attribute name");
    }
}
 
Example 5
Source File: XStream.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Setup the security framework of a XStream instance.
 * <p>
 * This method is a pure helper method for XStream 1.4.x. It initializes an XStream instance with a white list of
 * well-known and simply types of the Java runtime as it is done in XStream 1.5.x by default. This method will do
 * therefore nothing in XStream 1.5.
 * </p>
 * 
 * @param xstream
 * @since 1.4.10
 */
public static void setupDefaultSecurity(final XStream xstream) {
    if (!xstream.securityInitialized) {
        xstream.addPermission(NoTypePermission.NONE);
        xstream.addPermission(NullPermission.NULL);
        xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
        xstream.addPermission(ArrayTypePermission.ARRAYS);
        xstream.addPermission(InterfaceTypePermission.INTERFACES);
        xstream.allowTypeHierarchy(Calendar.class);
        xstream.allowTypeHierarchy(Collection.class);
        xstream.allowTypeHierarchy(Map.class);
        xstream.allowTypeHierarchy(Map.Entry.class);
        xstream.allowTypeHierarchy(Member.class);
        xstream.allowTypeHierarchy(Number.class);
        xstream.allowTypeHierarchy(Throwable.class);
        xstream.allowTypeHierarchy(TimeZone.class);

        Class type = JVM.loadClassForName("java.lang.Enum");
        if (type != null) {
            xstream.allowTypeHierarchy(type);
        }
        type = JVM.loadClassForName("java.nio.file.Path");
        if (type != null) {
            xstream.allowTypeHierarchy(type);
        }

        final Set types = new HashSet();
        types.add(BitSet.class);
        types.add(Charset.class);
        types.add(Class.class);
        types.add(Currency.class);
        types.add(Date.class);
        types.add(DecimalFormatSymbols.class);
        types.add(File.class);
        types.add(Locale.class);
        types.add(Object.class);
        types.add(Pattern.class);
        types.add(StackTraceElement.class);
        types.add(String.class);
        types.add(StringBuffer.class);
        types.add(JVM.loadClassForName("java.lang.StringBuilder"));
        types.add(URL.class);
        types.add(URI.class);
        types.add(JVM.loadClassForName("java.util.UUID"));
        if (JVM.isSQLAvailable()) {
            types.add(JVM.loadClassForName("java.sql.Timestamp"));
            types.add(JVM.loadClassForName("java.sql.Time"));
            types.add(JVM.loadClassForName("java.sql.Date"));
        }
        if (JVM.isVersion(8)) {
            xstream.allowTypeHierarchy(JVM.loadClassForName("java.time.Clock"));
            types.add(JVM.loadClassForName("java.time.Duration"));
            types.add(JVM.loadClassForName("java.time.Instant"));
            types.add(JVM.loadClassForName("java.time.LocalDate"));
            types.add(JVM.loadClassForName("java.time.LocalDateTime"));
            types.add(JVM.loadClassForName("java.time.LocalTime"));
            types.add(JVM.loadClassForName("java.time.MonthDay"));
            types.add(JVM.loadClassForName("java.time.OffsetDateTime"));
            types.add(JVM.loadClassForName("java.time.OffsetTime"));
            types.add(JVM.loadClassForName("java.time.Period"));
            types.add(JVM.loadClassForName("java.time.Ser"));
            types.add(JVM.loadClassForName("java.time.Year"));
            types.add(JVM.loadClassForName("java.time.YearMonth"));
            types.add(JVM.loadClassForName("java.time.ZonedDateTime"));
            xstream.allowTypeHierarchy(JVM.loadClassForName("java.time.ZoneId"));
            types.add(JVM.loadClassForName("java.time.chrono.HijrahDate"));
            types.add(JVM.loadClassForName("java.time.chrono.JapaneseDate"));
            types.add(JVM.loadClassForName("java.time.chrono.JapaneseEra"));
            types.add(JVM.loadClassForName("java.time.chrono.MinguoDate"));
            types.add(JVM.loadClassForName("java.time.chrono.ThaiBuddhistDate"));
            types.add(JVM.loadClassForName("java.time.chrono.Ser"));
            xstream.allowTypeHierarchy(JVM.loadClassForName("java.time.chrono.Chronology"));
            types.add(JVM.loadClassForName("java.time.temporal.ValueRange"));
            types.add(JVM.loadClassForName("java.time.temporal.WeekFields"));
        }
        types.remove(null);

        final Iterator iter = types.iterator();
        final Class[] classes = new Class[types.size()];
        for (int i = 0; i < classes.length; ++i) {
            classes[i] = (Class)iter.next();
        }
        xstream.allowTypes(classes);
    } else {
        throw new IllegalArgumentException("Security framework of XStream instance already initialized");
    }
}