本文整理汇总了Java中org.supercsv.exception.SuperCsvReflectionException类的典型用法代码示例。如果您正苦于以下问题:Java SuperCsvReflectionException类的具体用法?Java SuperCsvReflectionException怎么用?Java SuperCsvReflectionException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SuperCsvReflectionException类属于org.supercsv.exception包,在下文中一共展示了SuperCsvReflectionException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getValue
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
@Override
public Object getValue(Field field, Object obj) {
try {
field.setAccessible(true);
Object result = field.get(obj);
if(result != null && java.util.Optional.class.isAssignableFrom(result.getClass())){
Optional optionalResult = (java.util.Optional) result;
return optionalResult.orElse(null);
}
return result;
} catch (IllegalAccessException e) {
throw new SuperCsvReflectionException(Form.at("Error extracting bean value for field {}",
field.getName()), e);
}
}
示例2: create
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public CellProcessorFactory create(ProcessingMetadata<CellProcessorFactoryMethod> metadata) {
return new CellProcessorFactory() {
@Override
public int getOrder() {
return metadata.getAnnotation().order();
}
@Override
public CellProcessor create(CellProcessor next) {
CellProcessorFactoryMethod annotation = metadata.getAnnotation();
Class<?> type = annotation.type().equals(CellProcessorFactoryMethod.DeclaredType.class) ? metadata.getBeanDescriptor().getBeanType() : annotation.type();
try {
Method method = type.getDeclaredMethod(annotation.methodName(), CellProcessor.class);
return (CellProcessor) method.invoke(null, next);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) {
throw new SuperCsvReflectionException(Form.at("Can not find CellProcessorFactoryMethod '{}' - it needs to be static, defined in '{}' and accept exactly one parameter of type CellProcessor!",
annotation.methodName(), type.getName()), ex);
}
}
};
}
示例3: getValue
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
/**
* フィールドの値を取得する。
* @param record レコードオブジェクト。
* @return フィールドの値。
* @throws IllegalArgumentException レコードのインスタンスがフィールドが定義されているクラスと異なる場合。
* @throws SuperCsvReflectionException フィールドの値の取得に失敗した場合。
*/
public Object getValue(final Object record) {
Objects.requireNonNull(record);
if(!getDeclaredClass().equals(record.getClass())) {
throw new IllegalArgumentException(String.format("not match record class type. expected=%s. actual=%s, ",
type.getName(), record.getClass().getName()));
}
try {
return field.get(record);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new SuperCsvReflectionException("fail get field value.", e);
}
}
示例4: create
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
@Override
public Object create(final Class<?> clazz) {
Objects.requireNonNull(clazz, "clazz should not be null.");
try {
if(clazz.isInterface()) {
return BeanInterfaceProxy.createProxy(clazz);
} else {
Constructor<?> cons = clazz.getDeclaredConstructor();
cons.setAccessible(true);
return cons.newInstance();
}
} catch (ReflectiveOperationException e) {
throw new SuperCsvReflectionException(String.format("fail create Bean instance of '%s'", clazz.getName()), e);
}
}
示例5: populateBean
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
/**
* Beanの各フィールドに対して値を設定する。
* @param resultBean
* @param nameMapping
* @param bindingErrors
*/
protected void populateBean(final T resultBean, final String[] nameMapping, final CsvBindingErrors bindingErrors) {
// map each column to its associated field on the bean
for( int i = 0; i < nameMapping.length; i++ ) {
final String fieldName = nameMapping[i];
final Object fieldValue = processedColumns.get(i);
// don't call a set-method in the bean if there is no name mapping for the column or no result to store
if( fieldName == null || fieldValue == null || bindingErrors.hasFieldErrors(fieldName)) {
continue;
}
// invoke the setter on the bean
final Method setMethod = cache.getSetMethod(resultBean, fieldName, fieldValue.getClass());
try {
setMethod.invoke(resultBean, fieldValue);
} catch(final Exception e) {
throw new SuperCsvReflectionException(String.format("error invoking method %s()", setMethod.getName()), e);
}
}
}
示例6: setValue
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
@Override
public void setValue(Field field, Object obj, Object value) {
try {
field.setAccessible(true);
field.set(obj, value);
} catch (IllegalAccessException e) {
throw new SuperCsvReflectionException(Form.at("Cannot set value on field '{}'", field.getName()), e);
}
}
示例7: createCellProcessorFor
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
@SuppressWarnings({"rawtypes", "unchecked"})
public static CellProcessor createCellProcessorFor(BeanDescriptor beanDescriptor, Field field, String context) {
List<Annotation> annotations = extractAnnotations(field);
Collections.reverse(annotations);
List<CellProcessorDefinition> factories = new ArrayList<>();
for (Annotation annotation : annotations) {
CellProcessorAnnotationDescriptor cellProcessorMarker = annotation
.annotationType().getAnnotation(CellProcessorAnnotationDescriptor.class);
if (cellProcessorMarker != null && Arrays.asList(cellProcessorMarker.contexts()).contains(context)) {
DeclarativeCellProcessorProvider provider = ReflectionUtilsExt.instantiateBean(cellProcessorMarker
.provider());
if (!provider.getType().isAssignableFrom(annotation.getClass())) {
throw new SuperCsvReflectionException(
Form.at(
"Provider declared in annotation of type '{}' cannot be used since accepted annotation-type is not compatible",
annotation.getClass().getName()));
}
factories.add(new CellProcessorDefinition(provider.create(new ProcessingMetadata(annotation, field, beanDescriptor)), cellProcessorMarker));
}
}
Collections.sort(factories, new OrderComparator());
return buildProcessorChain(factories);
}
示例8: getValue
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
@Override
public Object getValue(Field field, Object obj) {
try {
Method method = getReadMethod(field, obj);
Object result = method.invoke(obj);
if(result != null && java.util.Optional.class.isAssignableFrom(result.getClass())){
Optional optionalResult = (java.util.Optional) result;
return optionalResult.orElse(null);
}
return result;
} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
throw new SuperCsvReflectionException(Form.at("Error extracting bean value via getter for field {}",
field.getName()), e);
}
}
示例9: setValue
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
@Override
public void setValue(Field field, Object obj, Object value) {
try {
Method method = getWriteMethod(field, obj);
method.invoke(obj, value);
} catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
throw new SuperCsvReflectionException(Form.at("Cannot set value via setter on field '{}'", field.getName()), e);
}
}
示例10: findGetter
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
/**
* Returns the getter method associated with the object's field.
*
* @param object
* the object
* @param fieldName
* the name of the field
* @return the getter method
* @throws NullPointerException
* if object or fieldName is null
* @throws SuperCsvReflectionException
* if the getter doesn't exist or is not visible
*/
public static Method findGetter(final Object object, final String fieldName) {
if( object == null ) {
throw new NullPointerException("object should not be null");
} else if( fieldName == null ) {
throw new NullPointerException("fieldName should not be null");
}
final Class<?> clazz = object.getClass();
// find a standard getter
final String standardGetterName = getMethodNameForField(GET_PREFIX, fieldName);
Method getter = findGetterWithCompatibleReturnType(standardGetterName, clazz, false);
// if that fails, try for an isX() style boolean getter
if( getter == null ) {
final String booleanGetterName = getMethodNameForField(IS_PREFIX, fieldName);
getter = findGetterWithCompatibleReturnType(booleanGetterName, clazz, true);
}
if( getter == null ) {
throw new SuperCsvReflectionException(
String
.format(
"unable to find getter for field %s in class %s - check that the corresponding nameMapping element matches the field name in the bean",
fieldName, clazz.getName()));
}
return getter;
}
示例11: findSetter
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
/**
* Returns the setter method associated with the object's field.
* <p>
* This method handles any autoboxing/unboxing of the argument passed to the setter (e.g. if the setter type is a
* primitive {@code int} but the argument passed to the setter is an {@code Integer}) by looking for a setter with
* the same type, and failing that checking for a setter with the corresponding primitive/wrapper type.
* <p>
* It also allows for an argument type that is a subclass or implementation of the setter type (when the setter type
* is an {@code Object} or {@code interface} respectively).
*
* @param object
* the object
* @param fieldName
* the name of the field
* @param argumentType
* the type to be passed to the setter
* @return the setter method
* @throws NullPointerException
* if object, fieldName or fieldType is null
* @throws SuperCsvReflectionException
* if the setter doesn't exist or is not visible
*/
public static Method findSetter(final Object object, final String fieldName, final Class<?> argumentType) {
if( object == null ) {
throw new NullPointerException("object should not be null");
} else if( fieldName == null ) {
throw new NullPointerException("fieldName should not be null");
} else if( argumentType == null ) {
throw new NullPointerException("argumentType should not be null");
}
final String setterName = getMethodNameForField(SET_PREFIX, fieldName);
final Class<?> clazz = object.getClass();
// find a setter compatible with the supplied argument type
Method setter = findSetterWithCompatibleParamType(clazz, setterName, argumentType);
// if that failed, try the corresponding primitive/wrapper if it's a type that can be autoboxed/unboxed
if( setter == null && AUTOBOXING_CONVERTER.containsKey(argumentType) ) {
setter = findSetterWithCompatibleParamType(clazz, setterName, AUTOBOXING_CONVERTER.get(argumentType));
}
if( setter == null ) {
throw new SuperCsvReflectionException(
String
.format(
"unable to find method %s(%s) in class %s - check that the corresponding nameMapping element matches the field name in the bean, "
+ "and the cell processor returns a type compatible with the field", setterName,
argumentType.getName(), clazz.getName()));
}
return setter;
}
示例12: extractBeanValues
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
/**
* Extracts the bean values, using the supplied name mapping array.
*
* @param source
* the bean
* @param nameMapping
* the name mapping
* @throws NullPointerException
* if source or nameMapping are null
* @throws SuperCsvReflectionException
* if there was a reflection exception extracting the bean value
*/
private void extractBeanValues(final Object source, final String[] nameMapping) {
if( source == null ) {
throw new NullPointerException("the bean to write should not be null");
} else if( nameMapping == null ) {
throw new NullPointerException(
"the nameMapping array can't be null as it's used to map from fields to columns");
}
beanValues.clear();
for( int i = 0; i < nameMapping.length; i++ ) {
final String fieldName = nameMapping[i];
if( fieldName == null ) {
beanValues.add(null); // assume they always want a blank column
} else {
Method getMethod = cache.getGetMethod(source, fieldName);
try {
beanValues.add(getMethod.invoke(source));
}
catch(final Exception e) {
throw new SuperCsvReflectionException(String.format("error extracting bean value for field %s",
fieldName), e);
}
}
}
}
示例13: execute
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
protected void execute(final Object record, final Object[] paramValues) {
try {
method.invoke(record, paramValues);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
Throwable t = e.getCause() == null ? e : e.getCause();
throw new SuperCsvReflectionException(
String.format("Fail execute method '%s#%s'.", record.getClass().getName(), method.getName()),
t);
}
}
示例14: create
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
@Override
public Object create(final Class<?> clazz) {
Objects.requireNonNull(clazz, "clazz should not be null.");
final String beanName = getBeanName(clazz);
if(beanFactory.containsBean(beanName)) {
// Spring管理のクラスの場合
return beanFactory.getBean(beanName, clazz);
} else {
// 通常のBeanクラスの場合
Object obj;
try {
if(clazz.isInterface()) {
obj = BeanInterfaceProxy.createProxy(clazz);
} else {
Constructor<?> cons = clazz.getDeclaredConstructor();
cons.setAccessible(true);
obj = cons.newInstance();
}
} catch (ReflectiveOperationException e) {
throw new SuperCsvReflectionException(String.format("Fail create bean instance of '%s'", clazz.getName()), e);
}
// Springコンテナ管理外でもインジェクションする。
beanFactory.autowireBean(obj);
return obj;
}
}
示例15: execute
import org.supercsv.exception.SuperCsvReflectionException; //导入依赖的package包/类
@Override
protected void execute(final Object record, final Object[] paramValues) {
try {
method.invoke(listener, paramValues);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
Throwable t = e.getCause() == null ? e : e.getCause();
throw new SuperCsvReflectionException(
String.format("Fail execute method '%s#%s'.", listener.getClass().getName(), method.getName()),
t);
}
}