下面列出了怎么用java.io.InvalidObjectException的API类实例代码及写法,或者点击链接到github查看源代码。
Object toJavaTypeData(Object data)
throws OpenDataException, InvalidObjectException {
// If the base element type is a basic type
// return the data as no conversion is needed.
if (baseElementType.isBasicType()) {
return data;
}
final Object[] openArray = (Object[]) data;
final Object[] array = (Object[])
Array.newInstance((Class) componentType.getJavaType(),
openArray.length);
int i = 0;
for (Object o : openArray) {
if (o == null) {
array[i] = null;
} else {
array[i] = componentType.toJavaTypeData(o);
}
i++;
}
return array;
}
/**
* This method can replace a deserialized instance of this
* class with another instance. For example, it might replace
* a deserialized empty ImmutableDescriptor with
* {@link #EMPTY_DESCRIPTOR}.
*
* @return the replacement object, which may be {@code this}.
*
* @throws InvalidObjectException if the read object has invalid fields.
*/
private Object readResolve() throws InvalidObjectException {
boolean bad = false;
if (names == null || values == null || names.length != values.length)
bad = true;
if (!bad) {
if (names.length == 0 && getClass() == ImmutableDescriptor.class)
return EMPTY_DESCRIPTOR;
final Comparator<String> compare = String.CASE_INSENSITIVE_ORDER;
String lastName = ""; // also catches illegal null name
for (int i = 0; i < names.length; i++) {
if (names[i] == null ||
compare.compare(lastName, names[i]) >= 0) {
bad = true;
break;
}
lastName = names[i];
}
}
if (bad)
throw new InvalidObjectException("Bad names or values");
return this;
}
private static void setLongField(Object o, Class<?> c, String fieldName, long v)
{
try {
Field fld = getDeclaredField( c, fieldName ) ;
if ((fld != null) && (fld.getType() == Long.TYPE)) {
long key = bridge.objectFieldOffset( fld ) ;
bridge.putLong( o, key, v ) ;
} else {
throw new InvalidObjectException("Field Type mismatch");
}
} catch (Exception e) {
if (o != null) {
throw utilWrapper.errorSetLongField( e, fieldName,
o.toString(),
new Long(v) ) ;
} else {
throw utilWrapper.errorSetLongField( e, fieldName,
"null " + c.getName() + " object",
new Long(v) ) ;
}
}
}
@Override
final Object fromNonNullOpenValue(Object openValue)
throws InvalidObjectException {
final TabularData table = (TabularData) openValue;
final Collection<CompositeData> rows = cast(table.values());
final Map<Object, Object> valueMap =
sortedMap ? newSortedMap() : newInsertionOrderMap();
for (CompositeData row : rows) {
final Object key =
keyMapping.fromOpenValue(row.get("key"));
final Object value =
valueMapping.fromOpenValue(row.get("value"));
if (valueMap.put(key, value) != null) {
final String msg =
"Duplicate entry in TabularData: key=" + key;
throw new InvalidObjectException(msg);
}
}
return valueMap;
}
private static void setShortField(Object o, Class<?> c, String fieldName, short v)
{
try {
Field fld = getDeclaredField( c, fieldName ) ;
if ((fld != null) && (fld.getType() == Short.TYPE)) {
long key = bridge.objectFieldOffset( fld ) ;
bridge.putShort( o, key, v ) ;
} else {
throw new InvalidObjectException("Field Type mismatch");
}
} catch (Exception e) {
if (o != null) {
throw utilWrapper.errorSetShortField( e, fieldName,
o.toString(),
new Short(v) ) ;
} else {
throw utilWrapper.errorSetShortField( e, fieldName,
"null " + c.getName() + " object",
new Short(v) ) ;
}
}
}
/**
* This method can replace a deserialized instance of this
* class with another instance. For example, it might replace
* a deserialized empty ImmutableDescriptor with
* {@link #EMPTY_DESCRIPTOR}.
*
* @return the replacement object, which may be {@code this}.
*
* @throws InvalidObjectException if the read object has invalid fields.
*/
private Object readResolve() throws InvalidObjectException {
boolean bad = false;
if (names == null || values == null || names.length != values.length)
bad = true;
if (!bad) {
if (names.length == 0 && getClass() == ImmutableDescriptor.class)
return EMPTY_DESCRIPTOR;
final Comparator<String> compare = String.CASE_INSENSITIVE_ORDER;
String lastName = ""; // also catches illegal null name
for (int i = 0; i < names.length; i++) {
if (names[i] == null ||
compare.compare(lastName, names[i]) >= 0) {
bad = true;
break;
}
lastName = names[i];
}
}
if (bad)
throw new InvalidObjectException("Bad names or values");
return this;
}
@Override
public final Object fromOpenValue(Object openValue)
throws InvalidObjectException {
if (openValue == null)
return null;
else
return fromNonNullOpenValue(openValue);
}
/**
* Return the singleton WeekFields associated with the
* {@code firstDayOfWeek} and {@code minimalDays}.
* @return the singleton WeekFields for the firstDayOfWeek and minimalDays.
* @throws InvalidObjectException if the serialized object has invalid
* values for firstDayOfWeek or minimalDays.
*/
private Object readResolve() throws InvalidObjectException {
try {
return WeekFields.of(firstDayOfWeek, minimalDays);
} catch (IllegalArgumentException iae) {
throw new InvalidObjectException("Invalid serialized WeekFields: " + iae.getMessage());
}
}
Object toJavaTypeData(Object data)
throws OpenDataException, InvalidObjectException {
final TabularData td = (TabularData) data;
Map<Object, Object> result = new HashMap<>();
for (CompositeData row : (Collection<CompositeData>) td.values()) {
Object key = keyType.toJavaTypeData(row.get(KEY));
Object value = valueType.toJavaTypeData(row.get(VALUE));
result.put(key, value);
}
return result;
}
final Object fromCompositeData(CompositeData cd,
String[] itemNames,
MXBeanMapping[] converters)
throws InvalidObjectException {
try {
return MethodUtil.invoke(fromMethod, null, new Object[] {cd});
} catch (Exception e) {
final String msg = "Failed to invoke from(CompositeData)";
throw invalidObjectException(msg, e);
}
}
final Object fromCompositeData(CompositeData cd,
String[] itemNames,
MXBeanMapping[] converters)
throws InvalidObjectException {
try {
return MethodUtil.invoke(fromMethod, null, new Object[] {cd});
} catch (Exception e) {
final String msg = "Failed to invoke from(CompositeData)";
throw invalidObjectException(msg, e);
}
}
void checkCallFromOpen() {
try {
for (MXBeanMapping paramConverter : paramMappings)
paramConverter.checkReconstructible();
} catch (InvalidObjectException e) {
throw new IllegalArgumentException(e);
}
}
String applicable(Method[] getters) {
for (int i = 0; i < getters.length; i++) {
try {
getterConverters[i].checkReconstructible();
} catch (InvalidObjectException e) {
possibleCause = e;
return "method " + getters[i].getName() + " returns type " +
"that cannot be mapped back from OpenData";
}
}
return "";
}
/**
* Resolves instances being deserialized to the predefined constants.
*
* @throws InvalidObjectException if the constant could not be
* resolved.
* @return resolved MessageFormat.Field constant
*/
protected Object readResolve() throws InvalidObjectException {
if (this.getClass() != MessageFormat.Field.class) {
throw new InvalidObjectException("subclass didn't correctly implement readResolve");
}
return ARGUMENT;
}
/**
* Restore the state of an ValueRange from the stream.
* Check that the values are valid.
*
* @param s the stream to read
* @throws InvalidObjectException if
* the smallest minimum is greater than the smallest maximum,
* or the smallest maximum is greater than the largest maximum
* or the largest minimum is greater than the largest maximum
* @throws ClassNotFoundException if a class cannot be resolved
*/
private void readObject(ObjectInputStream s)
throws IOException, ClassNotFoundException, InvalidObjectException
{
s.defaultReadObject();
if (minSmallest > minLargest) {
throw new InvalidObjectException("Smallest minimum value must be less than largest minimum value");
}
if (maxSmallest > maxLargest) {
throw new InvalidObjectException("Smallest maximum value must be less than largest maximum value");
}
if (minLargest > maxLargest) {
throw new InvalidObjectException("Minimum value must be less than maximum value");
}
}
/**
* After reading an object from the input stream, the format
* pattern in the object is verified.
* <p>
* @exception InvalidObjectException if the pattern is invalid
*/
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
try {
compiledPattern = compile(pattern);
} catch (Exception e) {
throw new InvalidObjectException("invalid pattern");
}
if (serialVersionOnStream < 1) {
// didn't have defaultCenturyStart field
initializeDefaultCentury();
}
else {
// fill in dependent transient field
parseAmbiguousDatesAsAfter(defaultCenturyStart);
}
serialVersionOnStream = currentSerialVersion;
// If the deserialized object has a SimpleTimeZone, try
// to replace it with a ZoneInfo equivalent in order to
// be compatible with the SimpleTimeZone-based
// implementation as much as possible.
TimeZone tz = getTimeZone();
if (tz instanceof SimpleTimeZone) {
String id = tz.getID();
TimeZone zi = TimeZone.getTimeZone(id);
if (zi != null && zi.hasSameRules(tz) && zi.getID().equals(id)) {
setTimeZone(zi);
}
}
}
/**
* Restore the state of an ValueRange from the stream.
* Check that the values are valid.
*
* @param s the stream to read
* @throws InvalidObjectException if
* the smallest minimum is greater than the smallest maximum,
* or the smallest maximum is greater than the largest maximum
* or the largest minimum is greater than the largest maximum
* @throws ClassNotFoundException if a class cannot be resolved
*/
private void readObject(ObjectInputStream s)
throws IOException, ClassNotFoundException, InvalidObjectException
{
s.defaultReadObject();
if (minSmallest > minLargest) {
throw new InvalidObjectException("Smallest minimum value must be less than largest minimum value");
}
if (maxSmallest > maxLargest) {
throw new InvalidObjectException("Smallest maximum value must be less than largest maximum value");
}
if (minLargest > maxLargest) {
throw new InvalidObjectException("Minimum value must be less than maximum value");
}
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
ObjectInputStream.GetField gf = ois.readFields();
String principalName = (String)gf.get("name", null);
try {
validate(principalName);
this.name = principalName;
} catch (NullPointerException e) {
throw new InvalidObjectException(e.getMessage());
}
}
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
// Reading private fields of base class
in.defaultReadObject();
try {
validate(super.getName(),super.getActions());
} catch (IllegalArgumentException e) {
throw new InvalidObjectException(e.getMessage());
}
}
/**
* Return the singleton WeekFields associated with the
* {@code firstDayOfWeek} and {@code minimalDays}.
* @return the singleton WeekFields for the firstDayOfWeek and minimalDays.
* @throws InvalidObjectException if the serialized object has invalid
* values for firstDayOfWeek or minimalDays.
*/
private Object readResolve() throws InvalidObjectException {
try {
return WeekFields.of(firstDayOfWeek, minimalDays);
} catch (IllegalArgumentException iae) {
throw new InvalidObjectException("Invalid serialized WeekFields: " + iae.getMessage());
}
}
@Override
final Object fromNonNullOpenValue(Object openValue)
throws InvalidObjectException {
MXBeanLookup lookup = lookupNotNull(InvalidObjectException.class);
ObjectName name = (ObjectName) openValue;
Object mxbean =
lookup.objectNameToMXBean(name, (Class<?>) getJavaType());
if (mxbean == null) {
final String msg =
"No MXBean for name: " + name;
throw new InvalidObjectException(msg);
}
return mxbean;
}
/**
* After reading an object from the input stream, do a simple verification
* to maintain class invariants.
* @throws InvalidObjectException if the objects read from the stream is invalid.
*/
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
if (choiceLimits.length != choiceFormats.length) {
throw new InvalidObjectException(
"limits and format arrays of different length.");
}
}
/**
* First, read in the default serializable data.
*
* Then, if <code>serialVersionOnStream</code> is less than 1, indicating that
* the stream was written by JDK 1.1,
* set the <code>int</code> fields such as <code>maximumIntegerDigits</code>
* to be equal to the <code>byte</code> fields such as <code>maxIntegerDigits</code>,
* since the <code>int</code> fields were not present in JDK 1.1.
* Finally, set serialVersionOnStream back to the maximum allowed value so that
* default serialization will work properly if this object is streamed out again.
*/
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException
{
stream.defaultReadObject();
///CLOVER:OFF
// we don't have serialization data for this format
if (serialVersionOnStream < 1) {
// Didn't have additional int fields, reassign to use them.
maximumIntegerDigits = maxIntegerDigits;
minimumIntegerDigits = minIntegerDigits;
maximumFractionDigits = maxFractionDigits;
minimumFractionDigits = minFractionDigits;
}
if (serialVersionOnStream < 2) {
// Didn't have capitalizationSetting, set it to default
capitalizationSetting = DisplayContext.CAPITALIZATION_NONE;
}
///CLOVER:ON
/*Bug 4185761
Validate the min and max fields [Richard/GCL]
*/
if (minimumIntegerDigits > maximumIntegerDigits ||
minimumFractionDigits > maximumFractionDigits ||
minimumIntegerDigits < 0 || minimumFractionDigits < 0) {
throw new InvalidObjectException("Digit count range invalid");
}
serialVersionOnStream = currentSerialVersion;
}
/**
* Return the singleton WeekFields associated with the
* {@code firstDayOfWeek} and {@code minimalDays}.
* @return the singleton WeekFields for the firstDayOfWeek and minimalDays.
* @throws InvalidObjectException if the serialized object has invalid
* values for firstDayOfWeek or minimalDays.
*/
private Object readResolve() throws InvalidObjectException {
try {
return WeekFields.of(firstDayOfWeek, minimalDays);
} catch (IllegalArgumentException iae) {
throw new InvalidObjectException("Invalid serialized WeekFields: " + iae.getMessage());
}
}
/**
* Resolves instances being deserialized to the predefined constants.
*/
protected Object readResolve() throws InvalidObjectException {
if (this.getClass() != TextAttribute.class) {
throw new InvalidObjectException(
"subclass didn't correctly implement readResolve");
}
TextAttribute instance = instanceMap.get(getName());
if (instance != null) {
return instance;
} else {
throw new InvalidObjectException("unknown attribute name");
}
}
protected Object readResolve()
throws ObjectStreamException
{
try
{
return forName(literalName);
} catch (IllegalArgumentException e) {
throw new InvalidObjectException(e.getMessage());
}
}
Object readResolve() throws ObjectStreamException {
try {
return new ProcessorImpl(component, name, plugin, internalConfiguration, loadDelegate(value, plugin));
} catch (final IOException | ClassNotFoundException e) {
final InvalidObjectException invalidObjectException = new InvalidObjectException(e.getMessage());
invalidObjectException.initCause(e);
throw invalidObjectException;
}
}
/**
* Resolves instances being deserialized to the predefined constants.
*/
protected Object readResolve() throws InvalidObjectException {
if (this.getClass() != TextAttribute.class) {
throw new InvalidObjectException(
"subclass didn't correctly implement readResolve");
}
TextAttribute instance = instanceMap.get(getName());
if (instance != null) {
return instance;
} else {
throw new InvalidObjectException("unknown attribute name");
}
}
public static Sprite getSpriteFromFile(File path) throws Exception {
if (!path.exists()) throw new FileNotFoundException("Not found: "+path);
Sprite sprite;
if (SVGSprite.canRead(path)) {
sprite = SVGSprite.create(path);
} else if (ImageSprite.canRead(path)) {
sprite = ImageSprite.create(path);
} else if (path.toString().contains(" ") || path.toString().contains("#")){
throw new InvalidPathException(path.toString(), "Path of file contains invalid symbols such as spaces or sharps");
} else {
throw new InvalidObjectException("Cannot load sprite from file "+ path + ", unknown type of file");
}
return sprite;
}
private void readObject(ObjectInputStream stream)
throws ClassNotFoundException, IOException {
stream.defaultReadObject();
if (reason == null) {
reason = BasicReason.UNSPECIFIED;
}
if (certPath == null && index != -1) {
throw new InvalidObjectException("certpath is null and index != -1");
}
if (index < -1 ||
(certPath != null && index >= certPath.getCertificates().size())) {
throw new InvalidObjectException("index out of range");
}
}