Skip to content

Commit

Permalink
Reflection API
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzor committed Jul 30, 2024
1 parent 1309c16 commit b963039
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/ru/core/classes/EngineStart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.core.classes;

public class EngineStart {
public static void main(String[] args) {
StaticNestedClassCar.Engine engine = new StaticNestedClassCar.Engine(500);
StaticNestedClassCar car = new StaticNestedClassCar("blue", "bugatti", engine, 6000);
System.out.println(engine);
System.out.println(car);

}
}
41 changes: 41 additions & 0 deletions src/main/java/ru/core/classes/StaticNestedClassCar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ru.core.classes;

public class StaticNestedClassCar {
String color;
String model;
Engine engine;
int engineVolume;

public StaticNestedClassCar(String color, String model, Engine engine, int engineVolume) {
this.color = color;
this.model = model;
this.engine = engine;
this.engineVolume = engineVolume;
}


@Override
public String toString() {
String sb = "Car has is {" + "color='" + color + '\''
+ ", model='" + model + '\''
+ ", engine=" + engine
+ ", engineVolume=" + engineVolume
+ '}';
return sb;
}

public static class Engine {
int horsePower;

public Engine(int horsePower) {
this.horsePower = horsePower;
}

@Override
public String toString() {
String sb = "Engine has is {" + "horsePower=" + horsePower
+ '}';
return sb;
}
}
}
49 changes: 49 additions & 0 deletions src/main/java/ru/j4j/reflection/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ru.j4j.reflection;

import lombok.Getter;
import lombok.Setter;

public class Employee {
public int id;
public String name;
public String department;
@Setter
@Getter
private double salary;


public Employee() {
}

public Employee(int id, String name, String department) {
this.id = id;
this.name = name;
this.department = department;
}

public Employee(int id, String name, String department, double salary) {
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}

public void increaseSalary(double salary, int percentage) {
this.salary *= percentage;
}

private void changeDepartment(String newDepartment) {
this.department = newDepartment;
System.out.println("Department changed to " + newDepartment);
}

@Override
public String toString() {
String sb = "Employee has is{" + "id=" + id
+ ", name='" + name + '\''
+ ", department='" + department + '\''
+ ", salary=" + salary
+ '}';
return sb;
}
}
95 changes: 95 additions & 0 deletions src/main/java/ru/j4j/reflection/FirstExam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package ru.j4j.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

public class FirstExam {
public static void main(String[] args) throws Exception {
try {
Class<?> employeeClass = Class.forName("ru.j4j.reflection.Employee");

Class<?> employeeClass2 = Employee.class;

Employee employee = new Employee();
Class<?> employeeClass3 = employee.getClass();

Field fieldEmployee = employeeClass.getField("id");
Field fieldEmployee1 = employeeClass.getField("name");
Field fieldEmployee2 = employeeClass.getField("department");


System.out.println("Type of id Field: " + fieldEmployee.getType());
System.out.println("Type of name Field: " + fieldEmployee1.getType());
System.out.println("Type of department Field: " + fieldEmployee2.getType());

Field[] fieldsEmployee = employeeClass.getFields();
System.out.println(Arrays.toString(fieldsEmployee));
for (Field field : fieldsEmployee) {
System.out.println("Name: " + field.getName() + ", Type: " + field.getType());
}

Field[] fieldsDeclaredEmployee = employeeClass.getDeclaredFields();
for (Field field : fieldsDeclaredEmployee) {
System.out.println("Name: " + field.getName() + ", Type: " + field.getType());
}

Method increaseSalary = employeeClass.getMethod("increaseSalary", double.class, int.class);
System.out.println("Increasing Salary: " + increaseSalary.invoke(employee, 1.0, 2));
System.out.println("Return type of method increaseSalary = " + increaseSalary.getReturnType()
+ System.lineSeparator() + ", parameter type: " + Arrays.toString(increaseSalary.getParameterTypes()));

Method setSalary = employeeClass.getMethod("setSalary", double.class);
System.out.println("Set Salary: " + setSalary.invoke(employee, 1.0));
System.out.println("Return type of method setSalary = " + increaseSalary.getReturnType()
+ System.lineSeparator() + ", parameter type: " + Arrays.toString(increaseSalary.getParameterTypes()));

System.out.println("****************");
Method[] methodsEmployee = employeeClass.getMethods();
for (Method method : methodsEmployee) {
System.out.println("Name: of method " + method.getName() + ", Return type: " + method.getReturnType()
+ ", Parameter type: " + Arrays.toString(method.getParameterTypes()));
}

System.out.println("****************");
Method[] methodsDeclaredEmployee = employeeClass.getDeclaredMethods();
for (Method method : methodsDeclaredEmployee) {
System.out.println("Name: of method " + method.getName() + ", Return type: " + method.getReturnType()
+ ", Parameter type: " + Arrays.toString(method.getParameterTypes()));
}

System.out.println("****************");
Method[] methodsDeclaredEmployee2 = employeeClass.getDeclaredMethods();
for (Method method : methodsDeclaredEmployee2) {
if (Modifier.isPublic(method.getModifiers())) {
System.out.println("Name: of method " + method.getName() + ", Return type: " + method.getReturnType()
+ ", Parameter type: " + Arrays.toString(method.getParameterTypes()));
}
}

System.out.println("****************");
Constructor<?> constructorEmployee = employeeClass.getConstructor();
System.out.println("Constructor has = " + constructorEmployee.getParameterCount()
+ System.lineSeparator() + " parameters, their types are: "
+ Arrays.toString(constructorEmployee.getParameterTypes()));

System.out.println("****************");
Constructor<?> constructorEmployeeWithParameter = employeeClass.getConstructor(int.class, String.class, String.class);
System.out.println("Constructor has = " + constructorEmployeeWithParameter.getParameterCount()
+ System.lineSeparator() + " parameters, their types are: "
+ Arrays.toString(constructorEmployeeWithParameter.getParameterTypes()));

System.out.println("****************");
Constructor<?>[] constructorEmployeeWithAllParameter = employeeClass.getConstructors();
for (Constructor<?> constructor : constructorEmployeeWithAllParameter) {
System.out.println("Name of constructor " + constructor.getName()
+ ", and has " + constructor.getParameterCount() + System.lineSeparator()
+ " parameters, their types are: " + Arrays.toString(constructor.getParameterTypes()));
}
} catch (Exception e) {
throw new Exception(e);
}
}
}

0 comments on commit b963039

Please sign in to comment.