java.lang.reflect.Proxy.isProxyClass() Method Example



Description

The java.lang.reflect.Proxy.isProxyClass(Class<?> cl) method returns true if and only if the specified class was dynamically generated to be a proxy class using the getProxyClass method or the newProxyInstance method.

Declaration

Following is the declaration for java.lang.reflect.Proxy.isProxyClass(Class<?> cl) method.

public static boolean isProxyClass(Class<?> cl)

Parameters

cl − the class to test.

Returns

true if the class is a proxy class and false otherwise.

Exceptions

NullPointerException − if cl is null.

Example

The following example shows the usage of java.lang.reflect.Proxy.isProxyClass(Class<?> cl) method.

package com.tutorialspoint;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyDemo {
   public static void main(String[] args) 
      throws IllegalArgumentException, InstantiationException, 
         IllegalAccessException, InvocationTargetException, 
         NoSuchMethodException, SecurityException {
      InvocationHandler handler = new SampleInvocationHandler() ;

      Class proxyClass = Proxy.getProxyClass(
      SampleClass.class.getClassLoader(), new Class[] { SampleInterface.class });
      SampleInterface proxy = (SampleInterface) proxyClass.
         getConstructor(new Class[] { InvocationHandler.class }).
         newInstance(new Object[] { handler });
      System.out.println(Proxy.isProxyClass(proxyClass));
      proxy.showMessage();
   }
}

class SampleInvocationHandler implements InvocationHandler {

   @Override
   public Object invoke(Object proxy, Method method, Object[] args)
      throws Throwable {
      System.out.println("Welcome to TutorialsPoint");   
      return null;
   }
}

interface SampleInterface {
   void showMessage();
}

class SampleClass implements SampleInterface {
   public void showMessage(){
      System.out.println("Hello World");   
   }
}

Let us compile and run the above program, this will produce the following result −

true
Welcome to TutorialsPoint
java_reflect_proxy.htm
Advertisements