写点什么

我看 JAVA 之 Class

用户头像
awen
关注
发布于: 2021 年 02 月 27 日

我看 JAVA 之 Class


注:基于 jdk11


Class


类 Class 的实例代表运行时java应用程序中的类和接口。枚举类型是一种类,注解类是一种接口。数组是共享同一种类的。基本类型(boolean、byte、char、short、int、long、float和double)和关键字void也表示为类对象。Class 没有公开的构造方法,只能由JVM创建。 当类加载器调用defineClass方法并传递类文件字节给jvm,jvm会自动构建Class 对象。
复制代码


实现了如下接口


1. java.io.Serializable2. GenericDeclaration  GenericDeclaration 所有声明泛型变量的实体要实现此接口(注:后续会在“我看JAVA 之 泛型“阐述)  唯一的方法:  public TypeVariable<?>[] getTypeParameters();//获取当前“实体”上声明的“泛型变量"。3. Type   Type是java语言中所有类型的公共高级接口,它们包括原始类型、参数化类型、数组类型、泛型变量和基本类型。  默认方法:  default String getTypeName() {      return toString();  }
4. AnnotatedElement java.lang.reflect.AnnotatedElement 接口定义了获取在当前JVM中运行程序的注解元素。这个接口允许通过反射的方式获取注解。通过当前接口方法获取到的注解都是不可变且序列化的。
复制代码


几个重点方法


  1. forName()


      public static Class<?> forName(String className)                  throws ClassNotFoundException {          Class<?> caller = Reflection.getCallerClass();          return forName0(className, true, ClassLoader.getClassLoader(caller), caller);      }
@CallerSensitive public static Class<?> forName(String name, boolean initialize, ClassLoader loader) throws ClassNotFoundException { Class<?> caller = null; SecurityManager sm = System.getSecurityManager(); if (sm != null) { // Reflective call to get caller class is only needed if a security manager // is present. Avoid the overhead of making this call otherwise. caller = Reflection.getCallerClass(); if (loader == null) { ClassLoader ccl = ClassLoader.getClassLoader(caller); if (ccl != null) { sm.checkPermission( SecurityConstants.GET_CLASSLOADER_PERMISSION); } } } return forName0(name, initialize, loader, caller); }
/** Called after security check for system loader access checks have been made. */ private static native Class<?> forName0(String name, boolean initialize, ClassLoader loader, Class<?> caller) throws ClassNotFoundException;
复制代码


几个获取 Class 对象引用方式


1. Integer.class2. new Integer(1).getClass()3. Class.forName("java.lang.Integer")
复制代码


代码示例


    package chapter03;
import java.lang.annotation.ElementType; import java.lang.reflect.InvocationTargetException;
public class TestClass {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
System.out.println("--- 3 mode get obj's Class ---"); System.out.println(Integer.class); System.out.println(Integer.valueOf(1).getClass()); System.out.println(Class.forName("java.lang.Integer")); System.out.println(int.class); System.out.println("int is privitive type or not ? " + int.class.isPrimitive());
// array System.out.println("--- array' Class ---"); System.out.println(new Integer[]{1, 3, 5}.getClass()); System.out.println(new Object[]{1, 3, 5}.getClass()); System.out.println(new Integer[][]{{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}.getClass());
// void package System.out.println("--- void & package' Class ---"); System.out.println(Void.class); System.out.println(void.class); System.out.println(Package.class);
// enum System.out.println("--- enum' Class ---"); System.out.println(ElementType.class); System.out.println("ElementType is enum or not ? " + ElementType.class.isEnum());
// annotation System.out.println("--- annotation' Class ---"); System.out.println(Deprecated.class); System.out.println("Deprecated is interface or not ? " + Deprecated.class.isInterface()); System.out.println("Deprecated is annotation or not ? " + Deprecated.class.isAnnotation());
// class' Class System.out.println("--- class' Class ---"); System.out.println(Class.forName("java.lang.Integer").getClass());
// module System.out.println(Class.forName("chapter03.Test").getModule());
//new instance System.out.println(Class.forName("chapter03.Test").getDeclaredConstructor()); Class.forName("chapter03.Test").getDeclaredConstructor().newInstance(); Class.forName("chapter03.Test").newInstance();
} }
class Test { private Test() {
} }
复制代码

打印结果:

--- 3 mode get obj's Class ---

class java.lang.Integer

class java.lang.Integer

class java.lang.Integer

int

int is privitive type or not ? true

--- array' Class ---

class [Ljava.lang.Integer;

class [Ljava.lang.Object;

class [[Ljava.lang.Integer;

--- void & package' Class ---

class java.lang.Void

void

class java.lang.Package

--- enum' Class ---

class java.lang.annotation.ElementType

ElementType is enum or not ? true

--- annotation' Class ---

interface java.lang.Deprecated

Deprecated is interface or not ? true

Deprecated is annotation or not ? true

--- class' Class ---

class java.lang.Class

unnamed module @67b64c45

private chapter03.Test()

Exception in thread "main" java.lang.IllegalAccessException: class chapter03.TestClass cannot access a member of class chapter03.Test with modifiers "private"

at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)

at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591)

at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)

at chapter03.TestClass.main(TestClass.java:50)


用户头像

awen

关注

Things happen for a reason. 2019.11.15 加入

还未添加个人简介

评论

发布
暂无评论
我看JAVA 之 Class