-
Notifications
You must be signed in to change notification settings - Fork 8
/
JavaToLMI.java
215 lines (184 loc) · 6.74 KB
/
JavaToLMI.java
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
/// Copyright (C) eValuator, Lda
/// THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
/// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
/// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
/// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
/// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
/// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
/// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
/// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
/// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
/// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import java.lang.reflect.*;
import java.io.*;
public class JavaToLMI {
public static void main(String[] args) throws IOException {
if (args.length == 0) { //Server mode
String request;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while ((request = in.readLine()) != null) {
processRequest(request, false);
}
} else if (args.length == 1) { //One-shot mode
processRequest(args[0], true);
} else {
System.err.println("Usage: JavaToLMI classname");
System.exit(1);
}
}
public static void processRequest(String name, boolean oneShotP) {
try {
dumpClassInfo(Class.forName(name, false, JavaToLMI.class.getClassLoader()));
//Class.forName(name));
} catch (ClassNotFoundException cnfe) {
if (oneShotP) {
System.err.println("Class '" + name + "' not found");
System.exit(1);
} else {
println("()");
}
}
}
public static void dumpClassInfo(Class classToDump) {
printsp(classToDump.isInterface() ? "(defmixin-0" : "(defclass-0");
printsp(toTypeName(classToDump));
if (! classToDump.isInterface()) {
Class superclass = classToDump.getSuperclass();
if (superclass == null) {
printsp("()");
} else {
printsp(toTypeName(superclass));
}
}
print("(");
Class[] interfaces = classToDump.getInterfaces();
for(int i = 0; i < interfaces.length; i++) {
printsp(toTypeName(interfaces[i]));
}
println(")");
println("()"); //options
Field[] fields = classToDump.getDeclaredFields();
for(int i = 0; i < fields.length; i++) {
dumpFieldInfo(fields[i]);
}
Constructor[] constructors = classToDump.getDeclaredConstructors();
for(int i = 0; i < constructors.length; i++) {
dumpConstructorInfo(constructors[i]);
}
Method[] methods = classToDump.getDeclaredMethods();
for(int i = 0; i < methods.length; i++) {
if (! methods[i].isBridge()) {
dumpMethodInfo(methods[i]);
}
}
// Class[] classes = classToDump.getDeclaredClasses();
// for(int i = 0; i < classes.length; i++) {
// dumpClassInfo(classes[i]);
// }
println(")");
}
public static void dumpFieldInfo(Field field) {
if (isPublicOrProtected(field)) {
printsp(" (defslot-0");
print("(");
printsp(field.getName());
printsp("(the");
print(toTypeName(field.getType()));
printsp("))");
// print(field.getName());
// print("/");
// printsp(toTypeName(field.getType()));
dumpMemberOptions(field);
println(")");
}
}
public static void dumpConstructorInfo(Constructor constructor) {
if (isPublicOrProtected(constructor)) {
printsp(" (defnew");
dumpArglist(constructor.getParameterTypes(), false);
dumpMemberOptions(constructor);
printsp("");
dumpThrows(constructor.getExceptionTypes());
println(")");
}
}
public static void dumpMethodInfo(Method method) {
if (isPublicOrProtected(method)) {
printsp(" (defmethod");
// printsp(toTypeName(method.getReturnType()));
printsp(method.getName());
dumpArglist(method.getParameterTypes(),
! Modifier.isStatic(method.getModifiers()));
dumpMemberOptions(method);
printsp("");
if (Modifier.isAbstract(method.getModifiers())) {
printsp(":category :abstract");
}
printsp(" :returns");
printsp(toTypeName(method.getReturnType()));
dumpThrows(method.getExceptionTypes());
println(")");
}
}
public static void dumpMemberOptions(Member member) {
int mods = member.getModifiers();
if (Modifier.isStatic(mods)) {
printsp(":allocation :class");
}
if (Modifier.isFinal(mods)) {
printsp(":constant t");
}
printsp(":visibility");
if (Modifier.isPublic(mods)) {
print(":public");
} else {
print(":protected");
}
}
public static void dumpArglist(Class[] argTypes, boolean withThis) {
print("(");
if (withThis) {
printsp("this");
}
for(int i = 0; i < argTypes.length; i++) {
print("(arg" + i + " (the ");
print(toTypeName(argTypes[i]));
printsp("))");
}
printsp(")");
}
public static void dumpThrows(Class[] excepTypes) {
if (excepTypes.length > 0) {
print(":throws (");
for(int i = 0; i < excepTypes.length; i++) {
printsp(toTypeName(excepTypes[i]));
}
printsp(")");
}
}
public static boolean isPublicOrProtected(Member member) {
int mods = member.getModifiers();
return (Modifier.isPublic(mods) || Modifier.isProtected(mods));
}
public static String toTypeName(Class classObj) {
return toPrintableName(classObj);
}
protected static String toPrintableName(Class classObj) {
if (classObj.isArray()) {
return toPrintableName(classObj.getComponentType()) + "[]";
} else {
return classObj.getName().replace('$', '/');
}
}
protected static void println(String text) {
System.out.println(text);
}
protected static void printsp(String text) {
System.out.print(text);
System.out.print(" ");
}
protected static void print(String text) {
System.out.print(text);
}
}