欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java 创建动态类和查看方法列表信息的实例

时间:2020-11-06 09:52:41|栏目:JAVA代码|点击:

 Java 创建动态类和查看方法列表信息的实例

Sample code :

import java.lang.reflect.Constructor; 
import java.lang.reflect.InvocationHandler; 
import java.lang.reflect.Method; 
import java.lang.reflect.Proxy; 
import java.lang.reflect.Type; 
import java.util.ArrayList; 
import java.util.Collection; 
 
public class ProxyTest { 
 
  public static void main(String[] args) throws Exception { 
    // TODO Auto-generated method stub 
    Class clazzProxy = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class); 
    System.out.println(clazzProxy); 
    System.out.println("------constructor method list ------"); 
    Constructor[] constructors = clazzProxy.getConstructors(); 
    for(Constructor constructor:constructors){ 
      StringBuilder sb = new StringBuilder(constructor.getName()); 
      sb.append("("); 
      Type[] parameterTypes = constructor.getParameterTypes(); 
      for(Type parameterType:parameterTypes){ 
        sb.append(parameterType.toString()+","); 
      } 
      if(parameterTypes.length>0){ 
        sb.deleteCharAt(sb.length()-1); 
      } 
      sb.append(")"); 
      System.out.println(sb.toString()); 
       
    } 
    System.out.println("------constructor method list ------\n\n"); 
     
    System.out.println("------ method list ------"); 
    Method[] methods = clazzProxy.getMethods(); 
    for(Method method:methods){ 
      StringBuilder sb2 = new StringBuilder(method.getName()); 
      sb2.append("("); 
      Type[] parameterTypes = method.getParameterTypes(); 
      for(Type parameterType:parameterTypes){ 
        sb2.append(parameterType.toString()+","); 
      } 
      if(parameterTypes.length>0){ 
        sb2.deleteCharAt(sb2.length()-1); 
      } 
      sb2.append(")"); 
      System.out.println(sb2.toString()); 
       
    } 
    System.out.println("------ method list ------"); 
     
    Constructor proxyConstructor = clazzProxy.getConstructor(InvocationHandler.class); 
    class MyInvocationHandler implements InvocationHandler{ 
      ArrayList target = new ArrayList(); 
      public Object invoke(Object proxy, Method method, Object[] args) 
          throws Throwable { 
        Object obj = method.invoke(target, args); 
        return obj; 
      } 
       
    } 
    MyInvocationHandler mih = new MyInvocationHandler(); 
    Collection collectionProxy = (Collection) proxyConstructor.newInstance(mih); 
    collectionProxy.add("zhuang"); 
    collectionProxy.add("alex"); 
    System.out.println("collectionProxy size:"+collectionProxy.size()); 
     
    Collection collectionProxy2 = (Collection)Proxy.newProxyInstance(Collection.class.getClassLoader(),new Class[] {Collection.class},new InvocationHandler(){ 
      ArrayList target = new ArrayList(); 
      public Object invoke(Object proxy, Method method, Object[] args) 
          throws Throwable { 
        Object obj = method.invoke(target, args); 
        return obj; 
      } 
    }); 
     
    collectionProxy2.add("one"); 
    collectionProxy2.add("two"); 
    collectionProxy2.add("three"); 
    System.out.println("collectionProxy2 size:"+collectionProxy2.size()); 
  } 

运行结果:

class $Proxy0
------constructor method list ------
$Proxy0(interface Java.lang.reflect.InvocationHandler)
------constructor method list ------


------ method list ------
add(class java.lang.Object)
hashCode()
equals(class java.lang.Object)
clear()
toString()
contains(class java.lang.Object)
isEmpty()
addAll(interface java.util.Collection)
iterator()
size()
toArray(class [Ljava.lang.Object;)
toArray()
remove(class java.lang.Object)
containsAll(interface java.util.Collection)
removeAll(interface java.util.Collection)
retainAll(interface java.util.Collection)
isProxyClass(class java.lang.Class)
getProxyClass(class java.lang.ClassLoader,class [Ljava.lang.Class;)
newProxyInstance(class java.lang.ClassLoader,class [Ljava.lang.Class;,interface java.lang.reflect.InvocationHandler)
getInvocationHandler(class java.lang.Object)
wait()
wait(long,int)
wait(long)
getClass()
notify()
notifyAll()
------ method list ------
collectionProxy size:2
collectionProxy2 size:3


感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:java实现时钟效果

栏    目:JAVA代码

下一篇:Spring Cloud Feign 自定义配置(重试、拦截与错误码处理) 代码实践

本文标题:Java 创建动态类和查看方法列表信息的实例

本文地址:http://www.codeinn.net/misctech/19550.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有