Skip to content
Open

done #21

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
<artifactId>custom-classloader</artifactId>
<version>1.0-SNAPSHOT</version>


<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down
44 changes: 40 additions & 4 deletions src/main/java/club/shengsheng/MyClassLoader.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
package club.shengsheng;


/**
* @author [email protected]
**/
import java.io.File;
import java.nio.file.Files;

public class MyClassLoader extends ClassLoader {
@Override
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) {
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 rootFile = System.getProperty("user.dir").concat(".加密".replace('.', File.separatorChar)).concat(".class");
File classFile = new File(rootFile);
byte[] classBytes;
try {
classBytes = Files.readAllBytes(classFile.toPath());
for (int i = 0; i < classBytes.length; i++) {
classBytes[i] -= 1;
}

} catch (Exception e) {
throw new ClassNotFoundException();
}
return defineClass(name, classBytes, 0, classBytes.length);

}
}
}
Loading