Lab 12
[valid 2025-2026]
Reflection
Create an application that runs code found in unknown java classes.
The application will receive as input java classes and attempt to find "runnable" methods and invoke them.
The main specifications of the application are:
Compulsory (1p)
- The input will be a .class file, located in the classpath of the application.
- Load the specified class in memory, assuming its package is known.
- Using reflection, determine if the class contains a method named
run with no arguments.
- Using reflection, invoke the
run method.
Homework (2p)
- The input is a folder located anywhere in the file system and containing
.class files.
- Determine which
.class files represent annotation types.
- Find all public classes in the folder and:
- Display their prototype (class name, methods, etc.)
- Invoke the methods annotated with the identified annotations in the following cases: the method requires no arguments;
or the method requires a single integer argument — in this case, use a mock value.
Advanced (2p)
- Consider the case when the input files may be .java source files and compile them before performing the required actions.
(use Java Compiler, for example).
- Use a bytecode manipulation and analysis framework, such as ASM, BCEL
or Javassist in order to perform bytecode engineering:
modify loaded classes so that the target methods print a log message before execution.
Resources
Objectives
- Understand Java class loading mechanism.
- Learn how to set the CLASSPATH and how to use the system class loader.
- Load classes dynamically.
- Instantiate objects of a class whose name is known only at runtime.
- Use Reflection API to inspect or use types at runtime.
- Understand the role of annotations in the context of modern programming techniques.