//------------------------------------------------------------------------- /// <summary> /// Test a private no-arg constructor the primary purpose of increasing test coverage. /// </summary> /// <param name="clazz"> the class to test </param> public static void coverPrivateConstructor(Type clazz) { assertNotNull(clazz, "coverPrivateConstructor() called with null class"); AtomicBoolean isPrivate = new AtomicBoolean(false); ignoreThrows(() => { System.Reflection.ConstructorInfo <object> con = clazz.DeclaredConstructor; isPrivate.set(Modifier.isPrivate(con.Modifiers)); con.Accessible = true; con.newInstance(); }); assertTrue(isPrivate.get(), "No-arg constructor must be private"); }
public static object instantiate(string className, object[] args) { Type clazz = loadClass(className); //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: Constructor<?> constructor = findMatchingConstructor(clazz, args); System.Reflection.ConstructorInfo <object> constructor = findMatchingConstructor(clazz, args); ensureNotNull("couldn't find constructor for " + className + " with args " + Arrays.asList(args), "constructor", constructor); try { return(constructor.newInstance(args)); } catch (Exception e) { throw LOG.exceptionWhileInstantiatingClass(className, e); } }
//------------------------------------------------------------------------- /// <summary> /// Asserts that a class is a well-defined utility class. /// <para> /// Must be final and with one zero-arg private constructor. /// All public methods must be static. /// /// </para> /// </summary> /// <param name="clazz"> the class to test </param> public static void assertUtilityClass(Type clazz) { assertNotNull(clazz, "assertUtilityClass() called with null class"); assertTrue(Modifier.isFinal(clazz.Modifiers), "Utility class must be final"); assertEquals(clazz.GetConstructors(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance).length, 1, "Utility class must have one constructor"); //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: Constructor<?> con = clazz.getDeclaredConstructors()[0]; System.Reflection.ConstructorInfo <object> con = clazz.GetConstructors(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)[0]; assertEquals(con.ParameterTypes.length, 0, "Utility class must have zero-arg constructor"); assertTrue(Modifier.isPrivate(con.Modifiers), "Utility class must have private constructor"); foreach (System.Reflection.MethodInfo method in clazz.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)) { if (Modifier.isPublic(method.Modifiers)) { assertTrue(Modifier.isStatic(method.Modifiers), "Utility class public methods must be static"); } } // coverage ignoreThrows(() => { con.Accessible = true; con.newInstance(); }); }
// parses the alternate names //JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @SuppressWarnings("unchecked") private static <R extends Named> com.google.common.collect.ImmutableList<NamedLookup<R>> parseProviders(com.opengamma.strata.collect.io.IniFile config, Class<R> enumType) private static ImmutableList <NamedLookup <R> > parseProviders <R>(IniFile config, Type <R> enumType) where R : Named { if (!config.contains(PROVIDERS_SECTION)) { return(ImmutableList.of()); } PropertySet section = config.section(PROVIDERS_SECTION); ImmutableList.Builder <NamedLookup <R> > builder = ImmutableList.builder(); foreach (string key in section.keys()) { Type cls; try { cls = RenameHandler.INSTANCE.lookupType(key); } catch (Exception ex) { throw new System.ArgumentException("Unable to find enum provider class: " + key, ex); } string value = section.value(key); if (value.Equals("constants")) { // extract public static final constants builder.add(parseConstants(enumType, cls)); } else if (value.Equals("lookup")) { // class is a named lookup if (!cls.IsAssignableFrom(typeof(NamedLookup))) { //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method: throw new System.ArgumentException("Enum provider class must implement NamedLookup " + cls.FullName); } try { //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: Constructor<?> cons = cls.getDeclaredConstructor(); System.Reflection.ConstructorInfo <object> cons = cls.DeclaredConstructor; if (!Modifier.isPublic(cls.Modifiers)) { cons.Accessible = true; } builder.add((NamedLookup <R>)cons.newInstance()); } catch (Exception ex) { //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method: throw new System.ArgumentException("Invalid enum provider constructor: new " + cls.FullName + "()", ex); } } else if (value.Equals("instance")) { // class has a named lookup INSTANCE static field try { System.Reflection.FieldInfo field = cls.getDeclaredField("INSTANCE"); if (!Modifier.isStatic(field.Modifiers) || !field.Type.IsAssignableFrom(typeof(NamedLookup))) { //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method: throw new System.ArgumentException("Invalid enum provider instance: " + cls.FullName + ".INSTANCE"); } if (!Modifier.isPublic(cls.Modifiers) || !Modifier.isPublic(field.Modifiers)) { field.Accessible = true; } builder.add((NamedLookup <R>)field.get(null)); } catch (Exception ex) { //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method: throw new System.ArgumentException("Invalid enum provider instance: " + cls.FullName + ".INSTANCE", ex); } } else { throw new System.ArgumentException("Provider value must be either 'constants' or 'lookup'"); } } return(builder.build()); }