Skip to content

Commit f41862d

Browse files
author
coursar
committed
feat(inheritance): reflection sample added
1 parent cda5940 commit f41862d

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ru.netology</groupId>
8+
<artifactId>reflection-sample</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.junit.jupiter</groupId>
20+
<artifactId>junit-jupiter</artifactId>
21+
<version>5.4.2</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-surefire-plugin</artifactId>
31+
<version>2.22.2</version>
32+
</plugin>
33+
</plugins>
34+
</build>
35+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ru.netology;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
public class DemoTest {
6+
@Test
7+
public void shouldBeCalled() {
8+
System.out.println("Test method called");
9+
}
10+
11+
public void shouldNotBeCalled() {
12+
System.out.println("Invalid method called");
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.netology;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.lang.reflect.InvocationTargetException;
6+
import java.lang.reflect.Method;
7+
8+
public class Launcher {
9+
// что такое исключения мы разберём на следующей лекции
10+
public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException {
11+
// создаём объект типа `Class` (generic'и мы пока не знаем, поэтому и так "сойдёт")
12+
Class clazz = DemoTest.class;
13+
// берём все методы класса
14+
Method[] methods = clazz.getDeclaredMethods();
15+
// перебираем методы
16+
for (Method method : methods) {
17+
// смотрим, есть ли над методом аннотация @Test
18+
if (method.isAnnotationPresent(Test.class)) {
19+
// создаём объект класса (newInstance помечен аннотацией @Deprecated, но для простоты мы будем использовать его, в противном случае, нужно выбирать конструкторы)
20+
Object object = clazz.newInstance();
21+
// вызываем метод на объекте
22+
method.invoke(object);
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)