BAEL-4587: Update Guide to Java Reflection (#9997)

* BAEL-4587: Migrate Guide to Java Reflection to core-java-11-2

* BAEL-4587: Replace deprecated isAccessible method with canAccess
This commit is contained in:
kwoyke 2020-09-12 19:08:22 +02:00 committed by GitHub
parent faa858e766
commit 972ff96699
15 changed files with 46 additions and 54 deletions

View File

@ -4,5 +4,5 @@ This module contains articles about Java 11 core features
### Relevant articles
- [Guide to Java 8 Optional](https://www.baeldung.com/java-optional)
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
- [Guide to Java 8s Collectors](https://www.baeldung.com/java-8-collectors)

View File

@ -1,4 +1,4 @@
package com.baeldung.java.reflection;
package com.baeldung.reflection;
public abstract class Animal implements Eating {

View File

@ -1,4 +1,4 @@
package com.baeldung.java.reflection;
package com.baeldung.reflection;
public class Bird extends Animal {
private boolean walks;

View File

@ -1,4 +1,4 @@
package com.baeldung.java.reflection;
package com.baeldung.reflection;
import java.lang.annotation.Annotation;

View File

@ -1,4 +1,4 @@
package com.baeldung.java.reflection;
package com.baeldung.reflection;
public interface Eating {
String eats();

View File

@ -1,4 +1,4 @@
package com.baeldung.java.reflection;
package com.baeldung.reflection;
public class Goat extends Animal implements Locomotion {

View File

@ -1,4 +1,4 @@
package com.baeldung.java.reflection;
package com.baeldung.reflection;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

View File

@ -1,4 +1,4 @@
package com.baeldung.java.reflection;
package com.baeldung.reflection;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

View File

@ -1,4 +1,4 @@
package com.baeldung.java.reflection;
package com.baeldung.reflection;
@Greeter(greet="Good morning")
public class Greetings {

View File

@ -1,4 +1,4 @@
package com.baeldung.java.reflection;
package com.baeldung.reflection;
public interface Locomotion {
String getLocomotion();

View File

@ -1,4 +1,4 @@
package com.baeldung.java.reflection;
package com.baeldung.reflection;
public class Person {
private String name;

View File

@ -1,4 +1,4 @@
package com.baeldung.java.reflection;
package com.baeldung.reflection;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertFalse;

View File

@ -1,4 +1,4 @@
package com.baeldung.java.reflection;
package com.baeldung.reflection;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -32,23 +32,23 @@ public class ReflectionUnitTest {
final Class<?> clazz = goat.getClass();
assertEquals("Goat", clazz.getSimpleName());
assertEquals("com.baeldung.java.reflection.Goat", clazz.getName());
assertEquals("com.baeldung.java.reflection.Goat", clazz.getCanonicalName());
assertEquals("com.baeldung.reflection.Goat", clazz.getName());
assertEquals("com.baeldung.reflection.Goat", clazz.getCanonicalName());
}
@Test
public void givenClassName_whenCreatesObject_thenCorrect() throws ClassNotFoundException {
final Class<?> clazz = Class.forName("com.baeldung.java.reflection.Goat");
final Class<?> clazz = Class.forName("com.baeldung.reflection.Goat");
assertEquals("Goat", clazz.getSimpleName());
assertEquals("com.baeldung.java.reflection.Goat", clazz.getName());
assertEquals("com.baeldung.java.reflection.Goat", clazz.getCanonicalName());
assertEquals("com.baeldung.reflection.Goat", clazz.getName());
assertEquals("com.baeldung.reflection.Goat", clazz.getCanonicalName());
}
@Test
public void givenClass_whenRecognisesModifiers_thenCorrect() throws ClassNotFoundException {
final Class<?> goatClass = Class.forName("com.baeldung.java.reflection.Goat");
final Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
final Class<?> goatClass = Class.forName("com.baeldung.reflection.Goat");
final Class<?> animalClass = Class.forName("com.baeldung.reflection.Animal");
final int goatMods = goatClass.getModifiers();
final int animalMods = animalClass.getModifiers();
@ -63,7 +63,7 @@ public class ReflectionUnitTest {
final Class<?> goatClass = goat.getClass();
final Package pkg = goatClass.getPackage();
assertEquals("com.baeldung.java.reflection", pkg.getName());
assertEquals("com.baeldung.reflection", pkg.getName());
}
@Test
@ -81,8 +81,8 @@ public class ReflectionUnitTest {
@Test
public void givenClass_whenGetsImplementedInterfaces_thenCorrect() throws ClassNotFoundException {
final Class<?> goatClass = Class.forName("com.baeldung.java.reflection.Goat");
final Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
final Class<?> goatClass = Class.forName("com.baeldung.reflection.Goat");
final Class<?> animalClass = Class.forName("com.baeldung.reflection.Animal");
final Class<?>[] goatInterfaces = goatClass.getInterfaces();
final Class<?>[] animalInterfaces = animalClass.getInterfaces();
@ -94,16 +94,16 @@ public class ReflectionUnitTest {
@Test
public void givenClass_whenGetsConstructor_thenCorrect() throws ClassNotFoundException {
final Class<?> goatClass = Class.forName("com.baeldung.java.reflection.Goat");
final Class<?> goatClass = Class.forName("com.baeldung.reflection.Goat");
final Constructor<?>[] constructors = goatClass.getConstructors();
assertEquals(1, constructors.length);
assertEquals("com.baeldung.java.reflection.Goat", constructors[0].getName());
assertEquals("com.baeldung.reflection.Goat", constructors[0].getName());
}
@Test
public void givenClass_whenGetsFields_thenCorrect() throws ClassNotFoundException {
final Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
final Class<?> animalClass = Class.forName("com.baeldung.reflection.Animal");
final Field[] fields = animalClass.getDeclaredFields();
final List<String> actualFields = getFieldNames(fields);
@ -114,7 +114,7 @@ public class ReflectionUnitTest {
@Test
public void givenClass_whenGetsMethods_thenCorrect() throws ClassNotFoundException {
final Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
final Class<?> animalClass = Class.forName("com.baeldung.reflection.Animal");
final Method[] methods = animalClass.getDeclaredMethods();
final List<String> actualMethods = getMethodNames(methods);
@ -124,7 +124,7 @@ public class ReflectionUnitTest {
@Test
public void givenClass_whenGetsAllConstructors_thenCorrect() throws ClassNotFoundException {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
final Constructor<?>[] constructors = birdClass.getConstructors();
assertEquals(3, constructors.length);
@ -132,7 +132,7 @@ public class ReflectionUnitTest {
@Test
public void givenClass_whenGetsEachConstructorByParamTypes_thenCorrect() throws Exception {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
birdClass.getConstructor();
birdClass.getConstructor(String.class);
birdClass.getConstructor(String.class, boolean.class);
@ -140,7 +140,7 @@ public class ReflectionUnitTest {
@Test
public void givenClass_whenInstantiatesObjectsAtRuntime_thenCorrect() throws Exception {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
final Constructor<?> cons1 = birdClass.getConstructor();
final Constructor<?> cons2 = birdClass.getConstructor(String.class);
@ -159,7 +159,7 @@ public class ReflectionUnitTest {
@Test
public void givenClass_whenGetsPublicFields_thenCorrect() throws ClassNotFoundException {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
final Field[] fields = birdClass.getFields();
assertEquals(1, fields.length);
assertEquals("CATEGORY", fields[0].getName());
@ -168,7 +168,7 @@ public class ReflectionUnitTest {
@Test
public void givenClass_whenGetsPublicFieldByName_thenCorrect() throws Exception {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
final Field field = birdClass.getField("CATEGORY");
assertEquals("CATEGORY", field.getName());
@ -176,7 +176,7 @@ public class ReflectionUnitTest {
@Test
public void givenClass_whenGetsDeclaredFields_thenCorrect() throws ClassNotFoundException {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
final Field[] fields = birdClass.getDeclaredFields();
assertEquals(1, fields.length);
assertEquals("walks", fields[0].getName());
@ -184,7 +184,7 @@ public class ReflectionUnitTest {
@Test
public void givenClass_whenGetsFieldsByName_thenCorrect() throws Exception {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
final Field field = birdClass.getDeclaredField("walks");
assertEquals("walks", field.getName());
@ -192,14 +192,14 @@ public class ReflectionUnitTest {
@Test
public void givenClassField_whenGetsType_thenCorrect() throws Exception {
final Field field = Class.forName("com.baeldung.java.reflection.Bird").getDeclaredField("walks");
final Field field = Class.forName("com.baeldung.reflection.Bird").getDeclaredField("walks");
final Class<?> fieldClass = field.getType();
assertEquals("boolean", fieldClass.getSimpleName());
}
@Test
public void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
final Bird bird = (Bird) birdClass.getConstructor().newInstance();
final Field field = birdClass.getDeclaredField("walks");
field.setAccessible(true);
@ -216,7 +216,7 @@ public class ReflectionUnitTest {
@Test
public void givenClassField_whenGetsAndSetsWithNull_thenCorrect() throws Exception {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
final Field field = birdClass.getField("CATEGORY");
field.setAccessible(true);
@ -225,7 +225,7 @@ public class ReflectionUnitTest {
@Test
public void givenClass_whenGetsAllPublicMethods_thenCorrect() throws ClassNotFoundException {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
final Method[] methods = birdClass.getMethods();
final List<String> methodNames = getMethodNames(methods);
@ -235,7 +235,7 @@ public class ReflectionUnitTest {
@Test
public void givenClass_whenGetsOnlyDeclaredMethods_thenCorrect() throws ClassNotFoundException {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
final List<String> actualMethodNames = getMethodNames(birdClass.getDeclaredMethods());
final List<String> expectedMethodNames = Arrays.asList("setWalks", "walks", "getSound", "eats");
@ -248,24 +248,17 @@ public class ReflectionUnitTest {
@Test
public void givenMethodName_whenGetsMethod_thenCorrect() throws Exception {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Method walksMethod = birdClass.getDeclaredMethod("walks");
final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class);
assertFalse(walksMethod.isAccessible());
assertFalse(setWalksMethod.isAccessible());
walksMethod.setAccessible(true);
setWalksMethod.setAccessible(true);
assertTrue(walksMethod.isAccessible());
assertTrue(setWalksMethod.isAccessible());
final Bird bird = new Bird();
final Method walksMethod = bird.getClass().getDeclaredMethod("walks");
final Method setWalksMethod = bird.getClass().getDeclaredMethod("setWalks", boolean.class);
assertTrue(walksMethod.canAccess(bird));
assertTrue(setWalksMethod.canAccess(bird));
}
@Test
public void givenMethod_whenInvokes_thenCorrect() throws Exception {
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
final Bird bird = (Bird) birdClass.getConstructor().newInstance();
final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class);
final Method walksMethod = birdClass.getDeclaredMethod("walks");

View File

@ -3,7 +3,6 @@
- [Void Type in Java](https://www.baeldung.com/java-void-type)
- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields)
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
- [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params)
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)