-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathProgram.cs
More file actions
173 lines (162 loc) · 5.41 KB
/
Program.cs
File metadata and controls
173 lines (162 loc) · 5.41 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using Mono.Options;
using Xamarin.Android.Tools.Bytecode;
namespace Xamarin.Android.Tools {
class App {
public static void Main (string[] args)
{
JavaDocletType docsType = 0;
bool dump = false;
bool help = false;
int verbosity = 0;
bool autorename = false;
var outputFile = (string) null;
string platform = null;
var docsPaths = new List<string> ();
var p = new OptionSet () {
"usage: class-dump [-dump] FILES",
"",
"View the metadata contents of a Java .class or .jar file.",
"",
"Options:",
{ "dump",
"Dump out .class metadata, including constant pool.\nDefault is XML output.",
v => dump = v != null },
{ "o=",
"Write output to {PATH}.",
v => outputFile = v },
{ "docspath=",
"Documentation {PATH} for parameter fixup",
doc => docsPaths.Add (doc) },
{ "docstype=",
"{TYPE} of the docs within --docspath. Values:\n " +
string.Join ("\n ", JavaDocletTypeMapping.Keys.OrderBy (s => s)),
t => docsType = GetJavaDocletType (t) },
{ "v|verbose:",
"See stack traces on error.",
(int? v) => verbosity = v.HasValue ? v.Value : verbosity + 1 },
{ "autorename",
"Renames parameter names in the interfaces by derived classes.",
v => autorename = v != null },
{ "platform=",
"(Internal use only) specify Android framework platform ID",
v => platform = v },
{ "h|?|help",
"Show this message and exit.",
v => help = v != null },
};
var files = p.Parse (args);
if (help) {
p.WriteOptionDescriptions (Console.Out);
return;
}
var output = outputFile == null
? Console.Out
: (TextWriter) new StreamWriter (outputFile, append: false, encoding: new UTF8Encoding (encoderShouldEmitUTF8Identifier: false));
Log.OnLog = (t, v, m, a) => {
Console.Error.WriteLine(m, a);
};
var classPath = new ClassPath () {
ApiSource = "class-parse",
AndroidFrameworkPlatform = platform,
DocumentationPaths = docsPaths.Count == 0 ? null : docsPaths,
DocletType = docsType,
AutoRename = autorename
};
foreach (var file in files) {
try {
if (dump) {
DumpClassFile (file, output);
continue;
}
DumpFileToXml (classPath, file);
} catch (Exception e) {
Console.Error.WriteLine ("class-parse: Unable to read file '{0}': {1}",
file, verbosity == 0 ? e.Message : e.ToString ());
Environment.ExitCode = 1;
}
}
if (!dump)
classPath.SaveXmlDescription (output);
if (outputFile != null)
output.Close ();
}
static Dictionary<string, JavaDocletType> JavaDocletTypeMapping = new Dictionary<string, JavaDocletType> {
{ "droiddoc", JavaDocletType.DroidDoc },
{ "droiddoc2", JavaDocletType.DroidDoc2 },
{ "java6", JavaDocletType.Java6 },
{ "java7", JavaDocletType.Java7 },
{ "java8", JavaDocletType.Java8 },
{ "apixml", JavaDocletType._ApiXml },
};
static JavaDocletType GetJavaDocletType (string value)
{
JavaDocletType type;
if (value != null && JavaDocletTypeMapping.TryGetValue (value.ToLowerInvariant (), out type))
return type;
return JavaDocletType.DroidDoc;
}
static void DumpFileToXml (ClassPath jar, string file)
{
using (var s = File.OpenRead (file)) {
if (ClassFile.IsClassFile (s)) {
s.Position = 0;
var c = new ClassFile (s);
jar.Add (c);
return;
}
}
if (ClassPath.IsJarFile (file)) {
jar.Load (file);
return;
}
Console.Error.WriteLine ("class-parse: Unable to read file '{0}': Unknown file format.");
Environment.ExitCode = 1;
}
static ClassFile LoadClassFile (string file)
{
using (var s = File.OpenRead (file)) {
return new ClassFile (s);
}
}
static void DumpClassFile (string file, TextWriter output)
{
var c = LoadClassFile (file);
output.WriteLine (".class version: {0}.{1}", c.MajorVersion, c.MinorVersion);
output.WriteLine ("ConstantPool Count: {0}", c.ConstantPool.Count);
for (int i = 0; i < c.ConstantPool.Count; ++i) {
output.WriteLine ("\t{0}: {1}", i, c.ConstantPool [i]);
}
output.WriteLine ("ThisClass: {0}", c.ThisClass.Name);
output.WriteLine ("SuperClass: {0}", c.SuperClass?.Name);
output.WriteLine ("AccessFlags: {0}", c.AccessFlags);
output.WriteLine ("Attributes Count: {0}", c.Attributes.Count);
for (int i = 0; i < c.Attributes.Count; ++i) {
output.WriteLine ("\t{0}: {1}", i, c.Attributes [i]);
}
output.WriteLine ("Interfaces Count: {0}", c.Interfaces.Count);
for (int i = 0; i < c.Interfaces.Count; ++i) {
output.WriteLine ("\t{0}: {1}", i, c.Interfaces [i].Name.Value);
}
output.WriteLine ("Fields Count: {0}", c.Fields.Count);
for (int i = 0; i < c.Fields.Count; ++i) {
output.WriteLine ("\t{0}: {1} {2} {3}", i, c.Fields [i].Name, c.Fields [i].Descriptor, c.Fields [i].AccessFlags);
foreach (var attr in c.Fields [i].Attributes) {
output.WriteLine ("\t\t{0}", attr);
}
}
output.WriteLine ("Methods Count: {0}", c.Methods.Count);
for (int i = 0; i < c.Methods.Count; ++i) {
output.WriteLine ("\t{0}: {1} {2} {3}", i, c.Methods [i].Name, c.Methods [i].Descriptor, c.Methods [i].AccessFlags);
foreach (var attr in c.Methods [i].Attributes) {
output.WriteLine ("\t\t{0}", attr);
}
}
}
}
}