private static ObjectAccessException wrap(final String message, final Class type, final String name, final Throwable ex) { final ObjectAccessException exception = new ObjectAccessException(message, ex); exception.add("field", type.getName() + "." + name); return exception; } }
public Object clone() { try { XmlFriendlyNameCoder coder = (XmlFriendlyNameCoder)super.clone(); coder.readResolve(); return coder; } catch (CloneNotSupportedException e) { throw new ObjectAccessException("Cannot clone XmlFriendlyNameCoder", e); } }
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()); } } }
/** * Instantiates a converter using its default constructor. * * @param type the converter type to instantiate * @return the new instance */ private Converter newInstance(Class<? extends ConverterMatcher> type) { Converter converter; // TODO: We need a separate exception for runtime initialization. try { if (SingleValueConverter.class.isAssignableFrom(type)) { final SingleValueConverter svc = (SingleValueConverter)type.getConstructor().newInstance(); converter = new SingleValueConverterWrapper(svc); } else { converter = (Converter)type.getConstructor().newInstance(); } } catch (InvocationTargetException e) { throw new ObjectAccessException("Cannot construct " + type.getName(), e .getCause()); } catch (InstantiationException e) { throw new ObjectAccessException("Cannot construct " + type.getName(), e); } catch (IllegalAccessException e) { throw new ObjectAccessException("Cannot construct " + type.getName(), e); } catch (NoSuchMethodException e) { throw new ObjectAccessException("Cannot construct " + type.getName(), e); } return converter; }
public Object newInstance() { ErrorWritingException ex = null; try { return editorType.newInstance(); } catch (InstantiationException e) { ex = new ConversionException("Faild to call default constructor", e); } catch (IllegalAccessException e) { ex = new ObjectAccessException("Cannot call default constructor", e); } ex.add("construction-type", editorType.getName()); throw ex; }
public Object fromString(String str) { try { return ctor.newInstance(new Object[] {str}); } catch (InstantiationException e) { throw new ConversionException("Unable to instantiate single String param constructor", e); } catch (IllegalAccessException e) { throw new ObjectAccessException("Unable to access single String param constructor", e); } catch (InvocationTargetException e) { throw new ConversionException("Unable to target single String param constructor", e.getTargetException()); } } }
public Object newInstance(Class type) { ErrorWritingException ex = null; try { return type.newInstance(); } catch (InstantiationException e) { ex = new ConversionException("Cannot construct type", e); } catch (IllegalAccessException e) { ex = new ObjectAccessException("Cannot construct type", e); } catch (SecurityException e) { ex = new ObjectAccessException("Cannot construct type", e); } catch (ExceptionInInitializerError e) { ex = new ConversionException("Cannot construct type", e); } ex.add("construction-type", type.getName()); throw ex; }
private Map writeValueToImplicitCollection(UnmarshallingContext context, Object value, Map implicitCollections, Object result, String itemFieldName) { String fieldName = mapper.getFieldNameForItemTypeAndName(context.getRequiredType(), value.getClass(), itemFieldName); if (fieldName != null) { if (implicitCollections == null) { implicitCollections = new HashMap(); // lazy instantiation } Collection collection = (Collection) implicitCollections.get(fieldName); if (collection == null) { Class fieldType = mapper.defaultImplementationOf(reflectionProvider.getFieldType(result, fieldName, null)); if (!Collection.class.isAssignableFrom(fieldType)) { throw new ObjectAccessException("Field " + fieldName + " of " + result.getClass().getName() + " is configured for an implicit Collection, but field is of type " + fieldType.getName()); } if (pureJavaReflectionProvider == null) { pureJavaReflectionProvider = new PureJavaReflectionProvider(); } collection = (Collection)pureJavaReflectionProvider.newInstance(fieldType); reflectionProvider.writeField(result, fieldName, collection, null); implicitCollections.put(fieldName, collection); } collection.add(value); } return implicitCollections; }
public void callWriteObject(final Class type, final Object instance, final ObjectOutputStream stream) { ErrorWritingException ex = null; try { Method readObjectMethod = getMethod( type, "writeObject", new Class[]{ObjectOutputStream.class}, false); readObjectMethod.invoke(instance, new Object[]{stream}); } catch (IllegalAccessException e) { ex = new ObjectAccessException("Cannot access method", e); } catch (InvocationTargetException e) { ex = new ConversionException("Failed calling method", e.getTargetException()); } if (ex != null) { ex.add("method", instance.getClass().getName() + ".writeObject()"); throw ex; } }
public void callReadObject(final Class type, final Object object, final ObjectInputStream stream) { ErrorWritingException ex = null; try { Method readObjectMethod = getMethod( type, "readObject", new Class[]{ObjectInputStream.class}, false); readObjectMethod.invoke(object, new Object[]{stream}); } catch (IllegalAccessException e) { ex = new ObjectAccessException("Cannot access method", e); } catch (InvocationTargetException e) { ex = new ConversionException("Failed calling method", e.getTargetException()); } if (ex != null) { ex.add("method", object.getClass().getName() + ".readObject()"); throw ex; } }
private Map buildMap(Class type) { Map nameMap = (Map)propertyNameCache.get(type); if (nameMap == null) { BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(type, Object.class); } catch (IntrospectionException e) { ObjectAccessException oaex = new ObjectAccessException("Cannot get BeanInfo of type", e); oaex.add("bean-type", type.getName()); throw oaex; } nameMap = new OrderRetainingMap(); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++ ) { PropertyDescriptor descriptor = propertyDescriptors[i]; nameMap.put(descriptor.getName(), descriptor); } nameMap = sorter.sort(type, nameMap); propertyNameCache.put(type, nameMap); } return nameMap; }
public void writeProperty(Object object, String propertyName, Object value) { ErrorWritingException ex = null; PropertyDescriptor property = getProperty(propertyName, object.getClass()); try { property.getWriteMethod().invoke(object, new Object[]{value}); } catch (IllegalArgumentException e) { ex = new ConversionException("Cannot set property", e); } catch (IllegalAccessException e) { ex = new ObjectAccessException("Cannot access property", e); } catch (InvocationTargetException e) { ex = new ConversionException("Cannot set property", e.getTargetException()); } if (ex != null) { ex.add("property", object.getClass() + "." + property.getName()); throw ex; } }
public Object newInstance(Class type) { if (exception != null) { ObjectAccessException ex = new ObjectAccessException("Cannot construct type", exception); ex.add("construction-type", type.getName()); throw ex; } ErrorWritingException ex = null; if (type == void.class || type == Void.class) { ex = new ConversionException("Type void cannot have an instance"); } else { try { return unsafe.allocateInstance(type); } catch (final SecurityException e) { ex = new ObjectAccessException("Cannot construct type", e); } catch (final InstantiationException e) { ex = new ConversionException("Cannot construct type", e); } catch (final IllegalArgumentException e) { ex = new ObjectAccessException("Cannot construct type", e); } } ex.add("construction-type", type.getName()); throw ex; }
/** * Create the DocumentBuilderFactory instance. * * @return the new instance * @since 1.4.9 */ protected DocumentBuilderFactory createDocumentBuilderFactory() { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); if (JVM.isVersion(5)) { try { Method method = DocumentBuilderFactory.class.getMethod("setFeature", new Class[]{ String.class, boolean.class }); method.invoke(factory, new Object[]{ "http://apache.org/xml/features/disallow-doctype-decl", Boolean.TRUE }); } catch (NoSuchMethodException e) { // Ignore } catch (IllegalAccessException e) { throw new ObjectAccessException("Cannot set feature of DocumentBuilderFactory.", e); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); if (JVM.isVersion(6) || (cause instanceof ParserConfigurationException && cause.getMessage().indexOf("disallow-doctype-decl") < 0)) { throw new StreamException(cause); } } } factory.setExpandEntityReferences(false); return factory; } }
/** * Resolves an object as native serialization does by calling readResolve(), if available. */ public Object callReadResolve(final Object result) { if (result == null) { return null; } else { final Class resultType = result.getClass(); final Method readResolveMethod = getRRMethod(resultType, "readResolve"); if (readResolveMethod != null) { ErrorWritingException ex = null; try { return readResolveMethod.invoke(result, EMPTY_ARGS); } catch (IllegalAccessException e) { ex = new ObjectAccessException("Cannot access method", e); } catch (InvocationTargetException e) { ex = new ConversionException("Failed calling method", e.getTargetException()); } ex.add("method", resultType.getName() + ".readResolve()"); throw ex; } else { return result; } } }
public Object callWriteReplace(final Object object) { if (object == null) { return null; } else { final Class objectType = object.getClass(); final Method writeReplaceMethod = getRRMethod(objectType, "writeReplace"); if (writeReplaceMethod != null) { ErrorWritingException ex = null; try { Object replaced = writeReplaceMethod.invoke(object, EMPTY_ARGS); if (replaced != null && !object.getClass().equals(replaced.getClass())) { // call further writeReplace methods on replaced replaced = callWriteReplace(replaced); } return replaced; } catch (final IllegalAccessException e) { ex = new ObjectAccessException("Cannot access method", e); } catch (InvocationTargetException e) { ex = new ConversionException("Failed calling method", e.getTargetException()); } catch (final ErrorWritingException e) { ex = e; } ex.add("method", objectType.getName() + ".writeReplace()"); throw ex; } else { return object; } } }
protected Object createCollection(Class type) { ErrorWritingException ex = null; Class defaultType = mapper().defaultImplementationOf(type); try { return defaultType.newInstance(); } catch (InstantiationException e) { ex = new ConversionException("Cannot instantiate default collection", e); } catch (IllegalAccessException e) { ex = new ObjectAccessException("Cannot instantiate default collection", e); } ex.add("collection-type", type.getName()); ex.add("default-type", defaultType.getName()); throw ex; } }
public void visitSerializableProperties(Object object, JavaBeanProvider.Visitor visitor) { PropertyDescriptor[] propertyDescriptors = getSerializableProperties(object); for (int i = 0; i < propertyDescriptors.length; i++ ) { ErrorWritingException ex = null; PropertyDescriptor property = propertyDescriptors[i]; try { Method readMethod = property.getReadMethod(); String name = property.getName(); Class definedIn = readMethod.getDeclaringClass(); if (visitor.shouldVisit(name, definedIn)) { Object value = readMethod.invoke(object, new Object[0]); visitor.visit(name, property.getPropertyType(), definedIn, value); } } catch (IllegalArgumentException e) { ex = new ConversionException("Cannot get property", e); } catch (IllegalAccessException e) { ex = new ObjectAccessException("Cannot access property", e); } catch (InvocationTargetException e) { ex = new ConversionException("Cannot get property", e.getTargetException()); } if (ex != null) { ex.add("property", object.getClass() + "." + property.getName()); throw ex; } } }
public static synchronized CustomObjectOutputStream getInstance(DataHolder whereFrom, StreamCallback callback) { try { CustomObjectOutputStream result = (CustomObjectOutputStream) whereFrom.get(DATA_HOLDER_KEY); if (result == null) { result = new CustomObjectOutputStream(callback); whereFrom.put(DATA_HOLDER_KEY, result); } else { result.pushCallback(callback); } return result; } catch (SecurityException e) { throw new ObjectAccessException("Cannot create CustomObjectStream", e); } catch (IOException e) { throw new StreamException("Cannot create CustomObjectStream", e); } }
public static synchronized CustomObjectInputStream getInstance(DataHolder whereFrom, CustomObjectInputStream.StreamCallback callback, ClassLoaderReference classLoaderReference) { try { CustomObjectInputStream result = (CustomObjectInputStream) whereFrom.get(DATA_HOLDER_KEY); if (result == null) { result = new CustomObjectInputStream(callback, classLoaderReference); whereFrom.put(DATA_HOLDER_KEY, result); } else { result.pushCallback(callback); } return result; } catch (SecurityException e) { throw new ObjectAccessException("Cannot create CustomObjectStream", e); } catch (IOException e) { throw new StreamException("Cannot create CustomObjectStream", e); } }