TestReflectionGetFields

xiaoxiao2023-03-19  37

import junit.framework.TestCase; import junit.framework.Assert; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lang.reflect.Constructor; public class TestReflectionGetFields extends TestCase {     public void testGetFieldsWillReturnOnlyPublicField ()     {         Field[] fields = TestSubClass.class.getFields();         for (Field field : fields)         {             Assert.assertTrue(Modifier.isPublic(field.getModifiers()));         }     }         public void testGetFieldsWillContainPublicFieldsInheriedFromSuper ()     {         Field[] fields = TestSubClass.class.getFields();         for (Field field : fields)         {             if(!TestSubClass.class.getName().equals(field.getDeclaringClass()))             {                 Assert.assertTrue(true);                 return;             }         }         Assert.fail("public field inherited from super class/interface should return ,but not");     }         public void testGetDeclaredFieldsWillReturnFieldsDefinedInThisClassWithoutThoseInheritedFromSuper ()     {         Field[] fields = TestSubClass.class.getDeclaredFields();         for (Field field : fields)         {             Assert.assertEquals(TestSubClass.class.getName(), field.getDeclaringClass().getName());         }     }         public void testGetDeclaredFieldsWillContainPrivateField ()     {         Field[] fields = TestSubClass.class.getDeclaredFields();         for (Field field : fields)         {             if (Modifier.isPrivate(field.getModifiers()))             {                 Assert.assertTrue("getDeclaredFields() will return private field of "+ field.getName(),true);                 return;             }         }         Assert.fail("private method should return,but not");     }         private static interface TestSubInterface extends TestParentInterface     {         String NAME = "NameOfTestSubInterface";         void methodOfTestSubInterface();     }         private static interface TestParentInterface     {         String NAME = "NameOfTestParentInterface";         void methodOfTestParentInterface();     }         private abstract static class TestParentClass implements TestParentInterface,TestSubInterface     {         private String privateFieldOfTestParentClass;         public  String publicFieldOfTestParentClass;         protected String protectedFieldOfTestParentClass;         String    fieldOfDefaultModifierOfTestParentClass;         public void publicMethodOfTestParentClass(){}         protected void protectedMethodOfTestParentClass(){}         private void privateMethodOfTestParentClass(){}         void defaultModifierMethodOfTestParentClass(){}     }         private  static class TestSubClass extends TestParentClass     {         private String privateFieldOfTestSubClass;         public  String publicFieldOfTestSubClass;         protected String protectedFieldOfTestSubClass;         String    fieldOfDefaultModifierOfTestParentClass;                 public TestSubClass(){};         public TestSubClass(boolean dummy){};         private TestSubClass(String dummy){};         TestSubClass(int dummy){};         protected TestSubClass(long dummy){};         public void publicMethodOfTestParentClass(){}         protected void protectedMethodOfTestParentClass(){}         private void privateMethodOfTestParentClass(){}         void defaultModifierMethodOfTestParentClass(){}         public void methodOfTestSubInterface(){};         public void methodOfTestParentInterface(){};     } } The following table summarizes the classes that compose the reflection API. The Class and Object classes are in the java.lang (in the API reference documentation)package. The other classes are contained in the java.lang.reflect (in the API reference documentation)package.

ClassDescriptionArray Provides static methods to dynamically create and access arrays. Class Represents, or reflects, classes and interfaces. ConstructorProvides information about, and access to, a constructor for a class. Allows you to instantiate a class dynamically. Field Provides information about, and dynamic access to, a field of a class or an interface. Method Provides information about, and access to, a single method on a class or interface. Allows you to invoke the method dynamically.

 

 

 

 

 

 

 

 

 

 

 

 

 

Modifier Provides static methods and constants that allow you to get information about the access modifiers of a class and its members.Object Provides the getClass method. 相关资源:Java 面经手册·小傅哥(公众号:bugstack虫洞栈).pdf
转载请注明原文地址: https://www.6miu.com/read-4986621.html

最新回复(0)