写点什么

java week1 练习

发布于: 2020 年 10 月 21 日

1. 练习2:自定义类加载器

描述: 加载指定的加密后java class文件,进行解密。在通过反射调用指定函数。



附件:

链接:https://pan.baidu.com/s/1Q0oYGrxKObosjbbH8BBQ

提取码:22se

复制这段内容后打开百度网盘手机App,操作更方便哦



package homework.week1;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* 自定义的ClassLoader
*/
public class XClass extends ClassLoader {
@Override
public Class<?> findClass(String name) {
String filePath = XClass.class.getResource("Hello.xlass").toString();
filePath = filePath.replace("file:/", "");
System.out.println("f: " + filePath);
File file = new File(filePath);
InputStream is = null;
try {
is = new FileInputStream(file);
int len = is.available();
byte[] b = new byte[len];
is.read(b, 0, len);
for (int i = 0; i < b.length; ++i) {
b[i] = (byte) (255 - b[i]);
}
return defineClass(name, b, 0, b.length);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}



import homework.week1.XClass;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws IllegalAccessException,
InstantiationException, NoSuchMethodException, InvocationTargetException {
XClass obj = new XClass();
Class<?> helloClass = obj.findClass("Hello");
Method hello = helloClass.getMethod("hello");
Object i = helloClass.newInstance();
hello.invoke(i);
}
}



2. 练习3: jvm内存结构描述



用户头像

还未添加个人签名 2018.11.15 加入

还未添加个人简介

评论

发布
暂无评论
java week1练习