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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:37:57+00:00 2026-06-01T23:37:57+00:00

Quick edit: As was pointed out below I should be doing a BFS but

  • 0

Quick edit: As was pointed out below I should be doing a BFS but I need a point to stop retrieving new fields, which I haven’t had time to think about yet. Thanks for all the help!

I’m trying to use Java reflection to recursively get the fields of classes, essentially creating a tree to display

Class
 field1 class
  field1.1 class
  field1.2 class
 field2 class
  field 2.1 class
  field 2.2 class

This is for a recursive decent parser, so I figured a recursive display function would be cool to make. Unfortunately it’s crushing me.

Example class Foo, with fields of Class1 and Class2, each of which could have more fields of different classes:

class Assignment extends Statement {
Variable target;
Expression source;
Assignment (Variable t, Expression e) {
    target = t;
    source = e;
}

My recursive method:

private void recurse(Object object){
    System.out.println(object.getClass());
    for (Field field : object.getClass().getDeclaredFields()){
        System.out.println(field.getType());
        System.out.println(field.getName());
        if(!field.getType().isPrimitive() || field.getType() instanceof Class || field.getName() != "clazz"){
            //recurse(field);
        }
    }

The println has been for testing and an example output for Foo would be (without recursion, it seems to work) giving

class Assignment
class Variable
target
class Expression
source

But I can’t figure out how to take the Variable class and then get its fields and so on. The field that contains this information gives me a class Field.

Also, I realize I need a point to stop the recursion but stopping at primative fields doesn’t seem to work. Any suggestions would be useful and really help me understand the nitty-gritty of what I seem to be flailing around in.

Side note: for the means of this class that this is for I know I can just put a display method in for each class and call those, but I the process of reflection seemed more interesting and possibly reusable.

TLDR: How do I get the class of the actual field contained in a Field?

Sorry if I’m missing anything, first question I’ve asked.

Thanks for your time!

-Alex

  • 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-01T23:38:00+00:00Added an answer on June 1, 2026 at 11:38 pm

    Well, you are describing a graph, and a search algorithm in it.

    Your graph is G=(V,E) where V = {all object types } and E = { (u,v) | u has a field of type v }.

    You can use BFS to “explore” your graph from a single source, and get all “accessable vertices [types]” from it [the single source].

    BFS holds a visited set, and avoids exploring vertices [types] that were already explored, and it [BFS] stops, when there is nothing new to discover [the queue is empty].

    The same thing can be done with DFS, but again – you are going to need a visited set, in order to avoid infinite loops when the graph has cycles [for example: in some implementations of composite classes, such as a tree, where each Node contains a Node itself].

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

Sidebar

Related Questions

Just doing a quick edit up at the top of my post. The question
Does anyone know how to enable or disable programmatically the Quick Edit Mode in
What's a quick and easy way to view and edit ID3 tags (artist, album,
Quick one, but thought I'd ask. Is there a better way of getting the
EDIT I was a bit quick there, the problem arises in the function and
I have a table with a quick edit function (updates only one field using
Quick one folks. Have a quick look at the code snippet below. I alloc
I'm at a client doing some quick fixes to their access application. It was
This is probably a somewhat out-of-wack question. I use tramp to edit remote files,
Quick question. What do you think, I have a few sites that use a

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.