summaryrefslogtreecommitdiff
path: root/src/msil/ch/epfl/lamp/compiler/msil/PEAssembly.java
blob: a31db16c92c4c63ed1558ddf15edd37f625d4ab6 (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
/*
 * System.Reflection-like API for access to .NET assemblies (DLL & EXE)
 */


package ch.epfl.lamp.compiler.msil;

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

import java.io.File;

import java.util.Map;
import java.util.HashMap;

/** Represents an assembly that resides in a real .NET assembly
 *
 * @author Nikolay Mihaylov
 * @version 1.0
 */
final class PEAssembly extends Assembly {

    private final PEFile pefile;

    private PEModule mainModule;

    public PEAssembly(PEFile pefile, AssemblyName an) {
	super(an, true);
	this.pefile = pefile;
	String name = pefile.ModuleDef(1).getName();
	mainModule = new PEModule(pefile, 1, name, this);
	addModule(name, mainModule);
        //initModules();
    }

    protected void loadModules() {
	File parentDir = pefile.getParentFile();
	FileDef fd = pefile.FileDef;
	for (int row = 1; row <= fd.rows; row++) {
	    fd.readRow(row);
	    String filename = fd.getName();
	    File f = new File(parentDir, filename);
	    PEFile pe = Assembly.getPEFile(f);
	    if (pe == null) {
		f = new File(filename);
		pe = Assembly.getPEFile(f);
		if (pe == null)
		    continue;
// 		throw new RuntimeException("Cannot find file " + filename +
// 					   " referenced by assembly " + this);
	    }
	    String name = pe.ModuleDef(1).getName();
	    PEModule module = new PEModule(pe, 1, name, this);
	    addModule(name, module);
	}
    }

    public File getFile() {
	return pefile.getUnderlyingFile();
    }

    protected void loadCustomAttributes(Type attributeType) {
        initModules();
        mainModule.initAttributes(this, 1, Table.AssemblyDef.ID, attributeType);
    }

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

} // class PEAssembly