Git Product home page Git Product logo

java11-reflection-methods's Introduction

Build Status

java11-reflection-methods

Using reflection to get information about the methods of a class.

preface

  • https://github.com/mtumilowicz/java11-reflection-executables
  • java.lang.reflect.Method is used to represent method
    • Class<?> getReturnType()
  • Class provides us with four methods to gather information concerning methods:
    • all methods:
    • if we know method's name and parameter types:
      • Method getMethod(String name, Class<?>... parameterTypes) - you can think of this method as a way of trying to find a method in getMethods() by its name and parameters or throwing NoSuchMethodException (if the method cannot be found)

        note that argument type has to be exact

        the algorithm of finding method is quite complex: more info: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html#getMethod(java.lang.String,java.lang.Class...)

        there may be more than one method with matching name and parameter types in a class because while the Java language forbids a class to declare multiple methods with the same signature but different return types, the Java virtual machine does not.

      • Method getDeclaredMethod(String name, Class<?>... parameterTypes) - you can think of this method as a way of trying to find a method in getDeclaredMethods() by its name and parameters or throwing NoSuchMethodException (if the method cannot be found)

project description

We will show how to obtain info about declared methods.

Class structure is as simple as it can be:

  • parent
    class Parent implements ParentInterface {
        private long privateParentMethod(String name) {
            return 1;
        }
    
        void packagePrivateParentMethod() {
    
        }
    
        protected String protectedParentMethod(int x1, int x2) {
            return "";
        }
    
        public void publicParentMethod() {
    
        }
        
        public CharSequence override(int count) {
            return "";
        }
    }
    
    interface ParentInterface {
        default void defaultParentInterfaceMethod() {
        }
    }
    
  • child
    class Child extends Parent implements ChildInterface {
        private int privateChildMethod(String name) {
            return 1;
        }
        
        void packagePrivateChildMethod() {
            
        }
        
        protected String protectedChildMethod(int x1, int x2) {
            return "";    
        }
        
        public void publicChildMethod() {
            
        }
    
        @Override
        public String override(int count) {
            return "";
        }
    }
    
    interface ChildInterface {
        default void defaultChildInterfaceMethod() {
        }
    }
    

All tests are in MethodReflectionTest class

  • getMethods
    var methods = Child.class.getMethods();
    
    assertThat(methods.length, is(15));
    
    var methodsAsString = Arrays.toString(methods);
    
    // Child
    assertThat(methodsAsString, containsString("public java.lang.String Child.override(int)"));
    assertThat(methodsAsString, containsString("public java.lang.CharSequence Child.override(int)"));
    assertThat(methodsAsString, containsString("public void Child.publicChildMethod()"));
    assertThat(methodsAsString, containsString("public default void ChildInterface.defaultChildInterfaceMethod()"));
    
    // Parent
    assertThat(methodsAsString, containsString("public void Parent.publicParentMethod()"));
    assertThat(methodsAsString, containsString("public default void ParentInterface.defaultParentInterfaceMethod()"));
    
    // Object
    assertThat(methodsAsString, containsString("public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException"));
    assertThat(methodsAsString, containsString("public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException"));
    assertThat(methodsAsString, containsString("public final void java.lang.Object.wait() throws java.lang.InterruptedException"));
    assertThat(methodsAsString, containsString("public boolean java.lang.Object.equals(java.lang.Object)"));
    assertThat(methodsAsString, containsString("public java.lang.String java.lang.Object.toString()"));
    assertThat(methodsAsString, containsString("public native int java.lang.Object.hashCode()"));
    assertThat(methodsAsString, containsString("public final native java.lang.Class java.lang.Object.getClass()"));
    assertThat(methodsAsString, containsString("public final native void java.lang.Object.notify()"));
    assertThat(methodsAsString, containsString("public final native void java.lang.Object.notifyAll()"));
    
  • getDeclaredMethods
    var methods = Child.class.getDeclaredMethods();
    
    assertThat(methods.length, is(6));
    
    var methodsAsString = Arrays.toString(methods);
    
    assertThat(methodsAsString, containsString("public java.lang.String Child.override(int)"));
    assertThat(methodsAsString, containsString("public java.lang.CharSequence Child.override(int)"));
    assertThat(methodsAsString, containsString("public void Child.publicChildMethod()"));
    assertThat(methodsAsString, containsString("private int Child.privateChildMethod(java.lang.String)"));
    assertThat(methodsAsString, containsString("void Child.packagePrivateChildMethod()"));
    assertThat(methodsAsString, containsString("protected java.lang.String Child.protectedChildMethod(int,int)"));
    
  • get method by name and params
    • not found - NoSuchMethodException
      @Test(expected = NoSuchMethodException.class)
      public void getMethod_notFound() throws NoSuchMethodException {
          Child.class.getMethod("not exists");
      }
      
    • private - NoSuchMethodException
      @Test(expected = NoSuchMethodException.class)
      public void getMethod_private() throws NoSuchMethodException {
          Child.class.getMethod("privateChildMethod", String.class);
      }
      
    • public
      assertThat(Child.class.getMethod("publicChildMethod").toGenericString(),
              is("public void Child.publicChildMethod()"));
      
    • from parent
      assertThat(Child.class.getMethod("publicParentMethod").toGenericString(),
              is("public void Parent.publicParentMethod()"));
      
  • get declared method by name and params
    • not found - NoSuchMethodException
      @Test(expected = NoSuchMethodException.class)
      public void getDeclaredMethod_notFound() throws NoSuchMethodException {
          Child.class.getDeclaredMethod("not exists");
      }
      
    • private
      assertThat(Child.class.getDeclaredMethod("privateChildMethod", String.class).toGenericString(),
              is("private int Child.privateChildMethod(java.lang.String)"));
      
    • argument must be exact
      @Test(expected = NoSuchMethodException.class)
      public void getMethod_public_byObjectParam() throws NoSuchMethodException {
          assertThat(Child.class.getDeclaredMethod("privateChildMethod", Object.class).toGenericString(),
                  is("private int Child.privateChildMethod(java.lang.String)"));
      }
      
    • from parent
      @Test(expected = NoSuchMethodException.class)
      public void getDeclaredMethod_parent() throws NoSuchMethodException {
          Child.class.getDeclaredMethod("publicParentMethod");
      }
      

java11-reflection-methods's People

Contributors

mtumilowicz avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.