Source code: ClassLib/ClassLibInterface.java
1 // ClassLibInterface.java, created Fri Jan 11 17:11:52 2002 by joewhaley
2 // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3 // Licensed under the terms of the GNU LGPL; see COPYING for details.
4 package ClassLib;
5
6 import Bootstrap.PrimordialClassLoader;
7 import Clazz.jq_Class;
8 import Clazz.jq_InstanceField;
9 import Clazz.jq_InstanceMethod;
10 import Clazz.jq_Member;
11 import Clazz.jq_NameAndDesc;
12 import Clazz.jq_Reference;
13 import Clazz.jq_StaticField;
14 import Clazz.jq_StaticMethod;
15 import Main.jq;
16 import Run_Time.Debug;
17 import UTF.Utf8;
18 import Util.Assert;
19
20 /**
21 * ClassLibInterface
22 *
23 * @author John Whaley <jwhaley@alum.mit.edu>
24 * @version $Id: ClassLibInterface.java,v 1.30 2003/08/09 11:28:31 joewhaley Exp $
25 */
26 public abstract class ClassLibInterface {
27
28 public static boolean USE_JOEQ_CLASSLIB;
29 public static final void useJoeqClasslib(boolean b) { USE_JOEQ_CLASSLIB = b; }
30
31 public static final ClassLib.Common.Interface DEFAULT;
32
33 /* Try the three current possibilities for the ClassLibInterface.
34 This would probably be rather more general with some kind of
35 iterator, but it does for now. */
36 static {
37 ClassLib.Common.Interface f = null;
38 String classlibinterface = System.getProperty("joeq.classlibinterface");
39 boolean nullVM = jq.nullVM || System.getProperty("joeq.nullvm") != null;
40
41 if (classlibinterface != null) {
42 f = attemptClassLibInterface(classlibinterface);
43 }
44 if (nullVM) {
45 f = new ClassLib.Common.NullInterfaceImpl();
46 }
47 if (f == null) {
48 String classlibrary = System.getProperty("classlibrary");
49 if (classlibrary == null) {
50 String javaversion = System.getProperty("java.version");
51 String javavmversion = System.getProperty("java.vm.version");
52 String javavmvendor = System.getProperty("java.vm.vendor");
53 String javaruntimeversion = System.getProperty("java.runtime.version");
54 String osarch = System.getProperty("os.arch");
55 String osname = System.getProperty("os.name");
56
57 if (osarch.equals("x86")) {
58 } else if (osarch.equals("i386")) {
59 } else {
60 System.err.println("Warning: architecture "+osarch+" is not yet supported.");
61 }
62 if (javavmvendor.equals("Sun Microsystems Inc.")) {
63 if (javaruntimeversion.equals("1.3.1_01")) {
64 classlibrary = "sun13_";
65 } else if (javaruntimeversion.equals("1.4.0-b92")) {
66 classlibrary = "sun14_";
67 } else if (javaruntimeversion.startsWith("1.4.2")) {
68 classlibrary = "sun142_";
69 } else {
70 if (javaruntimeversion.startsWith("1.4")) {
71 classlibrary = "sun14_";
72 } else {
73 classlibrary = "sun13_";
74 }
75 System.err.println("Warning: class library version "+javaruntimeversion+" is not yet supported, trying default "+classlibrary);
76 }
77 } else if (javavmvendor.equals("IBM Corporation")) {
78 if (javaruntimeversion.equals("1.3.0")) {
79 classlibrary = "ibm13_";
80 } else {
81 classlibrary = "ibm13_";
82 System.err.println("Warning: class library version "+javaruntimeversion+" is not yet supported, trying default "+classlibrary);
83 }
84 } else if (javavmvendor.equals("Apple Computer, Inc.")) {
85 if (javaruntimeversion.equals("1.3.1")) {
86 classlibrary = "apple13_";
87 } else {
88 classlibrary = "apple13_";
89 System.err.println("Warning: class library version "+javaruntimeversion+" is not yet supported, trying default "+classlibrary);
90 }
91 } else {
92 classlibrary = "sun13_";
93 System.err.println("Warning: vm vendor "+javavmvendor+" is not yet supported, trying default "+classlibrary);
94 }
95 if (osname.startsWith("Windows")) {
96 classlibrary += "win32";
97 } else if (osname.equals("Linux")) {
98 classlibrary += "linux";
99 } else if (osname.equals("Mac OS X")) {
100 classlibrary += "osx";
101 } else {
102 classlibrary += "win32";
103 System.err.println("Warning: OS "+osname+" is not yet supported, trying "+classlibrary);
104 }
105 }
106 f = attemptClassLibInterface("ClassLib."+classlibrary+".Interface");
107 }
108 if (f == null) {
109 f = new ClassLib.Common.NullInterfaceImpl();
110 }
111
112 DEFAULT = f;
113 }
114
115 private static ClassLib.Common.Interface attemptClassLibInterface(String s) {
116 try {
117 Class c = Class.forName(s);
118 return (ClassLib.Common.Interface)c.newInstance();
119 } catch (java.lang.ClassNotFoundException x) {
120 System.err.println("Cannot find class library interface "+s+": "+x);
121 System.err.println("Please check the version of your virtual machine.");
122 } catch (java.lang.InstantiationException x) {
123 System.err.println("Cannot instantiate class library interface "+s+": "+x);
124 } catch (java.lang.IllegalAccessException x) {
125 System.err.println("Cannot access class library interface "+s+": "+x);
126 }
127 return null;
128 }
129
130 public static final jq_Class _class = (jq_Class)PrimordialClassLoader.loader.getOrCreateBSType("LClassLib/ClassLibInterface;");
131
132 public static /*final*/ boolean TRACE = false;
133
134 // utility functions
135 public static jq_NameAndDesc convertClassLibNameAndDesc(jq_Class k, jq_NameAndDesc t) {
136 Utf8 d = convertClassLibDesc(t.getDesc());
137 Utf8 n = t.getName();
138 if (k.getDesc().toString().endsWith("/java/lang/Object;")) {
139 // trim initial "_", if it exists.
140 String s = n.toString();
141 if (s.charAt(0) == '_') {
142 n = Utf8.get(s.substring(1));
143 if (TRACE) Debug.writeln("special case for java.lang.Object: "+n+" "+d);
144 return new jq_NameAndDesc(n, d);
145 }
146 }
147 if (d == t.getDesc())
148 return t;
149 else
150 return new jq_NameAndDesc(n, d);
151 }
152
153 public static Utf8 convertClassLibDesc(Utf8 desc) {
154 return Utf8.get(convertClassLibDesc(desc.toString()));
155 }
156
157 public static String convertClassLibDesc(String desc) {
158 int i = desc.indexOf("ClassLib/");
159 if (i != -1) {
160 for (;;) {
161 int m = desc.indexOf(';', i+10);
162 if (m == -1) break;
163 int j = desc.indexOf('/', i+10);
164 if (j == -1 || j > m) break;
165 int k = desc.indexOf(';', j);
166 String t = desc.substring(j+1, k).replace('/','.');
167 try {
168 Class.forName(t, false, ClassLibInterface.class.getClassLoader());
169 desc = desc.substring(0, i) + desc.substring(j+1);
170 } catch (ClassNotFoundException x) {
171 }
172 i = desc.indexOf("ClassLib/", i+1);
173 if (i == -1) break;
174 }
175 }
176 return desc;
177 }
178
179 public static jq_Member convertClassLibCPEntry(jq_Member t) {
180 jq_NameAndDesc u1 = convertClassLibNameAndDesc(t.getDeclaringClass(), t.getNameAndDesc());
181 Utf8 u2 = convertClassLibDesc(t.getDeclaringClass().getDesc());
182 if (u1 == t.getNameAndDesc() && u2 == t.getDeclaringClass().getDesc())
183 return t;
184 else {
185 jq_Class c;
186 if (u2 != t.getDeclaringClass().getDesc())
187 c = (jq_Class)PrimordialClassLoader.getOrCreateType(t.getDeclaringClass().getClassLoader(), u2);
188 else
189 c = t.getDeclaringClass();
190 if (t instanceof jq_InstanceField) {
191 return c.getOrCreateInstanceField(u1);
192 } else if (t instanceof jq_StaticField) {
193 return c.getOrCreateStaticField(u1);
194 } else if (t instanceof jq_InstanceMethod) {
195 return c.getOrCreateInstanceMethod(u1);
196 } else if (t instanceof jq_StaticMethod) {
197 return c.getOrCreateStaticMethod(u1);
198 } else {
199 Assert.UNREACHABLE(); return null;
200 }
201 }
202 }
203
204 public static jq_Reference convertClassLibCPEntry(jq_Reference t) {
205 Utf8 u = convertClassLibDesc(t.getDesc());
206 if (u == t.getDesc())
207 return t;
208 else
209 return (jq_Reference)PrimordialClassLoader.getOrCreateType(t.getClassLoader(), u);
210 }
211
212 public static void init_zipfile_static(java.util.zip.ZipFile zf, java.lang.String s) {
213 try {
214 ClassLibInterface.DEFAULT.init_zipfile(zf, s);
215 } catch (java.io.IOException x) {
216 System.err.println("Note: cannot reopen zip file "+s);
217 try {
218 zf.close();
219 } catch (java.io.IOException y) {}
220 }
221 }
222
223 public static void init_inflater_static(java.util.zip.Inflater i, boolean nowrap)
224 throws java.io.IOException {
225 ClassLibInterface.DEFAULT.init_inflater(i, nowrap);
226 }
227 }