Week 11 Exercises Java Reflection

Richard van den Ham, Pieter van den Hombergh, Stefan Sobek V1.0 2018-03-11 :toc: right :icons: font :includes: ../exercises :sourcedir: ../exercises/code :docbase: .. :description: This site is contains the combined tasks for PRC2 (Java Programming 2) and Development Processes, starting in Februari 2018. :keywords: Real Java SEBI Venlo :source-highlighter: coderay

1. Reflection, how about class hierarchy

Study the lessondemo on reflection.

Play with it and modify it to your liking. Maybe try your hand at generate the a student mapper, by reflecting on the student class and have it transfer a record of a resultset on a student table in a database to a student object.

Hints: Get the field definitions from the student class reflectively, and generate (as a String!) a method that uses either a constructor, a series of setters or a builder to create a student object.

If you can make this work for a generic type, you should be able to generate mappers for any entity. Of course, you could also use the meta-information on the database tables (information_schema) to generate the student class in the first place.

Just play with it. It is by no means the intention that you make a full fledged object relation mapping framework, but by doing the experiments you get to understand the concepts of how frameworks like Hibernate and Eclipselink might work.

2. Dependency injection with Home Made Injection

In your project (prj2), instead of using Class.forName(String) to load a jdbc driver in the classes that run on the server, modify them to use a datasource like below (and the the lessondemo of dvp, ), modify the classes that run in the server to use a datasource.

    @Resource( lookup = "jdbc/fantys" )
    protected DataSource ds;

In the test classes also remove any database attributes such as credentials and data basename form the production and test code, but instead read it from a properties file with a well know name (to be agreed upon in your project team).

Hint: use different properties for production and test (implying: different databases for either).

Useful in tests.