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 8011869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:08:47+00:00 2026-06-04T19:08:47+00:00

I have code similar to following: void processObjectRecursively(Object obj) { Field[] fields = obj.getClass().getDeclaredFields();

  • 0

I have code similar to following:

void processObjectRecursively(Object obj)
{
    Field[] fields = obj.getClass().getDeclaredFields();

    for(Field field : fields)
    {
        FieldType type = FieldUtils.getType(field); // FieldType is enum
        Object value = field.get(obj);

        switch(type)
        {
            case A:
            processObjectRecursively(value);
            continue;

            case LIST:
            processList((List<?>)value);
            continue;

            case B:
            processB(value);
            continue;

            case C:
            processC(value);
            continue;
        }
    }
}

void processList(List<?> list)
{
    for(Object obj : list)
    {
        processObjectRecursively(obj);
    }
}

void processB(Object obj)
{
    // do something
}

void processC(Object obj)
{
    // do something
}

Now. I don’t like this long switch case. So I’m thinking of creating commands and populating a Map with them where the type is the key and the value is its corresponding command object. With this, the method would look something like:

void processObjectRecursively(Object obj)
{
    Field[] fields = obj.getClass().getDeclaredFields();

    for(Field field : fields)
    {
        FieldType type = FieldUtils.getType(field); // FieldType is enum
        Object value = field.get(obj);
        Command command = commandMap.get(type);
        command.execute(value, this); // 'this' needed for calling processObjectRecursively()
    }
}

But for this I will need to create one interface and four more classes. So is this approach okay or is it over-engineered? Can you suggest any other simpler approach to achieve the same effect?

  • 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-04T19:08:50+00:00Added an answer on June 4, 2026 at 7:08 pm

    I agree its always better to avoid switch case statement. So creating a command map would do. Another easier approach could be to add the process method inside your enum so that every enum type knows how to process the data.

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

Sidebar

Related Questions

I have multiple chunks of code similar to the following: void GetPerson(Action<PersonView, Exception> callback);
If I have code similar to the following: public Constructor(final Object o) { taskSystem.add(new
I have code similar to the following with a URL like this... If I
In a CUDA kernel, I have code similar to the following. I am trying
I have code, similar to the following, that I would like to modify: Sub
I have an ASP.NET web site with something similar to the following code: .aspx
The following code returns the error from the For Each loop. I have similar
I'm doing something similar to the following code. I have already gone through AddtoStructFunction()
I have a function similar to the following: public void Increment() { if (Count
I have been working on some code that is similar to the following: typedef

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.