jimu-decompiled/sources/com/thoughtworks/xstream/converters/javabean/BeanProvider.java
2025-05-13 19:24:51 +02:00

170 lines
7.4 KiB
Java

package com.thoughtworks.xstream.converters.javabean;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.ErrorWritingException;
import com.thoughtworks.xstream.converters.javabean.JavaBeanProvider;
import com.thoughtworks.xstream.converters.reflection.ObjectAccessException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
/* loaded from: classes.dex */
public class BeanProvider implements JavaBeanProvider {
protected static final Object[] NO_PARAMS = new Object[0];
protected PropertyDictionary propertyDictionary;
public interface Visitor extends JavaBeanProvider.Visitor {
}
public BeanProvider() {
this(new PropertyDictionary(new NativePropertySorter()));
}
@Override // com.thoughtworks.xstream.converters.javabean.JavaBeanProvider
public boolean canInstantiate(Class cls) {
if (cls == null) {
return false;
}
try {
return newInstance(cls) != null;
} catch (ErrorWritingException unused) {
return false;
}
}
protected boolean canStreamProperty(PropertyDescriptor propertyDescriptor) {
return (propertyDescriptor.getReadMethod() == null || propertyDescriptor.getWriteMethod() == null) ? false : true;
}
protected Constructor getDefaultConstrutor(Class cls) {
for (Constructor<?> constructor : cls.getConstructors()) {
if (constructor.getParameterTypes().length == 0 && Modifier.isPublic(constructor.getModifiers())) {
return constructor;
}
}
return null;
}
protected PropertyDescriptor getProperty(String str, Class cls) {
return this.propertyDictionary.propertyDescriptor(cls, str);
}
@Override // com.thoughtworks.xstream.converters.javabean.JavaBeanProvider
public Class getPropertyType(Object obj, String str) {
return getProperty(str, obj.getClass()).getPropertyType();
}
protected PropertyDescriptor[] getSerializableProperties(Object obj) {
ArrayList arrayList = new ArrayList();
Iterator propertiesFor = this.propertyDictionary.propertiesFor(obj.getClass());
while (propertiesFor.hasNext()) {
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) propertiesFor.next();
if (canStreamProperty(propertyDescriptor)) {
arrayList.add(propertyDescriptor);
}
}
return (PropertyDescriptor[]) arrayList.toArray(new PropertyDescriptor[arrayList.size()]);
}
@Override // com.thoughtworks.xstream.converters.javabean.JavaBeanProvider
public Object newInstance(Class cls) {
ErrorWritingException objectAccessException;
try {
return cls.newInstance();
} catch (ExceptionInInitializerError e) {
objectAccessException = new ConversionException("Cannot construct type", e);
objectAccessException.add("construction-type", cls.getName());
throw objectAccessException;
} catch (IllegalAccessException e2) {
objectAccessException = new ObjectAccessException("Cannot construct type", e2);
objectAccessException.add("construction-type", cls.getName());
throw objectAccessException;
} catch (InstantiationException e3) {
objectAccessException = new ConversionException("Cannot construct type", e3);
objectAccessException.add("construction-type", cls.getName());
throw objectAccessException;
} catch (SecurityException e4) {
objectAccessException = new ObjectAccessException("Cannot construct type", e4);
objectAccessException.add("construction-type", cls.getName());
throw objectAccessException;
}
}
@Override // com.thoughtworks.xstream.converters.javabean.JavaBeanProvider
public boolean propertyDefinedInClass(String str, Class cls) {
return this.propertyDictionary.propertyDescriptorOrNull(cls, str) != null;
}
public boolean propertyWriteable(String str, Class cls) {
return getProperty(str, cls).getWriteMethod() != null;
}
@Override // com.thoughtworks.xstream.converters.javabean.JavaBeanProvider
public void visitSerializableProperties(Object obj, JavaBeanProvider.Visitor visitor) {
ErrorWritingException conversionException;
for (PropertyDescriptor propertyDescriptor : getSerializableProperties(obj)) {
try {
Method readMethod = propertyDescriptor.getReadMethod();
String name = propertyDescriptor.getName();
Class<?> declaringClass = readMethod.getDeclaringClass();
if (visitor.shouldVisit(name, declaringClass)) {
visitor.visit(name, propertyDescriptor.getPropertyType(), declaringClass, readMethod.invoke(obj, new Object[0]));
}
conversionException = null;
} catch (IllegalAccessException e) {
conversionException = new ObjectAccessException("Cannot access property", e);
} catch (IllegalArgumentException e2) {
conversionException = new ConversionException("Cannot get property", e2);
} catch (InvocationTargetException e3) {
conversionException = new ConversionException("Cannot get property", e3.getTargetException());
}
if (conversionException != null) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(obj.getClass());
stringBuffer.append(".");
stringBuffer.append(propertyDescriptor.getName());
conversionException.add("property", stringBuffer.toString());
throw conversionException;
}
}
}
@Override // com.thoughtworks.xstream.converters.javabean.JavaBeanProvider
public void writeProperty(Object obj, String str, Object obj2) {
ErrorWritingException conversionException;
PropertyDescriptor property = getProperty(str, obj.getClass());
try {
property.getWriteMethod().invoke(obj, obj2);
conversionException = null;
} catch (IllegalAccessException e) {
conversionException = new ObjectAccessException("Cannot access property", e);
} catch (IllegalArgumentException e2) {
conversionException = new ConversionException("Cannot set property", e2);
} catch (InvocationTargetException e3) {
conversionException = new ConversionException("Cannot set property", e3.getTargetException());
}
if (conversionException == null) {
return;
}
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(obj.getClass());
stringBuffer.append(".");
stringBuffer.append(property.getName());
conversionException.add("property", stringBuffer.toString());
throw conversionException;
}
public BeanProvider(Comparator comparator) {
this(new PropertyDictionary(new ComparingPropertySorter(comparator)));
}
public BeanProvider(PropertyDictionary propertyDictionary) {
this.propertyDictionary = propertyDictionary;
}
}