1. Class对象:

    • 我们每创建一个类,经过build都会生成对应的.class文件
    • 该类无法只能由虚拟机创建对象,其构造函数为private
    • 当我们创建某个类的对象,ClassLoader(一个类)就会装载对应.class文件到虚拟机(仅一次)
    • 该Class对象存有函数对应的Constructors(搭配反射使用),Fields(访问私有变量,getFields()只能访问公有,getDeclaredFields()可以访问所有属性),Methods(可以用于动态代理)(以指针的形式)等信息,通常用于反射
    • 可以通过ClassName.class,className.getClass(),Class.forName("limitName")获取
  2. 为了保存class中的字段,构造器和方法,Java有定义了三种不同的类Constructor,Field,Method,同时在类对应的class文件中包含了它们的数组引用

  3. classLoader

    • 作用:加载.Class对象

    • 分类

      • BootstrapClassLoader:核心库加载,如%JRE_HOME%\lib
      • ExtClassLoader:扩展库加载,如%JRE_HOME%\lib\ext
      • AppClassLoader:程序库加载,如当前应用的classpath下的类
      • CustomClassLoader:自定义类加载器

      注:对于开发人员编写的类加载器来说,其父类加载器是加载此类加载器 Java 类的类加载器(正是这一条规定导致了JavaSPI机制需要破坏双亲委派机制)

    • 双亲委派机制:上面四个加载器构成上下级关系,双亲委派机制优先询问上级是否加载过相应的类。如果父类加载器无法完成加载,子类才去加载

          /**
      * Loads the class with the specified <a href="#name">binary name</a>. The
      * default implementation of this method searches for classes in the
      * following order:
      *
      * <ol>
      *
      * <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
      * has already been loaded. </p></li>
      *
      * <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method
      * on the parent class loader. If the parent is <tt>null</tt> the class
      * loader built-in to the virtual machine is used, instead. </p></li>
      *
      * <li><p> Invoke the {@link #findClass(String)} method to find the
      * class. </p></li>
      *
      * </ol>
      *
      * <p> If the class was found using the above steps, and the
      * <tt>resolve</tt> flag is true, this method will then invoke the {@link
      * #resolveClass(Class)} method on the resulting <tt>Class</tt> object.
      *
      * <p> Subclasses of <tt>ClassLoader</tt> are encouraged to override {@link
      * #findClass(String)}, rather than this method. </p>
      *
      * <p> Unless overridden, this method synchronizes on the result of
      * {@link #getClassLoadingLock <tt>getClassLoadingLock</tt>} method
      * during the entire class loading process.
      *
      * @param name
      * The <a href="#name">binary name</a> of the class
      *
      * @param resolve
      * If <tt>true</tt> then resolve the class
      *
      * @return The resulting <tt>Class</tt> object
      *
      * @throws ClassNotFoundException
      * If the class could not be found
      */
      protected Class<?> loadClass(String name, boolean resolve)
      throws ClassNotFoundException
      {
      synchronized (getClassLoadingLock(name)) {
      // First, check if the class has already been loaded
      Class<?> c = findLoadedClass(name);
      if (c == null) {
      long t0 = System.nanoTime();
      try {
      if (parent != null) {
      c = parent.loadClass(name, false);
      } else {
      c = findBootstrapClassOrNull(name);
      }
      } catch (ClassNotFoundException e) {
      // ClassNotFoundException thrown if class not found
      // from the non-null parent class loader
      } if (c == null) {
      // If still not found, then invoke findClass in order
      // to find the class.
      long t1 = System.nanoTime();
      c = findClass(name); // this is the defining class loader; record the stats
      sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
      sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
      sun.misc.PerfCounter.getFindClasses().increment();
      }
      }
      if (resolve) {
      resolveClass(c);
      }
      return c;
      }
      }

      具体流程:

      • 通过findLoadedClass查找是否该类是否已经被加载过(其底层是native的)

      • 如果没加载过,有父类则先尝试调用父类进行加载

      • 如果没加载过,没有父类则直接尝试用BootstrapClassLoader加载

        /**
        * Returns a class loaded by the bootstrap class loader;
        * or return null if not found.
        */
        private Class<?> findBootstrapClassOrNull(String name)
        {
        if (!checkName(name)) return null; return findBootstrapClass(name);
        }
    • 好处:

      • 避免重复加载
      • 安全
  4. 为什么要使用反射:因为在代码运行过程中,我们可能需要知道运行的类的相关信息并对其进行操作

    以动态反射为例,假如我们要给某个类添加日志操作,显然我们可以直接在对应的类中添加相关的打印日志信息的操作,显然这个方法不仅繁琐而且不符合设计模式对于解耦的追求。通过反射,即可将日志动作运行的类分离。

    package org.example;
    
    import org.omg.CORBA.portable.InvokeHandler;
    import redis.clients.jedis.Jedis; import java.lang.reflect.*; /**
    * Hello world!
    *
    */ interface DemoInf{
    public void doWork();
    } class Demo implements DemoInf{
    public void doWork(){
    System.out.println("Do something~");
    }
    } public class App
    {
    public static void main( String[] args ) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
    Demo demo = new Demo(); //这里是唯一创建的实例,而不是接口
    Class<?> proxyClass = Proxy.getProxyClass(DemoInf.class.getClassLoader(), DemoInf.class);
    Constructor<?> constructor = proxyClass.getConstructor(InvocationHandler.class);
    DemoInf DemoInf = (DemoInf)constructor.newInstance(new InvocationHandler() {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println("before methods");
    method.invoke(demo, args); //通过Method类的invoke方法调用,实际的Demo对象
    System.out.println("after methods");
    return null;
    }
    });
    DemoInf.doWork();
    }
    }
  5. 四个元注解

    • @Target:指定注解的使用范围,相应的参数可以通过ElementType配置
    • @Retention:指定注解的保留策略,源码,.Class文件中或是运行时,通过RetentionPolicy类配置
    • @Documented:是否将注解包含在javadoc中
    • @Inherited:可以被子类继承
    • @Repeatable:注解是否可以重复使用,比如某个类上重复添加某个注解
  6. 为什么要使用注解

    ​ 注解可以看做是对类或者他的方法和字段的额外信息,通过注解我们可以在不改变原代码和逻辑的情况下在源代码中嵌入补充信息。如,通过@Test注解实现对测试类的运行(类似Junit)

    • 定义

      package com.example.readfile.annotation;
      
      import java.lang.annotation.*;
      
      @Target(ElementType.METHOD)
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      public @interface Test {
      String value();
      }
    • 使用

      package com.example.readfile.service.impl;
      
      import com.example.readfile.annotation.Test;
      import com.example.readfile.service.List; public class UnorderList implements List { @Test("addContent")
      @Override
      public String addContent(String content) {
      System.out.println("have a test:"+content);
      return null;
      }
      }
    • 读取

      @Test
      void annotationTest() throws InvocationTargetException, IllegalAccessException, InstantiationException {
      Method[] methods = UnorderList.class.getMethods();
      for (Method method : methods) {
      com.example.readfile.annotation.Test annotation = method.getAnnotation(com.example.readfile.annotation.Test.class);
      if (annotation!=null){
      System.out.println(annotation.value());
      method.invoke(UnorderList.class.newInstance(),"annotationTest"); //Java中执行方法,隐式地规定了需要指明是哪个对象执行方法
      }
      }
      }

      注:函数上面那个@Test是模拟的Junit中的@Test

Java的Class类,注解与反射的更多相关文章

  1. Java注解和反射

    1.注解(Annotation) 1.1.什么是注解(Annotation) 注解不是程序本身,可以在程序编译.类加载和运行时被读取,并执行相应的处理.注解的格式为"@注释名(参数值)&qu ...

  2. Java枚举类、注解和反射

    本文主要介绍的是枚举类,注解和反射.还有一些基础知识:static,基本数据类型,运算符优先级放在文中,以便查阅复习. 其中牵扯到泛型的部分,可参考本人的另一篇博客:(Collection, List ...

  3. Java利用自定义注解、反射实现简单BaseDao

    在常见的ORM框架中,大都提供了使用注解方式来实现entity与数据库的映射,这里简单地使用自定义注解与反射来生成可执行的sql语句. 这是整体的目录结构,本来是为复习注解建立的项目^.^ 好的,首先 ...

  4. 【Java】利用注解和反射实现一个"低配版"的依赖注入

    在Spring中,我们可以通过 @Autowired注解的方式为一个方法中注入参数,那么这种方法背后到底发生了什么呢,这篇文章将讲述如何用Java的注解和反射实现一个“低配版”的依赖注入. 下面是我们 ...

  5. java自定义注解与反射

    java注解与反射一.Java中提供了四种元注解,专门负责注解其他的注解,分别如下 1.@Retention元注解,表示需要在什么级别保存该注释信息(生命周期).可选的RetentionPoicy参数 ...

  6. Java基于注解和反射导入导出Excel

    代码地址如下:http://www.demodashi.com/demo/11995.html 1. 构建项目 使用Spring Boot快速构建一个Web工程,并导入与操作Excel相关的POI包以 ...

  7. 使用Java元注解和反射实现简单MVC框架

    Springmvc的核心是DispatcherServlet来进行各种请求的拦截,进而进行后续的各种转发处理.流程图如下: 说明:客户端发出一个http请求给web服务器,web服务器对http请求进 ...

  8. Java基础--注解、反射

    一.注解(Annotation) 1.什么是注解? 从JDK5开始,Java增加了Annotation(注解),Annotation是代码里的特殊标记,这些标记可以在编译.类加载.运行时被读取,并执行 ...

  9. Java Scala获取所有注解的类信息

    要想获取使用指定注解的类信息,可借助工具: org.reflections.Reflections 此工具将Java反射进行了高级封装,Reflections 通过扫描 classpath,索引元数据 ...

  10. Java笔记---枚举类和注解

    Java笔记---枚举类和注解 一.枚举类 自定义枚举类 方式一:JDK5.0之前自定义枚举类 class Seasons { //1. 声明Seasons对象的属性 private final St ...

随机推荐

  1. P10114 [LMXOI Round 1] Size 题解

    题目链接:[LMXOI Round 1] Size 挺有意思的诈骗题,其实这类题都喜欢批一个外壳,例如数据范围提示之类的.记得以前遇到的很多诈骗题,有一道 cf 的高分题,问的是区间出现次数的次数 \ ...

  2. 素数打表,洛谷P1217 [USACO1.5]回文质数 Prime Palindromes

    这道题的最后一个样例TLE(超时)了,判断素数的条件是 i*i<n 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include ...

  3. react 新旧生命周期有什么区别?新增了哪些钩子?废弃了哪些钩子?为什么废弃?

    壹 ❀ 引 在日常面试中,若对于了解react的同学而言,多多少少会被问到生命周期相关的问题,比如大致阐述生命周期的运作流程,以及每个钩子函数大致的作用,而我在两位出去面试的同事那里了解到,他们都遇到 ...

  4. 【Android】使用AIDL实现进程间通讯简单案例

    1 AIDL 简介 ​ AIDL(Android Interface Definition Language)是一种接口定义语言,用于生成可在 Android 设备上两个进程之间进行进程间通信(IPC ...

  5. 通过weblogic发布服务器某个文件夹

    介绍 客户有一台老服务器,上面安装的是weblogic,现在有个需求是需要将服务器下面某个文件夹下的文件都发布出来供某前端直接访问.之前都是直接利用tomcat的webapps目录直接发布即可,搜索了 ...

  6. Kotlin 协程四 —— Flow 和 Channel 的应用

    目录 一. Flow 与 Channel 的相互转换 1.1 Flow 转换为 Channel 1.1.1 ChannelFlow 1.1.2 produceIn -- 将 Flow 转换为单播式 C ...

  7. ASP.NET Core 微信支付(四)【支付结果通知回调(未按照官方步骤) APIV3】

    官方文档 支付通知API 证书和回调报文解密 参考资料 netcore 中没有Request.InputStream 理论实战 对于我来说,这个微信支付结果通知回调有两个难点. 难点一 一开始在想是怎 ...

  8. 对于Celery原理的简单理解

    参考博客: https://www.cnblogs.com/forward-wang/p/5970806.html https://blog.csdn.net/cuomer/article/detai ...

  9. 04、RS232 协议介绍

    从之前的学习,我们知道了 SECS-I 使用的 RS232 来进行数据的传输,那我们也初略的了解下 RS232. 这一篇不用看也可以. 这是最低的协议层.它定义了设备上的物理接口.它是基于RS-232 ...

  10. 【Azure 应用服务】Azure Function App在部署时候遇见 503 ServiceUnavailable

    问题描述 在VS Code中编写好 Azure Function App代码后,通过  func azure functionapp publish 部署失败,抛出 503 Service Unava ...