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
|
/*
* System.Reflection-like API for access to .NET assemblies (DLL & EXE)
*/
// $Id$
package ch.epfl.lamp.compiler.msil;
import ch.epfl.lamp.compiler.msil.util.Table;
import ch.epfl.lamp.compiler.msil.util.Table.AssemblyDef;
import ch.epfl.lamp.compiler.msil.util.Table.ModuleDef;
import java.util.HashMap;
import java.util.Iterator;
import java.io.File;
import java.io.FileNotFoundException;
/**
* Defines an Assembly, which is a reusable, versionable, and self-describing
* building block of a common language runtime application.
*
* @author Nikolay Mihaylov
* @version 1.0
*/
public abstract class Assembly extends CustomAttributeProvider {
//##########################################################################
// static members
// all the assemblies
public static final HashMap assemblies = new HashMap();
/** Loads an assembly from the specified path. */
public static Assembly LoadFrom(String assemblyFileName) {
File afile = new File(assemblyFileName);
return LoadFrom(afile.getParentFile(), afile.getName());
}
/** Loads an assembly with the given name from the given directory. */
public static Assembly LoadFrom(File dir, String name) {
File file = null;
PEFile pefile = null;
// try {
// if (dir == null)
// dir = new File(".");
// dir = dir.getCanonicalFile();
// } catch (java.io.IOException e) {}
if (name.endsWith(".exe") || name.endsWith(".EXE") ||
name.endsWith(".dll") || name.endsWith(".DLL"))
{
file = new File(dir, name);
pefile = getPEFile(file);
name = name.substring(0, name.length() - 4);
}
File adir = pefile == null ? new File(dir, name) : null;
if (pefile == null) {
file = new File(dir, name + ".dll");
pefile = getPEFile(file);
}
if (pefile == null) {
file = new File(dir, name + ".DLL");
pefile = getPEFile(file);
}
if (pefile == null && adir.exists()) {
file = new File(adir, name + ".dll");
pefile = getPEFile(file);
}
if (pefile == null && adir.exists()) {
file = new File(adir, name + ".DLL");
pefile = getPEFile(file);
}
if (pefile == null) {
file = new File(dir, name + ".exe");
pefile = getPEFile(file);
}
if (pefile == null) {
file = new File(dir, name + ".EXE");
pefile = getPEFile(file);
}
if (pefile == null && adir.exists()) {
file = new File(adir, name + ".exe");
pefile = getPEFile(file);
}
if (pefile == null && adir.exists()) {
file = new File(adir, name + ".EXE");
pefile = getPEFile(file);
}
if (pefile == null)
throw new RuntimeException("Cannot find assembly " + new File(dir, name));
return getPEAssembly(pefile);
}
private static Assembly getPEAssembly(PEFile pefile) {
AssemblyDef assem = pefile.AssemblyDef;
if (assem == null)
throw new RuntimeException("File " + pefile
+ " does not contain a manifest");
assem.readRow(1);
String name = pefile.getString(assem.Name);
Assembly a = (Assembly) assemblies.get(name);
if (a != null) {
return a;
}
AssemblyName an = new AssemblyName();
an.Name = pefile.getString(assem.Name);
an.Version = new Version(assem.MajorVersion, assem.MinorVersion,
assem.BuildNumber, assem.RevisionNumber);
an.SetPublicKey(pefile.getBlob(assem.PublicKey));
return new PEAssembly(pefile, an);
}
protected static PEFile getPEFile(File f) {
PEFile pefile = null;
try { pefile = new PEFile(f.getAbsolutePath()); }
catch (FileNotFoundException e) {}
catch (RuntimeException e) {}
return pefile;
}
//##########################################################################
// public fields
/** The entry point of this assembly. */
public MethodInfo EntryPoint;
/** the display name of the assembly. */
public final String FullName;
//##########################################################################
// constructor
protected Assembly(AssemblyName an, boolean external) {
assemblyName = an;
FullName = an.toString();
if(external) {
assemblies.put(an.Name, this);
}
//System.out.println("assemblies after adding the current one: " + assemblies);
}
protected Assembly(AssemblyName an) {
this(an, false);
}
protected static Assembly getAssembly(String name) {
return (Assembly) assemblies.get(name);
}
//##########################################################################
// instrumental methods
/** @return the file from which this assembly was loaded. */
public File getFile() {
throw new RuntimeException("Not supported");
}
/** Gets the specified module in this assembly. Works on filenames. */
public Module GetModule(String name) {
initModules();
return (Module)modulesMap.get(name);
}
/** Get all the modules of the assembly. */
public Module[] GetModules() {
initModules();
return (Module[])modulesMap.values().
toArray(new Module[modulesMap.size()]);
}
/** Get the corresponding type. */
public Type GetType(String name) {
initModules();
Iterator modules = modulesMap.values().iterator();
Type t = null;
while (t == null && modules.hasNext()) {
t = ((Module)modules.next()).GetType(name);
}
return t;
}
/** @return an array of all types defined in the assembly. */
public synchronized Type[] GetTypes() {
if (types != null)
return (Type[])types.clone();
initModules();
Iterator modules = modulesMap.values().iterator();
Type[] newTypes = ((Module)modules.next()).GetTypes();
while (modules.hasNext()) {
Module module = (Module)modules.next();
Type[] mtypes = module.GetTypes();
Type[] oldTypes = newTypes;
newTypes = new Type[oldTypes.length + mtypes.length];
System.arraycopy(oldTypes, 0, newTypes, 0, oldTypes.length);
System.arraycopy(mtypes, 0, newTypes, oldTypes.length, mtypes.length);
}
types = newTypes;
return (Type[]) types.clone();
}
public AssemblyName GetName() {
return assemblyName;
}
public String toString() {
return FullName;
}
//##########################################################################
// protected members
// the assembly name
protected final AssemblyName assemblyName;
// all the types exported by the assembly
protected Type[] types = null;
// the module defined in this assembly (only one right now)
private final HashMap/*<String, Module>*/ modulesMap = new HashMap();
protected void addType(Type type) {
Type.addType(type);
}
protected void addModule(String name, Module module) {
modulesMap.put(name, module);
}
private boolean initModules = true;
protected final void initModules() {
if (initModules) {
loadModules();
initModules = false;
}
}
/** used for lazy construction of the Assembly. */
protected abstract void loadModules();
void dumpTypes() {
Type[] types = GetTypes();
for (int i = 0; i < types.length; i++)
System.out.println(types[i]);
}
//##########################################################################
} // class Assembly
|