public static boolean isAccessorPresent(String prefix, String property, Class<?> bean) { try { bean.getMethod(prefix + capitalize(property)); return true; } catch (NoSuchMethodException ex) { return false; } }
@Test public void uncapitalize() { assertEquals("x", BeanUtils.uncapitalize("X")); assertEquals("prop", BeanUtils.uncapitalize("Prop")); assertEquals("URL", BeanUtils.uncapitalize("URL")); assertEquals("cId", BeanUtils.uncapitalize("cId")); assertEquals("sEPOrder", BeanUtils.capitalize("sEPOrder")); }
@Override public Class<?> validate(Class<?> parent, String property, Class<?> propertyType) { Method getter = BeanUtils.getAccessor("get", property, parent); if (getter == null && Primitives.wrap(propertyType).equals(Boolean.class)) { getter = BeanUtils.getAccessor("is", property, parent); } if (getter != null) { if (Map.class.isAssignableFrom(getter.getReturnType())) { return (Class) ReflectionUtils.getTypeParameterAsClass(getter.getGenericReturnType(), 1); } else if (Collection.class.isAssignableFrom(getter.getReturnType())) { return (Class) ReflectionUtils.getTypeParameterAsClass(getter.getGenericReturnType(), 0); } else { return (Class) Primitives.wrap(getter.getReturnType()); } } else { return null; } } };
protected String propertyNameForGetter(Method method) { String name = method.getName(); name = name.startsWith("is") ? name.substring(2) : name.substring(3); return BeanUtils.uncapitalize(name); }
String propertyName; if (name.startsWith("get")) { propertyName = BeanUtils.uncapitalize(name.substring(3)); } else { propertyName = BeanUtils.uncapitalize(name.substring(2));
public static Method getAccessor(String prefix, String property, Class<?> bean) { try { return bean.getMethod(prefix + capitalize(property)); } catch (NoSuchMethodException e) { return null; } }
String name = method.getSimpleName().toString(); if (name.startsWith("get") && name.length() > 3 && method.getParameters().isEmpty()) { name = BeanUtils.uncapitalize(name.substring(3)); } else if (name.startsWith("is") && name.length() > 2 && method.getParameters().isEmpty()) { name = BeanUtils.uncapitalize(name.substring(2)); } else { continue;
@Override public Class<?> validate(Class<?> parent, String property, Class<?> propertyType) { Method getter = BeanUtils.getAccessor("get", property, parent); if (getter == null && Primitives.wrap(propertyType).equals(Boolean.class)) { getter = BeanUtils.getAccessor("is", property, parent); } if (getter != null) { if (Map.class.isAssignableFrom(getter.getReturnType())) { return (Class) ReflectionUtils.getTypeParameterAsClass(getter.getGenericReturnType(), 1); } else if (Collection.class.isAssignableFrom(getter.getReturnType())) { return (Class) ReflectionUtils.getTypeParameterAsClass(getter.getGenericReturnType(), 0); } else { return (Class) Primitives.wrap(getter.getReturnType()); } } else { return null; } } };
@Nullable public static Method getGetterOrNull(Class<?> beanClass, String name, Class<?> type) { String methodName = ((type.equals(Boolean.class) || type.equals(boolean.class)) ? "is" : "get") + BeanUtils.capitalize(name); while (beanClass != null && !beanClass.equals(Object.class)) { try { return beanClass.getDeclaredMethod(methodName); } catch (SecurityException e) { // skip } catch (NoSuchMethodException e) { // skip } beanClass = beanClass.getSuperclass(); } return null; }
protected String propertyNameForGetter(Method method) { String name = method.getName(); name = name.startsWith("is") ? name.substring(2) : name.substring(3); return BeanUtils.uncapitalize(name); }
private void assertReturnType(Method method) { assertEquals(BeanUtils.capitalize(method.getName()), method.getReturnType().getSimpleName()); }
@Test public void capitalize() { assertEquals("X", BeanUtils.capitalize("x")); assertEquals("Prop", BeanUtils.capitalize("prop")); assertEquals("URL", BeanUtils.capitalize("URL")); assertEquals("cId", BeanUtils.capitalize("cId")); assertEquals("sEPOrder", BeanUtils.capitalize("sEPOrder")); }
String propertyName = property.getEscapedName(); writer.beginPublicMethod(property.getType(), "get" + BeanUtils.capitalize(propertyName)); writer.line("return ", propertyName, ";"); writer.end(); writer.beginPublicMethod(Types.VOID, "set" + BeanUtils.capitalize(propertyName), parameter); writer.line("this.", propertyName, " = ", propertyName, ";"); writer.end();
public static Method getAccessor(String prefix, String property, Class<?> bean) { try { return bean.getMethod(prefix + capitalize(property)); } catch (NoSuchMethodException e) { return null; } }
public static boolean isAccessorPresent(String prefix, String property, Class<?> bean) { try { bean.getMethod(prefix + capitalize(property)); return true; } catch (NoSuchMethodException ex) { return false; } }
@Nullable public static Method getGetterOrNull(Class<?> beanClass, String name, Class<?> type) { String methodName = ((type.equals(Boolean.class) || type.equals(boolean.class)) ? "is" : "get") + BeanUtils.capitalize(name); while (beanClass != null && !beanClass.equals(Object.class)) { try { return beanClass.getDeclaredMethod(methodName); } catch (SecurityException e) { // skip } catch (NoSuchMethodException e) { // skip } beanClass = beanClass.getSuperclass(); } return null; }
/** * 获取Bean的属性Setter。 */ @SuppressWarnings("unchecked") public static Method findSetter(Object bean, String name, Class type) { Assert.notNull(bean, "Bean param missing"); String methodName = "set" + BeanUtils.capitalize(name); Class beanClass = bean.getClass(); while (beanClass != null && !beanClass.equals(Object.class)) { try { return beanClass.getDeclaredMethod(methodName, type); } catch (SecurityException | NoSuchMethodException e) { // 跳过当前类并从父类查找 beanClass = beanClass.getSuperclass(); } } return null; }
String propertyName = property.getEscapedName(); writer.beginPublicMethod(property.getType(), "get" + BeanUtils.capitalize(propertyName)); writer.line("return ", propertyName, ";"); writer.end(); writer.beginPublicMethod(Types.VOID, "set" + BeanUtils.capitalize(propertyName), parameter); writer.line("this.", propertyName, " = ", propertyName, ";"); writer.end();
String propertyName = property.getEscapedName(); writer.beginPublicMethod(property.getType(), "get" + BeanUtils.capitalize(propertyName)); writer.line("return ", propertyName, ";"); writer.end(); writer.beginPublicMethod(Types.VOID, "set" + BeanUtils.capitalize(propertyName), parameter); writer.line("this.", propertyName, " = ", propertyName, ";"); writer.end();
String propertyName = property.getEscapedName(); writer.beginPublicMethod(property.getType(), "get" + BeanUtils.capitalize(propertyName)); writer.line("return ", propertyName, ";"); writer.end(); writer.beginPublicMethod(Types.VOID, "set" + BeanUtils.capitalize(propertyName), parameter); writer.line("this.", propertyName, " = ", propertyName, ";"); writer.end();