private readonly String LambdaClassName; // Generated name for the generated class "X$$Lambda$1" /// <summary> /// General meta-factory constructor, supporting both standard cases and /// allowing for uncommon options such as serialization or bridging. /// </summary> /// <param name="caller"> Stacked automatically by VM; represents a lookup context /// with the accessibility privileges of the caller. </param> /// <param name="invokedType"> Stacked automatically by VM; the signature of the /// invoked method, which includes the expected static /// type of the returned lambda object, and the static /// types of the captured arguments for the lambda. In /// the event that the implementation method is an /// instance method, the first argument in the invocation /// signature will correspond to the receiver. </param> /// <param name="samMethodName"> Name of the method in the functional interface to /// which the lambda or method reference is being /// converted, represented as a String. </param> /// <param name="samMethodType"> Type of the method in the functional interface to /// which the lambda or method reference is being /// converted, represented as a MethodType. </param> /// <param name="implMethod"> The implementation method which should be called (with /// suitable adaptation of argument types, return types, /// and adjustment for captured arguments) when methods of /// the resulting functional interface instance are invoked. </param> /// <param name="instantiatedMethodType"> The signature of the primary functional /// interface method after type variables are /// substituted with their instantiation from /// the capture site </param> /// <param name="isSerializable"> Should the lambda be made serializable? If set, /// either the target type or one of the additional SAM /// types must extend {@code Serializable}. </param> /// <param name="markerInterfaces"> Additional interfaces which the lambda object /// should implement. </param> /// <param name="additionalBridges"> Method types for additional signatures to be /// bridged to the implementation method </param> /// <exception cref="LambdaConversionException"> If any of the meta-factory protocol /// invariants are violated </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public InnerClassLambdaMetafactory(MethodHandles.Lookup caller, MethodType invokedType, String samMethodName, MethodType samMethodType, MethodHandle implMethod, MethodType instantiatedMethodType, boolean isSerializable, Class[] markerInterfaces, MethodType[] additionalBridges) throws LambdaConversionException public InnerClassLambdaMetafactory(MethodHandles.Lookup caller, MethodType invokedType, String samMethodName, MethodType samMethodType, MethodHandle implMethod, MethodType instantiatedMethodType, bool isSerializable, Class[] markerInterfaces, MethodType[] additionalBridges) : base(caller, invokedType, samMethodName, samMethodType, implMethod, instantiatedMethodType, isSerializable, markerInterfaces, additionalBridges) { ImplMethodClassName = ImplDefiningClass.Name.Replace('.', '/'); ImplMethodName = ImplInfo.Name; ImplMethodDesc = ImplMethodType.ToMethodDescriptorString(); ImplMethodReturnClass = (ImplKind == MethodHandleInfo.REF_newInvokeSpecial) ? ImplDefiningClass : ImplMethodType.ReturnType(); ConstructorType = invokedType.ChangeReturnType(Void.TYPE); LambdaClassName = TargetClass.Name.Replace('.', '/') + "$$Lambda$" + Counter.IncrementAndGet(); Cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); int parameterCount = invokedType.ParameterCount(); if (parameterCount > 0) { ArgNames = new String[parameterCount]; ArgDescs = new String[parameterCount]; for (int i = 0; i < parameterCount; i++) { ArgNames[i] = "arg$" + (i + 1); ArgDescs[i] = BytecodeDescriptor.unparse(invokedType.ParameterType(i)); } } else { ArgNames = ArgDescs = EMPTY_STRING_ARRAY; } }
/// <summary> /// Facilitates the creation of simple "function objects" that implement one /// or more interfaces by delegation to a provided <seealso cref="MethodHandle"/>, /// after appropriate type adaptation and partial evaluation of arguments. /// Typically used as a <em>bootstrap method</em> for {@code invokedynamic} /// call sites, to support the <em>lambda expression</em> and <em>method /// reference expression</em> features of the Java Programming Language. /// /// <para>This is the standard, streamlined metafactory; additional flexibility /// is provided by <seealso cref="#altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)"/>. /// A general description of the behavior of this method is provided /// <seealso cref="LambdaMetafactory above"/>. /// /// </para> /// <para>When the target of the {@code CallSite} returned from this method is /// invoked, the resulting function objects are instances of a class which /// implements the interface named by the return type of {@code invokedType}, /// declares a method with the name given by {@code invokedName} and the /// signature given by {@code samMethodType}. It may also override additional /// methods from {@code Object}. /// /// </para> /// </summary> /// <param name="caller"> Represents a lookup context with the accessibility /// privileges of the caller. When used with {@code invokedynamic}, /// this is stacked automatically by the VM. </param> /// <param name="invokedName"> The name of the method to implement. When used with /// {@code invokedynamic}, this is provided by the /// {@code NameAndType} of the {@code InvokeDynamic} /// structure and is stacked automatically by the VM. </param> /// <param name="invokedType"> The expected signature of the {@code CallSite}. The /// parameter types represent the types of capture variables; /// the return type is the interface to implement. When /// used with {@code invokedynamic}, this is provided by /// the {@code NameAndType} of the {@code InvokeDynamic} /// structure and is stacked automatically by the VM. /// In the event that the implementation method is an /// instance method and this signature has any parameters, /// the first parameter in the invocation signature must /// correspond to the receiver. </param> /// <param name="samMethodType"> Signature and return type of method to be implemented /// by the function object. </param> /// <param name="implMethod"> A direct method handle describing the implementation /// method which should be called (with suitable adaptation /// of argument types, return types, and with captured /// arguments prepended to the invocation arguments) at /// invocation time. </param> /// <param name="instantiatedMethodType"> The signature and return type that should /// be enforced dynamically at invocation time. /// This may be the same as {@code samMethodType}, /// or may be a specialization of it. </param> /// <returns> a CallSite whose target can be used to perform capture, generating /// instances of the interface named by {@code invokedType} </returns> /// <exception cref="LambdaConversionException"> If any of the linkage invariants /// described <seealso cref="LambdaMetafactory above"/> /// are violated </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static CallSite metafactory(MethodHandles.Lookup caller, String invokedName, MethodType invokedType, MethodType samMethodType, MethodHandle implMethod, MethodType instantiatedMethodType) throws LambdaConversionException public static CallSite Metafactory(MethodHandles.Lookup caller, String invokedName, MethodType invokedType, MethodType samMethodType, MethodHandle implMethod, MethodType instantiatedMethodType) { AbstractValidatingLambdaMetafactory mf; mf = new InnerClassLambdaMetafactory(caller, invokedType, invokedName, samMethodType, implMethod, instantiatedMethodType, false, EMPTY_CLASS_ARRAY, EMPTY_MT_ARRAY); mf.ValidateMetafactoryArgs(); return(mf.BuildCallSite()); }
/// <summary> /// Facilitates the creation of simple "function objects" that implement one /// or more interfaces by delegation to a provided <seealso cref="MethodHandle"/>, /// after appropriate type adaptation and partial evaluation of arguments. /// Typically used as a <em>bootstrap method</em> for {@code invokedynamic} /// call sites, to support the <em>lambda expression</em> and <em>method /// reference expression</em> features of the Java Programming Language. /// /// <para>This is the general, more flexible metafactory; a streamlined version /// is provided by {@link #metafactory(java.lang.invoke.MethodHandles.Lookup, /// String, MethodType, MethodType, MethodHandle, MethodType)}. /// A general description of the behavior of this method is provided /// <seealso cref="LambdaMetafactory above"/>. /// /// </para> /// <para>The argument list for this method includes three fixed parameters, /// corresponding to the parameters automatically stacked by the VM for the /// bootstrap method in an {@code invokedynamic} invocation, and an {@code Object[]} /// parameter that contains additional parameters. The declared argument /// list for this method is: /// /// <pre>{@code /// CallSite altMetafactory(MethodHandles.Lookup caller, /// String invokedName, /// MethodType invokedType, /// Object... args) /// }</pre> /// /// </para> /// <para>but it behaves as if the argument list is as follows: /// /// <pre>{@code /// CallSite altMetafactory(MethodHandles.Lookup caller, /// String invokedName, /// MethodType invokedType, /// MethodType samMethodType, /// MethodHandle implMethod, /// MethodType instantiatedMethodType, /// int flags, /// int markerInterfaceCount, // IF flags has MARKERS set /// Class... markerInterfaces, // IF flags has MARKERS set /// int bridgeCount, // IF flags has BRIDGES set /// MethodType... bridges // IF flags has BRIDGES set /// ) /// }</pre> /// /// </para> /// <para>Arguments that appear in the argument list for /// <seealso cref="#metafactory(MethodHandles.Lookup, String, MethodType, MethodType, MethodHandle, MethodType)"/> /// have the same specification as in that method. The additional arguments /// are interpreted as follows: /// <ul> /// <li>{@code flags} indicates additional options; this is a bitwise /// OR of desired flags. Defined flags are <seealso cref="#FLAG_BRIDGES"/>, /// <seealso cref="#FLAG_MARKERS"/>, and <seealso cref="#FLAG_SERIALIZABLE"/>.</li> /// <li>{@code markerInterfaceCount} is the number of additional interfaces /// the function object should implement, and is present if and only if the /// {@code FLAG_MARKERS} flag is set.</li> /// <li>{@code markerInterfaces} is a variable-length list of additional /// interfaces to implement, whose length equals {@code markerInterfaceCount}, /// and is present if and only if the {@code FLAG_MARKERS} flag is set.</li> /// <li>{@code bridgeCount} is the number of additional method signatures /// the function object should implement, and is present if and only if /// the {@code FLAG_BRIDGES} flag is set.</li> /// <li>{@code bridges} is a variable-length list of additional /// methods signatures to implement, whose length equals {@code bridgeCount}, /// and is present if and only if the {@code FLAG_BRIDGES} flag is set.</li> /// </ul> /// /// </para> /// <para>Each class named by {@code markerInterfaces} is subject to the same /// restrictions as {@code Rd}, the return type of {@code invokedType}, /// as described <seealso cref="LambdaMetafactory above"/>. Each {@code MethodType} /// named by {@code bridges} is subject to the same restrictions as /// {@code samMethodType}, as described <seealso cref="LambdaMetafactory above"/>. /// /// </para> /// <para>When FLAG_SERIALIZABLE is set in {@code flags}, the function objects /// will implement {@code Serializable}, and will have a {@code writeReplace} /// method that returns an appropriate <seealso cref="SerializedLambda"/>. The /// {@code caller} class must have an appropriate {@code $deserializeLambda$} /// method, as described in <seealso cref="SerializedLambda"/>. /// /// </para> /// <para>When the target of the {@code CallSite} returned from this method is /// invoked, the resulting function objects are instances of a class with /// the following properties: /// <ul> /// <li>The class implements the interface named by the return type /// of {@code invokedType} and any interfaces named by {@code markerInterfaces}</li> /// <li>The class declares methods with the name given by {@code invokedName}, /// and the signature given by {@code samMethodType} and additional signatures /// given by {@code bridges}</li> /// <li>The class may override methods from {@code Object}, and may /// implement methods related to serialization.</li> /// </ul> /// /// </para> /// </summary> /// <param name="caller"> Represents a lookup context with the accessibility /// privileges of the caller. When used with {@code invokedynamic}, /// this is stacked automatically by the VM. </param> /// <param name="invokedName"> The name of the method to implement. When used with /// {@code invokedynamic}, this is provided by the /// {@code NameAndType} of the {@code InvokeDynamic} /// structure and is stacked automatically by the VM. </param> /// <param name="invokedType"> The expected signature of the {@code CallSite}. The /// parameter types represent the types of capture variables; /// the return type is the interface to implement. When /// used with {@code invokedynamic}, this is provided by /// the {@code NameAndType} of the {@code InvokeDynamic} /// structure and is stacked automatically by the VM. /// In the event that the implementation method is an /// instance method and this signature has any parameters, /// the first parameter in the invocation signature must /// correspond to the receiver. </param> /// <param name="args"> An {@code Object[]} array containing the required /// arguments {@code samMethodType}, {@code implMethod}, /// {@code instantiatedMethodType}, {@code flags}, and any /// optional arguments, as described /// <seealso cref="#altMetafactory(MethodHandles.Lookup, String, MethodType, Object...)"/> above} </param> /// <returns> a CallSite whose target can be used to perform capture, generating /// instances of the interface named by {@code invokedType} </returns> /// <exception cref="LambdaConversionException"> If any of the linkage invariants /// described <seealso cref="LambdaMetafactory above"/> /// are violated </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static CallSite altMetafactory(MethodHandles.Lookup caller, String invokedName, MethodType invokedType, Object... args) throws LambdaConversionException public static CallSite AltMetafactory(MethodHandles.Lookup caller, String invokedName, MethodType invokedType, params Object[] args) { MethodType samMethodType = (MethodType)args[0]; MethodHandle implMethod = (MethodHandle)args[1]; MethodType instantiatedMethodType = (MethodType)args[2]; int flags = (Integer)args[3]; Class[] markerInterfaces; MethodType[] bridges; int argIndex = 4; if ((flags & FLAG_MARKERS) != 0) { int markerCount = (Integer)args[argIndex++]; markerInterfaces = new Class[markerCount]; System.Array.Copy(args, argIndex, markerInterfaces, 0, markerCount); argIndex += markerCount; } else { markerInterfaces = EMPTY_CLASS_ARRAY; } if ((flags & FLAG_BRIDGES) != 0) { int bridgeCount = (Integer)args[argIndex++]; bridges = new MethodType[bridgeCount]; System.Array.Copy(args, argIndex, bridges, 0, bridgeCount); argIndex += bridgeCount; } else { bridges = EMPTY_MT_ARRAY; } bool isSerializable = ((flags & FLAG_SERIALIZABLE) != 0); if (isSerializable) { bool foundSerializableSupertype = invokedType.ReturnType().IsSubclassOf(typeof(Serializable)); foreach (Class c in markerInterfaces) { foundSerializableSupertype |= c.IsSubclassOf(typeof(Serializable)); } if (!foundSerializableSupertype) { markerInterfaces = Arrays.CopyOf(markerInterfaces, markerInterfaces.Length + 1); markerInterfaces[markerInterfaces.Length - 1] = typeof(Serializable); } } AbstractValidatingLambdaMetafactory mf = new InnerClassLambdaMetafactory(caller, invokedType, invokedName, samMethodType, implMethod, instantiatedMethodType, isSerializable, markerInterfaces, bridges); mf.ValidateMetafactoryArgs(); return(mf.BuildCallSite()); }
private static CleanerHandles FindHandlesForOldCleaner(MethodHandles.Lookup lookup) { try { Type oldCleaner = Type.GetType("sun.misc.Cleaner"); return(CleanerHandles.Of(FindCreationMethod("create", lookup, oldCleaner), FindCleanMethod(lookup, oldCleaner))); } catch (Exception oldCleanerException) when(oldCleanerException is ClassNotFoundException || oldCleanerException is NoSuchMethodException || oldCleanerException is IllegalAccessException) { throw new LinkageError("Unable to find cleaner methods.", oldCleanerException); } }
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); } }
private static object GlobalCleaner() { MethodHandles.Lookup lookup = MethodHandles.lookup(); try { Type newCleaner = Type.GetType("java.lang.ref.Cleaner"); MethodHandle createInstance = lookup.findStatic(newCleaner, "create", MethodType.methodType(newCleaner)); return(createInstance.invoke()); } catch (Exception) { return(null); } }
private static CleanerHandles FindHandlesForNewCleaner(MethodHandles.Lookup lookup) { try { Objects.requireNonNull(_globalCleanerInstance); Type newCleaner = _globalCleanerInstance.GetType(); Type newCleanable = Type.GetType("java.lang.ref.Cleaner$Cleanable"); MethodHandle registerHandle = FindCreationMethod("register", lookup, newCleaner); registerHandle = registerHandle.bindTo(_globalCleanerInstance); return(CleanerHandles.Of(registerHandle, FindCleanMethod(lookup, newCleanable))); } catch (Exception newCleanerException) when(newCleanerException is ClassNotFoundException || newCleanerException is NoSuchMethodException || newCleanerException is IllegalAccessException) { throw new LinkageError("Unable to find cleaner methods.", newCleanerException); } }
private static MethodHandle ArrayEncode() { // Because we need to be able to compile on IBM's JVM, we can't // depend on ArrayEncoder. Unfortunately, ArrayEncoders encode method // is twoish orders of magnitude faster than regular encoders for ascii // so we go through the hurdle of calling that encode method via // a MethodHandle. MethodHandles.Lookup lookup = MethodHandles.lookup(); try { return(lookup.unreflect(Type.GetType("sun.nio.cs.ArrayEncoder").GetMethod("encode", typeof(char[]), typeof(int), typeof(int), typeof(sbyte[])))); } catch (Exception e) { throw new AssertionError("This encoder depends on sun.nio.cs.ArrayEncoder, which failed to load: " + e.Message, e); } }
private static MethodHandle CharArrayGetter() { MethodHandles.Lookup lookup = MethodHandles.lookup(); try { System.Reflection.FieldInfo value = typeof(string).getDeclaredField("value"); if (value.Type != typeof(char[])) { throw new AssertionError("This encoder depends being able to access raw char[] in java.lang.String, but the class is backed by a " + value.Type.CanonicalName); } value.Accessible = true; return(lookup.unreflectGetter(value)); } catch (Exception e) { throw new AssertionError("This encoder depends being able to access raw char[] in java.lang.String, which failed: " + e.Message, e); } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: public void upgradeIndex(java.nio.file.Path indexPath) throws Throwable public virtual void UpgradeIndex(Path indexPath) { // since lucene use ServiceLocator to load services, context class loader need to be replaced as well ClassLoader contextClassLoader = Thread.CurrentThread.ContextClassLoader; try { if (_mainMethod == null) { _luceneLoader = _jarLoaderSupplier.get(); Type upgrader = _luceneLoader.loadEmbeddedClass(LUCENE_INDEX_UPGRADER_CLASS_NAME); MethodHandles.Lookup lookup = MethodHandles.lookup(); _mainMethod = lookup.findStatic(upgrader, "main", MethodType.methodType(typeof(void), typeof(string[]))); } Thread.CurrentThread.ContextClassLoader = _luceneLoader.JarsClassLoader; _mainMethod.invokeExact(new string[] { indexPath.ToString() }); } finally { Thread.CurrentThread.ContextClassLoader = contextClassLoader; } }
internal readonly MethodType[] AdditionalBridges; // Signatures of additional methods to bridge /// <summary> /// Meta-factory constructor. /// </summary> /// <param name="caller"> Stacked automatically by VM; represents a lookup context /// with the accessibility privileges of the caller. </param> /// <param name="invokedType"> Stacked automatically by VM; the signature of the /// invoked method, which includes the expected static /// type of the returned lambda object, and the static /// types of the captured arguments for the lambda. In /// the event that the implementation method is an /// instance method, the first argument in the invocation /// signature will correspond to the receiver. </param> /// <param name="samMethodName"> Name of the method in the functional interface to /// which the lambda or method reference is being /// converted, represented as a String. </param> /// <param name="samMethodType"> Type of the method in the functional interface to /// which the lambda or method reference is being /// converted, represented as a MethodType. </param> /// <param name="implMethod"> The implementation method which should be called /// (with suitable adaptation of argument types, return /// types, and adjustment for captured arguments) when /// methods of the resulting functional interface instance /// are invoked. </param> /// <param name="instantiatedMethodType"> The signature of the primary functional /// interface method after type variables are /// substituted with their instantiation from /// the capture site </param> /// <param name="isSerializable"> Should the lambda be made serializable? If set, /// either the target type or one of the additional SAM /// types must extend {@code Serializable}. </param> /// <param name="markerInterfaces"> Additional interfaces which the lambda object /// should implement. </param> /// <param name="additionalBridges"> Method types for additional signatures to be /// bridged to the implementation method </param> /// <exception cref="LambdaConversionException"> If any of the meta-factory protocol /// invariants are violated </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: AbstractValidatingLambdaMetafactory(MethodHandles.Lookup caller, MethodType invokedType, String samMethodName, MethodType samMethodType, MethodHandle implMethod, MethodType instantiatedMethodType, boolean isSerializable, Class[] markerInterfaces, MethodType[] additionalBridges) throws LambdaConversionException internal AbstractValidatingLambdaMetafactory(MethodHandles.Lookup caller, MethodType invokedType, String samMethodName, MethodType samMethodType, MethodHandle implMethod, MethodType instantiatedMethodType, bool isSerializable, Class[] markerInterfaces, MethodType[] additionalBridges) { if ((caller.LookupModes() & MethodHandles.Lookup.PRIVATE) == 0) { throw new LambdaConversionException(string.Format("Invalid caller: {0}", caller.LookupClass().Name)); } this.TargetClass = caller.LookupClass(); this.InvokedType = invokedType; this.SamBase = invokedType.ReturnType(); this.SamMethodName = samMethodName; this.SamMethodType = samMethodType; this.ImplMethod = implMethod; this.ImplInfo = caller.RevealDirect(implMethod); this.ImplKind = ImplInfo.ReferenceKind; this.ImplIsInstanceMethod = ImplKind == MethodHandleInfo.REF_invokeVirtual || ImplKind == MethodHandleInfo.REF_invokeSpecial || ImplKind == MethodHandleInfo.REF_invokeInterface; this.ImplDefiningClass = ImplInfo.DeclaringClass; this.ImplMethodType = ImplInfo.MethodType; this.InstantiatedMethodType = instantiatedMethodType; this.IsSerializable = isSerializable; this.MarkerInterfaces = markerInterfaces; this.AdditionalBridges = additionalBridges; if (!SamBase.Interface) { throw new LambdaConversionException(string.Format("Functional interface {0} is not an interface", SamBase.Name)); } foreach (Class c in markerInterfaces) { if (!c.Interface) { throw new LambdaConversionException(string.Format("Marker interface {0} is not an interface", c.Name)); } } }
private static MethodHandle OffsetHandle() { //We need access to the internal char[] in order to do gc free //encoding. However for ibm jdk it is not always true that //"foo" is backed by exactly ['f', 'o', 'o'], for example single //ascii characters strings like "a" is backed by: // // value = ['0', '1', ..., 'A', 'B', ..., 'a', 'b', ...] // offset = 'a' // //Hence we need access both to `value` and `offset` MethodHandles.Lookup lookup = MethodHandles.lookup(); try { System.Reflection.FieldInfo value = typeof(string).getDeclaredField("offset"); value.Accessible = true; return(lookup.unreflectGetter(value)); } catch (Exception) { //there is no offset in String implementation return(null); } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: private static MethodHandle findCreationMethod(String methodName, MethodHandles.Lookup lookup, Class cleaner) throws IllegalAccessException, NoSuchMethodException private static MethodHandle FindCreationMethod(string methodName, MethodHandles.Lookup lookup, Type cleaner) { return(lookup.unreflect(cleaner.getDeclaredMethod(methodName, typeof(object), typeof(ThreadStart)))); }
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; }
private static CleanerHandles FindCleanerHandles() { MethodHandles.Lookup lookup = MethodHandles.lookup(); return(_globalCleanerInstance == null?FindHandlesForOldCleaner(lookup) : FindHandlesForNewCleaner(lookup)); }
// this implements the upcall from the JVM, MethodHandleNatives.makeDynamicCallSite: internal static CallSite MakeSite(MethodHandle bootstrapMethod, String name, MethodType type, Object info, Class callerClass) // Callee information: // Extra arguments for BSM, if any: // Caller information: { MethodHandles.Lookup caller = IMPL_LOOKUP.@in(callerClass); CallSite site; try { Object binding; info = MaybeReBox(info); if (info == null) { binding = bootstrapMethod.invoke(caller, name, type); } else if (!info.GetType().IsArray) { binding = bootstrapMethod.invoke(caller, name, type, info); } else { Object[] argv = (Object[])info; MaybeReBoxElements(argv); switch (argv.Length) { case 0: binding = bootstrapMethod.invoke(caller, name, type); break; case 1: binding = bootstrapMethod.invoke(caller, name, type, argv[0]); break; case 2: binding = bootstrapMethod.invoke(caller, name, type, argv[0], argv[1]); break; case 3: binding = bootstrapMethod.invoke(caller, name, type, argv[0], argv[1], argv[2]); break; case 4: binding = bootstrapMethod.invoke(caller, name, type, argv[0], argv[1], argv[2], argv[3]); break; case 5: binding = bootstrapMethod.invoke(caller, name, type, argv[0], argv[1], argv[2], argv[3], argv[4]); break; case 6: binding = bootstrapMethod.invoke(caller, name, type, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]); break; default: const int NON_SPREAD_ARG_COUNT = 3; // (caller, name, type) if (NON_SPREAD_ARG_COUNT + argv.Length > MethodType.MAX_MH_ARITY) { throw new BootstrapMethodError("too many bootstrap method arguments"); } MethodType bsmType = bootstrapMethod.Type(); MethodType invocationType = MethodType.GenericMethodType(NON_SPREAD_ARG_COUNT + argv.Length); MethodHandle typedBSM = bootstrapMethod.AsType(invocationType); MethodHandle spreader = invocationType.Invokers().SpreadInvoker(NON_SPREAD_ARG_COUNT); binding = spreader.invokeExact(typedBSM, (Object)caller, (Object)name, (Object)type, argv); break; } } //System.out.println("BSM for "+name+type+" => "+binding); if (binding is CallSite) { site = (CallSite)binding; } else { throw new ClassCastException("bootstrap method failed to produce a CallSite"); } if (!site.Target.Type().Equals(type)) { throw WrongTargetType(site.Target, type); } } catch (Throwable ex) { BootstrapMethodError bex; if (ex is BootstrapMethodError) { bex = (BootstrapMethodError)ex; } else { bex = new BootstrapMethodError("call site initialization exception", ex); } throw bex; } return(site); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: private static MethodHandle findCleanMethod(MethodHandles.Lookup lookup, Class cleaner) throws IllegalAccessException, NoSuchMethodException private static MethodHandle FindCleanMethod(MethodHandles.Lookup lookup, Type cleaner) { return(lookup.unreflect(cleaner.getDeclaredMethod("clean"))); }