1

I have map table

public static final String key1 = "newKey";
public static final String key2 = "key2";
public static final String key3 = "key3";
public static final String key4 = "key4";

public static Map<String, String> objects = new TreeMap<String, String>();

TreeMap.put(key1,"Bob1")
TreeMap.put(key2,"Bob2")
TreeMap.put(key3,"Bob3")
TreeMap.put(key4,"Bob4")

The first parameter is the key. I want to check if the key exists. So I wrote this code

public String checkKey(String keyToCheck) {

    if (objects.containsKey(keyToCheck)) {
     .......
    }
}

The problem is that the user could call checkKey in these two ways:

  1. checkKey("newkey")

  2. checkKey("className.key1")

Either of these strings come from user input. In the first case, I don't have any problems because it's in the map. But in the second case, I need to convert it so that I can get the corresponding newkey value.

geffchang
  • 3,279
  • 2
  • 32
  • 58
user1365697
  • 5,819
  • 15
  • 60
  • 96

4 Answers4

3

Assuming you had this:

public class MyClass {
  public static final String key1 = "newKey";
}

You should be checking it like this:

checkKey(MyClass.key1);

NOT

checkKey("MyClass.key1");

Edit: Apparently, my answer is wrong. I edited his question to make it clearer.

geffchang
  • 3,279
  • 2
  • 32
  • 58
  • the problem that this is the input from the user "MyClass.key1" the value is string so it will look like checkKey("MyClass.key1"); and now I need to get the string and to find the value – user1365697 Jan 08 '13 at 05:58
  • @user1365697 Then you'd have to use reflection. This might be a starting point for you: http://stackoverflow.com/questions/8125086/reflection-constant-variables-within-a-class-loaded-via-reflection – geffchang Jan 08 '13 at 06:02
  • this would require to parse the string to extract the class name and the key out of it. – Narendra Pathai Jan 08 '13 at 06:04
  • Do I need to use reelection ? – user1365697 Jan 08 '13 at 06:12
1

You can try using reflection. But you should give fully qualified name of the class as input or else you will get a class not found exception.

In you case, you should have fully qualified name of the constants file.

Below sample is to be run from the default package.

import java.lang.reflect.Field;

    public class TestReflection {

        public static final String key1 = "newKey";

        public static void main(String[] args) throws SecurityException,
                NoSuchFieldException, IllegalArgumentException,
                IllegalAccessException, ClassNotFoundException {
            checkKey("newKey");
            checkKey("TestReflection.key1");
        }

        private static void checkKey(String str) throws NoSuchFieldException,
                IllegalAccessException, ClassNotFoundException {
            String[] str1 = str.split("\\.");
            if (str1.length == 1) {
                System.out.println(str1[0]);
            } else if (str1.length == 2) {
                Field value = Class.forName(str1[0]).getField(str1[1]);
                value.setAccessible(true);
                System.out.println(value.get(null));
            }
        }
    }
om39a
  • 1,406
  • 4
  • 20
  • 39
0

Dont use Quotes for passing variable references. Use checkKey(className.key1). The problem mentioned doesn't require any use of reflection.

Use Class.forName("classname after parsing").getField("field after parsing");

Hulk
  • 152
  • 3
  • 10
0

You can have another overloaded method for this:

checkKey(Class<?> clazz, String keyToCheck){

   //use reflection to check that key
}

I hope this will help.

Doing this user will get clear cut idea that the class that the key belongs to should be passed as another argument and not in the key name.

Otherwise do as other answers have correctly pointed out.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120