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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:19:39+00:00 2026-06-02T19:19:39+00:00

The title might seem unsettling, but let me explain. I’m facing an interesting challenge,

  • 0

The title might seem unsettling, but let me explain.

I’m facing an interesting challenge, where I have a hierarchy of classes that have associated an object that stores metadata related to each one of its attributes (an int-valued enum with edit flags like UPDATED or NO_UPDATE).

The problem comes when merging two objects, because I dont want to check EVERY field on a class to see if it was updated and skip or apply the changes.

My idea: Reflection.

All the objects are behind an interface, so I could use IObject.class.getMethods() and iterate over that array in this fashion:

IClass class = //Instance of the first class;
IAnotherClass anotherClass = //Instance of the second class;
for(Method m  : IObject.class.getMethods()) {
    if(m.getName().startsWith("get")) {
        try {
            //Under this method (which is a getter) I cast it on 
            //both classes who implement interfaces that extend an
            //interface that defines the getters to make them
            //consistent and ensure I'll invoke the same methods.
            String propertyClass = (String)m.invoke(class);
            String propertyAnotherClass = (String)m.invoke(anotherClass);
            if(propertyClass != propertyAnotherClass) {
                //Update attribute and attribute status.
            }
        } catch (Exception e) {

        }
    }
}

Is there another way to implement this or should I stick to lengthy methods invoking attribute per attribute and doing the checks like that?. The objects are not going to change that much and the architecture is quite modular, so there is not much update involved if the fields change but having to change a method like that worries me a little.

EDIT 1: I’m posting a working code of what I have got so far. This code is a solution for me but, tough it works, I’m using it as a last resource not because I have time to spend but because I don’t want to rediscover the wheel. If I use it, I’ll make a static list with the methods so I only have to fetch that list once, considering the fact that AlexR pointed out.

private static void merge(IClazz from, IClazz to) {
        Method methods[] = from.getClass().getDeclaredMethods();
        for(Method m : methods) {
            if(m.getName().startsWith("get") && !m.getName().equals("getMetadata")) {
                try {                   
                    String commonMethodAnchor = m.getName().split("get")[1];
                    if(!m.getReturnType().cast(m.invoke(from)).equals(m.getReturnType().cast(m.invoke(to)))) {
                        String setterMethodName = "set" + commonMethodAnchor;
                        Method setter = IClazz.class.getDeclaredMethod(setterMethodName, m.getReturnType());
                        setter.invoke(to, m.getReturnType().cast(m.invoke(from)));
                        //Updating metadata
                        String metadataMethodName = "set" + commonMethodAnchor + "Status";
                        Method metadataUpdater = IClazzMetadata.class.getDeclaredMethod(metadataMethodName, int.class);
                        metadataUpdater.invoke(to.getMetadata(), 1);
                    }

                } catch (Exception e) {

                }
            }
        }
    }

metadataUpdater sets the value to 1 just to simulate the “UPDATED” flag I’m using on the real case scenario.

EDIT 3: Thanks Juan, David and AlexR for your suggestions and directions! They really pointed me to consider things I did not consider at first (I’m upvoting all your answers because all of them helped me).

After adding what AlexR sugegsted and checking jDTO and Apache Commons (finding out that in the end the general concepts are quite similar) I’ve decided to stick to my code instead of using other tools, since it is working given the object hierarchy and metadata structure of the solution and there are no exceptions popping up so far. The code is the one on the 2nd edit and I’ve placed it on a helper class that did the trick in the end.

  • 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-02T19:19:40+00:00Added an answer on June 2, 2026 at 7:19 pm

    Your approach is OK, but keep in mind that getMethod() is much slower than invoke(), so if your code is performance critical you will probably want to cache the Method objects.

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

Sidebar

Related Questions

The title might be a little weird but let me explain. I have User,
the title might be a little bit confusing, let me explain, ;) I have
The title might not explain my problem fully but I wasn't sure how to
Sorry for the bad question title. Let's say that I have DateRange class that
I know that the title might seem silly, couldn't think of something better, sorry.
From reading the title it might seem like an odd request, so let me
The title might not be clear, but I'll explain it here. I'm a total
I asked a question , title of which might have been misleading so I'm
Does that title sound confusing? I thought it might well anyways. When I person
Apologies for the long winded title but looking for a solution to what might

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.