Java NoSuchMethodError Generators

I am trying to create a generic MsgRequest class that can set / get any type of parameter. The goal here is to create a lightweight container for the different types of parameters in the message and pass it on to various method calls.

public class MsgRequest {

//private HashMap<String, String> _params = new HashMap<>();
private final HashMap<String, Object> _params = new HashMap<>();

/**
 * Returns the value of a specified key, if found, null otherwise.
 * @param key
 * @return 
 */

public <T> T getX(String key) {
    return (T) _params.get(key);
}


/**
 * Sets / replaces a given key in Message params.
 * @param <T>
 * @param key 
 */
public <T> void setX(String key, T element) {
    //TODO: Implement 2nd param as a generic type.
    //_params.put(key, element);
}

      

When I try to test this class like below,

@Test
public void testGetString() {
    MsgRequest msg = new MsgRequest();
    String key = "one";
    String val = "This is one.";
    msg.setX(key, val);
    //String s = msg.getX("one");
    assertTrue("result should be string type", msg.getX("one") instanceof String);
}

      

Then it throws java.lang.NoSuchMethodError.

Testcase: testGetString (com.msgx.MsgRequest.MsgRequestTest): Thrown com.msgx.MsgRequest.MsgRequest.setX (Ljava / Languages ​​/ String; Ljava / Languages ​​/ Object;) V, java.lang.NoSuchMethodmsgxrror: MsgRequest.MsgRequest.setX (Ljava / lang / String; Ljava / lang / Object;) V at com.msgx.MsgRequest.MsgRequestTest.testGetString (MsgRequestTest.java:48)

There is no way to figure out how to fix this exception. Any suggestions?

+3


source to share


1 answer


Make the class MsgRequest

generic like

public class MsgRequest<T> {
    private final Map<String, T> _params = new HashMap<>();

    /**
     * Returns the value of a specified key, if found, null otherwise.
     * @param key
     * @return 
     */
    public T getX(String key) {
        return _params.get(key);
    }

    /**
     * Sets / replaces a given key in Message params.
     * @param <T>
     * @param key 
     */
    public void setX(String key, T element) {
        _params.put(key, element);
    }
}

      



And then use it like

@Test
public void testGetString() {
    MsgRequest<String> msg = new MsgRequest<>();
    String key = "one";
    String val = "This is one.";
    msg.setX(key, val);
    assertTrue("result should be string type", 
        msg.getX("one") instanceof String);
}

      

+2


source







All Articles