Java类类getClassLoader()方法及示例

类的类getClassLoader()方法 (Class class getClassLoader() method)

  • getClassLoader() method is available in java.lang package.

    getClassLoader()方法在java.lang包中可用。

  • getClassLoader() method is used to return the ClassLoader that loads the class or interface.

    getClassLoader()方法用于返回加载类或接口的ClassLoader。

  • getClassLoader() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    getClassLoader()方法是一个非静态方法,只能通过类对象进行访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • getClassLoader() method may throw SecurityException at the time of loading class or interface.

    在加载类或接口时, getClassLoader()方法可能会引发SecurityException

    SecurityException: In this exception, its checkPermission() method does not allow access classloader for the class when the security manager exists.

    SecurityException :在此异常中,当安全管理器存在时,其checkPermission()方法不允许访问该类的类加载器。

Syntax:

句法:

    public ClassLoader getClassLoader();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of this method is ClassLoader, it returns the following values based on the given cases,

此方法的返回类型为ClassLoader ,它根据给定的情况返回以下值:

  • It returns class loader that loads the class denoted by this object.

    它返回类加载器,该加载器加载此对象表示的类。

  • It returns null in case of bootstrap class loader because we don't need to implement bootstrap class loader.

    如果使用自举类加载器,则返回null ,因为我们无需实现自举类加载器。

Example:

例:

// Java program to demonstrate the example 
// of ClassLoader getClassLoader() method of Class 

public class GetClassLoaderOfClass {
 public static void main(String[] args) throws Exception {

  // It returns the Class object attached with the given 
  //classname
  Class cl = Class.forName("GetClassLoaderOfClass");

  // By using getClassLoader() is to load the class
  ClassLoader class_load = cl.getClassLoader();

  // If any ClassLoader associate with the Class
  if (class_load != null) {

   Class load_class = class_load.getClass();
   System.out.print("Associated Loader Class: ");
   System.out.print(load_class.getName());
  }
  // No Loader associated with the class
  else
   System.out.println("No system loader associated with the class");
 }
}

Output

输出量

Associated Loader Class: jdk.internal.loader.ClassLoaders$AppClassLoader


翻译自: https://www.includehelp.com/java/class-class-getclassloader-method-with-example.aspx

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
动态创建类和动态代理类是Java中非常重要的概念,下面分别进行介绍: 1. 动态创建类 Java中可以使用反射机制动态创建类,具体步骤如下: - 创建一个ClassWriter对象,用于生成类的二进制字节码; - 调用ClassWriter的visit方法,生成类的头部信息; - 调用visitField方法,生成类的成员变量; - 调用visitMethod方法,生成类的方法; - 调用visitEnd方法,生成类的尾部信息; - 调用ClassLoader的defineClass方法,将生成的字节码转换为Class对象。 下面是一个简单的示例代码: ```java import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; public class DynamicClassCreator { public static void main(String[] args) throws Exception { // 创建ClassWriter对象 ClassWriter cw = new ClassWriter(0); // 生成类的头部信息 cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "DynamicClass", null, "java/lang/Object", null); // 生成类的成员变量 cw.visitField(Opcodes.ACC_PRIVATE, "name", "Ljava/lang/String;", null, null).visitEnd(); // 生成类的构造方法 MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); // 生成类的方法 mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "sayHello", "()V", null, null); mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("Hello, world!"); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(2, 1); mv.visitEnd(); // 生成类的尾部信息 cw.visitEnd(); // 将生成的字节码转换为Class对象 byte[] code = cw.toByteArray(); Class<?> clazz = new MyClassLoader().defineClass("DynamicClass", code); // 创建对象并调用方法 Object obj = clazz.newInstance(); clazz.getMethod("sayHello").invoke(obj); } } class MyClassLoader extends ClassLoader { public Class<?> defineClass(String name, byte[] code) { return defineClass(name, code, 0, code.length); } } ``` 2. 动态代理类 Java中可以使用动态代理机制生成代理类,代理类可以在不改变原有代码的情况下增加一些额外的功能,具体步骤如下: - 创建一个InvocationHandler对象,用于处理代理类的方法调用; - 调用Proxy的newProxyInstance方法,生成代理类的实例。 下面是一个简单的示例代码: ```java import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class DynamicProxy { public static void main(String[] args) { // 创建InvocationHandler对象 InvocationHandler handler = new MyInvocationHandler(); // 创建代理类的实例 MyInterface proxy = (MyInterface) Proxy.newProxyInstance(MyInterface.class.getClassLoader(), new Class<?>[] { MyInterface.class }, handler); // 调用代理类的方法 proxy.sayHello(); } } interface MyInterface { void sayHello(); } class MyInvocationHandler implements InvocationHandler { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("Before method " + method.getName()); Object result = method.invoke(new MyClass(), args); System.out.println("After method " + method.getName()); return result; } } class MyClass implements MyInterface { public void sayHello() { System.out.println("Hello, world!"); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值