c_idle
c_idle

Reputation: 1564

How to access Class.typeName on Android api 21+

Class.typeName or Class.getTypeName() was added in Api 26 according to the Android docs. Is there a way to get the type name in some other fashion?

enter image description here

Upvotes: 1

Views: 324

Answers (2)

Holger
Holger

Reputation: 298163

The method getTypeName() has been added in Java 8, to implement the Type.getTypeName() method that was added in the same Java version (as Class implements Type).

For non-array classes the result is the same as getName(). For exactly the same result, you can use

static String getTypeName(Class<?> cl)
{
  if(!cl.isArray()) return cl.getName();
  int dimensions;
  for(dimensions = 0; cl.isArray(); cl = cl.getComponentType()) dimensions++;
  String name = cl.getName();
  StringBuilder sb = new StringBuilder(name.length() + dimensions * 2).append(name);
  for(; dimensions > 0; dimensions--) sb.append("[]");
  return sb.toString();
}

Upvotes: 2

sdex
sdex

Reputation: 3753

Nohow.

Class.getTypeName() was added in java 8. Android supports java 8 since API 26.

Upvotes: 0

Related Questions