From edf0da0781adbb16f0b00b8f212195d3ab70a59c Mon Sep 17 00:00:00 2001 From: Xuankun Yu <102799833+blackman788@users.noreply.github.com> Date: Tue, 3 Jun 2025 21:04:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/club/shengsheng/MyClassLoader.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/main/java/club/shengsheng/MyClassLoader.java b/src/main/java/club/shengsheng/MyClassLoader.java index 21026cf..9c3679d 100644 --- a/src/main/java/club/shengsheng/MyClassLoader.java +++ b/src/main/java/club/shengsheng/MyClassLoader.java @@ -1,9 +1,75 @@ package club.shengsheng; +import java.io.File; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Paths; + /** * @author gongxuanzhangmelt@gmail.com **/ public class MyClassLoader extends ClassLoader { + @Override + public 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) { + if (name.startsWith("tech")){ //加载我们自己的包名 + c = findClass(name); + }else{ //如果是其他包名,用父类加载器去加载 + c = getParent().loadClass(name); + } + } + if (resolve) { + resolveClass(c); + } + return c; + } + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + String absolutePath = Paths.get("").toAbsolutePath().toString(); + String filePath = absolutePath + "\\加密.class"; + System.out.println("filePath = " + filePath); + File file = new File(filePath); + ByteBuffer byteBuffer = handlerClass(file); + + return defineClass(name, byteBuffer.array(), 0, byteBuffer.limit()); + } + + private static ByteBuffer handlerClass(File file) { + ByteBuffer buffer = ByteBuffer.allocate((int) file.length()); + System.out.println("file.length(): " + file.length()); + try(FileChannel channel = new RandomAccessFile(file,"r").getChannel()){ + //准备缓冲区 + ByteBuffer tempBuff = ByteBuffer.allocate(20); //10字节 + while(true){ + //从 channel 中读取数据, 向 buffer 中写入 + int len = channel.read(tempBuff); + if (len == -1){ + break; //没有数据了 + } + //切换到读模式 + tempBuff.flip(); + //打印 buffer 中的内容 + while(tempBuff.hasRemaining()){ + byte b = tempBuff.get(); //一个字节一个字节取 + b -= (byte)1; + System.out.print(String.format("0x%02X ", b)); + buffer.put(b); + } + System.out.println(); + tempBuff.clear(); //切换为写模式 + } + } catch (Exception e) { + e.printStackTrace(); + } + return buffer; + } + } From 15adffe56d08204c8f07f33711901030c7e2421f Mon Sep 17 00:00:00 2001 From: Xuankun Yu <102799833+blackman788@users.noreply.github.com> Date: Wed, 4 Jun 2025 00:17:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A7=84=E8=8C=83=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E5=88=86=E9=9A=94=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/club/shengsheng/MyClassLoader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/club/shengsheng/MyClassLoader.java b/src/main/java/club/shengsheng/MyClassLoader.java index 9c3679d..3581aa8 100644 --- a/src/main/java/club/shengsheng/MyClassLoader.java +++ b/src/main/java/club/shengsheng/MyClassLoader.java @@ -34,7 +34,7 @@ public Class loadClass(String name,boolean resolve) throws ClassNotFoundExcep @Override protected Class findClass(String name) throws ClassNotFoundException { String absolutePath = Paths.get("").toAbsolutePath().toString(); - String filePath = absolutePath + "\\加密.class"; + String filePath = absolutePath + File.separator +"加密.class"; System.out.println("filePath = " + filePath); File file = new File(filePath); ByteBuffer byteBuffer = handlerClass(file);