private static MethodHandle GetSharedStringConstructorMethodHandle(MethodHandles.Lookup lookup) { try { System.Reflection.ConstructorInfo <string> constructor = typeof(string).getDeclaredConstructor(typeof(char[]), Boolean.TYPE); constructor.Accessible = true; return(lookup.unreflectConstructor(constructor)); } catch (Exception) { return(null); } }
//------------------------------------------------------------------------- /// <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(); }); }
static UnsafeUtil() { @unsafe = Unsafe; MethodHandles.Lookup lookup = MethodHandles.lookup(); _sharedStringConstructor = GetSharedStringConstructorMethodHandle(lookup); Type dbbClass = null; //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: Constructor<?> ctor = null; System.Reflection.ConstructorInfo <object> ctor = null; long dbbMarkOffset = 0; long dbbPositionOffset = 0; long dbbLimitOffset = 0; long dbbCapacityOffset = 0; long dbbAddressOffset = 0; int ps = 4096; try { dbbClass = Type.GetType("java.nio.DirectByteBuffer"); Type bufferClass = Type.GetType("java.nio.Buffer"); dbbMarkOffset = @unsafe.objectFieldOffset(bufferClass.getDeclaredField("mark")); dbbPositionOffset = @unsafe.objectFieldOffset(bufferClass.getDeclaredField("position")); dbbLimitOffset = @unsafe.objectFieldOffset(bufferClass.getDeclaredField("limit")); dbbCapacityOffset = @unsafe.objectFieldOffset(bufferClass.getDeclaredField("capacity")); dbbAddressOffset = @unsafe.objectFieldOffset(bufferClass.getDeclaredField("address")); ps = @unsafe.pageSize(); } catch (Exception e) { if (dbbClass == null) { throw new LinkageError("Cannot to link java.nio.DirectByteBuffer", e); } try { ctor = dbbClass.GetConstructor(Long.TYPE, Integer.TYPE); ctor.Accessible = true; } catch (NoSuchMethodException e1) { throw new LinkageError("Cannot find JNI constructor for java.nio.DirectByteBuffer", e1); } } DirectByteBufferClass = dbbClass; _directByteBufferCtor = ctor; _directByteBufferMarkOffset = dbbMarkOffset; _directByteBufferPositionOffset = dbbPositionOffset; _directByteBufferLimitOffset = dbbLimitOffset; _directByteBufferCapacityOffset = dbbCapacityOffset; _directByteBufferAddressOffset = dbbAddressOffset; _pageSize = ps; // See java.nio.Bits.unaligned() and its uses. string alignmentProperty = System.getProperty(ALLOW_UNALIGNED_MEMORY_ACCESS_PROPERTY); if (!string.ReferenceEquals(alignmentProperty, null) && (alignmentProperty.Equals("true", StringComparison.OrdinalIgnoreCase) || alignmentProperty.Equals("false", StringComparison.OrdinalIgnoreCase))) { AllowUnalignedMemoryAccess = bool.Parse(alignmentProperty); } else { bool unaligned; string arch = System.getProperty("os.arch", "?"); switch (arch) // list of architectures that support unaligned access to memory { case "x86_64": case "i386": case "x86": case "amd64": case "ppc64": case "ppc64le": case "ppc64be": unaligned = true; break; default: unaligned = false; break; } AllowUnalignedMemoryAccess = unaligned; } StoreByteOrderIsNative = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN; }
// 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()); }