190 * fully qualified name of the class in the format returned by 191 * {@code getName}. If this {@code Class} object represents a 192 * primitive type, this method returns the name of the primitive type. If 193 * this {@code Class} object represents void this method returns 194 * "void". If this {@code Class} object represents an array type, 195 * this method returns "class " followed by {@code getName}. 196 * 197 * @return a string representation of this class object. 198 */ 199 public String toString() { 200 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) 201 + getName(); 202 } 203 204 /** 205 * Returns a string describing this {@code Class}, including 206 * information about modifiers and type parameters. 207 * 208 * The string is formatted as a list of type modifiers, if any, 209 * followed by the kind of type (empty string for primitive types 210 * and {@code class}, {@code enum}, {@code interface}, or 211 * <code>@</code>{@code interface}, as appropriate), followed 212 * by the type's name, followed by an angle-bracketed 213 * comma-separated list of the type's type parameters, if any, 214 * including informative bounds on the type parameters, if any. 215 * 216 * A space is used to separate modifiers from one another and to 217 * separate any modifiers from the kind of type. The modifiers 218 * occur in canonical order. If there are no type parameters, the 219 * type parameter list is elided. 220 * 221 * For an array type, the string starts with the type name, 222 * followed by an angle-bracketed comma-separated list of the 223 * type's type parameters, if any, followed by a sequence of 224 * {@code []} characters, one set of brackets per dimension of 225 * the array. 226 * 227 * <p>Note that since information about the runtime representation 228 * of a type is being generated, modifiers not present on the 229 * originating source code or illegal on the originating source 230 * code may be present. 231 * 232 * @return a string describing this {@code Class}, including 233 * information about modifiers and type parameters 234 * 235 * @since 1.8 236 */ 237 public String toGenericString() { 238 if (isPrimitive()) { 239 return toString(); 240 } else { 241 StringBuilder sb = new StringBuilder(); 242 Class<?> component = this; 243 int arrayDepth = 0; 244 245 if (isArray()) { 246 do { 247 arrayDepth++; 248 component = component.getComponentType(); 249 } while (component.isArray()); 250 sb.append(component.getName()); 251 } else { 252 // Class modifiers are a superset of interface modifiers 253 int modifiers = getModifiers() & Modifier.classModifiers(); 254 if (modifiers != 0) { 255 sb.append(Modifier.toString(modifiers)); 256 sb.append(' '); 257 } 258 259 if (isAnnotation()) { 260 sb.append('@'); 261 } 262 if (isInterface()) { // Note: all annotation types are interfaces 263 sb.append("interface"); 264 } else { 265 if (isEnum()) 266 sb.append("enum"); 267 else 268 sb.append("class"); 269 } 270 sb.append(' '); 271 sb.append(getName()); 272 } 273 274 TypeVariable<?>[] typeparms = component.getTypeParameters(); 275 if (typeparms.length > 0) { 276 sb.append(Arrays.stream(typeparms) 277 .map(Class::typeVarBounds) 278 .collect(Collectors.joining(",", "<", ">"))); 279 } 280 281 if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth)); 282 283 return sb.toString(); 284 } 285 } 286 | 190 * fully qualified name of the class in the format returned by 191 * {@code getName}. If this {@code Class} object represents a 192 * primitive type, this method returns the name of the primitive type. If 193 * this {@code Class} object represents void this method returns 194 * "void". If this {@code Class} object represents an array type, 195 * this method returns "class " followed by {@code getName}. 196 * 197 * @return a string representation of this class object. 198 */ 199 public String toString() { 200 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) 201 + getName(); 202 } 203 204 /** 205 * Returns a string describing this {@code Class}, including 206 * information about modifiers and type parameters. 207 * 208 * The string is formatted as a list of type modifiers, if any, 209 * followed by the kind of type (empty string for primitive types 210 * and {@code class}, {@code enum}, {@code interface}, 211 * <code>@</code>{@code interface}, or {@code record} as appropriate), followed 212 * by the type's name, followed by an angle-bracketed 213 * comma-separated list of the type's type parameters, if any, 214 * including informative bounds on the type parameters, if any. 215 * 216 * A space is used to separate modifiers from one another and to 217 * separate any modifiers from the kind of type. The modifiers 218 * occur in canonical order. If there are no type parameters, the 219 * type parameter list is elided. 220 * 221 * For an array type, the string starts with the type name, 222 * followed by an angle-bracketed comma-separated list of the 223 * type's type parameters, if any, followed by a sequence of 224 * {@code []} characters, one set of brackets per dimension of 225 * the array. 226 * 227 * <p>Note that since information about the runtime representation 228 * of a type is being generated, modifiers not present on the 229 * originating source code or illegal on the originating source 230 * code may be present. 231 * 232 * @return a string describing this {@code Class}, including 233 * information about modifiers and type parameters 234 * 235 * @since 1.8 236 */ 237 @SuppressWarnings("preview") 238 public String toGenericString() { 239 if (isPrimitive()) { 240 return toString(); 241 } else { 242 StringBuilder sb = new StringBuilder(); 243 Class<?> component = this; 244 int arrayDepth = 0; 245 246 if (isArray()) { 247 do { 248 arrayDepth++; 249 component = component.getComponentType(); 250 } while (component.isArray()); 251 sb.append(component.getName()); 252 } else { 253 // Class modifiers are a superset of interface modifiers 254 int modifiers = getModifiers() & Modifier.classModifiers(); 255 if (modifiers != 0) { 256 sb.append(Modifier.toString(modifiers)); 257 sb.append(' '); 258 } 259 260 if (isAnnotation()) { 261 sb.append('@'); 262 } 263 if (isInterface()) { // Note: all annotation types are interfaces 264 sb.append("interface"); 265 } else { 266 if (isEnum()) 267 sb.append("enum"); 268 else if (isRecord()) 269 sb.append("record"); 270 else 271 sb.append("class"); 272 } 273 sb.append(' '); 274 sb.append(getName()); 275 } 276 277 TypeVariable<?>[] typeparms = component.getTypeParameters(); 278 if (typeparms.length > 0) { 279 sb.append(Arrays.stream(typeparms) 280 .map(Class::typeVarBounds) 281 .collect(Collectors.joining(",", "<", ">"))); 282 } 283 284 if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth)); 285 286 return sb.toString(); 287 } 288 } 289 |