`
niwtsew
  • 浏览: 71951 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

TestReflectionGetFields

阅读更多

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.

Class Description
Array Provides static methods to dynamically create and access arrays.
Class Represents, or reflects, classes and interfaces.
Constructor Provides information about, and access to, a constructor for a class.
Allows you to instantiate a class dynamically.
Field  (in the API reference documentation) Provides information about, and dynamic access to, a field of a class
or an interface.
Method  (in the API reference documentation) Provides information about, and access to, a single method on a class
or interface. Allows you to invoke the method dynamically.

 

 

 

 

 

 

 

 

 

 

 

 

 

Modifier  (in the API reference documentation) 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.
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics