本文整理汇总了Java中com.badlogic.gdx.utils.reflect.Constructor.newInstance方法的典型用法代码示例。如果您正苦于以下问题:Java Constructor.newInstance方法的具体用法?Java Constructor.newInstance怎么用?Java Constructor.newInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.badlogic.gdx.utils.reflect.Constructor
的用法示例。
在下文中一共展示了Constructor.newInstance方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createObject
import com.badlogic.gdx.utils.reflect.Constructor; //导入方法依赖的package包/类
/** @param constructor will be used to construct the instance.
* @param parameterTypes will be used to extract constructor parameters from the context.
* @return an instance of the class.
* @throws RuntimeException due to reflection issues. */
protected Object createObject(final Constructor constructor, final Class<?>[] parameterTypes) {
try {
if (parameterTypes.length == 0) {
return constructor.newInstance(Providers.EMPTY_ARRAY);
}
final Object[] dependencies = new Object[parameterTypes.length];
for (int index = 0, length = dependencies.length; index < length; index++) {
dependencies[index] = get(parameterTypes[index], null, new ConstructorMember(constructor));
}
return constructor.newInstance(dependencies);
} catch (final Exception exception) {
throw new RuntimeException("Unable to create an instance of: " + constructor.getDeclaringClass(),
exception);
}
}
示例2: getSystem
import com.badlogic.gdx.utils.reflect.Constructor; //导入方法依赖的package包/类
private <ES extends EntitySystem> ES getSystem(Class<ES> clazz) {
ES entitySystem = engine.getSystem(clazz);
if (entitySystem == null) {
try {
Constructor constructor = findConstructor(clazz);
entitySystem = (ES) constructor.newInstance((Object[]) null);
} catch (Exception ex) {
Log.debug(tag, "Error instance entitySystem %s", clazz.getSimpleName());
}
if (entitySystem != null) {
engine.addSystem(entitySystem);
} else {
Log.debug(tag, "Error instance entitySystem %s", clazz.getSimpleName());
}
}
return entitySystem;
}
示例3: getBrickBuilder
import com.badlogic.gdx.utils.reflect.Constructor; //导入方法依赖的package包/类
public <B extends BrickBuilder> B getBrickBuilder(Class<B> clazzBuilder) {
B builder = (B) bricksBuilders.get(clazzBuilder);
if (builder == null) {
synchronized (clazzBuilder) {
try {
Constructor constructor = findConstructor(clazzBuilder);
builder = (B) constructor.newInstance((Object[]) null);
bricksBuilders.put(clazzBuilder, builder);
} catch (Exception e) {
Log.debug(tag, "Error instance actuatorBuilder %s" + clazzBuilder.getSimpleName());
}
}
}
return builder;
}
示例4: newValueController
import com.badlogic.gdx.utils.reflect.Constructor; //导入方法依赖的package包/类
/**
*
* @param clazz
* class of the value
* @param widget
* the widget for the value
* @param args
* arguments are interpreted as a list of objects paired in
* class, value ([class, value, class, value]), and they would be
* use to find the right constructor
*/
@SuppressWarnings("unchecked")
private <V extends ValueController<T, S>, T extends Actor, W extends T, S> V newValueController(
Class<V> clazz, W widget, Object... args) {
try {
V value;
if (args.length == 0) {
value = ClassReflection.newInstance(clazz);
} else {
Class[] argsClass = new Class[args.length / 2];
Object[] argsObject = new Object[args.length / 2];
for (int i = 0; i < args.length; i += 2) {
argsClass[i] = (Class) args[i];
argsObject[i] = args[i + 1];
}
Constructor constructor = ClassReflection
.getDeclaredConstructor(clazz, argsClass);
value = (V) constructor.newInstance(argsObject);
}
value.build(controller, widget);
return value;
} catch (ReflectionException e) {
Gdx.app.error("OptionsController", "No value", e);
}
return null;
}
示例5: invokeConstructor
import com.badlogic.gdx.utils.reflect.Constructor; //导入方法依赖的package包/类
/** @param constructor will be invoked.
* @param parameters will be used to invoke the constructor.
* @return a new instance of the class.
* @throws ContextInitiationException if unable to invoke the constructor. */
private static Object invokeConstructor(final Constructor constructor, final Object[] parameters) {
try {
return constructor.newInstance(parameters);
} catch (final ReflectionException exception) {
throw new ContextInitiationException("Unable to create a new instance of class: "
+ constructor.getDeclaringClass() + " with constructor: " + constructor + " with parameters: "
+ GdxArrays.newArray(parameters), exception);
}
}
示例6: createEntity
import com.badlogic.gdx.utils.reflect.Constructor; //导入方法依赖的package包/类
private <T extends EntityFactory> Entity createEntity(Class<T> classFactory) {
Constructor constructor = findConstructor(classFactory);
T factory;
try {
factory = (T) constructor.newInstance(builders, assetManager);
} catch (Exception ex) {
Log.debug(tag, "Error instance entityFactory %s", classFactory.getSimpleName());
return null;
}
return factory.createEntity();
}
示例7: create
import com.badlogic.gdx.utils.reflect.Constructor; //导入方法依赖的package包/类
@Override
public void create () {
font = new BitmapFont();
batch = new SpriteBatch();
try {
Vector2 fromDefaultConstructor = ClassReflection.newInstance(Vector2.class);
println("From default constructor: " + fromDefaultConstructor);
Method mSet = ClassReflection.getMethod(Vector2.class, "set", float.class, float.class);
mSet.invoke(fromDefaultConstructor, 10, 11);
println("Set to 10/11: " + fromDefaultConstructor);
Constructor copyConstroctor = ClassReflection.getConstructor(Vector2.class, Vector2.class);
Vector2 fromCopyConstructor = (Vector2)copyConstroctor.newInstance(fromDefaultConstructor);
println("From copy constructor: " + fromCopyConstructor);
Method mMul = ClassReflection.getMethod(Vector2.class, "scl", float.class);
println("Multiplied by 2; " + mMul.invoke(fromCopyConstructor, 2));
Method mNor = ClassReflection.getMethod(Vector2.class, "nor");
println("Normalized: " + mNor.invoke(fromCopyConstructor));
Vector2 fieldCopy = new Vector2();
Field fx = ClassReflection.getField(Vector2.class, "x");
Field fy = ClassReflection.getField(Vector2.class, "y");
fx.set(fieldCopy, fx.get(fromCopyConstructor));
fy.set(fieldCopy, fy.get(fromCopyConstructor));
println("Copied field by field: " + fieldCopy);
Json json = new Json();
String jsonString = json.toJson(fromCopyConstructor);
Vector2 fromJson = json.fromJson(Vector2.class, jsonString);
println("JSON serialized: " + jsonString);
println("JSON deserialized: " + fromJson);
fromJson.x += 1;
fromJson.y += 1;
println("JSON deserialized + 1/1: " + fromJson);
Object array = ArrayReflection.newInstance(int.class, 5);
ArrayReflection.set(array, 0, 42);
println("Array int: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0));
array = ArrayReflection.newInstance(String.class, 5);
ArrayReflection.set(array, 0, "test string");
println("Array String: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0));
} catch (Exception e) {
message = "FAILED: " + e.getMessage() + "\n";
message += e.getClass();
}
}
示例8: loadSceneGraph
import com.badlogic.gdx.utils.reflect.Constructor; //导入方法依赖的package包/类
public static ISceneGraph loadSceneGraph(FileHandle[] jsonFiles, ITimeFrameProvider time, boolean multithreading, int maxThreads) {
ISceneGraph sg = null;
try {
Array<SceneGraphNode> nodes = new Array<SceneGraphNode>(false, 5000);
for (FileHandle jsonFile : jsonFiles) {
JsonReader jsonReader = new JsonReader();
InputStream is = jsonFile.read();
JsonValue model = jsonReader.parse(is);
JsonValue child = model.get("data").child;
while (child != null) {
String clazzName = child.getString("loader");
@SuppressWarnings("unchecked")
Class<Object> clazz = (Class<Object>) ClassReflection.forName(clazzName);
JsonValue filesJson = child.get("files");
if (filesJson != null) {
String[] files = filesJson.asStringArray();
Constructor c = ClassReflection.getConstructor(clazz);
ISceneGraphLoader loader = (ISceneGraphLoader) c.newInstance();
// Init loader
loader.initialize(files);
// Load data
Array<? extends SceneGraphNode> data = loader.loadData();
for (SceneGraphNode elem : data) {
nodes.add(elem);
}
}
child = child.next;
}
is.close();
}
// Initialize nodes and look for octrees
boolean hasOctree = false;
boolean hasStarGroup = false;
for (SceneGraphNode node : nodes) {
node.initialize();
if (node instanceof AbstractOctreeWrapper) {
hasOctree = true;
AbstractOctreeWrapper aow = (AbstractOctreeWrapper) node;
for (SceneGraphNode n : aow.children) {
if (n instanceof StarGroup) {
hasStarGroup = true;
break;
}
}
}
if (node instanceof StarGroup)
hasStarGroup = true;
}
sg = SceneGraphImplementationProvider.provider.getImplementation(multithreading, hasOctree, hasStarGroup, maxThreads);
sg.initialize(nodes, time, hasOctree, hasStarGroup);
} catch (Exception e) {
Logger.error(e);
}
return sg;
}