本文整理汇总了Java中org.apache.ibatis.reflection.ReflectionException类的典型用法代码示例。如果您正苦于以下问题:Java ReflectionException类的具体用法?Java ReflectionException怎么用?Java ReflectionException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ReflectionException类属于org.apache.ibatis.reflection包,在下文中一共展示了ReflectionException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setFieldEnumValueByOrdinal
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
/**
* 设置枚举类型的字段值
*
* @param target the target object from which to get the field
* @param field the field to set
* @param ordinal enum.ordinal
* @throws Exception IllegalArgumentException, IllegalAccess
*/
@SuppressWarnings("rawtypes")
public static void setFieldEnumValueByOrdinal(Object target, Field field, int ordinal) throws Exception {
if (field.getType().isEnum()) {
if (!field.isAccessible()) {
ReflectionUtils.makeAccessible(field);
}
Enum[] enumObjs = (Enum[]) (field.getType()).getEnumConstants();
for (Enum enumObj : enumObjs) {
if (enumObj.ordinal() == ordinal) {
field.set(target, enumObj);
}
}
} else {
throw new ReflectionException(target.getClass().getName() + "." + field.getName()
+ ":field type is not Enum, can not convertToEnum");
}
}
示例2: shouldInstantiateAndThrowAllCustomExceptions
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
@Test
public void shouldInstantiateAndThrowAllCustomExceptions() throws Exception {
Class<?>[] exceptionTypes = {
BindingException.class,
CacheException.class,
DataSourceException.class,
ExecutorException.class,
LogException.class,
ParsingException.class,
BuilderException.class,
PluginException.class,
ReflectionException.class,
PersistenceException.class,
SqlSessionException.class,
TransactionException.class,
TypeException.class,
ScriptingException.class
};
for (Class<?> exceptionType : exceptionTypes) {
testExceptionConstructors(exceptionType);
}
}
示例3: instantiatePropertyValue
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
public MetaObject instantiatePropertyValue(String name,
PropertyTokenizer prop, ObjectFactory objectFactory) {
MetaObject metaValue;
Class<?> type = getSetterType(prop.getName());
try {
Object newObject = objectFactory.create(type);
metaValue = MetaObject.forObject(newObject,
metaObject.getObjectFactory(),
metaObject.getObjectWrapperFactory());
set(prop, newObject);
} catch (Exception e) {
throw new ReflectionException("Cannot set value of property '"
+ name + "' because '" + name
+ "' is null and cannot be instantiated on instance of "
+ type.getName() + ". Cause:" + e.toString(), e);
}
return metaValue;
}
示例4: methodToProperty
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
public static String methodToProperty(String name) {
//去掉get|set|is
if (name.startsWith("is")) {
name = name.substring(2);
} else if (name.startsWith("get") || name.startsWith("set")) {
name = name.substring(3);
} else {
throw new ReflectionException("Error parsing property name '" + name + "'. Didn't start with 'is', 'get' or 'set'.");
}
//如果只有1个字母-->转为小写
//如果大于1个字母,第二个字母非大写-->转为小写
//String uRL -->String getuRL() {
if (name.length() == 1 || (name.length() > 1 && !Character.isUpperCase(name.charAt(1)))) {
name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
}
return name;
}
示例5: getFieldEnumOrdinal
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
/**
* 获取枚举类型的字段值-ordinal
*
* @param target the target object from which to get the field
* @param field the field to get
* @return enum.ordinal
* @throws Exception IllegalArgumentException, IllegalAccess
*/
@SuppressWarnings("rawtypes")
public static int getFieldEnumOrdinal(Object target, Field field) throws Exception {
if (field.getType().isEnum()) {
if (!field.isAccessible()) {
ReflectionUtils.makeAccessible(field);
}
return ((Enum) field.get(target)).ordinal();
} else {
throw new ReflectionException(target.getClass().getName() + "." + field.getName()
+ ":field type is not Enum, can not convertToEnum");
}
}
示例6: instantiateImmutable
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
/**
* This function should send the arguments to the "of" static constructor method of
* the immutable type.
*
* @param type
* @param constructorArgTypes
* @param constructorArgs
* @param <T>
* @return
*/
@SuppressWarnings("unchecked")
private <T> T instantiateImmutable(
Class<T> type,
List<Class<?>> constructorArgTypes,
List<Object> constructorArgs) throws NoSuchMethodException {
// Get the constructor method, throws NoSuchMethodException if not found
Method method = type
.getDeclaredMethod(
IMMUTABLE_CONSTRUCTOR_METHOD,
(Class[]) constructorArgTypes.toArray(new Class[constructorArgTypes.size()]));
// Try to invoke the method, throws IllegalAccessException if it isn't public, and throws
// InvocationTargetException if the constructor throws an error or the args are incorrect
try {
return (T) method.invoke(null, constructorArgs.toArray(new Object[constructorArgs.size()]));
} catch (InvocationTargetException | IllegalAccessException ex) {
String argTypes = constructorArgTypes
.stream()
.map(Class::getSimpleName)
.collect(Collectors.joining(", "));
String argValues = constructorArgs
.stream()
.map(String::valueOf)
.collect(Collectors.joining(", "));
throw new ReflectionException(
"Error instantiating " + type.getSimpleName() + " with invalid types (" + argTypes +
") or values (" + argValues + "). Cause: " + ex,
ex);
}
}
示例7: methodToProperty
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
public static String methodToProperty(String name) {
if (name.startsWith("is")) {
name = name.substring(2);
} else if (name.startsWith("get") || name.startsWith("set")) {
name = name.substring(3);
} else {
throw new ReflectionException("Error parsing property name '" + name + "'. Didn't start with 'is', 'get' or 'set'.");
}
if (name.length() == 1 || (name.length() > 1 && !Character.isUpperCase(name.charAt(1)))) {
name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
}
return name;
}
示例8: instantiateClass
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
<T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
try {
Constructor<T> constructor;
if (constructorArgTypes == null || constructorArgs == null) {
constructor = type.getDeclaredConstructor();
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
return constructor.newInstance();
}
constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[constructorArgTypes.size()]));
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
return constructor.newInstance(constructorArgs.toArray(new Object[constructorArgs.size()]));
} catch (Exception e) {
StringBuilder argTypes = new StringBuilder();
if (constructorArgTypes != null && !constructorArgTypes.isEmpty()) {
for (Class<?> argType : constructorArgTypes) {
argTypes.append(argType.getSimpleName());
argTypes.append(",");
}
argTypes.deleteCharAt(argTypes.length() - 1); // remove trailing ,
}
StringBuilder argValues = new StringBuilder();
if (constructorArgs != null && !constructorArgs.isEmpty()) {
for (Object argValue : constructorArgs) {
argValues.append(String.valueOf(argValue));
argValues.append(",");
}
argValues.deleteCharAt(argValues.length() - 1); // remove trailing ,
}
throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e);
}
}
示例9: getCollectionValue
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
if (collection instanceof Map) {
return ((Map) collection).get(prop.getIndex());
} else {
int i = Integer.parseInt(prop.getIndex());
if (collection instanceof List) {
return ((List) collection).get(i);
} else if (collection instanceof Object[]) {
return ((Object[]) collection)[i];
} else if (collection instanceof char[]) {
return ((char[]) collection)[i];
} else if (collection instanceof boolean[]) {
return ((boolean[]) collection)[i];
} else if (collection instanceof byte[]) {
return ((byte[]) collection)[i];
} else if (collection instanceof double[]) {
return ((double[]) collection)[i];
} else if (collection instanceof float[]) {
return ((float[]) collection)[i];
} else if (collection instanceof int[]) {
return ((int[]) collection)[i];
} else if (collection instanceof long[]) {
return ((long[]) collection)[i];
} else if (collection instanceof short[]) {
return ((short[]) collection)[i];
} else {
throw new ReflectionException("The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
}
}
}
示例10: setCollectionValue
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {
if (collection instanceof Map) {
((Map) collection).put(prop.getIndex(), value);
} else {
int i = Integer.parseInt(prop.getIndex());
if (collection instanceof List) {
((List) collection).set(i, value);
} else if (collection instanceof Object[]) {
((Object[]) collection)[i] = value;
} else if (collection instanceof char[]) {
((char[]) collection)[i] = (Character) value;
} else if (collection instanceof boolean[]) {
((boolean[]) collection)[i] = (Boolean) value;
} else if (collection instanceof byte[]) {
((byte[]) collection)[i] = (Byte) value;
} else if (collection instanceof double[]) {
((double[]) collection)[i] = (Double) value;
} else if (collection instanceof float[]) {
((float[]) collection)[i] = (Float) value;
} else if (collection instanceof int[]) {
((int[]) collection)[i] = (Integer) value;
} else if (collection instanceof long[]) {
((long[]) collection)[i] = (Long) value;
} else if (collection instanceof short[]) {
((short[]) collection)[i] = (Short) value;
} else {
throw new ReflectionException("The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
}
}
}
示例11: instantiatePropertyValue
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
@Override
public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
MetaObject metaValue;
Class<?> type = getSetterType(prop.getName());
try {
Object newObject = objectFactory.create(type);
metaValue = MetaObject.forObject(newObject, metaObject.getObjectFactory(), metaObject.getObjectWrapperFactory(), metaObject.getReflectorFactory());
set(prop, newObject);
} catch (Exception e) {
throw new ReflectionException("Cannot set value of property '" + name + "' because '" + name + "' is null and cannot be instantiated on instance of " + type.getName() + ". Cause:" + e.toString(), e);
}
return metaValue;
}
示例12: instantiateClassThrowsProperErrorMsg
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
@Test
public void instantiateClassThrowsProperErrorMsg() {
DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
try {
defaultObjectFactory.instantiateClass(TestClass.class, Collections.<Class<?>>singletonList(String.class), Collections.<Object>singletonList("foo"));
Assert.fail("Should have thrown ReflectionException");
} catch (Exception e) {
Assert.assertTrue("Should be ReflectionException", e instanceof ReflectionException);
Assert.assertTrue("Should not have trailing commas in types list", e.getMessage().contains("(String)"));
Assert.assertTrue("Should not have trailing commas in values list", e.getMessage().contains("(foo)"));
}
}
示例13: instantiateClass
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
private <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
try {
Constructor<T> constructor;
if (constructorArgTypes == null || constructorArgs == null) {
constructor = type.getDeclaredConstructor();
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
return constructor.newInstance();
}
constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[constructorArgTypes.size()]));
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
return constructor.newInstance(constructorArgs.toArray(new Object[constructorArgs.size()]));
} catch (Exception e) {
StringBuilder argTypes = new StringBuilder();
if (constructorArgTypes != null) {
for (Class<?> argType : constructorArgTypes) {
argTypes.append(argType.getSimpleName());
argTypes.append(",");
}
}
StringBuilder argValues = new StringBuilder();
if (constructorArgs != null) {
for (Object argValue : constructorArgs) {
argValues.append(String.valueOf(argValue));
argValues.append(",");
}
}
throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e);
}
}
示例14: instantiateClass
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
private <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
try {
Constructor<T> constructor;
//如果没有传入constructor,调用空构造函数,核心是调用Constructor.newInstance
if (constructorArgTypes == null || constructorArgs == null) {
constructor = type.getDeclaredConstructor();
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
return constructor.newInstance();
}
//如果传入constructor,调用传入的构造函数,核心是调用Constructor.newInstance
constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[constructorArgTypes.size()]));
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
return constructor.newInstance(constructorArgs.toArray(new Object[constructorArgs.size()]));
} catch (Exception e) {
//如果出错,包装一下,重新抛出自己的异常
StringBuilder argTypes = new StringBuilder();
if (constructorArgTypes != null) {
for (Class<?> argType : constructorArgTypes) {
argTypes.append(argType.getSimpleName());
argTypes.append(",");
}
}
StringBuilder argValues = new StringBuilder();
if (constructorArgs != null) {
for (Object argValue : constructorArgs) {
argValues.append(String.valueOf(argValue));
argValues.append(",");
}
}
throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e);
}
}
示例15: getCollectionValue
import org.apache.ibatis.reflection.ReflectionException; //导入依赖的package包/类
protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
if (collection instanceof Map) {
//map['name']
return ((Map) collection).get(prop.getIndex());
} else {
int i = Integer.parseInt(prop.getIndex());
if (collection instanceof List) {
//list[0]
return ((List) collection).get(i);
} else if (collection instanceof Object[]) {
return ((Object[]) collection)[i];
} else if (collection instanceof char[]) {
return ((char[]) collection)[i];
} else if (collection instanceof boolean[]) {
return ((boolean[]) collection)[i];
} else if (collection instanceof byte[]) {
return ((byte[]) collection)[i];
} else if (collection instanceof double[]) {
return ((double[]) collection)[i];
} else if (collection instanceof float[]) {
return ((float[]) collection)[i];
} else if (collection instanceof int[]) {
return ((int[]) collection)[i];
} else if (collection instanceof long[]) {
return ((long[]) collection)[i];
} else if (collection instanceof short[]) {
return ((short[]) collection)[i];
} else {
throw new ReflectionException("The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
}
}
}