欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-17 22:02:52
 java 创建动态类和查看方法列表信息的实例 sample code : import java.lang.reflect.constructor;...

 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


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

上一篇:

下一篇: