zoukankan      html  css  js  c++  java
  • java自定义ClassLoader加载指定的class文件实现例子

    			SyncExternalClassLoader loader = new SyncExternalClassLoader();
    			Class<?> aClass = loader.findClass("bug");
    			object = aClass.newInstance();
    

      

    public class SyncExternalClassLoader extends ClassLoader {
    
    	@Override
    	public Class<?> findClass(String name) {
    		String myPath = "file:///D:/infosec/netauth/accountsync/SynchAccountToCertAuth.class";
    		System.out.println(myPath);
    		byte[] cLassBytes = null;
    		Path path = null;
    		try {
    			path = Paths.get(new URI(myPath));
    			cLassBytes = Files.readAllBytes(path);
    		} catch (IOException | URISyntaxException e) {
    			e.printStackTrace();
    		}
    		Class clazz = defineClass("infosec.netauth.accountsync.SynchAccountToCertAuth", cLassBytes, 0, cLassBytes.length);
    		return clazz;
    	}
    }
    

      

    boolean interfaceFlag = guessInterface(object);
    

      

    	private boolean guessInterface(Object object) {
    		if (object instanceof SynchAccountApi) {
    			return true;
    		}
    		if (object instanceof SyncUsersFromAppInterface) {
    			return true;
    		}
    		Class<?>[] allInterfaces = ClassUtils.getAllInterfaces(object);
    		return Arrays.stream(allInterfaces).anyMatch(aClass -> aClass.getName().contains("SynchAccountApi"));
    	}
    

      

    	private List<PropertiesBean> getPropertiesBean(){
    		SyncExternalClassLoader loader = new SyncExternalClassLoader();
    		Class<?> aClass = loader.findClass("bug");
    		Object object;
    		try {
    			object = aClass.newInstance();
    			Method method = aClass.getMethod("getProperties");
    			List<PropertiesBean> list = (List<PropertiesBean>) method.invoke(object);
    			return list;
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    

      

  • 相关阅读:
    JQuery中的AJAX
    (十六)JQuery Ready和angularJS controller的运行顺序问题
    Excel自己定义纸张打印设置碰到无法对上尺寸的问题
    MVC整个样例的源代码
    将Image转化为BufferImage
    Graphics samples2
    Graphics samples
    please tell me the error about java Graphics
    获取网络资源大小
    java通过地址获取主机名
  • 原文地址:https://www.cnblogs.com/cuijinlong/p/13330561.html
Copyright © 2011-2022 走看看