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;
    	}
    

      

  • 相关阅读:
    好用的Win7下硬盘分区软件:Acronis Disk Director Suite
    SQL Server 相关create操作语句
    我也有博客了
    N层构架如何实现
    SQL相关增删改查语句
    1.MVC的工作流程
    回顾去年以来读过的书
    [Architecture]Facebook Chat
    [Tips]解决make_sock: could not bind to address 0.0.0.0:XXXX
    Emacs中的按键组合
  • 原文地址:https://www.cnblogs.com/cuijinlong/p/13330561.html
Copyright © 2011-2022 走看看