apim_4xx_support_multiple_analytics_publishers APIM manage workflow with multiple roles APIM 3.0.0 per API based subscription workflow Logging internal HTTP requests Log APIM analytics events to a file Monetization and sample with WSO2 API Manager 2.6.0 Share application and subscription among a set of specific groups or roles WSO2 APIM Correlating analytics event with correlationID APIM analytics distinguish production and sandbox traffic APIM 2.x.x analytics internal and analytics tuneup Configure APIM(Next release) Key Manager User stores APIM(Next release) working with key manager DAS 3.x Parse system variables to Spark Context Revoke OAuth application In APIM 2.1.0 Next WSO2 APIM powered by WSO2 Ballerina Configure WSO2 APIM Analytics on Cluster environment Configure WSO2 DAS 3.1.0 for WSO2 APIM 2.0.0 Analytics WSO2 APIM publishing custom statistics WSO2 APIM Error codes Working with WSO2 message tracer Use DAS admin service to query using Spark SQL Configure WSO2 APIM Analytics using XML WSO2 APIM Generating and Retrieving Custom Statistics Understanding WSO2 APIM Statistics Model Publishing WSO2 APIM 1.10.x Runtime Statistics to DAS with RDBMS Publishing_APIM_1100_Runtime_Statistics_to_DAS Aggregate functions with WSO2 DAS REST API Create a cApp for WSO2 DAS Debugging WSO2 Products using OSGI console. Publishing APIM Runtime Statistics to DAS Deploy cApp on WSO2 DAS How to configure and start the Accumulo minicluster How to setup DNS server on Ubuntu and Ubuntu server How to use Java Reflection how to install apache web server on ubuntu and ubuntu server How to install Mail server on Ubuntu and Ubuntu server How to install squirrelmail webmail client on Ubuntu and Ubuntu Server Pass and return String value to JNI method Pass and return numeric value to JNI method Calling a C Function from the Java Programming Language using JNI AXIS 2 Sample web service Client with maven and eclipse How to setup AXIS 2 with Apache Tomcat AXIS 2 Sample web service with maven and eclipse Robot framework Sample with Selenium Robot framework Custom Library Sample Behaviour-Driven Development with JBehave and Eclipse Play Audio with Netbeans and linking with LibVLC Implement LibVLC based player with QT-part2 Simple Audio playing sample with LibVLC How to install LibVLC on Ubuntu Implement LibVLC based player with QT-part1
How to use Java Reflection
  1. For the reflection examples, lets consider this Dog class

       package ref;
    
    /**
     *
     * @author Rukshan
     */
    public class Dog {
        
        private String name;
        public String type;
       
        public Dog() {        
        }
    
        private Dog(String type) {
            this.type=type;
        }  
        
        public void setName(String name) {
            this.name=name;
        }
        
        public void setType(String type){
            this.type=type;
        }
        
        public String getName(){
            return this.name;
        }
    
        public String getType(){
            return this.type;
        }
        
        private String Bark(String sound){
            return "Barked "+sound;
        }
    }
    
        
  2. Get the value of a field of object

       public static void getField() {
            try {
                Dog dog = new Dog();
                dog.setType("dog");
                Class<?> dogClass = dog.getClass();
                Field type = dogClass.getDeclaredField("type");
                String value = (String) type.get(dog);
                System.out.println(value);
    
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
  3. Get the value of protected field of object

    	public static void getProtectedField() {
            try {
                Dog dog = new Dog();
                dog.setName("lucky");
                Class<?> dogClass = dog.getClass();
                Field name = dogClass.getDeclaredField("name");
                name.setAccessible(true);
                String value = (String) name.get(dog);
                System.out.println(value);
    
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    	   
  4. Set the value of field in Object

          public static void setField() {
            try {
                Dog dog = new Dog();
                Class<?> dogClass = dog.getClass();
                Field type = dogClass.getDeclaredField("type");
                String value = "Dog";
                type.set(dog, value);
                System.out.println(dog.getType());
    
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
          
  5. Set the value of protected field in object

           public static void setProtectedField() {
            try {
                Dog dog = new Dog();
                Class<?> dogClass = dog.getClass();
                Field name = dogClass.getDeclaredField("name");
                String value = "Lucky";
                name.setAccessible(true);
                name.set(dog, value);
                System.out.println(dog.getName());
    
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
           
  6. Invoke method

          public static void invokeMethod() {
            try {
                Dog dog = new Dog();
                Class<?> dogClass = dog.getClass();
                Method setName = dogClass.getDeclaredMethod("setName", new Class[]{String.class});
                setName.invoke(dog, new Object[]{"Lucky"});
                System.out.println(dog.getName());
            } catch (SecurityException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchMethodException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
           
  7. Invoke proteced methods and get return value

          public static void invokeProtectedMethod() {
            try {
                Dog dog = new Dog();
                Class<?> dogClass = dog.getClass();
                Method bark = dogClass.getDeclaredMethod("Bark", new Class[]{String.class});
                bark.setAccessible(true);
                Object ob = bark.invoke(dog, new Object[]{"awwww"});
                String value = (String) ob;
                System.out.println(value);
            } catch (SecurityException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchMethodException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
           
  8. Create Instance with constructor

          public static void constructor(){
            try {
                Class<?> dogClass=Class.forName("ref.Dog");
                Constructor<?> construct=dogClass.getConstructor();
                Object dog=construct.newInstance();
                
                Method bark = dogClass.getDeclaredMethod("Bark", new Class[]{String.class});
                bark.setAccessible(true);
                Object ob = bark.invoke(dog, new Object[]{"huuuuu"});
                String value = (String) ob;
                System.out.println(value);
                
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchMethodException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
          
  9. Create Instance with proteced constructor

          public static void protectedConstructor(){
            try {
                Class<?> dogClass=Class.forName("ref.Dog");
                Constructor<?> construct=dogClass.getConstructor();
                construct.setAccessible(true);
                Object dog=construct.newInstance();
                
                Method bark = dogClass.getDeclaredMethod("Bark", new Class[]{String.class});
                bark.setAccessible(true);
                Object ob = bark.invoke(dog, new Object[]{"awwww huuuuu"});
                String value = (String) ob;
                System.out.println(value);
                
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchMethodException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
           
  10. Get the declared filed of a class

          public static void getFields(){
            try {
                Class<?> dogClass=Class.forName("ref.Dog");
                Field[] fields=dogClass.getDeclaredFields();
                for (Field field : fields) {
                    System.out.println(field.getName()+" "+field.getType().getCanonicalName());
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
           
  11. Get Declared methods and method parameters of a class

          public static void getMethods(){
            try {
                Class<?> dogClass=Class.forName("ref.Dog");
                Method[] methods=dogClass.getDeclaredMethods();
                for (Method method : methods) {
                    System.out.println(method.getName()+" "+method.getReturnType().getCanonicalName());
                    for (Class<?> class1 : method.getParameterTypes()) {
                        System.out.println(class1.getCanonicalName());
                    }
                    
                }
                
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Ref.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
          
  12. Download the Source code from here

Add Comment

* Required information
1000
Powered by Commentics

Comments (0)

No comments yet. Be the first!