Git Product home page Git Product logo

java11-reflection-classes's Introduction

Build Status

java11-reflection-classes

Using reflection to get the description of a class.

preface

https://github.com/mtumilowicz/java-reflection

project description

We will show how to obtain basic info about class (such as modifiers, implemented interfaces, etc.).

  1. We have simple class hierarchy in the family module:
    • module-info
      module family {
      }
      
    • parent
      public class Parent implements ParentInterface {
      }
      
      public interface ParentInterface {
      }
      
    • child
      public abstract class Child<T> extends Parent implements ChildInterface, Serializable {
      }
      
      public interface ParentInterface {
      }
      

All tests are in reflection.ClassReflectionTest class:

  • modifiers

    assertEquals(Modifier.toString(childClass.getModifiers() & Modifier.classModifiers()), "public abstract");
    
    • int getModifiers() - returns modifiers as int (https://github.com/mtumilowicz/java11-ORed-container)
    • int classModifiers() - returns possible modifiers for class
      note, that we have other similar methods:
      • interfaceModifiers()
      • constructorModifiers()
      • methodModifiers()
      • fieldModifiers()
      • parameterModifiers() (since java 8)
    • Modifier.toString(int mod) converts from int to string representation
  • all modifiers at once (since java 8)

    assertEquals(childClass.toGenericString(), "public abstract class child.Child<T>");
    
  • name

    assertEquals(childClass.getName(), "child.Child");
    
  • simple name

    assertEquals(childClass.getSimpleName(), "Child");
    
  • superclass

    assertEquals(childClass.getSuperclass(), Parent.class);
    
    • if there is no explicit superclasses and class differs from Object.class
      assertEquals(Parent.class.getSuperclass(), Object.class);
      
    • superclass of Object.class is null
      assertNull(Object.class.getSuperclass());
      
  • interfaces

    • only interfaces explicitly declared (inherited are not returned)
      assertArrayEquals(childClass.getInterfaces(), new Class<?>[]{ChildInterface.class, Serializable.class});
      
    • if there is no interfaces, the array is empty
      assertArrayEquals(Object.class.getInterfaces(), new Class<?>[]{});
      
  • type parameters

    assertEquals(Arrays.toString(childClass.getTypeParameters()), "[T]");
    
    • if there is no type parameters, the array is empty
      var typeParameters = Object.class.getTypeParameters();
      assertEquals(typeParameters.length, 0);
      
  • package name

    assertEquals(childClass.getPackageName(), "child");
    
  • module name

    • in tests:
      assertTrue(childClass.getModule().toString().contains("unnamed module"));
      assertNull(childClass.getModule().getName());
      
    • however in main:
      public static void main(String[] args) {
              System.out.println(Child.class.getModule().getName());
      }
      
      will print: family

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