package study.javaenhance;
import java.util.ArrayList;
public class ClassLoaderTest
{
public static void main(String[] args) throws Exception
{
//获取类加载器,那么这个获取的是一个实例对象,我们知道类加载器也有很多种,那么因此也有其对应的类存在,因此可以获取到对应的字节码
System.out.println(ClassLoaderTest.class.getClassLoader());
//获取类加载的字节码,然后获取到类加载字节码的名字
System.out.println(ClassLoaderTest.class.getClassLoader().getClass().getName());
//下面我们看下获取非我们定义的类,比如System ArrayList 等常用类
System.out.println(System.class.getClassLoader());
System.out.println(ArrayList.class.getClassLoader());
}
}
package study.javaenhance;
import java.util.ArrayList;
public class ClassLoaderTest
{
public static void main(String[] args) throws Exception
{
//获取类加载器,那么这个获取的是一个实例对象,我们知道类加载器也有很多种,那么因此也有其对应的类存在,因此可以获取到对应的字节码
System.out.println(ClassLoaderTest.class.getClassLoader());
//获取类加载的字节码,然后获取到类加载字节码的名字
System.out.println(ClassLoaderTest.class.getClassLoader().getClass().getName());
//下面我们看下获取非我们定义的类,比如System ArrayList 等常用类
System.out.println(System.class.getClassLoader());
System.out.println(ArrayList.class.getClassLoader());
//演示java 提供的类加载器关系
ClassLoader classloader = ClassLoaderTest.class.getClassLoader();
while(classloader != null)
{
System.out.print(classloader.getClass().getName()+"-->");
classloader = classloader.getParent();
}
System.out.println(classloader);
}
}
public Class<?> loadClass(String name) throws ClassNotFoundException {
return loadClass(name, false);
}
protected Class<?> loadClass(String name, boolean resolve)throws ClassNotFoundException{
//加上锁,同步处理,因为可能是多线程在加载类
synchronized (getClassLoadingLock(name)) {
//检查,是否该类已经加载过了,如果加载过了,就不加载了
Class c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
//如果自定义的类加载器的parent不为null,就调用parent的loadClass进行加载类
if (parent != null) {
c = parent.loadClass(name, false);
} else {
//如果自定义的类加载器的parent为null,就调用findBootstrapClass方法查找类,就是Bootstrap类加载器
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();
//如果parent加载类失败,就调用自己的findClass方法进行类加载
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;
}
}
private Class findBootstrapClassOrNull(String name)
{
if (!checkName(name)) return null;
return findBootstrapClass(name);
}
package study.javaenhance;
public class ClassLoaderAttachment {
@Override
public String toString() {
return "Hello ClassLoader!";
}
}
package study.javaenhance;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class MyClassLoader extends ClassLoader
{
//需要加载类.class文件的目录
private String classDir;
//无参的构造方法,用于class.newInstance()构造对象使用
public MyClassLoader(){
}
public MyClassLoader(String classDir){
this.classDir = classDir;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
System.out.println(name);
String classPathFile = classDir + "\\" + name.substring(name.lastIndexOf(".")+1) + ".class";
System.out.println(classPathFile);
try
{
System.out.println("my");
//将class文件进行解密
FileInputStream fis = new FileInputStream(classPathFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
encodeAndDecode(fis,bos);
byte[] classByte = bos.toByteArray();
//将字节流变成一个class
return defineClass(classByte,0,classByte.length);
} catch (Exception e)
{
e.printStackTrace();
}
return super.findClass(name);
}
//测试,先将ClassLoaderAttachment.class文件加密写到工程的class_temp目录下
public static void main(String[] args) throws Exception{
//配置运行参数
String srcPath = args[0];//ClassLoaderAttachment.class原路径
String desPath = args[1];//ClassLoaderAttachment.class输出的路径
String desFileName = srcPath.substring(srcPath.lastIndexOf("\\")+1);
String desPathFile = desPath + "/" + desFileName;
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(desPathFile);
//将class进行加密
encodeAndDecode(fis,fos);
fis.close();
fos.close();
}
/**
* 加密和解密算法
* @param is
* @param os
* @throws Exception
*/
private static void encodeAndDecode(InputStream is,OutputStream os) throws Exception{
int bytes = -1;
while((bytes = is.read())!= -1){
bytes = bytes ^ 0xff;//和0xff进行异或处理
os.write(bytes);
}
}
}
package study.javaenhance;
import java.util.ArrayList;
public class ClassLoaderTest
{
public static void main(String[] args) throws Exception
{
//获取类加载器,那么这个获取的是一个实例对象,我们知道类加载器也有很多种,那么因此也有其对应的类存在,因此可以获取到对应的字节码
System.out.println(ClassLoaderTest.class.getClassLoader());
//获取类加载的字节码,然后获取到类加载字节码的名字
System.out.println(ClassLoaderTest.class.getClassLoader().getClass().getName());
//下面我们看下获取非我们定义的类,比如System ArrayList 等常用类
System.out.println(System.class.getClassLoader());
System.out.println(ArrayList.class.getClassLoader());
//演示java 提供的类加载器关系
ClassLoader classloader = ClassLoaderTest.class.getClassLoader();
while(classloader != null)
{
System.out.print(classloader.getClass().getName()+"-->");
classloader = classloader.getParent();
}
System.out.println(classloader);
try {
//Class classDate = new MyClassLoader("class_temp").loadClass("ClassLoaderAttachment");
Class classDate = new MyClassLoader("class_temp").loadClass("study.javaenhance.ClassLoaderAttachment");
Object object = classDate.newInstance();
//输出ClassLoaderAttachment类的加载器名称
System.out.println("ClassLoader:"+object.getClass().getClassLoader().getClass().getName());
System.out.println(object);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有