Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get all Attributes of a final static class 1

Status
Not open for further replies.

kaeserea

Programmer
Feb 26, 2003
164
DE
Hello!

I'd like to get all Attributes and their values from a public final static class. And most of all I don't want to repeat them in a method.

The class looks like this:
[tt]
public class CommandIdsConst
{
public final static String OPEN_MAILBOX = "open my mailbox";
public final static String SORT_MAILBOX = "sort my mailbox";

.......

}
[/tt]

And in a JSP I'd like to get all these attributes and their values. Could be something like
[tt]
Hashtable myConsts = new Hashtable();
myConsts = CommandIdsConst.getAllAttributes();
[/tt]

Does something like that exist? I.e. by making the class CommandIdsConst a subclass of the one that has the getAllAttributes() methods?

Thanx for any help
Eva
 
Why don't you just have a properties file that looks like :

OPEN_MAILBOX=open my mailbox
SORT_MAILBOX=sort my mailbox

And then use the class java.util.Properties to retrieve the values ?

--------------------------------------------------
Free Database Connection Pooling Software
 
You can always define your attributes in a static final Hashtable.

Cheers.

Dian
 
Here's the code for the abstract class...

Code:
import java.util.Hashtable;
import java.lang.reflect.Field;

public abstract class Attributable {
    public Hashtable getAllAttributes() throws IllegalAccessException {
        Class c = this.getClass();
        Field[] fs = c.getFields();
        Hashtable r = new Hashtable(fs.length);
        for (int i = 0; i < fs.length; i++)
            r.put(fs[i].getName(), fs[i].get(new Object()));
        return r;
    }
}

And the code I used to test it...

Code:
import java.util.Hashtable;
import java.util.Set;
import java.util.Enumeration;

public class AttributableTest extends Attributable {
    public static final String VALUE_ONE = "Value One";
    public static final String VALUE_TWO = "Value Two";

    public static void main(String[] args) throws Exception {
        AttributableTest test = new AttributableTest();
        Hashtable ht = test.getAllAttributes();
        Set keys = ht.keySet();
        for (Enumeration e = ht.keys(); e.hasMoreElements();) {
            Object k = e.nextElement();
            System.out.println(k + " = " + ht.get(k));
        }
    }
}

But really, this would be a case for properties files ;)

Have fun.
 
Hi sneeu,

thanx for your super solution. I didn't take properties as I have lots of these classes with static attributes and already use them in so many other classes and JSPs. So I was looking for a solution that would spare me doing all the refactoring in there.

Best wishes
Eva
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top