/** * Constructs an {@code OpenMBeanParameterInfoSupport} instance, which describes the parameter * used in one or more operations or constructors of a class of open MBeans, with the specified * {@code name}, {@code openType}, {@code description}, and {@code descriptor}. * * <p>The {@code descriptor} can contain entries that will define the values returned by certain * methods of this class, as explained in the {@link <a href="package-summary.html#constraints"> * package description</a>}. * * @param name cannot be a null or empty string. * @param description cannot be a null or empty string. * @param openType cannot be null. * @param descriptor The descriptor for the parameter. This may be null which is equivalent to an * empty descriptor. * @throws IllegalArgumentException if {@code name} or {@code description} are null or empty * string, or {@code openType} is null, or the descriptor entries are invalid as described in * the {@link <a href="package-summary.html#constraints">package description</a>}. * @since 1.6 */ public OpenMBeanParameterInfoSupport( String name, String description, OpenType<?> openType, Descriptor descriptor) { // Construct parent's state // super( name, (openType == null) ? null : openType.getClassName(), description, ImmutableDescriptor.union( descriptor, (openType == null) ? null : openType.getDescriptor())); // Initialize this instance's specific state // this.openType = openType; descriptor = getDescriptor(); // replace null by empty this.defaultValue = valueFrom(descriptor, "defaultValue", openType); this.legalValues = valuesFrom(descriptor, "legalValues", openType); this.minValue = comparableValueFrom(descriptor, "minValue", openType); this.maxValue = comparableValueFrom(descriptor, "maxValue", openType); try { check(this); } catch (OpenDataException e) { throw new IllegalArgumentException(e.getMessage(), e); } }
/** * Returns true if the given key is valid for the * * @link{TabularType} of this instance. * @return true if the key is valid. * @throws NullPointerException if <code>key</code> is null. */ private boolean isKeyValid(Object[] key) { Iterator<String> it = tabularType.getIndexNames().iterator(); CompositeType rowType = tabularType.getRowType(); for (int a = 0; it.hasNext(); ++a) { OpenType<?> type = rowType.getType(it.next()); if (!(type.isValue(key[a]))) return false; } return true; }
private CompositeDataSupport(SortedMap<String, Object> items, CompositeType compositeType) throws OpenDataException { // Check compositeType is not null // if (compositeType == null) { throw new IllegalArgumentException("Argument compositeType cannot be null."); } // item names defined in compositeType: Set<String> namesFromType = compositeType.keySet(); Set<String> namesFromItems = items.keySet(); // This is just a comparison, but we do it this way for a better // exception message. if (!namesFromType.equals(namesFromItems)) { Set<String> extraFromType = new TreeSet<String>(namesFromType); extraFromType.removeAll(namesFromItems); Set<String> extraFromItems = new TreeSet<String>(namesFromItems); extraFromItems.removeAll(namesFromType); if (!extraFromType.isEmpty() || !extraFromItems.isEmpty()) { throw new OpenDataException( "Item names do not match CompositeType: " + "names in items but not in CompositeType: " + extraFromItems + "; names in CompositeType but not in items: " + extraFromType); } } // Check each value, if not null, is of the open type defined for the // corresponding item for (String name : namesFromType) { Object value = items.get(name); if (value != null) { OpenType<?> itemType = compositeType.getType(name); if (!itemType.isValue(value)) { throw new OpenDataException( "Argument value of wrong type for item " + name + ": value " + value + ", type " + itemType); } } } // Initialize internal fields: compositeType and contents // this.compositeType = compositeType; this.contents = items; }
private static <T> T convertFromString(String s, OpenType<T> openType) { Class<T> c; try { ReflectUtil.checkPackageAccess(openType.safeGetClassName()); c = cast(Class.forName(openType.safeGetClassName())); } catch (ClassNotFoundException e) { throw new NoClassDefFoundError(e.toString()); // can't happen } // Look for: public static T valueOf(String) Method valueOf; try { // It is safe to call this plain Class.getMethod because the class "c" // was checked before by ReflectUtil.checkPackageAccess(openType.safeGetClassName()); valueOf = c.getMethod("valueOf", String.class); if (!Modifier.isStatic(valueOf.getModifiers()) || valueOf.getReturnType() != c) valueOf = null; } catch (NoSuchMethodException e) { valueOf = null; } if (valueOf != null) { try { return c.cast(MethodUtil.invoke(valueOf, null, new Object[] {s})); } catch (Exception e) { final String msg = "Could not convert \"" + s + "\" using method: " + valueOf; throw new IllegalArgumentException(msg, e); } } // Look for: public T(String) Constructor<T> con; try { // It is safe to call this plain Class.getConstructor because the class "c" // was checked before by ReflectUtil.checkPackageAccess(openType.safeGetClassName()); con = c.getConstructor(String.class); } catch (NoSuchMethodException e) { con = null; } if (con != null) { try { return con.newInstance(s); } catch (Exception e) { final String msg = "Could not convert \"" + s + "\" using constructor: " + con; throw new IllegalArgumentException(msg, e); } } throw new IllegalArgumentException( "Don't know how to convert " + "string to " + openType.getTypeName()); }
private <T> OpenMBeanParameterInfoSupport( String name, String description, OpenType<T> openType, T defaultValue, T[] legalValues, Comparable<T> minValue, Comparable<T> maxValue) throws OpenDataException { super( name, (openType == null) ? null : openType.getClassName(), description, makeDescriptor(openType, defaultValue, legalValues, minValue, maxValue)); this.openType = openType; Descriptor d = getDescriptor(); this.defaultValue = defaultValue; this.minValue = minValue; this.maxValue = maxValue; // We already converted the array into an unmodifiable Set // in the descriptor. this.legalValues = (Set<?>) d.getFieldValue("legalValues"); check(this); }
private static <T> T convertFrom(Object x, OpenType<T> openType) { if (openType.isValue(x)) { T t = OpenMBeanAttributeInfoSupport.<T>cast(x); return t; } return convertFromStrings(x, openType); }
static <T> Set<T> valuesFrom(Descriptor d, String name, OpenType<T> openType) { Object x = d.getFieldValue(name); if (x == null) return null; Collection<?> coll; if (x instanceof Set<?>) { Set<?> set = (Set<?>) x; boolean asis = true; for (Object element : set) { if (!openType.isValue(element)) { asis = false; break; } } if (asis) return cast(set); coll = set; } else if (x instanceof Object[]) { coll = Arrays.asList((Object[]) x); } else { final String msg = "Descriptor value for " + name + " must be a Set or " + "an array: " + x.getClass().getName(); throw new IllegalArgumentException(msg); } Set<T> result = new HashSet<T>(); for (Object element : coll) result.add(convertFrom(element, openType)); return result; }
/** * Constructs an {@code OpenMBeanOperationInfoSupport} instance, which describes the operation of * a class of open MBeans, with the specified {@code name}, {@code description}, {@code * signature}, {@code returnOpenType}, {@code impact}, and {@code descriptor}. * * <p>The {@code signature} array parameter is internally copied, so that subsequent changes to * the array referenced by {@code signature} have no effect on this instance. * * @param name cannot be a null or empty string. * @param description cannot be a null or empty string. * @param signature can be null or empty if there are no parameters to describe. * @param returnOpenType cannot be null: use {@code SimpleType.VOID} for operations that return * nothing. * @param impact must be one of {@code ACTION}, {@code ACTION_INFO}, {@code INFO}, or {@code * UNKNOWN}. * @param descriptor The descriptor for the operation. This may be null, which is equivalent to an * empty descriptor. * @throws IllegalArgumentException if {@code name} or {@code description} are null or empty * string, or {@code returnOpenType} is null, or {@code impact} is not one of {@code ACTION}, * {@code ACTION_INFO}, {@code INFO}, or {@code UNKNOWN}. * @throws ArrayStoreException If {@code signature} is not an array of instances of a subclass of * {@code MBeanParameterInfo}. * @since 1.6 */ public OpenMBeanOperationInfoSupport( String name, String description, OpenMBeanParameterInfo[] signature, OpenType<?> returnOpenType, int impact, Descriptor descriptor) { super( name, description, arrayCopyCast(signature), // must prevent NPE here - we will throw IAE later on if // returnOpenType is null (returnOpenType == null) ? null : returnOpenType.getClassName(), impact, ImmutableDescriptor.union( descriptor, // must prevent NPE here - we will throw IAE later on if // returnOpenType is null (returnOpenType == null) ? null : returnOpenType.getDescriptor())); // check parameters that should not be null or empty // (unfortunately it is not done in superclass :-( ! ) // if (name == null || name.trim().equals("")) { throw new IllegalArgumentException("Argument name cannot " + "be null or empty"); } if (description == null || description.trim().equals("")) { throw new IllegalArgumentException("Argument description cannot " + "be null or empty"); } if (returnOpenType == null) { throw new IllegalArgumentException("Argument returnOpenType " + "cannot be null"); } if (impact != ACTION && impact != ACTION_INFO && impact != INFO && impact != UNKNOWN) { throw new IllegalArgumentException( "Argument impact can only be " + "one of ACTION, ACTION_INFO, " + "INFO, or UNKNOWN: " + impact); } this.returnOpenType = returnOpenType; }
/* A Descriptor contained an array value encoded as Strings. The Strings must be organized in an array corresponding to the desired array. If the desired array has n dimensions, so must the String array. We will convert element by element from String to desired component type. */ private static <T> T convertFromStringArray(Object x, OpenType<T> openType) { ArrayType<?> arrayType = (ArrayType<?>) openType; OpenType<?> baseType = arrayType.getElementOpenType(); int dim = arrayType.getDimension(); String squareBrackets = "["; for (int i = 1; i < dim; i++) squareBrackets += "["; Class<?> stringArrayClass; Class<?> targetArrayClass; try { stringArrayClass = Class.forName(squareBrackets + "Ljava.lang.String;"); targetArrayClass = Class.forName(squareBrackets + "L" + baseType.safeGetClassName() + ";"); } catch (ClassNotFoundException e) { throw new NoClassDefFoundError(e.toString()); // can't happen } if (!stringArrayClass.isInstance(x)) { final String msg = "Value for " + dim + "-dimensional array of " + baseType.getTypeName() + " must be same type or a String " + "array with same dimensions"; throw new IllegalArgumentException(msg); } OpenType<?> componentOpenType; if (dim == 1) componentOpenType = baseType; else { try { componentOpenType = new ArrayType<T>(dim - 1, baseType); } catch (OpenDataException e) { throw new IllegalArgumentException(e.getMessage(), e); // can't happen } } int n = Array.getLength(x); Object[] targetArray = (Object[]) Array.newInstance(targetArrayClass.getComponentType(), n); for (int i = 0; i < n; i++) { Object stringish = Array.get(x, i); // String or String[] etc Object converted = convertFromStrings(stringish, componentOpenType); Array.set(targetArray, i, converted); } return OpenMBeanAttributeInfoSupport.<T>cast(targetArray); }
static <T> T valueFrom(Descriptor d, String name, OpenType<T> openType) { Object x = d.getFieldValue(name); if (x == null) return null; try { return convertFrom(x, openType); } catch (Exception e) { final String msg = "Cannot convert descriptor field " + name + " to " + openType.getTypeName(); throw EnvHelp.initCause(new IllegalArgumentException(msg), e); } }
private static <T> T convertFromStrings(Object x, OpenType<T> openType) { if (openType instanceof ArrayType<?>) return convertFromStringArray(x, openType); else if (x instanceof String) return convertFromString((String) x, openType); final String msg = "Cannot convert value " + x + " of type " + x.getClass().getName() + " to type " + openType.getTypeName(); throw new IllegalArgumentException(msg); }
public Builder add(String name, String description, OpenType<?> type, Object value) throws OpenDataException { checkNotNull(name); checkNotNull(type); if (value != null && !type.isValue(value)) throw new OpenDataException( "Argument value of wrong type for item " + name + ": value " + value + ", type " + type); if (types.containsKey(name)) { throw new IllegalArgumentException("Argument type already exist for item " + name); } types.put(name, type); values.put(name, value); descriptions.put(name, description); return this; }
static <T> Descriptor makeDescriptor( OpenType<T> openType, T defaultValue, T[] legalValues, Comparable<T> minValue, Comparable<T> maxValue) { Map<String, Object> map = new HashMap<String, Object>(); if (defaultValue != null) map.put("defaultValue", defaultValue); if (legalValues != null) { Set<T> set = new HashSet<T>(); for (T v : legalValues) set.add(v); set = Collections.unmodifiableSet(set); map.put("legalValues", set); } if (minValue != null) map.put("minValue", minValue); if (maxValue != null) map.put("maxValue", maxValue); if (map.isEmpty()) { return openType.getDescriptor(); } else { map.put("openType", openType); return new ImmutableDescriptor(map); } }
static void check(OpenMBeanParameterInfo info) throws OpenDataException { OpenType<?> openType = info.getOpenType(); if (openType == null) throw new IllegalArgumentException("OpenType cannot be null"); if (info.getName() == null || info.getName().trim().equals("")) throw new IllegalArgumentException("Name cannot be null or empty"); if (info.getDescription() == null || info.getDescription().trim().equals("")) throw new IllegalArgumentException("Description cannot be null or empty"); // Check and initialize defaultValue // if (info.hasDefaultValue()) { // Default value not supported for ArrayType and TabularType // Cast to Object because "OpenType<T> instanceof" is illegal if (openType.isArray() || (Object) openType instanceof TabularType) { throw new OpenDataException( "Default value not supported " + "for ArrayType and TabularType"); } // Check defaultValue's class if (!openType.isValue(info.getDefaultValue())) { final String msg = "Argument defaultValue's class [\"" + info.getDefaultValue().getClass().getName() + "\"] does not match the one defined in openType[\"" + openType.getClassName() + "\"]"; throw new OpenDataException(msg); } } // Check that we don't have both legalValues and min or max // if (info.hasLegalValues() && (info.hasMinValue() || info.hasMaxValue())) { throw new OpenDataException("cannot have both legalValue and " + "minValue or maxValue"); } // Check minValue and maxValue if (info.hasMinValue() && !openType.isValue(info.getMinValue())) { final String msg = "Type of minValue [" + info.getMinValue().getClass().getName() + "] does not match OpenType [" + openType.getClassName() + "]"; throw new OpenDataException(msg); } if (info.hasMaxValue() && !openType.isValue(info.getMaxValue())) { final String msg = "Type of maxValue [" + info.getMaxValue().getClass().getName() + "] does not match OpenType [" + openType.getClassName() + "]"; throw new OpenDataException(msg); } // Check that defaultValue is a legal value // if (info.hasDefaultValue()) { Object defaultValue = info.getDefaultValue(); if (info.hasLegalValues() && !info.getLegalValues().contains(defaultValue)) { throw new OpenDataException("defaultValue is not contained " + "in legalValues"); } // Check that minValue <= defaultValue <= maxValue // if (info.hasMinValue()) { if (compare(info.getMinValue(), defaultValue) > 0) { throw new OpenDataException("minValue cannot be greater " + "than defaultValue"); } } if (info.hasMaxValue()) { if (compare(info.getMaxValue(), defaultValue) < 0) { throw new OpenDataException("maxValue cannot be less " + "than defaultValue"); } } } // Check legalValues // if (info.hasLegalValues()) { // legalValues not supported for TabularType and arrays if ((Object) openType instanceof TabularType || openType.isArray()) { throw new OpenDataException("Legal values not supported " + "for TabularType and arrays"); } // Check legalValues are valid with openType for (Object v : info.getLegalValues()) { if (!openType.isValue(v)) { final String msg = "Element of legalValues [" + v + "] is not a valid value for the specified openType [" + openType.toString() + "]"; throw new OpenDataException(msg); } } } // Check that, if both specified, minValue <= maxValue // if (info.hasMinValue() && info.hasMaxValue()) { if (compare(info.getMinValue(), info.getMaxValue()) > 0) { throw new OpenDataException("minValue cannot be greater " + "than maxValue"); } } }
@Override public void printStackTrace() { CLog.e(CLog.EXCEPTION, LOG_PREFIX + mType.getMessage()); super.printStackTrace(); }
public OpenCameraException(OpenType type) { super(type.getMessage()); mType = type; }