Search in sources :

Example 1 with CannotResolveClassException

use of com.thoughtworks.xstream.mapper.CannotResolveClassException in project hudson-2.x by hudson.

the class RobustReflectionConverter method doUnmarshal.

public Object doUnmarshal(final Object result, final HierarchicalStreamReader reader, final UnmarshallingContext context) {
    final SeenFields seenFields = new SeenFields();
    Iterator it = reader.getAttributeNames();
    // Remember outermost Saveable encountered, for reporting below
    if (result instanceof Saveable && context.get("Saveable") == null)
        context.put("Saveable", result);
    // Process attributes before recursing into child elements.
    while (it.hasNext()) {
        String attrAlias = (String) it.next();
        String attrName = mapper.attributeForAlias(attrAlias);
        Class classDefiningField = determineWhichClassDefinesField(reader);
        boolean fieldExistsInClass = fieldDefinedInClass(result, attrName);
        if (fieldExistsInClass) {
            Field field = reflectionProvider.getField(result.getClass(), attrName);
            SingleValueConverter converter = mapper.getConverterFromAttribute(field.getDeclaringClass(), attrName, field.getType());
            Class type = field.getType();
            if (converter == null) {
                converter = mapper.getConverterFromItemType(type);
            }
            if (converter != null) {
                Object value = converter.fromString(reader.getAttribute(attrAlias));
                if (type.isPrimitive()) {
                    type = Primitives.box(type);
                }
                if (value != null && !type.isAssignableFrom(value.getClass())) {
                    throw new ConversionException("Cannot convert type " + value.getClass().getName() + " to type " + type.getName());
                }
                reflectionProvider.writeField(result, attrName, value, classDefiningField);
                seenFields.add(classDefiningField, attrName);
            }
        }
    }
    Map implicitCollectionsForCurrentObject = null;
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        try {
            String fieldName = mapper.realMember(result.getClass(), reader.getNodeName());
            boolean implicitCollectionHasSameName = mapper.getImplicitCollectionDefForFieldName(result.getClass(), reader.getNodeName()) != null;
            Class classDefiningField = determineWhichClassDefinesField(reader);
            boolean fieldExistsInClass = !implicitCollectionHasSameName && fieldDefinedInClass(result, fieldName);
            Class type = determineType(reader, fieldExistsInClass, result, fieldName, classDefiningField);
            final Object value;
            if (fieldExistsInClass) {
                Field field = reflectionProvider.getField(result.getClass(), fieldName);
                value = unmarshalField(context, result, type, field);
                // TODO the reflection provider should have returned the proper field in first place ....
                Class definedType = reflectionProvider.getFieldType(result, fieldName, classDefiningField);
                if (!definedType.isPrimitive()) {
                    type = definedType;
                }
            } else {
                value = context.convertAnother(result, type);
            }
            if (value != null && !type.isAssignableFrom(value.getClass())) {
                LOGGER.warning("Cannot convert type " + value.getClass().getName() + " to type " + type.getName());
            // behave as if we didn't see this element
            } else {
                if (fieldExistsInClass) {
                    reflectionProvider.writeField(result, fieldName, value, classDefiningField);
                    seenFields.add(classDefiningField, fieldName);
                } else {
                    implicitCollectionsForCurrentObject = writeValueToImplicitCollection(context, value, implicitCollectionsForCurrentObject, result, fieldName);
                }
            }
        } catch (NonExistentFieldException e) {
            LOGGER.log(WARNING, "Skipping a non-existent field " + e.getFieldName());
            addErrorInContext(context, e);
        } catch (CannotResolveClassException e) {
            LOGGER.log(WARNING, "Skipping a non-existent type " + e.getMessage());
            addErrorInContext(context, e);
        } catch (LinkageError e) {
            LOGGER.log(WARNING, "Failed to resolve a type " + e.getMessage());
            addErrorInContext(context, e);
        }
        reader.moveUp();
    }
    // Report any class/field errors in Saveable objects
    if (context.get("ReadError") != null && context.get("Saveable") == result) {
        OldDataMonitor.report((Saveable) result, (ArrayList<Throwable>) context.get("ReadError"));
        context.put("ReadError", null);
    }
    return result;
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) Saveable(hudson.model.Saveable) NonExistentFieldException(com.thoughtworks.xstream.converters.reflection.NonExistentFieldException) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException) Field(java.lang.reflect.Field) Iterator(java.util.Iterator) SingleValueConverter(com.thoughtworks.xstream.converters.SingleValueConverter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with CannotResolveClassException

use of com.thoughtworks.xstream.mapper.CannotResolveClassException in project camel by apache.

the class XmlRestProcessor method processResponse.

@Override
protected void processResponse(Exchange exchange, InputStream responseEntity, SalesforceException exception, AsyncCallback callback) {
    final XStream localXStream = xStream.get();
    try {
        // do we need to un-marshal a response
        if (responseEntity != null) {
            final Class<?> responseClass = exchange.getProperty(RESPONSE_CLASS, Class.class);
            Object response;
            if (responseClass != null) {
                // its ok to call this multiple times, as xstream ignores duplicate calls
                localXStream.processAnnotations(responseClass);
                final String responseAlias = exchange.getProperty(RESPONSE_ALIAS, String.class);
                if (responseAlias != null) {
                    // extremely dirty, need to flush entire cache if its holding on to an old alias!!!
                    final CachingMapper mapper = (CachingMapper) localXStream.getMapper();
                    try {
                        if (mapper.realClass(responseAlias) != responseClass) {
                            mapper.flushCache();
                        }
                    } catch (CannotResolveClassException ignore) {
                        // recent XStream versions add a ClassNotFoundException to cache
                        mapper.flushCache();
                    }
                    localXStream.alias(responseAlias, responseClass);
                }
                response = responseClass.newInstance();
                localXStream.fromXML(responseEntity, response);
            } else {
                // return the response as a stream, for getBlobField
                response = responseEntity;
            }
            exchange.getOut().setBody(response);
        } else {
            exchange.setException(exception);
        }
        // copy headers and attachments
        exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
        exchange.getOut().getAttachmentObjects().putAll(exchange.getIn().getAttachmentObjects());
    } catch (XStreamException e) {
        String msg = "Error parsing XML response: " + e.getMessage();
        exchange.setException(new SalesforceException(msg, e));
    } catch (Exception e) {
        String msg = "Error creating XML response: " + e.getMessage();
        exchange.setException(new SalesforceException(msg, e));
    } finally {
        // cleanup temporary exchange headers
        exchange.removeProperty(RESPONSE_CLASS);
        exchange.removeProperty(RESPONSE_ALIAS);
        // consume response entity
        if (responseEntity != null) {
            try {
                responseEntity.close();
            } catch (IOException ignored) {
            }
        }
        // notify callback that exchange is done
        callback.done(false);
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) XStreamException(com.thoughtworks.xstream.XStreamException) XStream(com.thoughtworks.xstream.XStream) CachingMapper(com.thoughtworks.xstream.mapper.CachingMapper) IOException(java.io.IOException) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException) XStreamException(com.thoughtworks.xstream.XStreamException) IOException(java.io.IOException) SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException)

Example 3 with CannotResolveClassException

use of com.thoughtworks.xstream.mapper.CannotResolveClassException in project jmeter by apache.

the class SaveService method readTree.

/**
     * 
     * @param inputStream {@link InputStream} 
     * @param file the JMX file used only for debug, can be null
     * @return the loaded tree
     * @throws IOException if there is a problem reading the file or processing it
     */
private static HashTree readTree(InputStream inputStream, File file) throws IOException {
    ScriptWrapper wrapper = null;
    try {
        // Get the InputReader to use
        InputStreamReader inputStreamReader = getInputStreamReader(inputStream);
        wrapper = (ScriptWrapper) JMXSAVER.fromXML(inputStreamReader);
        inputStreamReader.close();
        if (wrapper == null) {
            log.error("Problem loading XML: see above.");
            return null;
        }
        return wrapper.testPlan;
    } catch (CannotResolveClassException e) {
        if (file != null) {
            throw new IllegalArgumentException("Problem loading XML from:'" + file.getAbsolutePath() + "', cannot determine class for element: " + e, e);
        } else {
            throw new IllegalArgumentException("Problem loading XML, cannot determine class for element: " + e, e);
        }
    } catch (ConversionException | NoClassDefFoundError e) {
        if (file != null) {
            throw new IllegalArgumentException("Problem loading XML from:'" + file.getAbsolutePath() + "', missing class " + e, e);
        } else {
            throw new IllegalArgumentException("Problem loading XML, missing class " + e, e);
        }
    }
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) InputStreamReader(java.io.InputStreamReader) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException)

Aggregations

CannotResolveClassException (com.thoughtworks.xstream.mapper.CannotResolveClassException)3 ConversionException (com.thoughtworks.xstream.converters.ConversionException)2 XStream (com.thoughtworks.xstream.XStream)1 XStreamException (com.thoughtworks.xstream.XStreamException)1 SingleValueConverter (com.thoughtworks.xstream.converters.SingleValueConverter)1 NonExistentFieldException (com.thoughtworks.xstream.converters.reflection.NonExistentFieldException)1 CachingMapper (com.thoughtworks.xstream.mapper.CachingMapper)1 Saveable (hudson.model.Saveable)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)1