Merge branch 'master' into JAVA-7244-Review_log_statements_for_projects

This commit is contained in:
Loredana Crusoveanu
2021-12-09 10:22:41 +02:00
committed by GitHub
1079 changed files with 12491 additions and 4924 deletions

View File

@@ -6,3 +6,4 @@
- [Checking if a Java Class is abstract Using Reflection](https://www.baeldung.com/java-reflection-is-class-abstract)
- [Invoking a Private Method in Java](https://www.baeldung.com/java-call-private-method)
- [Finding All Classes in a Java Package](https://www.baeldung.com/java-find-all-classes-in-package)
- [Invoke a Static Method Using Java Reflection API](https://www.baeldung.com/java-invoke-static-method-reflection)

View File

@@ -24,12 +24,12 @@
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.12</version>
<version>${reflections.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
<version>${guava.version}</version>
</dependency>
</dependencies>
@@ -56,7 +56,8 @@
</build>
<properties>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<reflections.version>0.9.12</reflections.version>
<guava.version>30.1.1-jre</guava.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>
<spring.version>5.3.4</spring.version>

View File

@@ -0,0 +1,12 @@
package com.baeldung.reflection.access.staticmethods;
public class GreetingAndBye {
public static String greeting(String name) {
return String.format("Hey %s, nice to meet you!", name);
}
private static String goodBye(String name) {
return String.format("Bye %s, see you next time.", name);
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.reflection.check.abstractclass;
import com.baeldung.reflection.access.staticmethods.GreetingAndBye;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class GreetingAndByeUnitTest {
@Test
void givenPublicStaticMethod_whenCallWithReflection_thenReturnExpectedResult() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<GreetingAndBye> clazz = GreetingAndBye.class;
Method method = clazz.getMethod("greeting", String.class);
Object result = method.invoke(null, "Eric");
Assertions.assertEquals("Hey Eric, nice to meet you!", result);
}
@Test
void givenPrivateStaticMethod_whenCallWithReflection_thenReturnExpectedResult() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<GreetingAndBye> clazz = GreetingAndBye.class;
Method method = clazz.getDeclaredMethod("goodBye", String.class);
method.setAccessible(true);
Object result = method.invoke(null, "Eric");
Assertions.assertEquals("Bye Eric, see you next time.", result);
}
}