java 实例化工具

我们在java反射实例化的一个类的实例的时候采用 Class.newInstance() 的时候,这个类必须包含一个无参的构造方法,这个在我们实现一些java的框架,比如 RPC 等功能的时候存在限制。

偶尔发现类一个这个项目,大体介绍如下:

项目地址:http://objenesis.org/

When would you want this?

Java already supports this dynamic instantiation of classes using Class.newInstance(). However, this only works if the class has an appropriate constructor. There are many times when a class cannot be instantiated this way, such as when the class contains:

  • Constructors that require arguments.
  • Constructors that have side effects.
  • Constructors that throw exceptions.

As a result, it is common to see restrictions in libraries stating that classes must require a default constructor. Objenesis aims to overcome these restrictions by bypassing the constructor on object instantiation.

Typical uses

Needing to instantiate an object without calling the constructor is a fairly specialized task, however there are certain cases when this is useful:

  • Serialization, Remoting and Persistence - Objects need to be instantiated and restored to a specific state, without invoking code.
  • Proxies, AOP Libraries and Mock Objects - Classes can be subclassed without needing to worry about the super() constructor.
  • Container Frameworks - Objects can be dynamically instantatiated in non-standard ways.

使用:

maven 依赖:

<dependency>
    <groupId>org.objenesis</groupId>
    <artifactId>objenesis</artifactId>
    <version>2.1</version>
</dependency>

Step By Step

There are many different strategies that Objenesis uses for instantiating objects based on the JVM vendor, JVM version, SecurityManager and type of class being instantiated.

We have defined that two different kinds of instantiation are required:

  • Stardard - No constructor will be called
  • Serializable compliant - Acts like an object instantiated by java standard serialization. It means that the constructor of the first non-serializable parent class will be called. However, readResolve is not called and we never check if the object is serializable.

The simplest way to use Objenesis is by using ObjenesisStd (Standard) and ObjenesisSerializer (Serializable compliant). By default, automatically determines the best strategy - so you don‘t have to.

Objenesis objenesis = new ObjenesisStd(); // or ObjenesisSerializer

Once you have the Objenesis implementation, you can then create an ObjectInstantiator, for a specific type.

ObjectInstantiator thingyInstantiator = objenesis.getInstantiatorOf(MyThingy.class);

Finally, you can use this to instantiate new instances of this type.

MyThingy thingy1 = (MyThingy)thingyInstantiator.newInstance();
MyThingy thingy2 = (MyThingy)thingyInstantiator.newInstance();
MyThingy thingy3 = (MyThingy)thingyInstantiator.newInstance();
时间: 2024-08-27 19:59:09

java 实例化工具的相关文章

java学习之常用Java Profiling工具的分析与比较

在 Java 程序的开发过程中,不可避免地会遇到内存使用.性能瓶颈等问题.Java Profiler 工具能帮助开发人员快速.有效地定位这些问题,因此成为了 Java 开发过程中的一个重要工具.目前市场上的 Java Profiler 工具种类繁多,本文将对目前比较常见的几种工具进行简要介绍,并从功能.性能等角度作比较,从而帮助 Java 程序员选择合适的 Java Profiler 工具. 本文主要分为三个部分:第一部分简要介绍 Java Profiler 工具的原理:第二部分对目前常见的 J

java 常用工具

System:类中的属性方法都是静态的.无法实例化 err:"标准"错误输出流 in:"标准"输入流 out:"标准"输出流 常见方法: long currentTimeMillis();获取当前时间的毫秒值 Properties    getProperties(); 获取系统属性 Properties集合中存储的都是String类型的键和值. Runtime:没有构造方法摘要,说明该类不可以创建对象. 方法又是非静态,说明该类提供静态返回该类

Java Properties工具类详解

1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠定了Properties类的本质其实是一个HashTable,那么对于各种工具类的典型特性就是在HashTable的基础之上做各种封装,以降低操作的难度.说白了,本质上还是key = value结构,只不过key和value都是字符串而已.可以理解成为一个简化版的Map<String, String&g

Java 数组工具类排序,最大值最小值等

public class ArrayUtils{ /** * 返回数组最大值 * * @param a * @return */ public static int max(int[] a){ // 返回数组最大值 int x; int aa[]=new int[a.length]; System.arraycopy(a,0,aa,0,a.length); x=aa[0]; for(int i=1;i<aa.length;i++){ if(aa[i]>x){ x=aa[i]; } } retu

JAVA GUI 工具

Java GUI图形界面开发工具 上大学那会儿比较主流的Java图形开发插件是:Visual Editor 和 SWT Designer, 不久又出了个Jigloo, 但去官网看了下发现这个东西也很久没有更新了,不过据说短小精悍,五脏俱全.SWT Designer不久前也被Google收购后重新整合进自己的产品中,然后开源了,现在名字叫:WindowBuilder Pro,看上去界面组件很丰富强大的样子,支持Swing, AWT, SWT, 以及Google自家的GWT等等. 目前JAVA图形界

每个Java程序员需要了解的8个Java开发工具

Java是计算机应用程序编程语言,被广泛用于创建Web应用.服务器处理.用户端的API开发乃至数据库等多个领域.下面列出了8个有助于你开发Java应用程序最好的Java工具. 1. SparkJava 这是一款体形轻巧,功能强大的Web应用框架.Spark拥有简单而直接的方法.您可以通过一些繁琐的XML配置来使用Spark框架编写应用程序.Spark能让你花费最少的精力来开发Java的Web应用程序. 官方网站:http://sparkjava.com/ 2. jClarity jClarity

Java开发工具

每一个程序员都有一个自己的开发工具,使用得心应手,好的工具能做到事半功倍效果.但人与人不同,喜好不同,使用的工具也不同.就像侠客需要武器,十八般兵器,各有所长,每个侠客都有属于自己的兵器.下面介绍下IT界的兵器们: JDK (Java Development Kit)Java开发工具集 .Eclipse.MyEclipse.UltraEdit.Java Workshop.NetBeans 与Sun Java Studio 5.Borland 的JBuilder.Oracle 的JDevelope

java流工具类使用很方便

package com.auto.generate.utils ; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; /** * File Name: StreamTool.jav

java常用工具类(java技术交流群57388149)

package com.itjh.javaUtil; import java.util.ArrayList; import java.util.List; /** * * String工具类. <br> * * @author 宋立君 * @date 2014年06月24日 */ public class StringUtil { private static final int INDEX_NOT_FOUND = -1; private static final String EMPTY =