JVM 加载类 ClassLoader 类方法

来源:互联网 发布:网络层的主要功能 编辑:程序博客网 时间:2024/06/06 12:32
package com.budco.bmp.core.email.TestMail;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;

public class TestClassLoader extends ClassLoader {

    // 存在文件的路径
    private static final String DRIVE = "c:\\com\\budco\\bmp\\";
    private static final String DRIVE1 = "com\\budco\\bmp\\";
    private static final String FILETYPE = ".class";

    @Override
    public final Class<?> findClass(final String name) {
        final byte[] data = loadClassData(name);

        // final String classPath = ClassLoader.getSystemResource((DRIVE1 +
        // name).replace('\\', '.')).getFile().substring(
        // 1);
        // 把你要加载类用'.'代替\\ 应该是包路径+类名 //(DRIVE1 + name).replace('\\', '.')==
        // com.budco.bmp.HelloWorld
        return defineClass((DRIVE1 + name).replace('\\', '.'), data, 0, data.length);
    }

    public byte[] loadClassData(final String name) {
        // name = name.substring(0,name.lastIndexOf('.'));
        FileInputStream fis = null;
        byte[] data = null;
        final byte buffer[] = new byte[1024];
        try {
            fis = new FileInputStream(new File(DRIVE + name + FILETYPE));
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len = 0;
            try {
                while ((len = fis.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }
                data = baos.toByteArray();
                System.out.println("-----------------|||||||||||||||||--------------");
                // System.out.println(new String(data));
                baos.close();
                fis.close();
            } catch (final IOException e) {

                e.printStackTrace();
            }
        } catch (final FileNotFoundException e) {

            e.printStackTrace();
        }
        return data;
    }

    /**
     * @param args
     */
    public static void main(final String[] args) throws Exception {
        // TODO Auto-generated method stub
        final TestClassLoader loader = new TestClassLoader();
        final TestClassLoader loader2 = new TestClassLoader();
        final Class<?> obj = loader.findClass("HelloWorld");
        final Class<?> o = Class.forName("com.budco.bmp.HelloWorld");
        final Field fs = o.getField("RS");
        System.out.println("------------------------ooo==" + fs.get("RS"));
        // final Class obj2 = loader2.loadClass("Test2", true);
        final Object objclass = obj.newInstance();

        System.out.println("==========1" + objclass.getClass());
        System.out.println("==========2" + obj.getName());
        System.out.println("==========3" + obj.getClassLoader());
        System.out.println("----------------------4  " + obj.getCanonicalName());

        final Field f1 = obj.getField("RS");
        final String s = (String) f1.get("RS");
        System.out.println("-------------------" + s);
        // 只可以 获得public 域名称

        final Field f2 = obj.getField("txt");
        final String s2 = (String) f2.get("txt");
        System.out.println("-------------------" + s2);

    }

    public final class gg {

    }

}


///////////下面是测试的类HelloWorld.java 要这个类编译.class文件


package com.budco.bmp;

public class HelloWorld {

    private String name = null;
    private static final String DRIVE = "c:\\com\\budco\\bmp\\";
    public final static String RS = "HAL";
    public final static String txt = "TXT";

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    private String getMethodName() {
        this.setName("ale");
        System.out.println("-------------");
        return "";

    }

    /**
     * @param args
     */
    public static void main(final String[] args) {
        // TODO Auto-generated method stub
        System.out.println("---------HelloWorld!----");

        String path = "";
        path = DRIVE.replace('\\', '.');
        System.out.println(path);
    }

}



原创粉丝点击