- 浏览: 3502871 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
wanglf1207:
EJB的确是个不错的产品,只是因为用起来有点门槛,招来太多人吐 ...
weblogic-ejb-jar.xml的元素解析 -
qwfys200:
总结的不错。
Spring Web Flow 2.0 入门 -
u011577913:
u011577913 写道也能给我发一份翻译文档? 邮件437 ...
Hazelcast 参考文档-4 -
u011577913:
也能给我发一份翻译文档?
Hazelcast 参考文档-4 -
songzj001:
DbUnit入门实战
from:http://tutorials.jenkov.com/java-reflection/index.html
Despite the common belief it is actually possible to access private fields and methods of other classes via Java Reflection. It is not even that difficult. This can be very handy during unit testing. This text will show you how.
Note: This only works when running the code as a standalone Java application, like you do with unit tests and regular applications. If you try to do this inside a Java Applet, you will need to fiddle around with the SecurityManager. But, since that is not something you need to do very often, it is left out of this text so far.
Here is a list of the topics covered in this text:
Accessing Private Fields
To access a private field you will need to call the Class.getDeclaredField(String name)
or Class.getDeclaredFields()
method. The methods Class.getField(String name)
and Class.getFields()
methods only return public fields, so they won't work.
Here is a simple example of a class with a private field, and below that the code to access that
field via Java Reflection:
public class PrivateObject { private String privateString = null; public PrivateObject(String privateString) { this.privateString = privateString; } }
PrivateObject privateObject = new PrivateObject("The Private Value"); Field privateStringField = PrivateObject.class. getDeclaredField("privateString"); privateStringField.setAccessible(true); String fieldValue = (String) privateStringField.get(privateObject); System.out.println("fieldValue = " + fieldValue);
This code example will print out the text "fieldValue = The Private Value", which
is the value of the private field privateString
of the PrivateObject
instance created at the beginning of the code sample.
Notice the use of the method PrivateObject.class.getDeclaredField("privateString")
.
It is this method call that returns the private field. This method only returns fields declared
in that particular class, not fields declared in any superclasses.
Notice the line in bold too. By calling Field.setAcessible(true)
you turn
off the access checks for this particular Field
instance, for reflection only.
Now you can access it even if it is private, protected or package scope, even if
the caller is not part of those scopes.
You still can't access the field using normal code. The compiler won't allow it.
Accessing Private Methods
To access a private method you will need to call the Class.getDeclaredMethod(String name, Class[] parameterTypes)
or Class.getDeclaredMethods()
method. The methods Class.getMethod(String name, Class[] parameterTypes)
and Class.getMethods()
methods only return public methods, so they won't work.
Here is a simple example of a class with a private method, and below that the code to access that
method via Java Reflection:
public class PrivateObject { private String privateString = null; public PrivateObject(String privateString) { this.privateString = privateString; } private String getPrivateString(){ return this.privateString; } }
PrivateObject privateObject = new PrivateObject("The Private Value"); Method privateStringMethod = PrivateObject.class. getDeclaredMethod("getPrivateString", null); privateStringMethod.setAccessible(true); String returnValue = (String) privateStringMethod.invoke(privateObject, null); System.out.println("returnValue = " + returnValue);
This code example will print out the text "returnValue = The Private Value", which
is the value returned by the method getPrivateString()
when invoked on
the PrivateObject
instance created at the beginning of the code sample.
Notice the use of the method PrivateObject.class.getDeclaredMethod("privateString")
.
It is this method call that returns the private method. This method only returns methods declared
in that particular class, not methods declared in any superclasses.
Notice the line in bold too. By calling Method.setAcessible(true)
you turn
off the access checks for this particular Method
instance, for reflection only.
Now you can access it even if it is private, protected or package scope, even if
the caller is not part of those scopes.
You still can't access the method using normal code. The compiler won't allow it.
发表评论
-
字符串分割--java中String.split()用法
2013-03-06 14:25 74151在java.lang包中有String.sp ... -
用 HttpServletResponseWrapper 实现 Etag 过滤器
2012-07-09 16:58 3759原文出处:http://blog.chenlb.com/200 ... -
Fitnesse使用
2012-05-05 13:27 23494Fitnesse 的使用 一,介绍 Fitnesse是一种 ... -
Customizing the new FitNesse parser
2012-05-05 13:13 2134FitNesse began its life using ... -
java application中内嵌ActiveX控件
2011-11-14 15:57 5524我这里用的是SWT/JFace开发application,SW ... -
Google Java Developer Tools Downloads
2011-08-09 00:04 2346WindowBuilder Pro原来叫WindowB ... -
Jalita
2011-08-06 00:49 1565Jalita (Java light terminal ada ... -
【转】用Java写字符终端界面
2011-07-29 13:13 2121终端界面GUI开源项目charva。 这个框架让你可以用开发 ... -
[转]mybatis下的分页,支持所有的数据库
2011-07-21 13:21 14841大 家都知道,mybatis的自带分页方法只是逻 ... -
Java framework for text- & console-based forms?
2011-07-21 01:06 1711charva jcurses JNA , ... -
JNA(Java Native Access)学习入门
2011-07-21 01:04 22625Java Native Access 项目 在 ... -
JAVA上加密算法的实现用例
2011-06-25 12:38 4884来源:www.ibm.com ... -
如何将GlassFish作为Windows服务运行
2011-05-18 23:21 2375本文档来自GlassFish官方网站,详细介绍了将 G ... -
JAVA UDP打洞必备知识点---NAT
2011-05-05 12:56 8699一、引言 RFCl631 ... -
Keystore概念,Keytool工具使用
2011-04-28 16:20 2906近来由于项目需要做Single Sign On, 研究了一 ... -
利用Eclipse Profile Plugin监控分析Tomcat性能
2011-04-18 16:14 3702目前新版本的Eclipse在启动应用服务器的时候有一个新的选 ... -
m2eclipse: Eclipse is running in a JRE, but a JDK is required
2011-02-04 23:43 2542Eclipse 安装了Maven插件,启动Eclipse ... -
利用JNative实现Java调用动态库
2010-10-18 00:43 2100由于项目要求,需要用J ... -
RHEL5支持大内存
2010-10-08 16:19 3005安装 RHEL 5 ,硬件为 4G 内存,安装完成 ... -
Windows Server 2003 和 Windows 2000 提供大内存支持
2010-10-08 16:19 1854本文介绍物理地址扩展 ...
相关推荐
Item 16: In public classes, use accessor methods, not public fields Item 17: Minimize mutability Item 18: Favor composition over inheritance Item 19: Design and document for inheritance or else ...
在Java编程语言中,反射(Reflection)是一项强大的特性,它允许程序在运行时动态地获取类的信息,并能够直接操作这些信息。通过反射,我们可以获取类的字段、方法等元数据,甚至可以创建实例并调用其方法。本文将...
System.out.println("Methods:"); for (Method method : methods) { System.out.println(method.getName()); } } } class MyClass { private int myNumber; public void setMyNumber(int number) { my...
### Java反射机制详解 #### 引言 Java反射机制,作为Java语言动态特性的核心,赋予了Java在运行时分析和操作类的能力。这不仅增强了Java的灵活性,还使其成为处理元数据、进行代码生成、执行框架开发以及实现依赖...
Java 中 DAO 层反射使用 Java 中的 DAO 层是数据访问对象的缩写,主要负责数据的访问和操作。在 Java 中,反射机制是实现 DAO 层的关键技术之一。本文将对 Java 中 DAO 层反射使用进行详细的介绍。 反射机制 Java...
类是创建对象的蓝图,包含属性(Fields)和方法(Methods)。 - 包(Package):组织类的容器,提供命名空间管理,避免类名冲突。 - 接口(Interface):定义了一组抽象方法,是多继承的实现方式,用于定义行为...
5. **类的成员**:包括属性(fields)和方法(methods),了解如何声明、初始化和访问它们。 6. **访问修饰符**:如public、private、protected和默认(package-private),理解它们对类和成员的可见性影响。 7. *...
在Java中,每一个类都被编译成.class文件,这些文件包含了类的所有信息,包括字段(fields)、方法(methods)和构造器(constructors)。反射API提供了一组类和接口,如Class、Constructor、Method和Field,它们...
类的成员包括属性(Fields)和方法(Methods),属性用于存储数据,方法用于执行操作。属性可以有默认的初始化值,也可以通过构造器来设置。方法则包含了一段可执行的代码,可以操作和修改类的属性。此外,类的成员...
Sealed Classes and Methods 115 Constructors of Derived Classes 116 Modifiers 122 Visibility Modifiers 122 Other Modifiers 123 Interfaces 123 Defining and Implementing Interfaces 125 Derived Interfaces...
类是对象的模板,它描述了对象的属性(fields)和行为(methods)。理解如何定义类、创建对象以及如何通过对象进行交互是学习OOP的基础。 2. **封装**:封装是将数据和操作这些数据的方法绑定在一起的过程,以防止...
- **元数据和反射**(Metadata and Reflection):允许程序在运行时检查自身和其它程序集的信息。 - **多线程**(Multithreading):C#提供对多线程的支持,允许同时执行多个任务。 - **.NET Framework和.NET ...
4. 查找特殊成员:对于每个类型,可以使用Type对象的方法来查找特殊成员,如GetFields、GetMethods、GetProperties等,这些方法接受BindingFlags参数来指定搜索范围,如IsPrivate、IsProtected、IsInternal等。...
它们可以是私有的(private),仅在类内部访问,也可以是公共的(public),允许外部代码直接访问。此外,还有受保护的(protected)字段,仅允许子类访问,以及内部的(internal)字段,限于同一命名空间内的代码...