Skip to content

GabrielGordon-Hall/sdp-sp3-worksheet-two

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 Cannot retrieve latest commit at this time.

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Worksheet Two on Reflection

The Reflection API allows a JVM based program to inspect and manipulate itself; it comprises the KClass and Class classes together with the kotlin.reflect and kotlin.reflect.jvm packages; the latter is for interoperability with Java.

  1. Write a Kotlin program that reads the name of a class from the command line and emits the interface of the class (interface or class, modifiers, constructors, methods, fields; no method bodies).

Hint: You can load a class whose name you know with Class.forName(). The Class class offers a rich interface that enables you to inspect the interface of any class.

Apply this program to a set of classes and interfaces as test input. You may also use the program on itself. You should see how a Java program behaves when provided as an argument to this program.

  1. Write a program that reads a class name and a list of arguments, and creates an object of that class where the arguments are passed to the constructor.

    Hint: Treat arguments as strings. A Class can enumerate its constructors. Choose a constructor with the appropriate parameter count. Then, find the parameter types. To create typed argument objects, call the appropriate constructors that take a string as their only argument.

  2. Write a JUnit test to help grade the internal correctness of a student's submitted program for a hypothetical assignment.

Make the tests fail if the class under test has any of the following:

  1. Normally it is up to the programmer to write a tailored toString() method for each class one creates. This exercise is about writing a general toString method once and for all (very much like the one we get synthesised for Data classes).

As part of the reflection API, it is possible to find out which fields exist for a given object, and to get their values. The purpose of this exercise is to make a toString method that gives a printed representation of any object, in such a manner that all fields are printed, and references to other objects are handled as well.

To solve this exercise, you will need to examine the reflection API in detail.

About

Worksheet Two on Reflection

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 83.1%
  • PureBasic 16.9%