600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > loadClass findClass defineClass

loadClass findClass defineClass

时间:2024-02-24 14:30:04

相关推荐

loadClass findClass defineClass

loadclass:判断是否已加载,使用双亲委派模型,请求父加载器,都为空,使用findclass

findclass:根据名称或位置加载.class字节码,然后使用defineClass

defineclass:解析定义.class字节流,返回class对象

loadclass

protected Class<?> loadClass(String name, boolean resolve)throws ClassNotFoundException{synchronized (getClassLoadingLock(name)) {// First, check if the class has already been loadedClass<?> c = findLoadedClass(name);if (c == null) {long t0 = System.nanoTime();try {if (parent != null) {c = parent.loadClass(name, false);} else {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();c = findClass(name);// this is the defining class loader; record the statssun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);sun.misc.PerfCounter.getFindClasses().increment();}}if (resolve) {resolveClass(c);}return c;}}

findclass

需要被子类重写

protected Class<?> findClass(String name) throws ClassNotFoundException {throw new ClassNotFoundException(name);}

自定义实现findclass

引用/FlyAway/p/5640586.html

protected Class<?> findClass(String name) throws ClassNotFoundException {// return super.findClass(name);System.out.println( "try findClass " + name);InputStream is = null;Class class1 = null;try {String classPath = name.replace(".", "\\") + ".class";// String[] fqnArr = name.split("\\."); // split("."); 是不行的, 必须split("\\.")// if (fqnArr == null || fqnArr.length == 0) {//System.out.println("ClassLoaderLK.findClass()");//fqnArr = name.split("\\.");// } else {//System.out.println( name + fqnArr.length);// }String classFile = path + classPath;byte[] data = getClassFileBytes(classFile );class1 = defineClass(name, data, 0, data.length);if (class1 == null) {System.out.println("ClassLoaderLK.findClass() ERR ");throw new ClassFormatError();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}}return class1;}private byte[] getClassFileBytes(String classFile) throws Exception {FileInputStream fis = new FileInputStream(classFile );FileChannel fileC = fis.getChannel();ByteArrayOutputStream baos = new ByteArrayOutputStream();WritableByteChannel outC = Channels.newChannel(baos);ByteBuffer buffer = ByteBuffer.allocateDirect(1024);while (true) {int i = fileC.read(buffer);if (i == 0 || i == -1) {break;}buffer.flip();outC.write(buffer);buffer.clear();}fis.close();return baos.toByteArray();}}

definclass

protected final Class<?> defineClass(String name, byte[] b, int off, int len,ProtectionDomain protectionDomain)throws ClassFormatError{protectionDomain = preDefineClass(name, protectionDomain);String source = defineClassSourceLocation(protectionDomain);Class<?> c = defineClass1(name, b, off, len, protectionDomain, source);postDefineClass(c, protectionDomain);return c;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。
扩展阅读