Skip to content

Codefinity-Java/article-reflection-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Reflection with Annotations Example

This Java application demonstrates the use of reflection to manipulate fields annotated with custom annotations. The example shows how to inject random values into an object's fields based on the parameters defined in a custom annotation.

Classes Overview

Main

The Main class serves as the entry point for the application. It creates a Student object, uses the RandomNumberInjector to inject random values into annotated fields, and then prints the student's details to the console.

RandomNumber

The RandomNumber annotation defines two parameters: min and max, which specify the range for the random number generation. Fields annotated with @RandomNumber will be assigned a random value within this range.

RandomNumberInjector

The RandomNumberInjector class contains the logic for injecting random numbers into fields annotated with RandomNumber. It uses reflection to:

  • Scan the object's fields for the @RandomNumber annotation.
  • Generate a random value within the specified range.
  • Set the generated value to the corresponding field.

Student

The Student class represents a student with the following fields:

  • name: The student's name.
  • grade: The student's grade level.
  • score: The student's score, which is annotated with @RandomNumber. A random value is injected into this field.
  • age: The student's age.

How It Works

The application creates a Student object and passes it to the RandomNumberInjector. The injector scans the object's fields and assigns a random value to any field annotated with @RandomNumber. This demonstrates how annotations can be used to drive custom behavior in a program.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages