001: /*
002: * Copyright (C) 2007 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 01. February 2007 by Joerg Schaible
010: */
011: package com.thoughtworks.xstream.converters.reflection;
012:
013: import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
014:
015: import java.lang.reflect.Field;
016: import java.lang.reflect.InvocationTargetException;
017: import java.lang.reflect.Method;
018: import java.lang.reflect.Modifier;
019: import java.text.AttributedCharacterIterator;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: /**
025: * An abstract converter implementation for constants of
026: * {@link AttributedCharacterIterator.Attribute} and derived types.
027: *
028: * @author Jörg Schaible
029: * @since 1.2.2
030: */
031: public class AbstractAttributedCharacterIteratorAttributeConverter
032: extends AbstractSingleValueConverter {
033:
034: private static final Method getName;
035: static {
036: try {
037: getName = AttributedCharacterIterator.Attribute.class
038: .getDeclaredMethod("getName", (Class[]) null);
039: } catch (NoSuchMethodException e) {
040: throw new ExceptionInInitializerError(
041: "Missing AttributedCharacterIterator.Attribute.getName()");
042: }
043: }
044:
045: private final Class type;
046: private transient Map attributeMap;
047: private transient FieldDictionary fieldDictionary;
048:
049: public AbstractAttributedCharacterIteratorAttributeConverter(
050: final Class type) {
051: super ();
052: this .type = type;
053: readResolve();
054: }
055:
056: public boolean canConvert(final Class type) {
057: return type == this .type;
058: }
059:
060: public String toString(final Object source) {
061: AttributedCharacterIterator.Attribute attribute = (AttributedCharacterIterator.Attribute) source;
062: try {
063: if (!getName.isAccessible()) {
064: getName.setAccessible(true);
065: }
066: return (String) getName.invoke(attribute, (Object[]) null);
067: } catch (IllegalAccessException e) {
068: throw new ObjectAccessException(
069: "Cannot get name of AttributedCharacterIterator.Attribute",
070: e);
071: } catch (InvocationTargetException e) {
072: throw new ObjectAccessException(
073: "Cannot get name of AttributedCharacterIterator.Attribute",
074: e.getTargetException());
075: }
076: }
077:
078: public Object fromString(final String str) {
079: return attributeMap.get(str);
080: }
081:
082: private Object readResolve() {
083: fieldDictionary = new FieldDictionary();
084: attributeMap = new HashMap();
085: for (final Iterator iterator = fieldDictionary.fieldsFor(type); iterator
086: .hasNext();) {
087: final Field field = (Field) iterator.next();
088: if (field.getType() == type
089: && Modifier.isStatic(field.getModifiers())) {
090: try {
091: final Object attribute = field.get(null);
092: attributeMap.put(toString(attribute), attribute);
093: } catch (IllegalAccessException e) {
094: throw new ObjectAccessException(
095: "Cannot get object of " + field, e);
096: }
097: }
098: }
099: return this;
100: }
101:
102: }
|