猿问

Java思考:如何获取Java类的所有getter方法并调用它们

我写了一个有很多getter的java类。现在我想获取所有getter方法并在某个时候调用它们。我知道有一些方法,例如getMethods()或getMethod(String name,Class ... parameterTypes),但是我只想确实获得吸气剂...,使用正则表达式?有人可以告诉我吗?



小怪兽爱吃肉
浏览 600回答 3
3回答

临摹微笑

不要使用正则表达式,请使用Introspector:for(PropertyDescriptor propertyDescriptor :&nbsp;&nbsp; &nbsp; Introspector.getBeanInfo(yourClass).getPropertyDescriptors()){&nbsp; &nbsp; // propertyEditor.getReadMethod() exposes the getter&nbsp; &nbsp; // btw, this may be null if you have a write-only property&nbsp; &nbsp; System.out.println(propertyDescriptor.getReadMethod());}通常,您不需要Object.class的属性,因此可以使用带有两个参数的方法:Introspector.getBeanInfo(yourClass, stopClass)// usually with Object.class as 2nd param// the first class is inclusive, the second exclusive顺便说一句:有一些框架可以为您做到这一点,并向您展示高层次的观点。例如commons / beanutils有方法Map<String, String> properties = BeanUtils.describe(yourObject);(docs此处)就是这样做的:查找并执行所有getter并将结果存储在地图中。不幸的是,BeanUtils.describe()在返回之前将所有属性值转换为字符串。WTF。谢谢@danw更新:这是一个Java 8方法,该方法Map<String, Object>基于对象的bean属性返回a 。public static Map<String, Object> beanProperties(Object bean) {&nbsp; try {&nbsp; &nbsp; return Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Introspector.getBeanInfo(bean.getClass(), Object.class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.getPropertyDescriptors()&nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; // filter out properties with setters only&nbsp; &nbsp; &nbsp; .filter(pd -> Objects.nonNull(pd.getReadMethod()))&nbsp; &nbsp; &nbsp; .collect(Collectors.toMap(&nbsp; &nbsp; &nbsp; &nbsp; // bean property name&nbsp; &nbsp; &nbsp; &nbsp; PropertyDescriptor::getName,&nbsp; &nbsp; &nbsp; &nbsp; pd -> { // invoke method to get value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return pd.getReadMethod().invoke(bean);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // replace this with better error handling&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }));&nbsp; } catch (IntrospectionException e) {&nbsp; &nbsp; // and this, too&nbsp; &nbsp; return Collections.emptyMap();&nbsp; }}不过,您可能想使错误处理更可靠。很抱歉,已检查的异常阻止我们在此无法正常运行。事实证明Collectors.toMap()讨厌空值。这是上述代码的更命令性版本:public static Map<String, Object> beanProperties(Object bean) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> map = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(Introspector.getBeanInfo(bean.getClass(), Object.class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getPropertyDescriptors())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // filter out properties with setters only&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(pd -> Objects.nonNull(pd.getReadMethod()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(pd -> { // invoke method to get value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object value = pd.getReadMethod().invoke(bean);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (value != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(pd.getName(), value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add proper error handling here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; return map;&nbsp; &nbsp; } catch (IntrospectionException e) {&nbsp; &nbsp; &nbsp; &nbsp; // and here, too&nbsp; &nbsp; &nbsp; &nbsp; return Collections.emptyMap();&nbsp; &nbsp; }}使用JavaSlang,以更简洁的方式提供了相同的功能:public static Map<String, Object> javaSlangBeanProperties(Object bean) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; return Stream.of(Introspector.getBeanInfo(bean.getClass(), Object.class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.getPropertyDescriptors())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.filter(pd -> pd.getReadMethod() != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.toJavaMap(pd -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return new Tuple2<>(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pd.getName(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pd.getReadMethod().invoke(bean));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new IllegalStateException();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; } catch (IntrospectionException e) {&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalStateException();&nbsp; &nbsp; }}这是番石榴的版本:public static Map<String, Object> guavaBeanProperties(Object bean) {&nbsp; &nbsp; Object NULL = new Object();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; return Maps.transformValues(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.stream(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Introspector.getBeanInfo(bean.getClass(), Object.class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getPropertyDescriptors())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(pd -> Objects.nonNull(pd.getReadMethod()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(ImmutableMap::<String, Object>builder,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(builder, pd) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Object result = pd.getReadMethod()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.invoke(bean);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;builder.put(pd.getName(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;firstNonNull(result, NULL));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw propagate(e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(left, right) -> left.putAll(right.build()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build(), v -> v == NULL ? null : v);&nbsp; &nbsp; } catch (IntrospectionException e) {&nbsp; &nbsp; &nbsp; &nbsp; throw propagate(e);&nbsp; &nbsp; }}

慕斯王

// Get the Class object associated with this class.&nbsp; &nbsp; MyClass myClass= new MyClass ();&nbsp; &nbsp; Class objClass= myClass.getClass();&nbsp; &nbsp; // Get the public methods associated with this class.&nbsp; &nbsp; Method[] methods = objClass.getMethods();&nbsp; &nbsp; for (Method method:methods)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Public method found: " +&nbsp; method.toString());&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答