转:http://tiantian911.iteye.com/blog/273164
关键的classloader类:
- package com.hitachi.classloader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.Hashtable;
- import java.util.jar.JarEntry;
- import java.util.jar.JarInputStream;
- public class MyClassLoader extends ClassLoader {
- private static String myClasspath = new String("");
- private static Hashtable<String, Class<?>> loadClassHashTable = new Hashtable<String, Class<?>>();
- private static Hashtable<String, Long> loadClassTime = new Hashtable<String, Long>();
- public MyClassLoader() {
- }
- /** */
- /**
- * create a classloader and specify a classpath.
- *
- * @param myClasspath
- * the specified classpath name.
- */
- public MyClassLoader(String myClasspath) {
- if (!myClasspath.endsWith("\\")) {
- myClasspath = myClasspath + "\\";
- }
- MyClassLoader.myClasspath = myClasspath;
- }
- /** */
- /**
- * set the classpath
- *
- * @param myClasspath
- * the specified classpath name
- */
- public void SetmyClasspath(String myClasspath) {
- if (!myClasspath.endsWith("\\")) {
- myClasspath = myClasspath + "\\";
- }
- MyClassLoader.myClasspath = myClasspath;
- }
- /** */
- /**
- * Loads the class with the specified binary name. This method searches for
- * classes in the same manner as the loadClass(String, boolean) method.
- * Invoking this method is equivalent to invoking {loadClass(name,false)}.
- *
- * @param className
- * The binary name of the class.
- *
- * @return The resulting <tt>Class</tt> object.
- *
- * @throws ClassNotFoundException
- * If the class was not found.
- */
- @SuppressWarnings("unchecked")
- public Class loadClass(String className) throws ClassNotFoundException {
- return loadClass(className, false);
- }
- /** */
- /**
- * Loads the class with the specified binary name. The default
- * implementation of this method searches for classes in the following
- * order:
- *
- * Invoke {findLoadedClass(String)} to check if the class has already been
- * loaded.
- *
- * Invoke {findSystemClass(String)} to load the system class.
- *
- * Invoke the {findClass(String)} method to find the class.
- *
- * If the class was found using the above steps, and the resolve flag is
- * true, this method will then invoke the {resolveClass(Class)} method on
- * the resulting Class object.
- *
- * @param name
- * The binary name of the class.
- *
- * @param resolve
- * If true then resolve the class.
- *
- * @return The resulting Class object.
- *
- * @throws ClassNotFoundException
- * If the class could not be found.
- */
- @SuppressWarnings("unchecked")
- protected Class loadClass(String name, boolean resolve)
- throws ClassNotFoundException {
- try {
- Class foundClass = findLoadedClass(name);
- // check if the class has already been loaded.
- if (foundClass != null) {
- System.out.println("Complete to load the class: " + name);
- return foundClass;
- }
- // if the class is systemClass, load the system class by system
- if (name.startsWith("java.")) {
- foundClass = findSystemClass(name);
- loadClassHashTable.put(name, foundClass);
- System.out.println("System is loading the class: " + name);
- return foundClass;
- }
- // invoke the findClass() method to load the class
- try {
- foundClass = findClass(name);
- } catch (Exception fnfe) {
- }
- if (resolve && (foundClass != null)) {
- resolveClass(foundClass);
- }
- return foundClass;
- } catch (Exception e) {
- throw new ClassNotFoundException(e.toString());
- }
- }
- /** */
- /**
- * Finds the class with the specified binary name.The default implementation
- * throws a ClassNotFoundException.
- *
- * @param className
- * The binary name of the class.
- *
- * @return The resulting Class object.
- *
- * @throws ClassNotFoundException
- * If the class could not be found.
- */
- @SuppressWarnings("unchecked")
- public Class findClass(String className) {
- byte[] classData = null;
- try {
- classData = loadClassData(className);
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (classData == null) {
- return null;
- }
- System.out.println("MyClassLoader is loading : " + className + "");
- Class c = defineClass(className, classData, 0, classData.length);
- MyClassLoader.loadClassHashTable.put(className, c);
- System.out.println("Complete to load the class :" + className);
- return c;
- }
- /** */
- /**
- * Loads the classData with the specified binary name. This method searches
- * for classes in the specified classpath as
- * searchFile(myClasspath,className) method.
- *
- * @param name
- * The binary name of the class
- *
- * @return The resulting the classData of the class object by byte[]
- *
- * @throws IOException
- * if have some failed or interrupted I/O operations.
- */
- private byte[] loadClassData(String className) throws IOException {
- String filePath = searchFile(myClasspath, className + ".class");
- if (!(filePath == null || filePath == "")) {
- System.out.println("It have found the file : " + className
- + ". Begin to read the data and load the class。");
- FileInputStream inFile = new FileInputStream(filePath);
- byte[] classData = new byte[inFile.available()];
- inFile.read(classData);
- inFile.close();
- loadClassTime.put(className, new File(filePath).lastModified());
- return classData;
- } else {
- filePath = searchFile(myClasspath, className + ".java");
- if (!(filePath == null || filePath == "")) {
- System.out.println("It have found the file : " + filePath
- + ". Begin to translate");
- Runtime.getRuntime().exec("javac " + filePath);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("Translate it over : " + filePath);
- return loadClassData(className);
- } else {
- System.out
- .println("Haven't found the file, and fail to read the classData!");
- return null;
- }
- }
- }
- /** */
- /**
- * Loads the class with the specified binary name.The default implementation
- * throws a ClassNotFoundException.
- *
- * @param classData
- * The data of the class.
- * @param className
- * The binary name of the class.
- *
- * @return The resulting Class object.
- *
- * @throws ClassNotFoundException
- * If the class could not be found.
- */
- public Class loadClass(byte[] classData, String className)
- throws ClassNotFoundException {
- System.out.println("MyClassLoader is loading : " + className + "");
- Class c = defineClass(className, classData, 0, classData.length);
- loadClassHashTable.put(className, c);
- System.out.println("Complete to load the class :" + className);
- return c;
- }
- /** */
- /**
- * Loads the class with the specified binary name.The default implementation
- * throws a ClassNotFoundException.
- *
- * @param className
- * The binary name of the class.
- * @param jarName
- * The binary name of the jar that search the class from it.
- *
- * @return The resulting Class object.
- *
- * @throws ClassNotFoundException
- * If the class could not be found.
- */
- protected Class loadClass(String className, String jarName)
- throws ClassNotFoundException {
- String jarPath = searchFile(myClasspath, jarName + ".jar");
- JarInputStream in = null;
- if (!(jarPath == null || jarPath == "")) {
- try {
- in = new JarInputStream(new FileInputStream(jarPath));
- JarEntry entry;
- while ((entry = in.getNextJarEntry()) != null) {
- String outFileName = entry.getName().substring(
- entry.getName().lastIndexOf("/") + 1,
- entry.getName().length());
- if (outFileName.equals(className + ".class")) {
- if (entry.getSize() == -1) {
- System.err.println("error : can't read the file!");
- return null;
- }
- byte[] classData = new byte[(int) entry.getSize()];
- System.out
- .println("It have found the file : "
- + className
- + ". Begin to read the data and load the class。");
- in.read(classData);
- return loadClass(classData, className);
- }
- }
- System.out.println("Haven't found the file " + className
- + " in " + jarName + ".jar.");
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- } else {
- System.out.println("Haven't found the jarFile: " + jarName
- + ".jar.");
- return null;
- }
- return null;
- }
- /** */
- /**
- * Reloads the class with the specified binary name. Needn't have to restart
- * JVM then reload the class.
- *
- * @param className
- * The binary name of the class need to reload .
- *
- * @return The resulting Class object.
- *
- * @throws ClassNotFoundException
- * If the class was not found.
- */
- public Class reload(String fileName) {
- String filePath = searchFile(myClasspath, fileName + ".class");
- Long a = new File(filePath).lastModified();
- if (!a.equals(loadClassTime.get(fileName))) {
- loadClassHashTable.remove(fileName);
- loadClassTime.remove(fileName);
- try {
- MyClassLoader mc2 = new MyClassLoader(myClasspath);
- mc2.loadClass(fileName);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- } else {
- System.out
- .println("The class is the newest version , needn't reloading.");
- }
- return null;
- }
- /** */
- /**
- * search the file with the specified binary name. Needn't have to restart
- * JVM then reload the class.
- *
- * @param classpath
- * the specified path where we search.
- * @param fileName
- * The binary name of the file that want to search.
- *
- * @return The resulting file path.
- */
- public String searchFile(String classpath, String fileName) {
- String cut = fileName.substring(fileName.lastIndexOf('.'), fileName
- .length());
- String path = fileName.substring(0, fileName.lastIndexOf('.')).replace(
- '.', '/')
- + cut;
- File f = new File(classpath + path);
- if (f.isFile()) {
- return f.getPath();
- } else {
- String objects[] = new File(classpath).list();
- for (int i = 0; i < objects.length; i++) {
- if (new File(classpath + File.separator + objects[i])
- .isDirectory()) {
- String s = searchFile(classpath + objects[i]
- + File.separator, fileName);
- if (s == null || s == "") {
- continue;
- } else {
- return s;
- }
- }
- }
- }
- return null;
- };
- }
相关推荐
自定义classloader的使用
让Java支持热加载是个不错的想法。如何做到的呢? 1. 定义好接口和实现类 2. 让代理类通过反射的方式调用实现类,对外暴露的是代理类。 3. 自定义URLClassLoader。检查实现类.class文件的...Java自定义classloader;
热修复和插件化是目前比较热门的技术,要想更好的掌握它们需要了解ClassLoader,下面这篇文章主要给大家介绍了关于Android中自定义ClassLoader耗时问题追查的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧
自定义 ClassLoader 加载任何类时的类名。 ":myCommand" 命令位于默认 REPL 命令之上。 scala > val hello = " hello " MyClassLoader loads classOf < root>.$line3 <<中略>> MyClassLoader loads classOf ...
案1:修改序列化byte数据该法可解决序列化最终数据的serialVesionUID不致,但法解决Object的serialVesionUID不案2:反射修改s
ClassLoader运行机制 自己写的ClassLoader运行机制 自己写的ClassLoader运行机制 自己写的ClassLoader运行机制 自己写的ClassLoader运行机制 自己写的
这篇文章主要讲类加载器在android中如何动态的加载其他工程类的过程,对于类加载器的知识就跳过了。
3-7Tomcat中自定义类加载器的使用与源码实现(1).mp4
classloader 源码,自定义classloader
java自定义类加载classloader文档,包括代码,以及详细的原理及过程
eclipse工程格式 博文链接:https://aga.iteye.com/blog/200818
自定义ClassLoader,控制台输入调试。 运行期间 重新载入指定目录的class文件。可实现对于类的功能函数更新。 用到java 反射,@interface 等技术
java9 源码下载 问题引入 我们平时的开发中经常要引入各种sdk,现在我希望在代码中引入 ,你可以把这个demo 看作是MQ 的sdk,echo方法看成是MQ的send方法,功能就是输出序列化好的字符串... public ...
运用代理模式,通过自定义classloader对代码加密,啊;敌法;打飞机
框架开发学习 此仓库给出了在框架开发中经常使用的不同Java概念和编程技术的示例。 例如,当我们想从头开始创建RPC框架时,我们需要了解Java动态代理和序列化。 这是清单: 技术 模块/链接 设想 ...
Java 自定义ClassLoader 实现类的热替换核心代码
通过自定义ClassLoader,实现动态加载apk,jar包功能。
使用自定义Classloader机制,实现xlass的加载。 实现xlass打包的xar(类似class文件打包的jar)的加载:xar里是xlass。 基于自定义Classloader实现类的动态加载和卸载:需要设计加载和卸载。 基于自定义Classloader...
13. 自定义ClassLoader 13 14. double保留两位数的两个方案 18 16. 用户输入输出,文件输入输出范例(1) 19 17.以指定符号分割显示 22 17. 单例模式 22 18. 工厂模式 23 19. 读取配置文件(1) 25 20. 根据libName动态...