微信
支付宝
# Java 类反射 ## Java反射 - Java类反射 我们可以使用Java反射来获取关于类的信息,例如作为其包名称,其访问修饰符等。 要获得简单的类名,请使用\` Class \`中的\` getSimpleName()\`方法。 \`\`\` String simpleName = c.getSimpleName(); \`\`\` 类的修饰符是关键字之前的关键字类在类声明中,如\` abstract \`,\` public \`。 \`Class \`中的\` getModifiers()\`方法返回类的所有修饰符。 \`getModifiers()\`方法返回一个整数。我们必须调用\` java.lang.reflect.Modifier.toString(int modifiers)\`以获得修饰符的文本形式。 要获取超类的名称,请使用\` Class \`中的\` getSuperclass()\`方法。 如果对Object类调用getSuperclass()方法,它将返回null,因为它没有超类。 要获取类实现的所有接口的名称,请使用\` getInterfaces()\`。 \`\`\` Class\[\] interfaces = c.getInterfaces(); \`\`\` ## 例子 \`\`\` import java.io.Serializable; import java.lang.reflect.Modifier; import java.lang.reflect.TypeVariable; class MyClass implements Cloneable, Serializable { private int id = -1; private String name = "Unknown"; public MyClass(int id, String name) { this.id = id; this.name = name; } public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { throw new RuntimeException(e.getMessage()); } } public String toString() { return "MyClass: id=" + this.id + ", name=" + this.name; } } public class Main { public static void main(String\[\] args) { // Print the class declaration for the Class class String classDesciption = getClassDescription(MyClass.class); System.out.println(classDesciption); } public static String getClassDescription(Class c) { StringBuilder classDesc = new StringBuilder(); int modifierBits = 0; String keyword = ""; if (c.isInterface()) { modifierBits = c.getModifiers() \& Modifier.interfaceModifiers(); if (c.isAnnotation()) { keyword = "@interface"; } else { keyword = "interface"; } } else if (c.isEnum()) { modifierBits = c.getModifiers() \& Modifier.classModifiers(); keyword = "enum"; } modifierBits = c.getModifiers() \& Modifier.classModifiers(); keyword = "class"; String modifiers = Modifier.toString(modifierBits); classDesc.append(modifiers); classDesc.append(" " + keyword); String simpleName = c.getSimpleName(); classDesc.append(" " + simpleName); String genericParms = getGenericTypeParams(c); classDesc.append(genericParms); Class superClass = c.getSuperclass(); if (superClass != null) { String superClassSimpleName = superClass.getSimpleName(); classDesc.append(" extends " + superClassSimpleName); } String interfaces = Main.getClassInterfaces(c); if (interfaces != null) { classDesc.append(" implements " + interfaces); } return classDesc.toString(); } public static String getClassInterfaces(Class c) { Class\[\] interfaces = c.getInterfaces(); String interfacesList = null; if (interfaces.length \> 0) { String\[\] interfaceNames = new String\[interfaces.length\]; for (int i = 0; i \< interfaces.length; i++) { interfaceNames\[i\] = interfaces\[i\].getSimpleName(); } interfacesList = String.join(", ", interfaceNames); } return interfacesList; } public static String getGenericTypeParams(Class c) { StringBuilder sb = new StringBuilder(); TypeVariable\[\] typeParms = c.getTypeParameters(); if (typeParms.length \> 0) { String\[\] paramNames = new String\[typeParms.length\]; for (int i = 0; i \< typeParms.length; i++) { paramNames\[i\] = typeParms\[i\].getTypeName(); } sb.append("\<"); String parmsList = String.join(",", paramNames); sb.append(parmsList); sb.append("\>"); } return sb.toString(); } } \`\`\` 上面的代码生成以下结果。 \`\`\` class MyClass extends Object implements Cloneable, Serializable \`\`\` 上面的代码生成以下结果。 !\[img\](https://atts.w3cschool.cn/attachments/jimg/java_reflection/EXAMPLE__4D27D52431E4B4B8282A.png)
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Veylor
最近发布
常用SQL