Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6941811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:56:01+00:00 2026-05-27T12:56:01+00:00

I write a java class which has many getters..now I want to get all

  • 0

I write a java class which has many getters..now I want to get all getter methods and invoke them sometime..I know there are methods such as getMethods() or getMethod(String name, Class… parameterTypes) ,but i just want to get the getter indeed…, use regex? anyone can tell me ?Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:56:01+00:00Added an answer on May 27, 2026 at 12:56 pm

    Don’t use regex, use the Introspector:

    for(PropertyDescriptor propertyDescriptor : 
        Introspector.getBeanInfo(yourClass).getPropertyDescriptors()){
    
        // propertyEditor.getReadMethod() exposes the getter
        // btw, this may be null if you have a write-only property
        System.out.println(propertyDescriptor.getReadMethod());
    }
    

    Usually you don’t want properties from Object.class, so you’d use the method with two parameters:

    Introspector.getBeanInfo(yourClass, stopClass)
    // usually with Object.class as 2nd param
    // the first class is inclusive, the second exclusive
    

    BTW: there are frameworks that do that for you and present you a high-level view. E.g.
    commons/beanutils has the method

    Map<String, String> properties = BeanUtils.describe(yourObject);
    

    (docs here) which does just that: find and execute all the getters and store the result in a map. Unfortunately, BeanUtils.describe() converts all the property values to Strings before returning. WTF. Thanks @danw


    Update:

    Here’s a Java 8 method that returns a Map<String, Object> based on an object’s bean properties.

    public static Map<String, Object> beanProperties(Object bean) {
      try {
        return Arrays.asList(
             Introspector.getBeanInfo(bean.getClass(), Object.class)
                         .getPropertyDescriptors()
          )
          .stream()
          // filter out properties with setters only
          .filter(pd -> Objects.nonNull(pd.getReadMethod()))
          .collect(Collectors.toMap(
            // bean property name
            PropertyDescriptor::getName,
            pd -> { // invoke method to get value
                try { 
                    return pd.getReadMethod().invoke(bean);
                } catch (Exception e) {
                    // replace this with better error handling
                   return null;
                }
            }));
      } catch (IntrospectionException e) {
        // and this, too
        return Collections.emptyMap();
      }
    }
    

    You probably want to make error handling more robust, though. Sorry for the boilerplate, checked exceptions prevent us from going fully functional here.


    Turns out that Collectors.toMap() hates null values. Here’s a more imperative version of the above code:

    public static Map<String, Object> beanProperties(Object bean) {
        try {
            Map<String, Object> map = new HashMap<>();
            Arrays.asList(Introspector.getBeanInfo(bean.getClass(), Object.class)
                                      .getPropertyDescriptors())
                  .stream()
                  // filter out properties with setters only
                  .filter(pd -> Objects.nonNull(pd.getReadMethod()))
                  .forEach(pd -> { // invoke method to get value
                      try {
                          Object value = pd.getReadMethod().invoke(bean);
                          if (value != null) {
                              map.put(pd.getName(), value);
                          }
                      } catch (Exception e) {
                          // add proper error handling here
                      }
                  });
            return map;
        } catch (IntrospectionException e) {
            // and here, too
            return Collections.emptyMap();
        }
    }
    

    Here’s the same functionality in a more concise way, using JavaSlang:

    public static Map<String, Object> javaSlangBeanProperties(Object bean) {
        try {
            return Stream.of(Introspector.getBeanInfo(bean.getClass(), Object.class)
                                         .getPropertyDescriptors())
                         .filter(pd -> pd.getReadMethod() != null)
                         .toJavaMap(pd -> {
                             try {
                                 return new Tuple2<>(
                                         pd.getName(),
                                         pd.getReadMethod().invoke(bean));
                             } catch (Exception e) {
                                 throw new IllegalStateException();
                             }
                         });
        } catch (IntrospectionException e) {
            throw new IllegalStateException();
    
        }
    }
    

    And here’s a Guava version:

    public static Map<String, Object> guavaBeanProperties(Object bean) {
        Object NULL = new Object();
        try {
            return Maps.transformValues(
                    Arrays.stream(
                            Introspector.getBeanInfo(bean.getClass(), Object.class)
                                        .getPropertyDescriptors())
                          .filter(pd -> Objects.nonNull(pd.getReadMethod()))
                          .collect(ImmutableMap::<String, Object>builder,
                                   (builder, pd) -> {
                                       try {
                                           Object result = pd.getReadMethod()
                                                             .invoke(bean);
                                           builder.put(pd.getName(),
                                                       firstNonNull(result, NULL));
                                       } catch (Exception e) {
                                           throw propagate(e);
                                       }
                                   },
                                   (left, right) -> left.putAll(right.build()))
                          .build(), v -> v == NULL ? null : v);
        } catch (IntrospectionException e) {
            throw propagate(e);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dummy Java Program, which I want to write in Clojure. It
There is a java file, which has some dependencies jars. But now, I don't
hei. the language is java. i want to extend this class which the constructor
Java Code In Java code I have class called IdentificationResult which has 3 members:
I am trying to write a Java class to extract a large zip file
I would like to be able to write a Java class in one package
I need to write a Java Comparator class that compares Strings, however with one
I am about to write junit tests for a XML parsing Java class that
Write a class named RaceHorse that extends Thread. Each RaceHorse has a name and
I am trying to write a small program which has to store and retrieve

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.