-
-
[求助]Java项目中的ClassLoader方法的作用?
-
发表于: 2017-11-4 02:25 2142
-
想请教下Java大神,下面这段代码都做了些什么?能给解释下吗?
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class FineClassLoader extends ClassLoader { private Map<String, Class> loadedClassPool = new ConcurrentHashMap(); public Class<?> findClass(String paramString) { byte[] arrayOfByte = loadClassData(paramString); return defineClass(paramString, arrayOfByte, 0, arrayOfByte.length); } public synchronized Class<?> loadClass(String paramString, boolean paramBoolean) throws ClassNotFoundException { Class localClass = null; if (this.loadedClassPool.containsKey(paramString)) { localClass = (Class)this.loadedClassPool.get(paramString); } else { if (!paramString.startsWith("com.fr")) try { localClass = super.loadClass(paramString, false); } catch (ClassNotFoundException localClassNotFoundException) { localClassNotFoundException.printStackTrace(); } try { if (localClass == null) localClass = loadByFineClassLoader(paramString); } catch (Exception localException) { localException.printStackTrace(); } if (localClass != null) this.loadedClassPool.put(paramString, localClass); } if (paramBoolean) resolveClass(localClass); return localClass; } private Class loadByFineClassLoader(String paramString) { Class localClass = null; try { byte[] arrayOfByte = loadClassData(paramString); if (arrayOfByte != null) localClass = defineClass(paramString, arrayOfByte, 0, arrayOfByte.length); } catch (Exception localException) { localClass = null; } return localClass; } private byte[] loadClassData(String paramString) { String str = "/" + paramString.replace('.', '/') + ".class"; InputStream localInputStream = FineClassLoader.class.getResourceAsStream(str); if (localInputStream == null) return null; try { return readFully(localInputStream); } catch (IOException localIOException) { localIOException.printStackTrace(); } return null; } private static byte[] readFully(InputStream paramInputStream) throws IOException { ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream(); byte[] arrayOfByte = new byte[8192]; int i; while ((i = paramInputStream.read(arrayOfByte)) > 0) localByteArrayOutputStream.write(arrayOfByte, 0, i); return localByteArrayOutputStream.toByteArray(); } }
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!
赞赏
他的文章
看原图
赞赏
雪币:
留言: