summaryrefslogtreecommitdiff
path: root/src/msil/ch/epfl/lamp/compiler/msil/util/VJSAssembly.java2
blob: ada30ca7c952a0d4d05ebea326c845a1eb14d923 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// $Id: VJSAssembly.java 145 2005-05-06 07:55:25Z mihaylov $

package ch.epfl.lamp.compiler.msil.util;

import java.lang.reflect.*;
import java.util.HashMap;

import ch.epfl.lamp.compiler.msil.*;

/**
 * Represents the Microsoft vjslib.dll assembly.
 */
public final class VJSAssembly extends Assembly {

    private VJSAssembly(AssemblyName an) { super(an); }

    protected void loadModules() {
	VJSLIB_DLL.init();
	addModule("vjslib.dll", VJSLIB_DLL);
    }

    public static final Assembly VJSLIB;
    private static final VJSModule VJSLIB_DLL;

    static {
	AssemblyName an = new AssemblyName();
	an.Name = "vjslib";
	an.Version = new Version(1, 0, 3300, 0);
	an.SetPublicKeyToken(new byte [] {(byte)0xB0, 0x3F, 0x5F, 0x7F,
					  0x11, (byte)0xD5, 0x0A, 0x3A});
	VJSLIB = new VJSAssembly(an);
	VJSLIB_DLL = new VJSModule(VJSLIB);
    }


    //##########################################################################

    private static final class VJSModule extends Module {

	VJSModule(Assembly vjslib) {
	    super("vjslib", "vjslib.dll", "vjslib.dll", vjslib);
	}

	void init() {
	    addType("void",    Type.GetType("System.Void"));
	    addType("boolean", Type.GetType("System.Boolean"));
	    addType("char",    Type.GetType("System.Char"));
	    addType("float",   Type.GetType("System.Single"));
	    addType("double",  Type.GetType("System.Double"));
	    addType("byte",    Type.GetType("System.SByte"));
	    addType("short",   Type.GetType("System.Int16"));
	    addType("int",     Type.GetType("System.Int32"));
	    addType("long",    Type.GetType("System.Int64"));
	    addType("java.lang.Object", Type.GetType("System.Object"));
	    addType("java.lang.String", Type.GetType("System.String"));
	}

	public Type[] GetTypes() {
	    throw new RuntimeException("Operation not supported!");
	}

	public Type GetType(String name) {
	    // look it up in the typesMap
	    Type type = super.GetType(name);
	    if (type != null)
		return type;
	    Class clazz = null;
	    try {
		clazz = Class.forName(name);
	    } catch (ClassNotFoundException e) {}
	    return getType(clazz);
	}

	Type getType(Class clazz) {
	    if (clazz == null) return null;
	    Type type = null;
	    if (clazz.isArray()) {
		Type elemType = getType(clazz.getComponentType());
		type = super.GetType(elemType.FullName + "[]");
		if (type != null)
		    return type;
		type = Type.mkArray(elemType, 1);
	    } else {
		type = super.GetType(clazz.getName());
		if (type != null)
		    return type;
		type = new JavaType(clazz);
	    }
	    return addType(type);
	}

	Type[] getTypes(Class [] classes) {
	    if (classes.length == 0) return Type.EmptyTypes;
	    Type [] types = new Type[classes.length];
	    for (int i = 0; i < classes.length; i++)
		types[i] = getType(classes[i]);
	    return types;
	}

    } // class VJSModule

    //##########################################################################

    private static final class JavaType extends Type {

	//######################################################################

	private final Class clazz;

	JavaType(Class clazz, int attrs, String name,
		 Type baseType, Type[] interfaces, Type declType)
	{
	    super(VJSLIB_DLL, attrs, name, baseType, interfaces, declType, 0);
	    this.clazz = clazz;
	}

	JavaType(Class clazz, String name) {
	    super(VJSLIB_DLL,
		  translateTypeAttrs(clazz.getModifiers()),
		  name,
		  VJSLIB_DLL.getType(clazz.getSuperclass()),
		  null,
		  VJSLIB_DLL.getType(clazz.getDeclaringClass()),
		  0);
	    this.clazz = clazz;
	}

	JavaType(Class clazz) {
	    this(clazz, clazz.getName());
	}

	//######################################################################
	// lazy JavaType member loaders

	protected void loadInterfaces() {
	    this.interfaces = VJSLIB_DLL.getTypes(clazz.getInterfaces());
	}

	protected void loadNestedTypes() {
	    Class [] nested = clazz.getDeclaredClasses();
	    this.nestedTypes = new Type[nested.length];
	    for (int i = 0; i < nested.length; i++) {
		String name = nested[i].getName();
		name = name.substring(name.lastIndexOf('$') + 1);
		nestedTypes[i] = new JavaType(nested[i], name);
	    }
	}

	protected void loadFields() {
	    Field [] jfields = clazz.getDeclaredFields();
	    FieldInfo[] fields = new FieldInfo[jfields.length];
	    for (int i = 0; i < jfields.length; i++)
		fields[i] = new JavaFieldInfo(jfields[i]);
	    this.fields = fields;
	}

	protected void loadMethods() {
	    Constructor[] jconstrs = clazz.getDeclaredConstructors();
	    this.constructors = new ConstructorInfo[jconstrs.length];
	    for (int i = 0; i < jconstrs.length; i++)
		this.constructors[i] = new JavaConstructorInfo(jconstrs[i]);

	    Method[] jmethods = clazz.getDeclaredMethods();
	    this.methods = new MethodInfo[jmethods.length];
	    for (int i = 0; i < jmethods.length; i++)
		this.methods[i] = JavaMethodInfo.getMethod(jmethods[i]);
	}

	//######################################################################
	// type resolution methods


	static int translateTypeAttrs(int mods) {
	    int attr = 0;

	    if (Modifier.isInterface(mods))
		attr |= TypeAttributes.Interface;
	    else
		attr |= TypeAttributes.Class;

	    if (Modifier.isAbstract(mods))
		attr |= TypeAttributes.Abstract;

	    if (Modifier.isFinal(mods))
		attr |= TypeAttributes.Sealed;

	    if (Modifier.isPublic(mods))
		attr |= TypeAttributes.Public;
	    else
		attr |= TypeAttributes.NotPublic;

	    return attr;
	}

	//######################################################################
    } // class JavaType

    //##########################################################################

    private static final class JavaFieldInfo extends FieldInfo {
	JavaFieldInfo(Field field) {
	    super(field.getName(),
		  VJSLIB_DLL.getType(field.getDeclaringClass()),
		  translateFieldAttrs(field.getModifiers()),
		  VJSLIB_DLL.getType(field.getType()));
	}

	//translate java modifiers into corresponding .NET Field attributes
	static short translateFieldAttrs(int mods) {
	    short attr = 0;

	    if (Modifier.isFinal(mods))
		attr |= FieldAttributes.InitOnly;

	    if (Modifier.isPublic(mods))
		attr |= FieldAttributes.Public;
	    else if (Modifier.isProtected(mods))
		attr |= FieldAttributes.FamORAssem;
	    else if (Modifier.isPrivate(mods))
		attr |= FieldAttributes.Private;

	    if (Modifier.isStatic(mods))
		attr |= FieldAttributes.Static;

	    return attr;
	}

    } // class JavaFieldInfo

    //##########################################################################

    private static final class JavaConstructorInfo extends ConstructorInfo {
	JavaConstructorInfo(Constructor constr) {
	    super(VJSLIB_DLL.getType(constr.getDeclaringClass()),
		  JavaMethodInfo.translateMethodAttrs(constr.getModifiers()),
		  VJSLIB_DLL.getTypes(constr.getParameterTypes()));
	}
    }

    //##########################################################################

    private static final class JavaMethodInfo extends MethodInfo {

	JavaMethodInfo(String name, Type declType, int attrs,
		       Type returnType, Type[] paramTypes) {
	    super(name, declType, declType, attrs, returnType, paramTypes);
	}

	static MethodInfo getMethod(Method method) {
	    String name = method.getName();
	    if (name.equals("toString"))
		name = "ToString";
	    else if(name.equals("equals"))
		name = "Equals";
	    else if (name.equals("hashCode"))
		name = "GetHashCode";
	    return new JavaMethodInfo
		(name, VJSLIB_DLL.getType(method.getDeclaringClass()),
		 translateMethodAttrs(method.getModifiers()),
		 VJSLIB_DLL.getType(method.getReturnType()),
		 VJSLIB_DLL.getTypes(method.getParameterTypes()));
	}

	static short translateMethodAttrs(int mods) {
	    short attr = 0;

	    if (Modifier.isAbstract(mods))
		attr |= MethodAttributes.Abstract;

	    if (Modifier.isFinal(mods))
		attr |= MethodAttributes.Final;

	    if (Modifier.isPublic(mods))
		attr |= MethodAttributes.Public;
	    else if (Modifier.isProtected(mods))
		attr |= MethodAttributes.FamORAssem;
	    else if (Modifier.isPrivate(mods))
		attr |= MethodAttributes.Private;

	    if (Modifier.isStatic(mods))
		attr |= MethodAttributes.Static;
	    else
		attr |= MethodAttributes.Virtual;

	    //if (Modifier.isSynchronized(mods))
	    //	attr |= MethodAttributes.Synchronized;

	    return attr;
	}
    } // class JavaMethodInfo

    //##########################################################################

} // class VJSAssembly