Beispiel #1
0
        /*non-public*/ internal virtual MethodHandle MakeDynamicInvoker()
        {
            MethodHandle getTarget = GET_TARGET.BindArgumentL(0, this);
            MethodHandle invoker   = MethodHandles.ExactInvoker(this.Type());

            return(MethodHandles.FoldArguments(invoker, getTarget));
        }
Beispiel #2
0
 /// <summary>
 /// Returns a method handle which always delegates either to the target or the fallback.
 /// The method handle will delegate to the target exactly as long as the switch point is valid.
 /// After that, it will permanently delegate to the fallback.
 /// <para>
 /// The target and fallback must be of exactly the same method type,
 /// and the resulting combined method handle will also be of this type.
 ///
 /// </para>
 /// </summary>
 /// <param name="target"> the method handle selected by the switch point as long as it is valid </param>
 /// <param name="fallback"> the method handle selected by the switch point after it is invalidated </param>
 /// <returns> a combined method handle which always calls either the target or fallback </returns>
 /// <exception cref="NullPointerException"> if either argument is null </exception>
 /// <exception cref="IllegalArgumentException"> if the two method types do not match </exception>
 /// <seealso cref= MethodHandles#guardWithTest </seealso>
 public virtual MethodHandle GuardWithTest(MethodHandle target, MethodHandle fallback)
 {
     if (Mcs.Target == K_false)
     {
         return(fallback);                // already invalid
     }
     return(MethodHandles.GuardWithTest(McsInvoker, target, fallback));
 }
        internal virtual LambdaForm SpreadArgumentsForm(int pos, Class arrayType, int arrayLength)
        {
            Class elementType     = arrayType.ComponentType;
            Class erasedArrayType = arrayType;

            if (!elementType.Primitive)
            {
                erasedArrayType = typeof(Object[]);
            }
            BasicType bt             = basicType(elementType);
            int       elementTypeKey = bt.ordinal();

            if (bt.basicTypeClass() != elementType)
            {
                if (elementType.Primitive)
                {
                    elementTypeKey = TYPE_LIMIT + Wrapper.forPrimitiveType(elementType).ordinal();
                }
            }
            Transform  key  = Transform.Of(Transform.Kind.SPREAD_ARGS, pos, elementTypeKey, arrayLength);
            LambdaForm form = GetInCache(key);

            if (form != null)
            {
                assert(form.Arity_Renamed == LambdaForm.Arity_Renamed - arrayLength + 1);
                return(form);
            }
            LambdaFormBuffer buf = Buffer();

            buf.StartEdit();

            assert(pos <= MethodType.MAX_JVM_ARITY);
            assert(pos + arrayLength <= LambdaForm.Arity_Renamed);
            assert(pos > 0);             // cannot spread the MH arg itself

            Name spreadParam = new Name(L_TYPE);
            Name checkSpread = new Name(MethodHandleImpl.Lazy.NF_checkSpreadArgument, spreadParam, arrayLength);

            // insert the new expressions
            int exprPos = LambdaForm.Arity();

            buf.InsertExpression(exprPos++, checkSpread);
            // adjust the arguments
            MethodHandle aload = MethodHandles.ArrayElementGetter(erasedArrayType);

            for (int i = 0; i < arrayLength; i++)
            {
                Name loadArgument = new Name(aload, spreadParam, i);
                buf.InsertExpression(exprPos + i, loadArgument);
                buf.ReplaceParameterByCopy(pos + i, exprPos + i);
            }
            buf.InsertParameter(pos, spreadParam);

            form = buf.EndEdit();
            return(PutInCache(key, form));
        }
Beispiel #4
0
 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);
     }
 }
Beispiel #5
0
 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);
     }
 }
Beispiel #6
0
 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);
     }
 }
Beispiel #7
0
//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;
            }
        }
Beispiel #8
0
 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);
     }
 }
Beispiel #9
0
        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;
        }
Beispiel #10
0
 private static CleanerHandles FindCleanerHandles()
 {
     MethodHandles.Lookup lookup = MethodHandles.lookup();
     return(_globalCleanerInstance == null?FindHandlesForOldCleaner(lookup) : FindHandlesForNewCleaner(lookup));
 }