Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions jme3-core/src/main/java/com/jme3/export/SavableClassUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2012 jMonkeyEngine
* Copyright (c) 2009-2019 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -36,7 +36,9 @@
import com.jme3.material.MatParamTexture;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -161,16 +163,19 @@ public static int getSavedSavableVersion(Object savable, Class<? extends Savable
* @return the Savable instance of the class.
* @throws InstantiationException thrown if the class does not have an empty constructor.
* @throws IllegalAccessException thrown if the class is not accessable.
* @throws java.lang.reflect.InvocationTargetException
* @throws ClassNotFoundException thrown if the class name is not in the classpath.
* @throws IOException when loading ctor parameters fails
*/
public static Savable fromName(String className) throws InstantiationException,
IllegalAccessException, ClassNotFoundException, IOException {

public static Savable fromName(String className)
throws ClassNotFoundException, IllegalAccessException,
InstantiationException, InvocationTargetException {
className = remapClass(className);

Constructor noArgConstructor = findNoArgConstructor(className);
noArgConstructor.setAccessible(true);
try {
return (Savable) Class.forName(className).newInstance();
} catch (InstantiationException e) {
return (Savable) noArgConstructor.newInstance();
} catch (InvocationTargetException | InstantiationException e) {
Logger.getLogger(SavableClassUtil.class.getName()).log(
Level.SEVERE, "Could not access constructor of class ''{0}" + "''! \n"
+ "Some types need to have the BinaryImporter set up in a special way. Please doublecheck the setup.", className);
Expand All @@ -184,6 +189,7 @@ public static Savable fromName(String className) throws InstantiationException,
}

public static Savable fromName(String className, List<ClassLoader> loaders) throws InstantiationException,
InvocationTargetException, NoSuchMethodException,
IllegalAccessException, ClassNotFoundException, IOException {
if (loaders == null) {
return fromName(className);
Expand All @@ -208,4 +214,25 @@ public static Savable fromName(String className, List<ClassLoader> loaders) thro

return fromName(className);
}

/**
* Use reflection to gain access to the no-arg constructor of the named
* class.
*
* @return the pre-existing constructor (not null)
*/
private static Constructor findNoArgConstructor(String className)
throws ClassNotFoundException, InstantiationException {
Class clazz = Class.forName(className);
Constructor result;
try {
result = clazz.getDeclaredConstructor();
} catch (NoSuchMethodException e) {
throw new InstantiationException(
"Loading requires a no-arg constructor, but class "
+ className + " lacks one.");
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,7 @@ public Savable readObject(int id) {

return out;

} catch (IOException e) {
logger.logp(Level.SEVERE, this.getClass().toString(), "readObject(int id)", "Exception", e);
return null;
} catch (ClassNotFoundException e) {
logger.logp(Level.SEVERE, this.getClass().toString(), "readObject(int id)", "Exception", e);
return null;
} catch (InstantiationException e) {
logger.logp(Level.SEVERE, this.getClass().toString(), "readObject(int id)", "Exception", e);
return null;
} catch (IllegalAccessException e) {
} catch (Exception e) {
logger.logp(Level.SEVERE, this.getClass().toString(), "readObject(int id)", "Exception", e);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2012 jMonkeyEngine
* Copyright (c) 2009-2019 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -38,6 +38,7 @@
import com.jme3.util.BufferUtils;
import com.jme3.util.IntMap;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
Expand Down Expand Up @@ -962,6 +963,7 @@ public Savable readSavable(String name, Savable defVal) throws IOException {

private Savable readSavableFromCurrentElem(Savable defVal) throws
InstantiationException, ClassNotFoundException,
NoSuchMethodException, InvocationTargetException,
IOException, IllegalAccessException {
Savable ret = defVal;
Savable tmp = null;
Expand Down