0

I need to know the runtime class of Arrays.asList().

So when i do this:

List<String> list = Arrays.asList("one", "two");
logger.info(list.getClass()); // ==> class java.util.Arrays$ArrayList

The log shows: class java.util.Arrays$ArrayList .

Some search for, java.util.Arrays$ArrayList:

is a nested class inside the Arrays class that implements the List interface. This implementation has a fixed length and is backed by an array.

But i do not find the runtime class of it, to do this alt when for exemple: List args = Arrays.asList("one", "two")

public Object register(Object args) {

       if(args.getClass().equals(java.util.Arrays.class)) {
            //do something
            // don't work
            return "";
       }
       if(args.getClass().equals(ArrayList.class)) {
            //do something
            // don't work
            return "";
       }
       //other Alt statement code
       return "";
}

I have no idea how to do the comparaison.

Yugerten
  • 878
  • 1
  • 11
  • 30
  • 1
    Why do you *need* to know it? It's an implementation detail, and you shouldn't depend on it for your program correctness. – chrylis -cautiouslyoptimistic- Feb 14 '19 at 23:46
  • @chrylis, caus the param 'args' can be a String or Arrays.asList() and the implementation of the register method depends of the runtime class of the this param – Yugerten Feb 14 '19 at 23:59
  • Then your register method is broken in multiple ways: You should have multiple compile-time overloads, not accept `Object`, and you should not distinguish between the specific list type used by `asList()` and any general `List`. – chrylis -cautiouslyoptimistic- Feb 15 '19 at 01:57
  • @chrylis the register(Object args) method implemnts a Delegate pattern and the body of this method process of multiple alternative if(args.getclass().equals(String.class)){ // ..}.... if(args.getClass().equals(Integer.class)){//... }.... if(args.getClass().equals(java.util.Arrays$ArrayList)) { //.....} – Yugerten Feb 15 '19 at 02:19

0 Answers0