IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
RuntimeAnonymousJavaType.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2015 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.Collections.Generic;
26using System.Reflection;
27using System.Text;
28
29using IKVM.Attributes;
30
31namespace IKVM.Runtime
32{
33
34#if !IMPORTER && !EXPORTER
35
39 sealed class RuntimeAnonymousJavaType : RuntimeJavaType
40 {
41
42 readonly Type type;
43
49 internal RuntimeAnonymousJavaType(RuntimeContext context, Type type) :
50 base(context, TypeFlags.Anonymous, Modifiers.Final | Modifiers.Synthetic, GetName(context, type))
51 {
52 this.type = type;
53 }
54
55 internal static bool IsAnonymous(RuntimeContext context, Type type)
56 {
57 return type.IsSpecialName && type.Name.StartsWith(NestedTypeName.IntrinsifiedAnonymousClass, StringComparison.Ordinal) && context.AttributeHelper.IsJavaModule(type.Module);
58 }
59
60 private static string GetName(RuntimeContext context, Type type)
61 {
62 return context.ClassLoaderFactory.GetJavaTypeFromType(type.DeclaringType).Name + type.Name.Replace(NestedTypeName.IntrinsifiedAnonymousClass, "$$Lambda$");
63 }
64
65 internal override RuntimeClassLoader ClassLoader => Context.ClassLoaderFactory.GetJavaTypeFromType(type.DeclaringType).ClassLoader;
66
67 internal override Type TypeAsTBD
68 {
69 get { return type; }
70 }
71
72 internal override RuntimeJavaType BaseTypeWrapper
73 {
74 get { return Context.JavaBase.TypeOfJavaLangObject; }
75 }
76
77 internal override RuntimeJavaType[] Interfaces
78 {
79 get
80 {
81 var interfaces = GetImplementedInterfacesAsTypeWrappers(Context, type);
82 if (type.IsSerializable)
83 {
84 // we have to remove the System.Runtime.Serialization.ISerializable interface
85 var list = new List<RuntimeJavaType>(interfaces);
86 list.RemoveAll(Context.Serialization.IsISerializable);
87 return list.ToArray();
88 }
89
90 return interfaces;
91 }
92 }
93
94 protected override void LazyPublishMembers()
95 {
96 var methods = new List<RuntimeJavaMethod>();
97 foreach (var mi in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly))
98 {
99 if (mi.IsSpecialName)
100 {
101 // we use special name to hide default methods
102 }
103 else if (mi.IsPublic)
104 {
105 GetSig(mi, out var returnType, out var parameterTypes, out var signature);
106 methods.Add(new RuntimeTypicalJavaMethod(this, mi.Name, signature, mi, returnType, parameterTypes, Modifiers.Public, MemberFlags.None));
107 }
108 else if (mi.Name == "writeReplace")
109 {
110 methods.Add(new RuntimeTypicalJavaMethod(this, "writeReplace", "()Ljava.lang.Object;", mi, Context.JavaBase.TypeOfJavaLangObject, Array.Empty<RuntimeJavaType>(), Modifiers.Private | Modifiers.Final, MemberFlags.None));
111 }
112 }
113
114 SetMethods(methods.ToArray());
115
116 var fields = new List<RuntimeJavaField>();
117 foreach (var fi in type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly))
118 {
119 var fieldType = RuntimeManagedByteCodeJavaType.GetFieldTypeWrapper(Context, fi);
120 fields.Add(new RuntimeSimpleJavaField(this, fieldType, fi, fi.Name, fieldType.SigName, new ExModifiers(Modifiers.Private | Modifiers.Final, false)));
121 }
122
123 SetFields(fields.ToArray());
124 }
125
126 void GetSig(MethodInfo mi, out RuntimeJavaType returnType, out RuntimeJavaType[] parameterTypes, out string signature)
127 {
128 returnType = RuntimeManagedByteCodeJavaType.GetParameterTypeWrapper(Context, mi.ReturnParameter);
129 var parameters = mi.GetParameters();
130 parameterTypes = new RuntimeJavaType[parameters.Length];
131
132 var sb = new ValueStringBuilder(256);
133 sb.Append("(");
134 for (int i = 0; i < parameters.Length; i++)
135 {
136 parameterTypes[i] = RuntimeManagedByteCodeJavaType.GetParameterTypeWrapper(Context, parameters[i]);
137 sb.Append(parameterTypes[i].SigName);
138 }
139 sb.Append(')');
140 sb.Append(returnType.SigName);
141 signature = sb.ToString();
142 }
143 }
144
145#endif
146
147}
IKVM.Reflection.Type Type
IKVM.Reflection.MethodInfo MethodInfo
Represents an intrinsified anonymous class. Currently only used by LambdaMetafactory.
Runtime support for a class loader.
Maintains services relevant to an instane of the IKVM runtime.
AttributeHelper AttributeHelper
Gets the AttributeHelper associated with this instance of the runtime.
Serialization Serialization
Gets the Serialization associated with this instance of the runtime.
CoreClasses JavaBase
Gets the CoreClasses associated with this instance of the runtime.
RuntimeClassLoaderFactory ClassLoaderFactory
Gets the RuntimeClassLoaderFactory associated with this instance of the runtime.
Represents a runtime Java type derived from a .NET assembly which was the result of the IKVM compiler...
Field wrapper implementation for standard fields.
MemberFlags
Describes various options applied to a member.