IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Assertions.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2007-2013 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*/
24using System;
25using System.Globalization;
26
27using IKVM.Runtime;
28
29namespace IKVM.Runtime
30{
31
32 public static class Assertions
33 {
34
35 private static bool sysAsserts;
36 private static bool userAsserts;
37 private static OptionNode classes;
38 private static OptionNode packages;
39
40 private sealed class OptionNode
41 {
42 internal readonly string name;
43 internal readonly bool enabled;
44 internal readonly OptionNode next;
45
46 internal OptionNode(string name, bool enabled, OptionNode next)
47 {
48 this.name = name;
49 this.enabled = enabled;
50 this.next = next;
51 }
52 }
53
54 private static void AddOption(string classOrPackage, bool enabled)
55 {
56 if (classOrPackage == null)
57 {
58 throw new ArgumentNullException("classOrPackage");
59 }
60
61 if (classOrPackage.EndsWith("..."))
62 {
63 packages = new OptionNode(classOrPackage.Substring(0, classOrPackage.Length - 3), enabled, packages);
64 }
65 else
66 {
67 classes = new OptionNode(classOrPackage, enabled, classes);
68 }
69 }
70
71 public static void EnableAssertions(string classOrPackage)
72 {
73 AddOption(classOrPackage, true);
74 }
75
76 public static void DisableAssertions(string classOrPackage)
77 {
78 AddOption(classOrPackage, false);
79 }
80
81 public static void EnableAssertions()
82 {
83 userAsserts = true;
84 }
85
86 public static void DisableAssertions()
87 {
88 userAsserts = false;
89 }
90
91 public static void EnableSystemAssertions()
92 {
93 sysAsserts = true;
94 }
95
96 public static void DisableSystemAssertions()
97 {
98 sysAsserts = false;
99 }
100
101 internal static bool IsEnabled(RuntimeJavaType tw)
102 {
103 string className = tw.Name;
104
105 // match class name
106 for (OptionNode n = classes; n != null; n = n.next)
107 {
108 if (n.name == className)
109 {
110 return n.enabled;
111 }
112 }
113
114 // match package name
115 if (packages != null)
116 {
117 int len = className.Length;
118 while (len > 0 && className[--len] != '.') ;
119
120 do
121 {
122 for (OptionNode n = packages; n != null; n = n.next)
123 {
124 if (String.Compare(n.name, 0, className, 0, len, false, CultureInfo.InvariantCulture) == 0 && len == n.name.Length)
125 {
126 return n.enabled;
127 }
128 }
129 while (len > 0 && className[--len] != '.') ;
130 } while (len > 0);
131 }
132
133 return tw.ClassLoader == tw.Context.ClassLoaderFactory.GetBootstrapClassLoader() ? sysAsserts : userAsserts;
134 }
135
136 private static int Count(OptionNode n)
137 {
138 int count = 0;
139 while (n != null)
140 {
141 count++;
142 n = n.next;
143 }
144 return count;
145 }
146
147 internal static object RetrieveDirectives()
148 {
149#if FIRST_PASS
150 return null;
151#else
152 java.lang.AssertionStatusDirectives asd = new java.lang.AssertionStatusDirectives();
153 string[] arrStrings = new string[Count(classes)];
154 bool[] arrBools = new bool[arrStrings.Length];
155 OptionNode n = classes;
156 for (int i = 0; i < arrStrings.Length; i++)
157 {
158 arrStrings[i] = n.name;
159 arrBools[i] = n.enabled;
160 n = n.next;
161 }
162 asd.classes = arrStrings;
163 asd.classEnabled = arrBools;
164 arrStrings = new string[Count(packages)];
165 arrBools = new bool[arrStrings.Length];
166 n = packages;
167 for (int i = 0; i < arrStrings.Length; i++)
168 {
169 arrStrings[i] = n.name;
170 arrBools[i] = n.enabled;
171 n = n.next;
172 }
173 asd.packages = arrStrings;
174 asd.packageEnabled = arrBools;
175 asd.deflt = userAsserts;
176 return asd;
177#endif
178 }
179
180 }
181
182}
static void EnableSystemAssertions()
Definition Assertions.cs:91
static void DisableAssertions(string classOrPackage)
Definition Assertions.cs:76
static void DisableAssertions()
Definition Assertions.cs:86
static void EnableAssertions(string classOrPackage)
Definition Assertions.cs:71
static void DisableSystemAssertions()
Definition Assertions.cs:96
static void EnableAssertions()
Definition Assertions.cs:81
RuntimeClassLoaderFactory ClassLoaderFactory
Gets the RuntimeClassLoaderFactory associated with this instance of the runtime.