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

  • Home
  • SEARCH
  • 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 8800589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:37:46+00:00 2026-06-14T00:37:46+00:00

I am new to reflection and trying to create a generalized function which will

  • 0

I am new to reflection and trying to create a generalized function which will take in object and parse all the fields that are of type String, String[] or List<String>. Any String, String[] or List<String> that is present in nested object has also to be parsed. Is there any utility which can help me in doing that? Getting the values from parent object (User) was simple – used BeanUtils.describe(user); – it gives the String values in parent object but not String[], List<String> and nested object. I am sure I might not be the first one who needs this feature? Are there any utilities or code which could help me achieve do this?

public class User {
    private String somevalue;
    private String[] thisArray;
    private List<String> thisList;
    private UserDefinedObject myObject;
    .
    .
    .

}
  • 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-06-14T00:37:47+00:00Added an answer on June 14, 2026 at 12:37 am

    The method Class.getDeclaredFields will get you an array of Fields representing each field of the class. You could loop over these and check the type returned by Field.getType. Filtering fields of type List to List<String> is trickier – see this post for help with that.

    After doing the first dynamic lookup of the fields you want, you should keep track of (memoize) the relevant Field objects for better performance.

    Here’s a quick example:

    //for each field declared in User,
    for (Field field : User.class.getDeclaredFields()) {
        //get the static type of the field
        Class<?> fieldType = field.getType();
        //if it's String,
        if (fieldType == String.class) {
            // save/use field
        }
        //if it's String[],
        else if (fieldType == String[].class) {
            // save/use field
        }
        //if it's List or a subtype of List,
        else if (List.class.isAssignableFrom(fieldType)) {
            //get the type as generic
            ParameterizedType fieldGenericType =
                    (ParameterizedType)field.getGenericType();
            //get it's first type parameter
            Class<?> fieldTypeParameterType =
                    (Class<?>)fieldGenericType.getActualTypeArguments()[0];
            //if the type parameter is String,
            if (fieldTypeParameterType == String.class) {
                // save/use field
            }
        }
    }
    

    Note that I used reference equality (==) instead of isAssignableFrom to match String.class and String[].class since String is final.

    Edit: Just noticed your bit about finding nested UserDefinedObjects. You could apply recursion to the above strategy in order to search for those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an export Excel/CSV function that will iterate through a custom
I am trying to use reflection to create object array of the type created
I am trying to create a WiX custom action which will allow me to
I am trying to create an instance of an Entity Framework object using reflection:
I'm trying to execute an extension method that returns and object of type T,
First of all, this is Java 1.4 (project restrictions). I'm trying to create a
I'm trying to create an excel report that has multiple sheets. each of these
I'm trying to create a textview with reflection on android but I could not
The error message below i get when Im trying to create a new user
I've been trying to re-create BitConverter.GetBytes. However, every value type should be allowed to

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.