This repository was archived by the owner on Apr 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathJavaClassFile.cs
More file actions
216 lines (187 loc) · 6.69 KB
/
JavaClassFile.cs
File metadata and controls
216 lines (187 loc) · 6.69 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using JavaResolver.Class.Constants;
using JavaResolver.Class.Metadata;
namespace JavaResolver.Class
{
/// <summary>
/// Represents a file containing a class targeting the Java runtime.
/// </summary>
public class JavaClassFile : FileSegment, IAttributeProvider
{
/// <summary>
/// The signature every class file starts with.
/// </summary>
public const uint Magic = 0xCAFEBABE;
public static JavaClassFile FromFile(string path)
{
var reader = new MemoryBigEndianReader(File.ReadAllBytes(path));
return FromReader(reader);
}
/// <summary>
/// Parses a class file from a binary reader.
/// </summary>
/// <param name="reader">The reader to read from.</param>
/// <returns>The parsed class file.</returns>
/// <exception cref="BadImageFormatException">Occurs when the file does not provide a valid signature of
/// a class file.</exception>
public static JavaClassFile FromReader(IBigEndianReader reader)
{
uint magic = reader.ReadUInt32();
if (magic != Magic)
throw new BadImageFormatException("Image does not contain a valid class file.");
var file = new JavaClassFile
{
MinorVersion = reader.ReadUInt16(),
MajorVersion = reader.ReadUInt16(),
ConstantPool = ConstantPool.FromReader(reader),
AccessFlags = (ClassAccessFlags) reader.ReadUInt16(),
ThisClass = reader.ReadUInt16(),
SuperClass = reader.ReadUInt16(),
};
ushort interfaceCount = reader.ReadUInt16();
for (int i = 0; i < interfaceCount; i++)
file.Interfaces.Add(reader.ReadUInt16());
ushort fieldCount = reader.ReadUInt16();
for (int i = 0; i < fieldCount; i++)
file.Fields.Add(FieldInfo.FromReader(reader));
ushort methodCount = reader.ReadUInt16();
for (int i = 0; i < methodCount; i++)
file.Methods.Add(MethodInfo.FromReader(reader));
ushort attributeCount = reader.ReadUInt16();
for (int i = 0; i < attributeCount; i++)
file.Attributes.Add(AttributeInfo.FromReader(reader));
return file;
}
/// <summary>
/// Gets or sets the minor version the class file format is build for.
/// </summary>
public ushort MinorVersion
{
get;
set;
}
/// <summary>
/// Gets or sets the major version the class file format is build for.
/// </summary>
public ushort MajorVersion
{
get;
set;
}
/// <summary>
/// Gets the constant pool of the class file, containing all the literals
/// and references to external members.
/// </summary>
public ConstantPool ConstantPool
{
get;
internal set;
}
/// <summary>
/// Gets or sets the accessibility attributes for the class stored in the file.
/// </summary>
public ClassAccessFlags AccessFlags
{
get;
set;
}
/// <summary>
/// Gets or sets an index into the constant pool that references a <see cref="ClassInfo"/>
/// representing the class defined in the file.
/// </summary>
public ushort ThisClass
{
get;
set;
}
/// <summary>
/// Gets or sets an index into the constant pool that references a <see cref="ClassInfo"/>
/// representing the direct super class of the class defined in the file.
/// </summary>
public ushort SuperClass
{
get;
set;
}
/// <summary>
/// Gets a collection of indices into the constant pool referencing interfaces that the class implements.
/// </summary>
public IList<ushort> Interfaces
{
get;
} = new List<ushort>();
/// <summary>
/// Gets a collection of fields defined by the class.
/// </summary>
public IList<FieldInfo> Fields
{
get;
} = new List<FieldInfo>();
/// <summary>
/// Gets a collection of methods defined by the class.
/// </summary>
public IList<MethodInfo> Methods
{
get;
} = new List<MethodInfo>();
/// <summary>
/// Gets a collection of attributes defined by the class.
/// </summary>
public IList<AttributeInfo> Attributes
{
get;
} = new List<AttributeInfo>();
/// <summary>
/// Saves the Java class file to the disk.
/// </summary>
/// <param name="path">The path of the new file to create.</param>
public void Write(string path)
{
using (var fs = File.Create(path))
{
Write(fs);
}
}
/// <summary>
/// Saves the Java class file to an output stream.
/// </summary>
/// <param name="outputStream">The stream to write to.</param>
public void Write(Stream outputStream)
{
Write(new BigEndianStreamWriter(outputStream));
}
/// <summary>
/// Writes the Java class file to a specified binary writer.
/// </summary>
/// <param name="writer">The writer to use.</param>
public void Write(IBigEndianWriter writer)
{
Write(new WritingContext(writer));
}
public override void Write(WritingContext context)
{
var writer = context.Writer;
writer.Write(Magic);
writer.Write(MinorVersion);
writer.Write(MajorVersion);
ConstantPool.Write(context);
writer.Write((ushort) AccessFlags);
writer.Write(ThisClass);
writer.Write(SuperClass);
writer.Write((ushort) Interfaces.Count);
foreach (var @interface in Interfaces)
writer.Write(@interface);
writer.Write((ushort) Fields.Count);
foreach (var field in Fields)
field.Write(context);
writer.Write((ushort) Methods.Count);
foreach (var method in Methods)
method.Write(context);
writer.Write((ushort) Attributes.Count);
foreach (var attribute in Attributes)
attribute.Write(context);
}
}
}