org.scijava.util.ClassUtils Java Examples

The following examples show how to use org.scijava.util.ClassUtils. 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: TemplateTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Tests that generation works for scalars, lists and maps. */
@Test
public void testTemplate() throws IOException {
	final String prefix = ClassUtils.getLocation(getClass()).getFile();
	final File testFile = new File(prefix
			+ "/../generated-test-sources/from-template/TestTemplate.txt");
	final FileReader fileReader = new FileReader(testFile);
	final BufferedReader reader = new BufferedReader(fileReader);
	try {
		assertEquals("Template test: Pinky", reader.readLine());
		assertEquals("Multi-line: Ten weary, footsore travelers,", reader.readLine());
		assertNotNull(reader.readLine());
		assertNotNull(reader.readLine());
		assertEquals("one dark and rainy night...!", reader.readLine());
		assertNotNull(reader.readLine());
		assertEquals("The list of maps:", reader.readLine());
		assertEquals("alice who is 123 years old", reader.readLine());
		assertEquals("bob who is 1.2 years old", reader.readLine());
		assertEquals("charly ", reader.readLine());
		assertNull(reader.readLine());
	} finally {
		reader.close();
	}
}
 
Example #2
Source File: TemplateTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Tests that automagical type parsing works as intended. */
@Test
public void testTypeDetection() throws IOException {
	final String prefix = ClassUtils.getLocation(getClass()).getFile();
	final File testFile = new File(prefix
			+ "/../generated-test-sources/from-template/TestTypes.txt");
	final FileReader fileReader = new FileReader(testFile);
	final BufferedReader reader = new BufferedReader(fileReader);
	try {
		int i = 0;
		while (true) {
			i++;
			final String line = reader.readLine();
			if (line == null) break;
			final String[] tokens = line.split("\t");
			assertEquals(2, tokens.length);
			assertEquals("Failure on line " + i + ":", tokens[0], tokens[1]);
		}
	}
	finally {
		reader.close();
	}
}
 
Example #3
Source File: DefaultMetadataService.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void populate(final Object metadata, final Map<String, Object> map) {
	final List<java.lang.reflect.Field> fields = ClassUtils.getAnnotatedFields(
		metadata.getClass(), Field.class);
	for (final java.lang.reflect.Field field : fields) {
		final String name = field.getName();
		if (!map.containsKey(name)) {
			// no value given for this field
			continue;
		}
		final Object value = map.get(name);

		// TEMP: Until bug is fixed; see
		// https://github.com/scijava/scijava-common/issues/31
		if (value == null) continue;

		ClassUtils.setValue(field, metadata, value);
	}
}
 
Example #4
Source File: REPLEditor.java    From sciview with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ThreadService threadService() {
  // HACK: Get the SciJava context from the REPL.
  // This can be fixed if/when the REPL offers a getter for it.
  final Context ctx = (Context) ClassUtils.getValue(//
          Types.field(repl.getClass(), "context"), repl);
  return ctx.service(ThreadService.class);
}
 
Example #5
Source File: OpInfo.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** Helper method of {@link #getName} and {@link #getAliases}. */
private <T> T getFieldValue(final Class<T> fieldType, final String fieldName)
{
	final Class<? extends Op> opType = getType();
	final Field nameField = ClassUtils.getField(opType, fieldName);
	if (nameField == null) return null;
	if (!fieldType.isAssignableFrom(nameField.getType())) return null;
	@SuppressWarnings("unchecked")
	final T value = (T) ClassUtils.getValue(nameField, null);
	return value;
}
 
Example #6
Source File: AbstractNamespaceTest.java    From imagej-ops with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Checks that the given class's list of {@link OpMethod}-annotated methods
 * "covers" the available ops, and vice versa.
 * <p>
 * This method verifies that all ops with the given name have type-safe
 * {@link OpMethod}-annotated methods. And vice versa: it verifies that all
 * the annotated methods could theoretically invoke at least one op
 * implementation.
 * </p>
 * <p>
 * This method provides a general-purpose verification test which extensions
 * to Ops can also use to verify their own cache of type-safe methods provided
 * by their own service(s).
 * </p>
 * <p>
 * The completeness tests are not 100% accurate:
 * </p>
 * <ul>
 * <li>The comparison of method parameters to op parameters is too lenient:
 * the matching should ideally be exact rather than accepting "compatible"
 * (i.e., subtype) matches.</li>
 * <li>There are some limitations to the matching of generic parameters.</li>
 * <li>When a method is missing, the system generates a suggested code block,
 * but that code block includes only raw type parameters, not generified type
 * parameters. For details on why, see <a
 * href="http://stackoverflow.com/q/28143029">this post on StackOverflow</a>.</li>
 * </ul>
 * 
 * @param namespaceClass Class with the {@link OpMethod}-annotated methods.
 * @param qName The fully qualified (with namespace) name of the op to verify
 *          is completely covered.
 * @see GlobalNamespaceTest Usage examples for global namespace ops.
 * @see net.imagej.ops.math.MathNamespaceTest Usage examples for math ops.
 */
public boolean checkComplete(final Class<?> namespaceClass,
	final String qName)
{
	final String namespace = OpUtils.getNamespace(qName);
	final String opName = OpUtils.stripNamespace(qName);

	// obtain the list of built-in methods
	final List<Method> allMethods =
		ClassUtils.getAnnotatedMethods(namespaceClass, OpMethod.class);

	// obtain the list of ops
	final Collection<OpInfo> allOps = ops.infos();

	// filter methods and ops to only those with the given name
	final List<Method> methods;
	final Collection<OpInfo> opList;
	if (opName == null) {
		methods = allMethods;
		opList = allOps;
	}
	else {
		// filter the methods
		methods = new ArrayList<>();
		for (final Method method : allMethods) {
			if (opName.equals(method.getName())) methods.add(method);
		}

		// filter the ops
		opList = new ArrayList<>();
		for (final OpInfo op : allOps) {
			if (qName.equals(op.getName())) opList.add(op);
		}
	}

	// cross-check them!
	return checkComplete(namespace, methods, opList);
}
 
Example #7
Source File: DefaultTableIOPluginTest.java    From imagej-server with Apache License 2.0 5 votes vote down vote up
private void setValues(final Object instance, final String[] fieldNames,
	final Object[] values) throws SecurityException
{
	final Class<?> cls = instance.getClass();
	final List<Field> fields = ClassUtils.getAnnotatedFields(cls,
		Parameter.class);
	final HashMap<String, Field> fieldMap = new HashMap<>();
	for (final Field field : fields) {
		fieldMap.put(field.getName(), field);
	}
	for (int i = 0; i < fieldNames.length; i++) {
		ClassUtils.setValue(fieldMap.get(fieldNames[i]), instance, values[i]);
	}
}