java - How to create a dynamic Interface with properties file at compile time? -


the problem here property file use has insanely huge name key , of run incorrect key naming issues . got me thinking if there's way generate following interface based on property file. every change make property file auto-adjust properties interface. or there other solution?

property file

a=apple b=bannana c=cherry 

should generate following interface

interface properties{ public static final string = "a" // keys public static final string b = "b";  public static final string c = "c";   } 

so in application code

string a_value = prop.getstring(properties.a); 

there old rule programming , not it, if looks beautiful, right way do.

this approach not good, point of view.

the first thing:

do not declare constants in interfaces. violates incapsulation approach. check article please: http://en.wikipedia.org/wiki/constant_interface

the second thing:

use prefix name part of properties somehow special, let say: key_

and when load properties file, iterate on keys , extract keys name starts key_ , use values of these keys planned use constants in question.


update

assume, generate huge properties file upon compilation process, using our apache ant script.

for example, let's properties file (myapp.properties) looks that:

key_a = apple key_b = banana key_c = cherry anotherpropertykey1 = blablabla1 anotherpropertykey2 = blablabla2 

our special properties want handle have key names start key_ prefix.

so, write following code (please note, not optimized, proof of concept):

package propertiestest;  import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.inputstream; import java.util.arrays; import java.util.enumeration; import java.util.hashset; import java.util.properties; import java.util.set;   public class propertiestest {    public static void main(string[] args) throws ioexception {        final string properties_filename = "myapp.properties";               specialpropertykeysstore spkstore =                              new specialpropertykeysstore(properties_filename);         system.out.println(arrays.tostring(spkstore.getkeysarray()));     } }   class specialpropertykeysstore {      private final set<string> keys;      public specialpropertykeysstore(string propertiesfilename)                                      throws filenotfoundexception, ioexception {           // prefix of name of special property key         final string key_prefix = "key_";              properties propertieshandler = new properties();         keys = new hashset<>();      try (inputstream input = new fileinputstream(propertiesfilename)) {              propertieshandler.load(input);              enumeration<?> enumeration = propertieshandler.propertynames();             while (enumeration.hasmoreelements()) {                 string key = (string) enumeration.nextelement();                 if (key.startswith(key_prefix)) {                     keys.add(key);                 }             }     }             }      public boolean iskeypresent(string keyname) {         return keys.contains(keyname);     }      public string[] getkeysarray() {         string[] strtypeparam = new string[0];          return keys.toarray(strtypeparam);     } } 

class specialpropertykeysstore filters , collects special keys instance.

and can array of these keys, or check whether key present or not.

if run code, get:

[key_c, key_b, key_a] 

it string representation of returned array special key names.

change code want meet requirements.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -