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.

ClassDescription
Array 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值