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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:55:15+00:00 2026-05-27T05:55:15+00:00

I’m using a modified version of JosAH’s text-based menu system and am trying to

  • 0

I’m using a modified version of JosAH’s text-based menu system and am trying to figure out how to use reflection to create a list of items from a class’ declared subclasses. Because different menu items use different parameters in order to manipulate the underlying data, I’m having a problem generalizing their instantiation.

For example, the assignment was to create a custom linked list, so the menu options would consist of Print, Insert, Save, Quit etc. Printing and insertion just require a reference to the linked list in order to parse the input and call the appropriate functions in the linked list. Save additionally requires the file to save to while Quit doesn’t require any additional arguments.

public class MenuList extends TextMenu { //TextMenu extends MenuItem
    List<MenuItem> items;

    public MenuList (Object data, File file) {} //calls fillList

    //calls createList which fills a List<MenuItem> and copies it to this.items
    protected void fillList(Object data, File file) {
        this.items.addAll( createList(this.getClass(), ... args?) );
    }

    private static class MenuQuit extends MenuItem {
        public MenuQuit() {} //does not need data
    }

    private static class MenuOne extends MenuItem {
        public MenuOne(Object data) {} //manipulates data
    }

    private static class MenuTwo extends MenuItem {
        public MenuOne(Object data) {} //also manipulates data
    }

    private static class MenuThree extends MenuItem {
        //manipulates data and saves to file
        public MenuThree(Object data, File file) {} 
    }

    private static class NestedMenu extends MenuList {
        //needs parameters to pass to it's own menuItems
        public NestedMenu(Object data, File file) {}    
    }
}

public static List<MenuItem> createList(Class cls, ...args? ) {
    List<MenuItem> items = new ArrayList<MenuItem>();
    try {
        Classes<?>[] param = { parse(args)? };
        Class<?>[] menuList = cls.getDeclaredClasses();

        for(Class<?> c : menuList) {
            if(MenuItem.class.isAssignableFrom(c)) {//only adding the MenuItems
                Constructor<?> ct = c.getConstructor(param);
                Object menuItem = ct.newInstance( ...args? );
                items.add( (MenuItem) menuItem );
            }
        }
    } catch (Exception e) { }
}

Is there any way to generalize createList so that the menu items are created with the necessary parameters?

Also, because of how long I’ve been working on this, I’m completely open to the possibility that this is a completely overengineered solution and that I should scrap large parts, or perhaps the entirety, of this idea.

Edit: I don’t know if this is best place to add this piece information-
This was a solution I had been thinking about, and after reading through some responses and comments, it might be the correct one. If MenuItem always accepts a single Object as a parameter, I can make that assumption when calling getConstructor and newInstance. Afterwards, I’ll just leave it up the specific class to cast Object into something useful that they can pull their on data from.

public class MenuItem {
    Object data;
    public MenuItem(Object data) {
        this.data = data;
        parseData();
    }
    protected abstract void parseData();
}

private static class MenuQuit extends MenuItem {
    public MenuQuit(Object data) { super(data) } //nothing additional needed
    protected void parseData() {} //does nothing
}

private static class MenuSave extends MenuItem { //as outlined above
    private CustomLinkedList list;
    private File saveFile;
    public MenuSave(Object data) {
        super(data);
    }

    //may be able to use reflection to generalize this
    protected void parseData() { 
        this.list = ((MyCustomData) data).list;
        this.saveFile = ((MyCustomData) data).saveFile;
    }
}

public class MyCustomData {
    public CustomLinkedList list;
    public File saveFile;
    public int otherData;
    public MyCustomData(CustomLinkedList a, File b, int c) {} //assignment
}

I was trying to avoid this method because it starts adding complexity to the subclassed MenuItems, which I was hoping to keep as simple as possible. Is there a way to improve this solution?

  • 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-27T05:55:16+00:00Added an answer on May 27, 2026 at 5:55 am

    Not exactly an answer,
    but since I’m generally not a big fan of reflection here is another idea:

    You could add data and file to your base class MenuItem and leave them null where they are not applicable.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the

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.