132 133 /* 134 * Private constructor. Only the Java Virtual Machine creates Class objects. 135 * This constructor is not used and prevents the default constructor being 136 * generated. 137 */ 138 private Class(ClassLoader loader, Class<?> arrayComponentType) { 139 // Initialize final field for classLoader. The initialization value of non-null 140 // prevents future JIT optimizations from assuming this final field is null. 141 classLoader = loader; 142 componentType = arrayComponentType; 143 } 144 145 /** 146 * Converts the object to a string. The string representation is the 147 * string "class" or "interface", followed by a space, and then by the 148 * fully qualified name of the class in the format returned by 149 * {@code getName}. If this {@code Class} object represents a 150 * primitive type, this method returns the name of the primitive type. If 151 * this {@code Class} object represents void this method returns 152 * "void". 153 * 154 * @return a string representation of this class object. 155 */ 156 public String toString() { 157 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) 158 + getName(); 159 } 160 161 /** 162 * Returns a string describing this {@code Class}, including 163 * information about modifiers and type parameters. 164 * 165 * The string is formatted as a list of type modifiers, if any, 166 * followed by the kind of type (empty string for primitive types 167 * and {@code class}, {@code enum}, {@code interface}, or 168 * <code>@</code>{@code interface}, as appropriate), followed 169 * by the type's name, followed by an angle-bracketed 170 * comma-separated list of the type's type parameters, if any. 171 * 172 * A space is used to separate modifiers from one another and to 173 * separate any modifiers from the kind of type. The modifiers 174 * occur in canonical order. If there are no type parameters, the 175 * type parameter list is elided. 176 * 177 * <p>Note that since information about the runtime representation 178 * of a type is being generated, modifiers not present on the 179 * originating source code or illegal on the originating source 180 * code may be present. 181 * 182 * @return a string describing this {@code Class}, including 183 * information about modifiers and type parameters 184 * 185 * @since 1.8 186 */ 187 public String toGenericString() { 188 if (isPrimitive()) { 189 return toString(); 190 } else { 191 StringBuilder sb = new StringBuilder(); 192 193 // Class modifiers are a superset of interface modifiers 194 int modifiers = getModifiers() & Modifier.classModifiers(); 195 if (modifiers != 0) { 196 sb.append(Modifier.toString(modifiers)); 197 sb.append(' '); 198 } 199 200 if (isAnnotation()) { 201 sb.append('@'); 202 } 203 if (isInterface()) { // Note: all annotation types are interfaces 204 sb.append("interface"); 205 } else { 206 if (isEnum()) 207 sb.append("enum"); 208 else 209 sb.append("class"); 210 } 211 sb.append(' '); 212 sb.append(getName()); 213 214 TypeVariable<?>[] typeparms = getTypeParameters(); 215 if (typeparms.length > 0) { 216 boolean first = true; 217 sb.append('<'); 218 for(TypeVariable<?> typeparm: typeparms) { 219 if (!first) 220 sb.append(','); 221 sb.append(typeparm.getTypeName()); 222 first = false; 223 } 224 sb.append('>'); 225 } 226 227 return sb.toString(); 228 } 229 } 230 231 /** 232 * Returns the {@code Class} object associated with the class or 233 * interface with the given string name. Invoking this method is 234 * equivalent to: 235 * 236 * <blockquote> 237 * {@code Class.forName(className, true, currentLoader)} 238 * </blockquote> 239 * 240 * where {@code currentLoader} denotes the defining class loader of 241 * the current class. 242 * 243 * <p> For example, the following code fragment returns the 244 * runtime {@code Class} descriptor for the class named 245 * {@code java.lang.Thread}: | 132 133 /* 134 * Private constructor. Only the Java Virtual Machine creates Class objects. 135 * This constructor is not used and prevents the default constructor being 136 * generated. 137 */ 138 private Class(ClassLoader loader, Class<?> arrayComponentType) { 139 // Initialize final field for classLoader. The initialization value of non-null 140 // prevents future JIT optimizations from assuming this final field is null. 141 classLoader = loader; 142 componentType = arrayComponentType; 143 } 144 145 /** 146 * Converts the object to a string. The string representation is the 147 * string "class" or "interface", followed by a space, and then by the 148 * fully qualified name of the class in the format returned by 149 * {@code getName}. If this {@code Class} object represents a 150 * primitive type, this method returns the name of the primitive type. If 151 * this {@code Class} object represents void this method returns 152 * "void". If this {@code Class} object presents an array type, 153 * this method returns "class " followed by {@code getName}. 154 * 155 * @return a string representation of this class object. 156 */ 157 public String toString() { 158 return (isInterface() ? "interface " : (isPrimitive() ? "" : "class ")) 159 + getName(); 160 } 161 162 /** 163 * Returns a string describing this {@code Class}, including 164 * information about modifiers and type parameters. 165 * 166 * The string is formatted as a list of type modifiers, if any, 167 * followed by the kind of type (empty string for primitive types 168 * and {@code class}, {@code enum}, {@code interface}, or 169 * <code>@</code>{@code interface}, as appropriate), followed 170 * by the type's name, followed by an angle-bracketed 171 * comma-separated list of the type's type parameters, if any. 172 * 173 * A space is used to separate modifiers from one another and to 174 * separate any modifiers from the kind of type. The modifiers 175 * occur in canonical order. If there are no type parameters, the 176 * type parameter list is elided. 177 * 178 * For an array type, the string starts with the type name, 179 * followed by an angle-bracketed comma-separated list of the 180 * type's type parameters, if any, followed by a sequence of 181 * {@code []} characters, one set of brackets per dimension of 182 * the array. 183 * 184 * <p>Note that since information about the runtime representation 185 * of a type is being generated, modifiers not present on the 186 * originating source code or illegal on the originating source 187 * code may be present. 188 * 189 * @return a string describing this {@code Class}, including 190 * information about modifiers and type parameters 191 * 192 * @since 1.8 193 */ 194 public String toGenericString() { 195 if (isPrimitive()) { 196 return toString(); 197 } else { 198 StringBuilder sb = new StringBuilder(); 199 Class<?> component = this; 200 int arrayDepth = 0; 201 202 if (isArray()) { 203 do { 204 arrayDepth++; 205 component = component.getComponentType(); 206 } while (component.isArray()); 207 sb.append(component.getName()); 208 } else { 209 // Class modifiers are a superset of interface modifiers 210 int modifiers = getModifiers() & Modifier.classModifiers(); 211 if (modifiers != 0) { 212 sb.append(Modifier.toString(modifiers)); 213 sb.append(' '); 214 } 215 216 if (isAnnotation()) { 217 sb.append('@'); 218 } 219 if (isInterface()) { // Note: all annotation types are interfaces 220 sb.append("interface"); 221 } else { 222 if (isEnum()) 223 sb.append("enum"); 224 else 225 sb.append("class"); 226 } 227 sb.append(' '); 228 sb.append(getName()); 229 } 230 231 TypeVariable<?>[] typeparms = component.getTypeParameters(); 232 if (typeparms.length > 0) { 233 boolean first = true; 234 sb.append('<'); 235 for(TypeVariable<?> typeparm: typeparms) { 236 if (!first) 237 sb.append(','); 238 sb.append(typeparm.getTypeName()); 239 first = false; 240 } 241 sb.append('>'); 242 } 243 244 for (int i = 0; i < arrayDepth; i++) 245 sb.append("[]"); 246 247 return sb.toString(); 248 } 249 } 250 251 /** 252 * Returns the {@code Class} object associated with the class or 253 * interface with the given string name. Invoking this method is 254 * equivalent to: 255 * 256 * <blockquote> 257 * {@code Class.forName(className, true, currentLoader)} 258 * </blockquote> 259 * 260 * where {@code currentLoader} denotes the defining class loader of 261 * the current class. 262 * 263 * <p> For example, the following code fragment returns the 264 * runtime {@code Class} descriptor for the class named 265 * {@code java.lang.Thread}: |