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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:39:55+00:00 2026-05-20T15:39:55+00:00

I get object from hibernate query. Then i have list of fields, whose getters

  • 0

I get object from hibernate query. Then i have list of fields, whose getters i must invoke on this object. So after i invoke these getters (not depending on me, so i have no idea what they return), i put these objects in one array Object[]. Now i am iterating through these objects, some of which are again collections containing other objects.
So i check if objects is collection like this

private static boolean isCollection(Object obj){
    Class clazz = obj.getClass();
    return Collection.class.isAssignableFrom(clazz) ||     
     Map.class.isAssignableFrom(clazz);
}

now i need to do somethinhg like:

 if(isCollection(object)){
       get subobjects from this collection();
       getDeclaredMethods of this type of object();
       do something with everyObject();
}

Appreciate all the help on how to make this generic. Oh and yes, this method is checking for a Map, but in general in my case, map can not be returned, only sets and lists.

Thank you

EDIT: I will add exact code here for reference in case it is not clear

Object[] objectRow = parseObject(hibernateObject);
for(Object field: objectRow){
      get subobjects from this collection();
   getDeclaredMethods of this type of object();
   do something with everyObject();

}

public Object[] parseObject(Object mainObject) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
    rowToReturn = new Object[fieldObjects.size()];

    Method[] allMethods = mainObject.getClass().getMethods();
    Method[] getters = findGetters(allMethods);
    cutGetters(getters); // builds map so that we match getters with fields



    for(FieldObject field: fieldObjects){
        if(getterNameMethodMap.containsKey(field.getFinalFieldName())){
            Method methodToInvoke = getterNameMethodMap.get(field.getFinalFieldName());

            Object invokedObject = methodToInvoke.invoke(mainObject, (Object) null);
            rowToReturn[field.getOrder()] = invokedObject; // put what ever it is that we got, into the final array
        }
    }

    return rowToReturn;
}


private void cutGetters(Method[] getters){
    getterNameMethodMap = new HashMap<String, Method>();
    for(Method getter:getters){
        String stringToGet = getter.getName().substring(4,getter.getName().length());
        getterNameMethodMap.put(stringToGet, getter);
    }
  • 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-20T15:39:56+00:00Added an answer on May 20, 2026 at 3:39 pm

    This is exactly the code Generics try to prevent. Generics try to take the burden of scattering if object.type == class throughout your code from you. Have a look here, this is only an introduction:

    http://en.wikipedia.org/wiki/Generics_in_Java

    Since you have a collection of collections of objects, you would do something like this:

    List<List<? extends Shape>>
    

    This is to declare a list of lists and also to be as precise as possible which kind of object those inner lists contain (I chose Shape just as an example, no idea which kind of objects you want to deal with). You can find the explanation for that exact code here:

    http://download.oracle.com/javase/tutorial/extra/generics/methods.html

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

Sidebar

Related Questions

I have a simple object that get's geocoding data from the Google Maps API
Is there a way to get an object from a specific namespace? Perhaps with
Normally when you update an object in linq2sql you get the object from a
I'm trying to get an object element from my webpage using getElementById (ultimately so
Is it possible to get the originating port from an ActionExecutingContext object? If so,
I need to get the spring application context from a non bean object. In
I get this warning from GCC: warning: cannot pass objects of non-POD type 'class
When objects from a CallList intersect the near plane I get a flicker..., what
Can I get an object's name in run time (like getting an object's type
Is it possible to get an Object's attribute through the property tag in struts?

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.