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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:46:30+00:00 2026-05-28T20:46:30+00:00

I have a class which has several fields which are a subclass of another

  • 0

I have a class which has several fields which are a subclass of another class. I want to quickly find all instances of that subclass within the top level class.

For example

public class TopClass {
 private ClassIWant1 myVar1;
 private ClassIWant2 myVar2;
 private OtherJunk myVar3;
 private Nested myVar4;
}

public class Nested {
 private ClassIWant3 myVar11;
}

public class SuperClass {
}

public ClassIWant1 extends SuperClass {}
public ClassIWant2 extends SuperClass {}
public ClassIWant3 extends ClassIWant1 {}

If I were to run that example through with an instance of TopClass I would expect to get a List containing the values for myVar1, myVar2, and myVar11.

I have a general idea of how to use reflection to do this manually, but I’m hoping that I don’t have to reinvent the wheel. Is there a library that can do this?

I am familiar with ReflectUtils, but I am not sure if that can do this or not.

  • 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-28T20:46:32+00:00Added an answer on May 28, 2026 at 8:46 pm

    If I understand your request correctly, you’re looking for something like this:

    public class Test {
        public static void main(String[] args) {
            TopClass top = …; // initialise as appropriate
            System.out.println(findFields(top, SuperClass.class));
        }
    
        private static <T> List<T> findFields(Object haystack, Class<T> needle) {
            return findFields0(haystack, needle, new HashSet<Object>(), new ArrayList<T>());
        }
    
        private static <T> List<T> findFields0(Object haystack, Class<T> needle, Set<Object> visited, List<T> result) {
            if (visited.contains(haystack)) return result; // we already searched this object
    
            visited.add(haystack);
    
            for (Field field : haystack.getClass().getFields()) {
                field.setAccessible(true);
                Object fieldValue = null;
                try {
                    fieldValue = field.get(haystack);
                } catch (IllegalAccessException e) {
                    // shouldn't happen
                    throw new RuntimeException(e);
                }
                if (needle.isAssignableFrom(field.getType())) {
                    result.add(needle.cast(fieldValue));
                }
    
                // recurse
                findFields0(fieldValue, needle, visited, result);
            }
            return result;
        }
    }
    

    This works by using the static types of the fields as declared. That is, if you declare a field as Object but it holds an instance of SuperClass or one of its descendants, it won’t be found. It will also return nulls if the fields have them set as the value. I have no idea what this will do about primitive types.

    Disclaimer: Code was tested briefly on an optimistic example, I hold no responsibility if it causes your computer to catch fire.

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

Sidebar

Related Questions

I have a class that has a number of fields, two of which are
I have a class Foo which has several methods like button_0_0 , button_0_1 ,
I have a base class Character which has several classes deriving from it. The
In a C++ application, let's say I have a window class, which has several
I have a photo_album which has several photos, IDs: 4,9,10,11,31 -- all for photo_album:3
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have a class which has many small functions. By small functions, I mean
I have an class which has a enum property and a boolean property, based
I have a class which has a function to retrieve children elements from a
I have a class which has a private attribute which is a reference 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.