Example tutorial on creating dynamic classes in Java
This article mainly introduces relevant information on examples of creating dynamic classes and viewing method list information in Java. Friends in need can refer to
Examples of creating dynamic classes and viewing method list information in 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()); }
Run result:
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
The above is the detailed content of Example tutorial on creating dynamic classes in Java. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment