classloader

自动生成getter setter

血红的双手。 提交于 2020-04-07 10:35:11
如何使用java黑魔法给一个entity生成getter,setter方法? 由于java是一门静态语言,要给一个类动态添加方法,看似是不可能的。但牛B的程序员会让任何事情发生。我只知道有两种方式可以做到,一种是字节码加强,借助asm包;另一种是运行期加强,借助javassist包。下面,尝试用第二种方法,完成一个简单的demo。 大致思路如下 :先在Filter类里 扫描工程得到所有实体类,通过 创建自定义类加载器加载实体类 ,在加载的过程中通过javassist给每个实体类添加getter setter。 为什么要自定义类加载器呢? 我们知道,所有的类都是通过类加载器 加载到内存中,类加载器的加载通过它父加载器完成。java类加载器包括: 1、启动加载器 (Bootstrap ClassLoader) ,祖宗辈的,由c++语言实现,是jvm一部分, 负责加载JAVA_HOME\lib目录中并且能被虚拟机识别的类库 。 2、 扩展类加载器(Extension ClassLoader),爷爷辈的, 负责加载JAVA_HOME\lib\ext目录中的类库 。 3、 应用程序类加载器(Application ClassLoader),dady辈的, 负责加载用户类路径(Classpath)上所指定的类库, 如果应用程序中没有自定义过自己的类加载器,一般情况下这个就是程序中默认的类加载器

类加载器ClassLoader-2

南楼画角 提交于 2020-04-07 04:13:27
类装载器 大纲: n class装载验证流程 n 什么是类装载器ClassLoader n JDK中ClassLoader默认设计模式 n 打破常规模式 n 热替换 class装载验证流程: n 加载 n 链接 – 验证 – 准备 – 解析 n 初始化 加载 n 装载类的第一个阶段 n 取得类的二进制流 [z1] n 转为方法区数据结构 n 在 Java 堆中生成对应的 java.lang.Class 对象 链接 验证 n 链接 -> 验证 – 目的: 保证 Class 流的格式是正确的 文件格式的验证 n 是否以 0xCAFEBABE 开头 n 版本号是否合理 元数据验证 n 是否有父类 n 继承了 final 类? n 非抽象类实现了所有的抽象方法 1, 什么是元数据: 举例: 任何文件系统中的数据分为数据和元数据。数据是指普通文件中的实际数据 ,而 元数据指用来描述一个文件的特征的系统数据 , 诸如访问权限、文件拥有者以及文件数据块的分布信息 (inode...) 等等。 在集群文件系统中,分布信息包括文件在磁盘上的位置以及磁盘在集群中的位置。 用户需要操作一个文件必须首先得到它的元数据 , 才能定位到文件的位置并且得到文件的内容或相关属性。 2, 元数据最大的好处是 , 它使信息的描述和分类可以实现格式化 , 从而为机器处理创造了可能。 3, 元数据概念:

Dynamically compile java code which has dependencies on classes loaded by specific classloader

泪湿孤枕 提交于 2020-03-20 06:36:47
问题 We have an ability to compile java code dynamically on the fly. I know at least Java-Runtime-Compiler and InMemoryJavaCompiler But seems they cannot compile class which depends on some class from certain classloader. Is it possible to dynamically compile java code which depends on classes available only in specific classloader? Let's say: ClassLoader classloader = ... // only this CL can load class 'com.External' String source = "public class MyClass extends com.External {}"; Class<?>

Java class loader bug: Caused by: java.io.IOException: Stream closed

≡放荡痞女 提交于 2020-03-19 08:07:53
问题 I'm getting a strange bug regarding I believe class loader issues when I deploy my webapp to Tomcat. The bug doesn't appear when I run my webapp locally using Jetty. It seems like my input streams for my .yml resource files are being closed for some reason when they shouldn't be. This bug first appeared when I tried to convert my single module project into a multi module project. Before that, it was working fine on Tomcat using the exact same code: Caused by: org.yaml.snakeyaml.error

java安全沙箱(一)之ClassLoader双亲委派机制

余生长醉 提交于 2020-03-02 16:50:38
java是一种类型安全的语言,它有四类称为安全沙箱机制的安全机制来保证语言的安全性,这四类安全沙箱分别是: ‍ ‍ 类加载体系 ‍ ‍ .class文件检验器 内置于Java虚拟机(及语言)的安全特性 安全管理器及Java API 本篇博客主要介绍 “类加载体系”的基本原理; 如需了解其它几类安全机制可以通过上面的 博客链接进入查看。 简介 “类加载体系”及ClassLoader双亲委派机制。java程序中的 .java文件编译完会生成 .class文件,而 .class文件就是通过被称为类加载器的ClassLoader加载的,而ClassLoder在加载过程中会使用“双亲委派机制”来加载 .class文件,先上图: 看着图从上往下介绍: BootStrapClassLoader:启动类加载器,该 ClassLoader是jvm在启动时创建的,用于加载 $JAVA_HOME/jre/lib下面的类库(或者通过参数 -Xbootclasspath指定 )。由于引导类加载器涉及到虚拟机本地实现细节,开发者无法直接获取到启动类加载器的引用,所以不能直接通过引用进行操作。 ExtClassLoader:扩展类加载器, 该 ClassLoader是在sun.misc.Launcher 里 作为一个内部类 ExtClassLoader 定义的(即 sun.misc.Launcher

JarClassLoader - loading jars dynamically - how to?

梦想与她 提交于 2020-02-04 06:00:47
问题 I have seen many class loader questions, but still was not able to figure why, the error here. I am writing a program which uses 2 versions of jars. One is needed to get content from older storage, and another to store content in new storage. Since, I need either of the jar to be loaded at a time, I used JarClassLoader to create a proxy for adding one jar and loading its classes. But I face ClassNotFoundException. public class HbaseMigrator implements Runnable { public void run() {

JarClassLoader - loading jars dynamically - how to?

不羁的心 提交于 2020-02-04 06:00:40
问题 I have seen many class loader questions, but still was not able to figure why, the error here. I am writing a program which uses 2 versions of jars. One is needed to get content from older storage, and another to store content in new storage. Since, I need either of the jar to be loaded at a time, I used JarClassLoader to create a proxy for adding one jar and loading its classes. But I face ClassNotFoundException. public class HbaseMigrator implements Runnable { public void run() {

JarClassLoader - loading jars dynamically - how to?

邮差的信 提交于 2020-02-04 06:00:26
问题 I have seen many class loader questions, but still was not able to figure why, the error here. I am writing a program which uses 2 versions of jars. One is needed to get content from older storage, and another to store content in new storage. Since, I need either of the jar to be loaded at a time, I used JarClassLoader to create a proxy for adding one jar and loading its classes. But I face ClassNotFoundException. public class HbaseMigrator implements Runnable { public void run() {

Executing a class file sent over the network in java

白昼怎懂夜的黑 提交于 2020-02-02 13:02:47
问题 I am trying to send a class file to offload "work" from a client to a server. I send a class file "MyClass.class" and receive it as "MyFooClass.class" successfully. I need to execute the main() of MyFooClass at the server side and return the result. I am trying to load the MyFooClass with a classloader but get the ClassNotFoundException. Kindly help. Aditya 回答1: Unless you are doing RMI, the 'server' side won't have the class in its classpath and therefore fail with a ClassNotFoundException.

Executing a class file sent over the network in java

五迷三道 提交于 2020-02-02 13:01:08
问题 I am trying to send a class file to offload "work" from a client to a server. I send a class file "MyClass.class" and receive it as "MyFooClass.class" successfully. I need to execute the main() of MyFooClass at the server side and return the result. I am trying to load the MyFooClass with a classloader but get the ClassNotFoundException. Kindly help. Aditya 回答1: Unless you are doing RMI, the 'server' side won't have the class in its classpath and therefore fail with a ClassNotFoundException.