⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 openconverter.java

📁 java1.6众多例子参考
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                Comparator comparator = ((SortedMap) valueMap).comparator();                if (comparator != null) {                    final String msg =                        "Cannot convert SortedMap with non-null comparator: " +                        comparator;                    IllegalArgumentException iae = new IllegalArgumentException(msg);                    OpenDataException ode = new OpenDataException(msg);                    ode.initCause(iae);                    throw ode;                }            }	    final TabularType tabularType = (TabularType) getOpenType();	    final TabularData table = new TabularDataSupport(tabularType);	    final CompositeType rowType = tabularType.getRowType();	    for (Map.Entry entry : valueMap.entrySet()) {		final Object openKey =		    keyConverter.toOpenValue(lookup, entry.getKey());		final Object openValue =		    valueConverter.toOpenValue(lookup, entry.getValue());		final CompositeData row;                row =                    new CompositeDataSupport(rowType, keyValueArray,                                             new Object[] {openKey,                                                           openValue});		table.put(row);	    }	    return table;	}	public final Object fromNonNullOpenValue(MXBeanLookup lookup, Object openValue)                throws InvalidObjectException {	    final TabularData table = (TabularData) openValue;	    final Collection<CompositeData> rows =		(Collection<CompositeData>) table.values();            final Map<Object, Object> valueMap =                sortedMap ? newSortedMap() : newMap();	    for (CompositeData row : rows) {		final Object key =		    keyConverter.fromOpenValue(lookup, row.get("key"));		final Object value =		    valueConverter.fromOpenValue(lookup, row.get("value"));		if (valueMap.put(key, value) != null) {		    final String msg =			"Duplicate entry in TabularData: key=" + key;		    throw new InvalidObjectException(msg);		}	    }	    return valueMap;	}        void checkReconstructible() throws InvalidObjectException {            keyConverter.checkReconstructible();            valueConverter.checkReconstructible();        }	private final boolean sortedMap;        private final OpenConverter keyConverter;	private final OpenConverter valueConverter;    }    private static final class CompositeConverter extends OpenConverter {	CompositeConverter(Class targetClass,			   CompositeType compositeType,			   String[] itemNames,			   Method[] getters) throws OpenDataException {	    super(targetClass, compositeType, CompositeData.class);	    assert(itemNames.length == getters.length);	    this.itemNames = itemNames;	    this.getters = getters;	    this.getterConverters = new OpenConverter[getters.length];	    for (int i = 0; i < getters.length; i++) {		Type retType = getters[i].getGenericReturnType();		getterConverters[i] = OpenConverter.toConverter(retType);	    }	}	final Object toNonNullOpenValue(MXBeanLookup lookup, Object value)                throws OpenDataException {            CompositeType ct = (CompositeType) getOpenType();	    if (value instanceof CompositeDataView)		return ((CompositeDataView) value).toCompositeData(ct);            if (value == null)                return null;	    Object[] values = new Object[getters.length];	    for (int i = 0; i < getters.length; i++) {		try {		    Object got = getters[i].invoke(value, (Object[]) null);		    values[i] = getterConverters[i].toOpenValue(lookup, got);		} catch (Exception e) {		    throw openDataException("Error calling getter for " +                                            itemNames[i] + ": " + e, e);		}	    }	    return new CompositeDataSupport(ct, itemNames, values);	}	/** Determine how to convert back from the CompositeData into	    the original Java type.  For a type that is not reconstructible,            this method will fail every time, and will throw the right            exception. */	private synchronized void makeCompositeBuilder()		throws InvalidObjectException {	    if (compositeBuilder != null)		return;	    Class targetClass = (Class<?>) getTargetType();            /* In this 2D array, each subarray is a set of builders where               there is no point in consulting the ones after the first if               the first refuses.  */	    CompositeBuilder[][] builders = {                {                    new CompositeBuilderViaFrom(targetClass, itemNames),                },                {                    new CompositeBuilderViaConstructor(targetClass, itemNames),                },                {                    new CompositeBuilderCheckGetters(targetClass, itemNames,                                                     getterConverters),                    new CompositeBuilderViaSetters(targetClass, itemNames),                    new CompositeBuilderViaProxy(targetClass, itemNames),                },	    };	    CompositeBuilder foundBuilder = null;	    /* We try to make a meaningful exception message by	       concatenating each Builder's explanation of why it	       isn't applicable.  */	    StringBuffer whyNots = new StringBuffer();        find:	    for (CompositeBuilder[] relatedBuilders : builders) {                for (int i = 0; i < relatedBuilders.length; i++) {                    CompositeBuilder builder = relatedBuilders[i];                    String whyNot = builder.applicable(getters);                    if (whyNot == null) {                        foundBuilder = builder;                        break find;                    }                    if (whyNot.length() > 0) {                        if (whyNots.length() > 0)                            whyNots.append("; ");                        whyNots.append(whyNot);                        if (i == 0)                           break; // skip other builders in this group                    }                }	    }	    if (foundBuilder == null) {		final String msg =		    "Do not know how to make a " + targetClass.getName() +		    " from a CompositeData: " + whyNots;		throw new InvalidObjectException(msg);	    }	    compositeBuilder = foundBuilder;	}	void checkReconstructible() throws InvalidObjectException {            makeCompositeBuilder();        }        public final Object fromNonNullOpenValue(MXBeanLookup lookup, Object value)                throws InvalidObjectException {	    makeCompositeBuilder();	    return compositeBuilder.fromCompositeData(lookup,                                                      (CompositeData) value,                                                      itemNames,                                                      getterConverters);	}	private final String[] itemNames;	private final Method[] getters;	private final OpenConverter[] getterConverters;	private CompositeBuilder compositeBuilder;    }    /** Converts from a CompositeData to an instance of the targetClass.  */    private static abstract class CompositeBuilder {	CompositeBuilder(Class targetClass, String[] itemNames) {	    this.targetClass = targetClass;            this.itemNames = itemNames;	}	Class getTargetClass() {	    return targetClass;	}        String[] getItemNames() {            return itemNames;        }	/** If the subclass is appropriate for targetClass, then the	    method returns null.  If the subclass is not appropriate,	    then the method returns an explanation of why not.  If the	    subclass should be appropriate but there is a problem,	    then the method throws InvalidObjectException.  */	abstract String applicable(Method[] getters)                throws InvalidObjectException;        abstract Object fromCompositeData(MXBeanLookup lookup, CompositeData cd,                                          String[] itemNames,                                          OpenConverter[] converters)                throws InvalidObjectException;	private final Class targetClass;        private final String[] itemNames;    }    /** Builder for when the target class has a method "public static	from(CompositeData)".  */    private static final class CompositeBuilderViaFrom	    extends CompositeBuilder {	CompositeBuilderViaFrom(Class targetClass, String[] itemNames) {	    super(targetClass, itemNames);	}	String applicable(Method[] getters) throws InvalidObjectException {	    // See if it has a method "T from(CompositeData)"	    // as is conventional for a CompositeDataView	    Class targetClass = getTargetClass();	    try {		Method fromMethod =		    targetClass.getMethod("from",					  new Class[] {CompositeData.class});		if (!Modifier.isStatic(fromMethod.getModifiers())) {		    final String msg =			"Method from(CompositeData) is not static";		    throw new InvalidObjectException(msg);		}		if (fromMethod.getReturnType() != getTargetClass()) {		    final String msg =			"Method from(CompositeData) returns " +			fromMethod.getReturnType().getName() +			" not " + targetClass.getName();		    throw new InvalidObjectException(msg);		}		this.fromMethod = fromMethod;		return null; // success!	    } catch (InvalidObjectException e) {		throw e;	    } catch (Exception e) {		// OK: it doesn't have the method		return "no method from(CompositeData)";	    }	}	final Object fromCompositeData(MXBeanLookup lookup, CompositeData cd,                                 String[] itemNames,                                 OpenConverter[] converters)                throws InvalidObjectException {	    try {		return fromMethod.invoke(null, cd);	    } catch (Exception e) {		final String msg = "Failed to invoke from(CompositeData)";                throw invalidObjectException(msg, e);	    }	}	private Method fromMethod;    }    /** This builder never actually returns success.  It simply serves        to check whether the other builders in the same group have any        chance of success.  If any getter in the targetClass returns        a type that we don't know how to reconstruct, then we will        not be able to make a builder, and there is no point in repeating        the error about the problematic getter as many times as there are        candidate builders.  Instead, the "applicable" method will return        an explanatory string, and the other builders will be skipped.        If all the getters are OK, then the "applicable" method will return        an empty string and the other builders will be tried.  */    private static class CompositeBuilderCheckGetters extends CompositeBuilder {        CompositeBuilderCheckGetters(Class targetClass, String[] itemNames,                                     OpenConverter[] getterConverters) {            super(targetClass, itemNames);            this.getterConverters = getterConverters;        }        String applicable(Method[] getters) {            for (int i = 0; i < getters.length; i++) {                try {                    getterConverters[i].checkReconstructible();                } catch (InvalidObjectException e) {                    return "method " + getters[i].getName() + " returns type " +                        "that cannot be mapped back from OpenData";                }            }            return "";        }                final Object fromCompositeData(MXBeanLookup lookup, CompositeData cd,                                       String[] itemNames,                                       OpenConverter[] converters) {            throw new Error();        }                private final OpenConverter[] getterConverters;    }    /** Builder for when the target class has a setter for every getter. */    private static class CompositeBuilderViaSetters extends CompositeBuilder {        CompositeBuilderViaSetters(Class targetClass, String[] itemNames) {            super(targetClass, itemNames);        }        String applicable(Method[] getters) {            try {                Constructor c = getTargetClass().getConstructor((Class[]) null);            } catch (Exception e) {                return "does not have a public no-arg constructor";            }            Method[] setters = new Method[getters.length];            for (int i = 0; i < getters.length; i++) {                Method getter = getters[i];                Class returnType = getter.getReturnType();                String name = propertyName(getter);                String setterName = "set" + name;                Method setter;                try {                    setter = getTargetClass().getMethod(setterName, returnType);                    if (setter.getReturnType() != void.class)                        throw new Exception();                } catch (Exception e) {                    return "not all getters have corresponding setters " +                           "(" + getter + ")";                }                setters[i] = setter;            }            this.setters = setters;            return null;        }        Object fromCompositeData(MXBeanLookup lookup, CompositeData cd,                                 String[] itemNames,                                 OpenConverter[] converters)                throws InvalidObjectException {            Object o;            try {                o = getTargetClass().newInstance();                for (int i = 0; i < itemNames.length; i++) {		    if (cd.containsKey(itemNames[i])) {			Object openItem = cd.get(itemNames[i]);			Object javaItem =			    converters[i].fromOpenValue(lookup, openItem);			setters[i].invoke(o, javaItem);		    }                }            } catch (Exception e) {                throw invalidObjectException(e);            }            return o;        }        private Method[] setters;    }    /** Builder for when the target class has a constructor that is	annotated with @ConstructorProperties so we can see the correspondence

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -