public Object getValue(Object annotation){ try { return m.invoke(annotation, Arrays2.EMPTY_OBJECT_ARRAY); } catch (Exception e) { throw new ReflectException(e.getMessage(),e); } } }
/** * Get the field represented by the supplied {@link Field field object} on the * specified {@link Object target object}. In accordance with {@link Field#get(Object)} * semantics, the returned value is automatically wrapped if the underlying field * has a primitive type. * * @param field the field to get * @param target the target object from which to get the field * @return the field's current value */ public static Object getFieldValue(Object target,Field field) { try { return field.get(target); } catch (IllegalAccessException ex) { handleException(ex); throw new ReflectException("Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage()); } }
throw (Error) cause; throw new ReflectException(ex); throw new ReflectException(ex);
/** * Invoke the specified {@link Method} against the supplied target object with the supplied Args. The target * object can be <code>null</code> when invoking a static {@link Method}. * * @param method the method to invoke * @param target the target object to invoke the method on * @param args the invocation Arguments (may be <code>null</code>) * @return the invocation result, if any */ public static Object invokeMethod(Method method, Object target, Object... args) { try { if(!method.isAccessible()){ method.setAccessible(true); } return method.invoke(target, args); } catch (Exception ex) { handleException(ex); throw new ReflectException(ex.getMessage(), ex); } }
public static BeanType of(Class<?> beanClass) { Args.notNull(beanClass,"bean class"); BeanType accessor = cache.get(beanClass); if(null == accessor){ try { accessor = new BeanType(beanClass); } catch (Throwable e) { if(e instanceof ReflectException){ throw (ReflectException)e; }else{ throw new ReflectException("Error create bean type for class '" + beanClass.getName() + "' : " + e.getMessage(),e); } } cache.put(beanClass, accessor); } return accessor; }
@SuppressWarnings("unchecked") public <T> T newInstance(Object... args){ try { return (T)reflectedConstructor.newInstance(args); } catch (Exception e) { StringBuilder s = new StringBuilder(); for(int i=0;i<parameters.length;i++){ if(i>0){ s.append(','); } s.append(parameters[i].getName()); } throw new ReflectException(Strings.format( "Error newInstance in constructor '{0}({1})'", getDeclaringClass().getSimpleName(), s.toString()),e); } }
public Object invokeStatic(Object... args) { try { if(parameters.length != args.length){ throw new IllegalArgumentException("argument's length must be " + parameters.length); } for(int i=0;i<args.length;i++){ if(null == args[i]) { args[i] = Classes.getDefaultValue(parameters[i].getType()); } } return reflectedMethod.invoke(null, args); // if(index == -1){ // return javaMethod.invoke(null, args); // }else{ // return accessor.invokeMethod(null, index, args); // } } catch (Throwable e) { if(e instanceof InvocationTargetException){ e = e.getCause(); } if(e instanceof RuntimeException){ throw (RuntimeException)e; } throw new ReflectException(Strings.format("Error invoking method '{0}'",getName()),e); } }
throw (RuntimeException)e; throw new ReflectException(Strings.format("Error invoking method '{0}'",getName()),e);
public Object getValue(Object instance,boolean useGetter) { try { if(useGetter && null != getter){ if(getterAccessorIndex != -1){ return accessor.invokeMethod(instance, getterAccessorIndex, Arrays2.EMPTY_OBJECT_ARRAY); }else{ return getter.invoke(instance, Arrays2.EMPTY_OBJECT_ARRAY); } }else{ if(fieldAccessorIndex != -1){ return accessor.getFieldValue(instance, fieldAccessorIndex); }else { return field.get(instance); } } } catch (Throwable e) { if(e instanceof InvocationTargetException){ e = e.getCause(); } if(e instanceof RuntimeException){ throw (RuntimeException)e; } throw new ReflectException(Strings.format("Error getting value of field '{0}'",getName()),e); } }
@SuppressWarnings("unchecked") public <T> T newInstance() throws ReflectException{ if(null == defaultConstructor){ throw new ReflectException(Strings.format("there is no default constructor available in class '{0}'",getName())); } if(defaultConstructorInner){ return (T)defaultConstructor.newInstance(Reflection.newInstance(clazz.getEnclosingClass())); }else{ if(null != accessor && accessor.canNewInstance()){ return (T)accessor.newInstance(); }else{ return (T)defaultConstructor.newInstance((Object[])null); } } }
public void setValue(Object instance, Object value, boolean useSetter) { try { if(field.isSynthetic() || isFinal()){ field.set(instance, safeValue(value)); }else{ if(useSetter && null != setter){ if(setterAccessorIndex != -1){ accessor.invokeMethod(instance, setterAccessorIndex, safeValue(value)); }else{ setter.invoke(instance, safeValue(value)); } }else{ if(fieldAccessorIndex != -1){ accessor.setFieldValue(instance, fieldAccessorIndex, safeValue(value)); }else{ field.set(instance, safeValue(value)); } } } } catch (Exception e) { throw new ReflectException(Strings.format("Error setting value '{0}' to field '{1}'",value,getName()),e); } }
return accessor; } catch (Exception ex) { throw new ReflectException(Strings.format("Error constructing reflect accessor class: {0}",accessorClassName),ex);