IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Reflection.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2007-2014 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24
25using System;
26using System.Diagnostics;
27using System.Reflection;
29
30using IKVM.Attributes;
31using IKVM.Runtime;
32
34{
35
39 static class Reflection
40 {
41
42 static readonly ConditionalWeakTable<MethodBase, Lazy<HideFromJavaFlags>> hideFromJavaFlagCache = new ConditionalWeakTable<MethodBase, Lazy<HideFromJavaFlags>>();
43
49 internal static HideFromJavaFlags GetHideFromJavaFlags(MethodBase mb)
50 {
51 return hideFromJavaFlagCache.GetValue(mb, _ => new Lazy<HideFromJavaFlags>(() => GetHideFromJavaFlagsImpl(_), true)).Value;
52 }
53
59 static HideFromJavaFlags GetHideFromJavaFlagsImpl(MethodBase mb)
60 {
61#if FIRST_PASS
62 throw new NotImplementedException();
63#else
64 return mb.Name.StartsWith("__<", StringComparison.Ordinal) ? HideFromJavaFlags.All : JVM.Context.AttributeHelper.GetHideFromJavaFlags(mb);
65#endif
66 }
67
73 internal static bool IsHideFromStackWalk(MethodBase mb)
74 {
75#if FIRST_PASS
76 throw new NotImplementedException();
77#else
78 var type = mb.DeclaringType;
79 if (type == null ||
80 type.Assembly == typeof(object).Assembly ||
81 type.Assembly == typeof(Reflection).Assembly ||
82 type == typeof(global::java.lang.reflect.Method) ||
83 type == typeof(global::java.lang.reflect.Constructor) ||
84 (GetHideFromJavaFlags(mb) & HideFromJavaFlags.StackWalk) != 0)
85 return true;
86
87 return false;
88#endif
89 }
90
91 public static global::java.lang.Class getCallerClass()
92 {
93#if FIRST_PASS
94 throw new NotImplementedException();
95#else
96 throw new global::java.lang.InternalError("CallerSensitive annotation expected at frame 1");
97#endif
98 }
99
100 // NOTE this method is hooked up explicitly through map.xml to prevent inlining of the native stub
101 // and tail-call optimization in the native stub.
102 public static global::java.lang.Class getCallerClass(int realFramesToSkip)
103 {
104#if FIRST_PASS
105 throw new NotImplementedException();
106#else
107 if (realFramesToSkip <= 0)
108 return (global::java.lang.Class)ClassLiteral<global::sun.reflect.Reflection>.Value;
109
110 for (int i = 2; ;)
111 {
112 var method = new StackFrame(i++, false).GetMethod();
113 if (method == null)
114 return null;
115
116 if (IsHideFromStackWalk(method))
117 continue;
118
119 // HACK we skip HideFromJavaFlags.StackTrace too because we want to skip the LambdaForm methods
120 // that are used by late binding
121 if ((GetHideFromJavaFlags(method) & HideFromJavaFlags.StackTrace) != 0)
122 continue;
123
124 if (--realFramesToSkip == 0)
125 return JVM.Context.ClassLoaderFactory.GetJavaTypeFromType(method.DeclaringType).ClassObject;
126 }
127#endif
128 }
129
130 public static int getClassAccessFlags(global::java.lang.Class clazz)
131 {
132 // the mask comes from JVM_RECOGNIZED_CLASS_MODIFIERS in src/hotspot/share/vm/prims/jvm.h
133 int mods = (int)RuntimeJavaType.FromClass(clazz).Modifiers & 0x7631;
134 // interface implies abstract
135 mods |= (mods & 0x0200) << 1;
136 return mods;
137 }
138
139 public static bool checkInternalAccess(global::java.lang.Class currentClass, global::java.lang.Class memberClass)
140 {
141 var current = RuntimeJavaType.FromClass(currentClass);
142 var member = RuntimeJavaType.FromClass(memberClass);
143 return member.IsInternal && member.InternalsVisibleTo(current);
144 }
145
146 }
147
148}
IKVM.Reflection.Assembly Assembly
IKVM.Reflection.MethodBase MethodBase
Provides the implementations of the native methods in global::sun.reflect.Reflection.
Definition Reflection.cs:40
static global::java.lang.Class getCallerClass()
Definition Reflection.cs:91
static global::java.lang.Class getCallerClass(int realFramesToSkip)
static int getClassAccessFlags(global::java.lang.Class clazz)
static bool checkInternalAccess(global::java.lang.Class currentClass, global::java.lang.Class memberClass)
Provides a static cache of java.lang.Class instances per .NET type.
static object Value
Gets the class literal value, caching the result.
Main state of the running JVM.
AttributeHelper AttributeHelper
Gets the AttributeHelper associated with this instance of the runtime.
RuntimeClassLoaderFactory ClassLoaderFactory
Gets the RuntimeClassLoaderFactory associated with this instance of the runtime.