Java Code Examples for org.rapidoid.u.U#single()

The following examples show how to use org.rapidoid.u.U#single() . 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: IoCContextImpl.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
private <T> T instantiate(Class<T> type, Map<String, Object> properties) {

		ClassMetadata meta = meta(type);

		if (U.notEmpty(meta.injectableConstructors)) {
			U.must(meta.injectableConstructors.size() == 1, "Found more than 1 @Inject-annotated constructor for: %s", type);

			Constructor<?> constr = U.single(meta.injectableConstructors);
			Class<?>[] paramTypes = constr.getParameterTypes();
			Object[] args = new Object[paramTypes.length];

			for (int i = 0; i < args.length; i++) {
				Class<?> paramType = paramTypes[i];
				String paramName = null; // FIXME inject by name
				args[i] = provideIoCInstanceOf(null, paramType, paramName, properties, false);
			}

			return Cls.invoke(constr, args);

		} else if (meta.defaultConstructor != null) {
			return Cls.invoke(meta.defaultConstructor);

		} else {
			throw U.illegal("Cannot find a default nor @Inject-annotated constructor for: %s", type);
		}
	}
 
Example 2
Source File: Msc.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public static String detectZipRoot(InputStream zip) {
	Set<String> roots = U.set();

	try {

		ZipInputStream zis = new ZipInputStream(zip);
		ZipEntry ze = zis.getNextEntry();

		while (ze != null) {

			if (ze.isDirectory()) {
				String fileName = ze.getName();
				String parentDir = fileName.split("/|\\\\")[0];
				roots.add(parentDir);
			}

			ze = zis.getNextEntry();
		}

		zis.closeEntry();
		zis.close();

	} catch (IOException e) {
		throw U.rte(e);
	}

	return roots.size() == 1 ? U.single(roots) : null;
}
 
Example 3
Source File: ClsTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithManyParams() {
	Method m = U.single(Cls.getMethodsNamed(Foo.class, "xy"));
	String[] names = Cls.getMethodParameterNames(m);
	eq(names, U.array("a", "b", "c", "d", "e", "f"));

	Method m2 = U.single(Cls.getMethodsNamed(Foo.class, "xyz"));
	String[] names2 = Cls.getMethodParameterNames(m2);
	eq(names2, U.array("a", "b", "c", "d", "e", "f", "g", "hh", "ii", "j"));
}
 
Example 4
Source File: AbstractRapidoidMojo.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
private String pickMainClass(List<String> mainClasses, MavenProject project) {

		// the.group.id.Main
		String byGroupId = project.getGroupId() + ".Main";
		if (mainClasses.contains(byGroupId)) return byGroupId;

		List<String> namedMain = U.list();
		List<String> withGroupIdPkg = U.list();

		for (String name : mainClasses) {
			if (name.equals("Main")) return "Main";

			if (name.endsWith(".Main")) {
				namedMain.add(name);
			}

			if (name.startsWith(project.getGroupId() + ".")) {
				withGroupIdPkg.add(name);
			}
		}

		// the.group.id.foo.bar.Main
		getLog().info("Candidates by group ID: " + withGroupIdPkg);
		if (withGroupIdPkg.size() == 1) return U.single(withGroupIdPkg);

		// foo.bar.Main
		getLog().info("Candidates named Main: " + namedMain);
		if (namedMain.size() == 1) return U.single(namedMain);

		namedMain.retainAll(withGroupIdPkg);
		getLog().info("Candidates by group ID - named Main: " + namedMain);
		if (namedMain.size() == 1) return U.single(namedMain);

		// the.group.id.foo.bar.Main (the shortest name)
		withGroupIdPkg.sort((s1, s2) -> s1.length() - s2.length());
		getLog().info("Candidates by group ID - picking one with the shortest name: " + withGroupIdPkg);

		return U.first(withGroupIdPkg);
	}
 
Example 5
Source File: ResultsImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public T single() {
	return U.single(retrievePage(0, 2));
}
 
Example 6
Source File: ResultsImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public final T first() {
	return U.single(retrievePage(0, 1));
}
 
Example 7
Source File: ResultsImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public final T last() {
	return U.single(retrievePage(count() - 1, 1));
}
 
Example 8
Source File: ClsTest.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Test
public void testInstanceMethodParams() {
	Method m = U.single(Cls.getMethodsNamed(Foo.class, "abc"));
	String[] names = Cls.getMethodParameterNames(m);
	eq(names, U.array("aa", "b", "чачача"));
}
 
Example 9
Source File: ClsTest.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Test
public void testStaticMethodParams() {
	Method m = U.single(Cls.getMethodsNamed(Foo.class, "statico"));
	String[] names = Cls.getMethodParameterNames(m);
	eq(names, U.array("foo", "arg1"));
}