Java Code Examples for com.badlogic.gdx.utils.reflect.ClassReflection#newInstance()

The following examples show how to use com.badlogic.gdx.utils.reflect.ClassReflection#newInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScriptCompiler.java    From talos with Apache License 2.0 6 votes vote down vote up
public SimpleReturnScript compile (String javaString) {
	JavaSourceFromString file = new JavaSourceFromString("SimpleRunIm", javaString);
	DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
	DynamicClassesFileManager manager = new DynamicClassesFileManager(compiler.getStandardFileManager(null, null, null));

	Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
	JavaCompiler.CompilationTask task = compiler.getTask(null, manager, diagnostics, null, null, compilationUnits);

	boolean success = task.call();
	for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
		System.err.print(String.format("Script compilation error: Line: %d - %s%n", diagnostic.getLineNumber(), diagnostic.getMessage(null)));
	}
	if (success) {
		try {

			System.out.println("Compiled");
			Class clazz = manager.loader.findClass("SimpleRunIm");

			return (SimpleReturnScript)ClassReflection.newInstance(clazz);
		} catch (ReflectionException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	return null;
}
 
Example 2
Source File: ModuleBoardWidget.java    From talos with Apache License 2.0 6 votes vote down vote up
public ModuleWrapper createModule (Class<? extends AbstractModule> clazz, float x, float y) {
    final AbstractModule module;
    try {
        module = ClassReflection.newInstance(clazz);

        if (TalosMain.Instance().TalosProject().getCurrentModuleGraph().addModule(module)) {
            final ModuleWrapper moduleWrapper = createModuleWrapper(module, x, y);
            moduleWrapper.setModuleToDefaults();
            module.setModuleGraph(TalosMain.Instance().TalosProject().getCurrentModuleGraph());

            TalosMain.Instance().ProjectController().setDirty();

            return moduleWrapper;
        } else {
            System.out.println("Did not create module: " + clazz.getSimpleName());
            return null;
        }
    } catch (ReflectionException e) {
        throw new GdxRuntimeException(e);
    }
}
 
Example 3
Source File: DefaultTypeRecognizer.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
public static Object getDefaultValue(Class type) {
    if (isFloatingPointNumberType(type)) {
        return 0.;
    } else if (isLongNumberType(type)) {
        return 0L;
    } else if (isBooleanType(type)) {
        return false;
    } else if (isStringType(type)) {
        return "";
    } else if (isCharType(type)) {
        return '\u0000';
    } else if (ClassReflection.isAssignableFrom(Map.class, type)) {
        return new HashMap<>();
    } else if (ClassReflection.isAssignableFrom(List.class, type)) {
        return new ArrayList<>();
    } else {
        try {
            return ClassReflection.newInstance(type);
        } catch (ReflectionException e) {
            GdxFIRLogger.error(CANT_FIND_DEFAULT_VALUE_FOR_GIVEN_TYPE);
            return null;
        }
    }
}
 
Example 4
Source File: ModuleBoardWidget.java    From talos with Apache License 2.0 5 votes vote down vote up
public <T extends AbstractModule> ModuleWrapper createModuleWrapper (T module, float x, float y) {
    ModuleWrapper<T> moduleWrapper = null;

    if (module == null) return null;

    Class<T> moduleClazz = (Class<T>)module.getClass();

    try {
        moduleWrapper = ClassReflection.newInstance(WrapperRegistry.get(moduleClazz));
        int id = getUniqueIdForModuleWrapper();
        moduleWrapper.setModule(module);
        moduleWrapper.setId(id);
        module.setIndex(id);
        moduleWrapper.setBoard(this);

        tmp.set(x, Gdx.graphics.getHeight() - y);
        moduleContainer.screenToLocalCoordinates(tmp);

        moduleWrapper.setPosition(tmp.x - moduleWrapper.getWidth()/2f, tmp.y - moduleWrapper.getHeight()/2f);
        getModuleWrappers().add(moduleWrapper);
        moduleContainer.addActor(moduleWrapper);

        selectWrapper(moduleWrapper);
    } catch (ReflectionException e) {
        e.printStackTrace();
    }


    // check if there was connect request
    tryAndConnectLasCC(moduleWrapper);


    return moduleWrapper;
}
 
Example 5
Source File: Reflection.java    From shattered-pixel-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public static <T> T newInstance( Class<T> cls ){
	try {
		return ClassReflection.newInstance(cls);
	} catch (ReflectionException e) {
		Game.reportException(e);
		return null;
	}
}
 
Example 6
Source File: ActionFactory.java    From bladecoder-adventure-engine with Apache License 2.0 5 votes vote down vote up
public static Action create(String tag, HashMap<String, String> params)
		throws ClassNotFoundException, ReflectionException {

	Action a = null;

	Class<?> c = tagToClass.get(tag);

	if (c == null) {
		c = Class.forName(tag, true, loader);
	}

	a = (Action) ClassReflection.newInstance(c);

	if (params != null) {

		for (String key : params.keySet()) {
			String value = params.get(key);

			try {
				ActionUtils.setParam(a, key, value);
			} catch (NoSuchFieldException | SecurityException | IllegalArgumentException
					| IllegalAccessException e) {
				EngineLogger.error("Error Setting Action Param - Action:" + tag + " Param: " + key + " Value: "
						+ value + " Msg: NOT FOUND " + e.getMessage());
			}
		}
	}

	return a;
}
 
Example 7
Source File: Reflection.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 5 votes vote down vote up
public static <T> T newInstance( Class<T> cls ){
	try {
		return ClassReflection.newInstance(cls);
	} catch (ReflectionException e) {
		Game.reportException(e);
		return null;
	}
}
 
Example 8
Source File: Engine.java    From ashley with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link Component}. To use that method your components must have a visible no-arg constructor
 */
public <T extends Component> T createComponent (Class<T> componentType) {
	try {
		return ClassReflection.newInstance(componentType);
	} catch (ReflectionException e) {
		return null;
	}
}
 
Example 9
Source File: Reflection.java    From shattered-pixel-dungeon with GNU General Public License v3.0 4 votes vote down vote up
public static <T> T newInstanceUnhandled( Class<T> cls ) throws Exception {
	return ClassReflection.newInstance(cls);
}
 
Example 10
Source File: BehaviorTreeParser.java    From gdx-ai with Apache License 2.0 4 votes vote down vote up
private void openTask (String name, boolean isGuard) {
	try {
		Task<E> task;
		if (isSubtreeRef) {
			task = subtreeRootTaskInstance(name);
		}
		else {
			String className = getImport(name);
			if (className == null) className = name;
			@SuppressWarnings("unchecked")
			Task<E> tmpTask = (Task<E>)ClassReflection.newInstance(ClassReflection.forName(className));
			task = tmpTask;
		}
		
		if (!currentTree.inited()) {
			initCurrentTree(task, indent);
			indent = 0;
		} else if (!isGuard) {
			StackedTask<E> stackedTask = getPrevTask();

			indent -= currentTreeStartIndent;
			if (stackedTask.task == currentTree.rootTask) {
				step = indent;
			}
			if (indent > currentDepth) {
				stack.add(stackedTask); // push
			} else if (indent <= currentDepth) {
				// Pop tasks from the stack based on indentation
				// and check their minimum number of children
				int i = (currentDepth - indent) / step;
				popAndCheckMinChildren(stack.size - i);
			}

			// Check the max number of children of the parent
			StackedTask<E> stackedParent = stack.peek();
			int maxChildren = stackedParent.metadata.maxChildren;
			if (stackedParent.task.getChildCount() >= maxChildren)
				throw stackedTaskException(stackedParent, "max number of children exceeded ("
					+ (stackedParent.task.getChildCount() + 1) + " > " + maxChildren + ")");

			// Add child task to the parent
			stackedParent.task.addChild(task);
		}
		updateCurrentTask(createStackedTask(name, task), indent, isGuard);
	} catch (ReflectionException e) {
		throw new GdxRuntimeException("Cannot parse behavior tree!!!", e);
	}
}
 
Example 11
Source File: Reflection.java    From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 4 votes vote down vote up
public static <T> T newInstanceUnhandled( Class<T> cls ) throws Exception {
	return ClassReflection.newInstance(cls);
}
 
Example 12
Source File: GdxReflection.java    From Cardshifter with Apache License 2.0 4 votes vote down vote up
@Override
public Object create(Class<?> aClass) throws Exception {
    return ClassReflection.newInstance(aClass);
}