Example #1
0
        private static TypeWrapper LoadTypeWrapper(string clazz, [email protected] callerId)
        {
#if FIRST_PASS
            return(null);
#else
            try
            {
                TypeWrapper           context = TypeWrapper.FromClass(callerId.getCallerClass());
                TypeWrapper           wrapper = ClassLoaderWrapper.FromCallerID(callerId).LoadClassByDottedName(clazz);
                java.lang.ClassLoader loader  = callerId.getCallerClassLoader();
                if (loader != null)
                {
                    loader.checkPackageAccess(wrapper.ClassObject, callerId.getCallerClass().pd);
                }
                if (!wrapper.IsAccessibleFrom(context))
                {
                    throw new java.lang.IllegalAccessError("Try to access class " + wrapper.Name + " from class " + context.Name);
                }
                wrapper.Finish();
                return(wrapper);
            }
            catch (RetargetableJavaException x)
            {
                throw x.ToJava();
            }
#endif
        }
Example #2
0
        private static java.lang.invoke.MethodHandle DynamicLoadMethodHandleImpl(int kind, string clazz, string name, string sig, [email protected] callerID)
        {
#if FIRST_PASS
            return(null);
#else
            java.lang.Class refc = LoadTypeWrapper(clazz, callerID).ClassObject;
            try
            {
                switch ((ClassFile.RefKind)kind)
                {
                case ClassFile.RefKind.getStatic:
                case ClassFile.RefKind.putStatic:
                case ClassFile.RefKind.getField:
                case ClassFile.RefKind.putField:
                    java.lang.Class type = ClassLoaderWrapper.FromCallerID(callerID).FieldTypeWrapperFromSig(sig).ClassObject;
                    return(java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(callerID.getCallerClass(), kind, refc, name, type));

                default:
                    java.lang.invoke.MethodType mt = null;
                    DynamicLoadMethodType(ref mt, sig, callerID);
                    // HACK linkMethodHandleConstant is broken for MethodHandle.invoke[Exact]
                    if (kind == (int)ClassFile.RefKind.invokeVirtual && refc == CoreClasses.java.lang.invoke.MethodHandle.Wrapper.ClassObject)
                    {
                        switch (name)
                        {
                        case "invokeExact":
                            return(java.lang.invoke.MethodHandles.exactInvoker(mt));

                        case "invoke":
                            return(java.lang.invoke.MethodHandles.invoker(mt));
                        }
                    }
                    java.lang.Class caller = callerID.getCallerClass();
                    DynamicTypeWrapper.FinishContext.HostCallerID hostCallerID = callerID as DynamicTypeWrapper.FinishContext.HostCallerID;
                    if (hostCallerID != null)
                    {
                        caller = hostCallerID.host.ClassObject;
                    }
                    return(java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(caller, kind, refc, name, mt));
                }
            }
            catch (RetargetableJavaException x)
            {
                throw x.ToJava();
            }
#endif
        }
Example #3
0
        private static TypeWrapper LoadTypeWrapper(string clazz, [email protected] callerId)
        {
#if FIRST_PASS
            return(null);
#else
            try
            {
                TypeWrapper context = TypeWrapper.FromClass(callerId.getCallerClass());
#if WINRT
                RuntimeReflectionHelper.Instance.Mark(clazz);
#endif
                System.Diagnostics.Debug.WriteLine("Trying to load " + clazz);
                TypeWrapper wrapper = null;
                try {
                    wrapper = ClassLoaderWrapper.FromCallerID(callerId).LoadClassByDottedName(clazz);
                } catch (NotImplementedException nie)
                {
#if WINRT
                    RuntimeReflectionHelper.Instance.Mark(clazz);
#endif
                    System.Diagnostics.Debug.WriteLine("Not implemented " + clazz);
                    throw nie;
                }

                java.lang.ClassLoader loader = callerId.getCallerClassLoader();
                if (loader != null)
                {
                    loader.checkPackageAccess(wrapper.ClassObject, callerId.getCallerClass().pd);
                }
                if (!wrapper.IsAccessibleFrom(context))
                {
                    throw new java.lang.IllegalAccessError("Try to access class " + wrapper.Name + " from class " + context.Name);
                }
                wrapper.Finish();
                return(wrapper);
            }
            catch (RetargetableJavaException x)
            {
                throw x.ToJava();
            }
#endif
        }
Example #4
0
        private static void DynamicLoadMethodTypeImpl(ref java.lang.invoke.MethodType cache, string sig, [email protected] callerID)
        {
#if !FIRST_PASS
            try
            {
                ClassLoaderWrapper loader = ClassLoaderWrapper.FromCallerID(callerID);
                TypeWrapper[]      args   = loader.ArgTypeWrapperListFromSig(sig, LoadMode.LoadOrThrow);
                java.lang.Class[]  ptypes = new java.lang.Class[args.Length];
                for (int i = 0; i < ptypes.Length; i++)
                {
                    ptypes[i] = args[i].ClassObject;
                }
                Interlocked.CompareExchange(ref cache, java.lang.invoke.MethodType.methodType(loader.RetTypeWrapperFromSig(sig, LoadMode.LoadOrThrow).ClassObject, ptypes), null);
            }
            catch (RetargetableJavaException x)
            {
                throw x.ToJava();
            }
#endif
        }
Example #5
0
        private static java.lang.invoke.MethodHandle DynamicLoadMethodHandleImpl(int kind, string clazz, string name, string sig, [email protected] callerID)
        {
#if FIRST_PASS
            return(null);
#else
            java.lang.invoke.MethodHandles.Lookup lookup = new java.lang.invoke.MethodHandles.Lookup(callerID.getCallerClass(),
                                                                                                     java.lang.invoke.MethodHandles.Lookup.PUBLIC |
                                                                                                     java.lang.invoke.MethodHandles.Lookup.PRIVATE |
                                                                                                     java.lang.invoke.MethodHandles.Lookup.PROTECTED |
                                                                                                     java.lang.invoke.MethodHandles.Lookup.PACKAGE,
                                                                                                     true);
            java.lang.Class refc = LoadTypeWrapper(clazz, callerID).ClassObject;
            try
            {
                switch ((ClassFile.RefKind)kind)
                {
                case ClassFile.RefKind.getStatic:
                case ClassFile.RefKind.putStatic:
                case ClassFile.RefKind.getField:
                case ClassFile.RefKind.putField:
                    java.lang.Class type = ClassLoaderWrapper.FromCallerID(callerID).FieldTypeWrapperFromSig(sig).ClassObject;
                    switch ((ClassFile.RefKind)kind)
                    {
                    case ClassFile.RefKind.getStatic:
                        return(lookup.findStaticGetter(refc, name, type));

                    case ClassFile.RefKind.putStatic:
                        return(lookup.findStaticSetter(refc, name, type));

                    case ClassFile.RefKind.getField:
                        return(lookup.findGetter(refc, name, type));

                    case ClassFile.RefKind.putField:
                        return(lookup.findSetter(refc, name, type));

                    default:
                        throw new InvalidOperationException();
                    }

                default:
                    java.lang.invoke.MethodType mt = null;
                    DynamicLoadMethodType(ref mt, sig, callerID);
                    switch ((ClassFile.RefKind)kind)
                    {
                    case ClassFile.RefKind.invokeInterface:
                        return(lookup.findVirtual(refc, name, mt));

                    case ClassFile.RefKind.invokeSpecial:
                        return(lookup.findSpecial(refc, name, mt, callerID.getCallerClass()));

                    case ClassFile.RefKind.invokeStatic:
                        return(lookup.findStatic(refc, name, mt));

                    case ClassFile.RefKind.invokeVirtual:
                        return(lookup.findVirtual(refc, name, mt));

                    case ClassFile.RefKind.newInvokeSpecial:
                        return(lookup.findConstructor(refc, mt));

                    default:
                        throw new InvalidOperationException();
                    }
                }
            }
            catch (RetargetableJavaException x)
            {
                throw x.ToJava();
            }
            catch (java.lang.ReflectiveOperationException x)
            {
                throw new java.lang.IncompatibleClassChangeError().initCause(x);
            }
#endif
        }