IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
TypeName.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2009-2012 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;
25
26namespace IKVM.Reflection
27{
28
29 // this respresents a type name as in metadata:
30 // - ns will be null for empty the namespace (never the empty string)
31 // - the strings are not escaped
32 readonly struct TypeName : IEquatable<TypeName>
33 {
34
35 public static bool operator ==(TypeName o1, TypeName o2)
36 {
37 return o1.ns == o2.ns && o1.name == o2.name;
38 }
39
40 public static bool operator !=(TypeName o1, TypeName o2)
41 {
42 return o1.ns != o2.ns || o1.name != o2.name;
43 }
44
45 readonly string ns;
46 readonly string name;
47
54 internal TypeName(string ns, string name)
55 {
56 this.ns = ns;
57 this.name = name ?? throw new ArgumentNullException("name");
58 }
59
60 internal string Name => name;
61
62 internal string Namespace => ns;
63
64 public override int GetHashCode() => ns == null ? name.GetHashCode() : ns.GetHashCode() * 37 + name.GetHashCode();
65
66 public override bool Equals(object obj)
67 {
68 var other = obj as TypeName?;
69 return other != null && other.Value == this;
70 }
71
72 public override string ToString()
73 {
74 return ns == null ? name : ns + "." + name;
75 }
76
77 bool IEquatable<TypeName>.Equals(TypeName other)
78 {
79 return this == other;
80 }
81
82 internal bool Matches(string fullName)
83 {
84 if (ns == null)
85 return name == fullName;
86
87 if (ns.Length + 1 + name.Length == fullName.Length)
88 return fullName.StartsWith(ns, StringComparison.Ordinal) && fullName[ns.Length] == '.' && fullName.EndsWith(name, StringComparison.Ordinal);
89
90 return false;
91 }
92
93 internal TypeName ToLowerInvariant() => new TypeName(ns?.ToLowerInvariant(), name.ToLowerInvariant());
94
95 internal static TypeName Split(string name)
96 {
97 int dot = name.LastIndexOf('.');
98 if (dot == -1)
99 return new TypeName(null, name);
100 else
101 return new TypeName(name.Substring(0, dot), name.Substring(dot + 1));
102 }
103
104 }
105
106}
global::java.lang.invoke.LambdaForm.Name Name
readonly string name
Definition TypeName.cs:46
override int GetHashCode()
override string ToString()
Definition TypeName.cs:72
static bool operator==(TypeName o1, TypeName o2)
Definition TypeName.cs:35
static bool operator!=(TypeName o1, TypeName o2)
Definition TypeName.cs:40
readonly string ns
Definition TypeName.cs:45
override bool Equals(object obj)
Definition TypeName.cs:66