![](/img/trans.png)
[英]java.lang.InstantiationError while running sparql query in jena
[英]java.lang.InstantiationError when creating instance of static inner class with Objenesis
我正在嘗試創建一個實用程序方法,該方法應該能夠深克隆任何對象。 ( Object.clone()
僅在實現Cloneable
對象上起作用,我聽說它還是有缺陷的。)
我正在使用Objenesis來創建對象的新實例,而無需使用構造函數。
但是,當嘗試克隆JFrame時,出現以下異常:
(使用該類,因為我認為這應該是一個很好且復雜的測試)
java.lang.InstantiationError: [Ljava.util.concurrent.ConcurrentHashMap$Node;
at sun.reflect.GeneratedSerializationConstructorAccessor12.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
我對任何解決方案持開放態度,不一定僅限於Objenesis。
我的代碼:
private static ObjenesisStd OBJENESIS = new ObjenesisStd();
@SuppressWarnings("unchecked")
public static <T> T clone(T object, boolean deep){
if(object == null){
return null;
}else{
try {
T clone = (T) OBJENESIS.newInstance(object.getClass());
List<Field> fields = ReflectionUtil.getAllFieldsInHierarchy(object.getClass());
for(Field field : fields){
boolean isAccessible = field.isAccessible();
boolean isFinal = ReflectionUtil.isFinal(field);
field.setAccessible(true);
ReflectionUtil.setFinal(field, false);
Class<?> type = field.getType();
if(!deep || type.isPrimitive() || type == String.class){
field.set(clone, field.get(object));
}else{
field.set(clone, clone(field.get(object), true));
}
field.setAccessible(isAccessible);
ReflectionUtil.setFinal(field, isFinal);
}
return clone;
} catch (Throwable e) {
e.printStackTrace();
//throw new RuntimeException("Failed to clone object of type " + object.getClass(), e);
return null;
}
}
}
public static void main(String[] args) {
GetterSetterAccess access = new GetterSetterAccess(JFrame.class);
JFrame frame = new JFrame("Test Frame");
for(String attr : access.getAttributes()){
System.out.println(attr + " " + access.getValue(frame, attr));
}
System.out.println("----------------------------------------------");
frame = clone(frame, true);
for(String attr : access.getAttributes()){
System.out.println(attr + " " + access.getValue(frame, attr));
}
}
編輯:得到了它與接受的答案和一些其他修復一起使用:
Integer.class
等) Class.class
類的對象) ==
)的Map,而不是使用equals()
。 我終於弄明白了。 您的代碼無法處理數組。 因此,由於實例化“ [Ljava.util.concurrent.ConcurrentHashMap $ Node;”而失敗。 這是一個節點數組。
但是,我會主張,實際上,您不應該這樣做。 您將得到相當復雜的代碼。 根據您要執行的操作,可以使用Jackson或XStream進行編組/解組來執行復制。
如果您確實想繼續該路徑,則在對clone
方法進行空檢查之后,您將需要類似這樣的內容。
if(object.getClass().isArray()) {
int length = Array.getLength(object);
Object array = Array.newInstance(object.getClass().getComponentType(), length);
for (int i = 0; i < length; i++) {
Array.set(array, i, clone(Array.get(object, i), true));
}
return (T) array;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.