Git Product home page Git Product logo

java11-reflection-constructors's Introduction

Build Status

java11-reflection-constructors

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

preface

  • https://github.com/mtumilowicz/java11-reflection-executables
  • java.lang.reflect.Constructor is used to represent method
  • Class provides us with four methods to gather information concerning constructors:
    • all constructors:
      • Constructor[] getConstructors() - returns all public constructors
      • Constructor[] getDeclaredConstructors() - returns all declared constructors
    • constructor by parameters
      • Constructor getConstructor() - you can think of this method as a way of trying to find a constructor in getConstructors() by parameters or throwing NoSuchMethodException (if the constructor cannot be found)
      • Constructor getDeclaredConstructor() - you can think of this method as a way of trying to find a constructor in getDeclaredConstructors() by parameters or throwing NoSuchMethodException (if the constructor cannot be found)

project description

Class structure:

class Child extends Parent {
    private Child() {

    }

    Child(String name) {

    }

    protected Child(int count) {

    }

    public Child(String name, int count) {

    }
}


class Parent {
    public Parent() {

    }
    
    public Parent(String s1, String s2, String s3) {
        
    }
}

All tests are in ConstructorReflectionTest

  • getConstructors
    var constructors = Child.class.getConstructors();
    
    assertThat(constructors.length, is(1));
    
    var constructorsAsString = Arrays.toString(constructors);
    assertThat(constructorsAsString, containsString("public Child(java.lang.String,int)"));
    
  • getDeclaredConstructors
    var constructors = Child.class.getDeclaredConstructors();
    
    assertThat(constructors.length, is(4));
    
    var constructorsAsString = Arrays.toString(constructors);
    assertThat(constructorsAsString, containsString("public Child(java.lang.String,int)"));
    assertThat(constructorsAsString, containsString("protected Child(int)"));
    assertThat(constructorsAsString, containsString("Child(java.lang.String)"));
    assertThat(constructorsAsString, containsString("private Child()"));
    
  • public constructor by params
    • not found - NoSuchMethodException
      @Test(expected = NoSuchMethodException.class)
      public void getConstructor_notFound() throws NoSuchMethodException {
          Child.class.getConstructor(int.class, int.class);
      }
      
    • not public - NoSuchMethodException
      @Test(expected = NoSuchMethodException.class)
      public void getConstructor_private() throws NoSuchMethodException {
          Child.class.getConstructor();
      }
      
    • from parent - NoSuchMethodException
      @Test(expected = NoSuchMethodException.class)
      public void getConstructor_fromParent() throws NoSuchMethodException {
          Child.class.getConstructor(String.class, String.class, String.class);
      }
      
    • public
      assertThat(Child.class.getConstructor(String.class, int.class).toGenericString(),
              is("public Child(java.lang.String,int)"));
      
  • declared constructor by params
    • not found - NoSuchMethodException
      @Test(expected = NoSuchMethodException.class)
      public void getDeclaredConstructor_notFound() throws NoSuchMethodException {
          Child.class.getDeclaredConstructor(int.class, int.class);
      }
      
    • private
      assertThat(Child.class.getDeclaredConstructor().toGenericString(),
              is("private Child()"));
      
    • public
      assertThat(Child.class.getDeclaredConstructor(String.class, int.class).toGenericString(),
              is("public Child(java.lang.String,int)"));
      

java11-reflection-constructors'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.