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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:57:28+00:00 2026-06-07T04:57:28+00:00

I want to create something that resembles an extendable Enum (understanding extending Enums isn’t

  • 0

I want to create something that resembles an extendable Enum (understanding extending Enums isn’t possible in Java 6).

Here is what im trying to do:
I have many “Model” classes and each of these classes have a set of Fields that are to be associated with it. These Fields are used to index into Maps that contain representations of the data.

I need to be able to access the Fields from an Class OR instance obj as follows:

MyModel.Fields.SOME_FIELD #=> has string value of "diff-from-field-name"

or

myModel.Fields.SOME_FIELD #=> has string value of "diff-from-field-name"

I also need to be able to get a list of ALL the fields for Model

MyModel.Fields.getKeys() #=> List<String> of all the string values ("diff-from-field name")

When defining the “Fields” class for each Model, I would like to be able to keep the definition in the same file as the Model.

public class MyModel {
    public static final Fields extends BaseFields { 
        public static final String SOME_FIELD = "diff-from-field-name";
        public static final String FOO = "bar";
    }

    public Fields Fields = new Fields();

    // Implement MyModel logic
}

I also want to have OtherModel extends MyModel and beable to inherit the Fields from MyModel.Fields and then add its own Fields on top if it ..

public class OtherModel extends MyModel {
   public static final class Fields extends MyModel.Fields { 
        public static final String CAT = "feline";
        ....

Which woulds allow

OtherModel.Fields.CAT #=> feline
OtherModel.Fields.SOME_FIELD #=> diff-from-field-name
OtherModel.Fields.FOO #=> bar
OtherModel.Fields.getKeys() #=> 3 ["feline", "diff-from-field-name", "bar"]

I am trying to make the definition of the “Fields” in the models as clean and simple as possible as a variety of developers will be building out these “Model” objects.

Thanks

  • 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-07T04:57:29+00:00Added an answer on June 7, 2026 at 4:57 am

    I was able to come up with a solution using reflection that seems to work — I haven’t gone through the full gamut of testing, this was more me just fooling around seeing what possible options I have.

    ActiveField : Java Class which all other “Fields” Classes (which will be inner classes in my Model classes) will extend. This has a non-static method “getKeys()” which looks at “this’s” class, and pulled a list of all the Fields from it. It then checks a few things like Modifiers, Field Type and Casing, to ensure that it only looks at Fields that match my convention: all “field keys” must be “public static final” of type String, and the field name must be all UPPERCASE.

    public class ActiveField {
    private final String key;
    
    protected ActiveField() {
        this.key = null;
    }
    
    public ActiveField(String key) {
        System.out.println(key);
        if (key == null) {
            this.key = "key:unknown";
        } else {
            this.key = key;
        }
    }
    
    public String toString() {
        return this.key;
    }
    
    @SuppressWarnings("unchecked")
    public List<String> getKeys() {
        ArrayList<String> keys = new ArrayList<String>();
        ArrayList<String> names = new ArrayList<String>();
    
        Class cls;
        try {
            cls = Class.forName(this.getClass().getName());
        } catch (ClassNotFoundException e) {
            return keys;
        }
    
        Field fieldList[] = cls.getFields();
    
        for (Field fld : fieldList) {
            int mod = fld.getModifiers();
    
            // Only look at public static final fields
            if(!Modifier.isPublic(mod) || !Modifier.isStatic(mod) || !Modifier.isFinal(mod)) {
                continue;
            }
    
            // Only look at String fields
            if(!String.class.equals(fld.getType())) {
                continue;
            }
    
            // Only look at upper case fields
            if(!fld.getName().toUpperCase().equals(fld.getName())) {
                continue;
            }
    
            // Get the value of the field
            String value = null;
            try {
                value = StringUtils.stripToNull((String) fld.get(this));
            } catch (IllegalArgumentException e) {
                continue;
            } catch (IllegalAccessException e) {
                continue;
            }
    
            // Do not add duplicate or null keys, or previously added named fields
            if(value == null || names.contains(fld.getName()) || keys.contains(value)) {
                continue;
            }
    
            // Success! Add key to key list
            keys.add(value);
            // Add field named to process field names list
            names.add(fld.getName());
    
        }
    
        return keys;
    }
    
    public int size() {
        return getKeys().size();
    } 
    }
    

    Then in my “Model” classes (which are fancy wrappers around a Map, which can be indexed using the Fields fields)

    public class ActiveResource {
    /**
    * Base fields for modeling ActiveResource objs – All classes that inherit from
    * ActiveResource should have these fields/values (unless overridden)
    */
    public static class Fields extends ActiveField {
    public static final String CREATED_AT = “node:created”;
    public static final String LAST_MODIFIED_AT = “node:lastModified”;
    }

    public static final Fields Fields = new Fields();
    
        ... other model specific stuff ...
    

    }

    I can then make a class Foo which extends my ActiveResource class

    public class Foo extends ActiveResource {
    
    public static class Fields extends ActiveResource.Fields {
        public static final String FILE_REFERENCE = "fileReference";
        public static final String TYPE = "type";
    }
    
    public static final Fields Fields = new Fields();
    
        ... other Foo specific stuff ...
    

    Now, I can do the following:

    ActiveResource ar = new ActiveResource().
    Foo foo = new Foo();
    
    ar.Fields.size() #=> 2
    foo.Fields.size() #=> 4
    
    ar.Fields.getKeys() #=> ["fileReference", "type", "node:created", "node:lastModified"]
    foo.Fields.getKeys() #=> ["node:created", "node:lastModified"]
    
    
    ar.Fields.CREATED_AT #=> "node:created"
    foo.Fields.CREATED_AT #=> "node:created"
    foo.Fields.TYPE #=> "type"
    etc.
    

    I can also access the Fields as a static field off my Model objects

    Foo.Fields.size(); Foo.Fields.getKeys(); Foo.Fields.CREATED_AT; Foo.Fields.FILE_REFERENCE;
    

    So far this looks like a pretty nice solution, that will require minimal instruction for building out new Models.

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

Sidebar

Related Questions

I want to create something like a leaflet/magazine using Latex. Is it possible to
I want create something that looks like the MSN Messenger chat forms. I am
What I want to do is to create something like that hotmail/facebook-style list of
I want to create something similar to TextExpander - a macro program that watches
I'm trying to create something that sort of resembles a histogram. I'm trying to
I simply want to create something like that: <strong>blah blah</strong> I've been trying to
I want to create build scripts for my MonoTouch apps. Basically, something that'll compile
I am developing a MVC 3 Web app and I want to create something
Is there a open source meme tracker ? I want to create something like
I've built a simple cms with an admin section. I want to create something

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.