View Javadoc

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 joeq.ClassLib;
5   
6   import joeq.Class.PrimordialClassLoader;
7   import joeq.Class.jq_Class;
8   import joeq.Class.jq_InstanceField;
9   import joeq.Class.jq_InstanceMethod;
10  import joeq.Class.jq_Member;
11  import joeq.Class.jq_NameAndDesc;
12  import joeq.Class.jq_Reference;
13  import joeq.Class.jq_StaticField;
14  import joeq.Class.jq_StaticMethod;
15  import joeq.Main.jq;
16  import joeq.Runtime.Debug;
17  import joeq.UTF.Utf8;
18  import jwutil.util.Assert;
19  
20  /***
21   * ClassLibInterface
22   *
23   * @author  John Whaley <jwhaley@alum.mit.edu>
24   * @version $Id: ClassLibInterface.java 1931 2004-09-22 22:17:47Z joewhaley $
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 joeq.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          joeq.ClassLib.Common.Interface f = null;
38          String classlibinterface = System.getProperty("joeq.classlibinterface");
39          boolean nullVM = jq.nullVM;
40  
41          if (classlibinterface != null) {
42              f = attemptClassLibInterface(classlibinterface);
43          }
44          if (nullVM) {
45              f = new joeq.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 if (javaruntimeversion.startsWith("1.5.0")) {
70                          classlibrary = "sun15_";
71                      } else {
72                          if (javaruntimeversion.startsWith("1.5")) {
73                              classlibrary = "sun15_";
74                          } else if (javaruntimeversion.startsWith("1.4")) {
75                              classlibrary = "sun14_";
76                          } else {
77                              classlibrary = "sun13_";
78                          }
79                          System.err.println("Warning: class library version "+javaruntimeversion+" is not yet supported, trying default "+classlibrary);
80                      }
81                  } else if (javavmvendor.equals("IBM Corporation")) {
82                      if (javaruntimeversion.equals("1.3.0")) {
83                          classlibrary = "ibm13_";
84                      } else {
85                          classlibrary = "ibm13_";
86                          System.err.println("Warning: class library version "+javaruntimeversion+" is not yet supported, trying default "+classlibrary);
87                      }
88                  } else if (javavmvendor.equals("Apple Computer, Inc.")) {
89                      if (javaruntimeversion.equals("1.3.1")) {
90                          classlibrary = "apple13_";
91                      } else {
92                          classlibrary = "apple13_";
93                          System.err.println("Warning: class library version "+javaruntimeversion+" is not yet supported, trying default "+classlibrary);
94                      }
95                  } else {
96                      classlibrary = "sun13_";
97                      System.err.println("Warning: vm vendor "+javavmvendor+" is not yet supported, trying default "+classlibrary);
98                  }
99                  if (osname.startsWith("Windows")) {
100                     classlibrary += "win32";
101                 } else if (osname.equals("Linux")) {
102                     classlibrary += "linux";
103                 } else if (osname.equals("Mac OS X")) {
104                     classlibrary += "osx";
105                 } else {
106                     classlibrary += "win32";
107                     System.err.println("Warning: OS "+osname+" is not yet supported, trying "+classlibrary);
108                 }
109             }
110             f = attemptClassLibInterface("joeq.ClassLib."+classlibrary+".Interface");
111         }
112         if (f == null) {
113             f = new joeq.ClassLib.Common.NullInterfaceImpl();
114         }
115         
116         DEFAULT = f;
117     }
118 
119     private static joeq.ClassLib.Common.Interface attemptClassLibInterface(String s) {
120         try {
121             Class c = Class.forName(s);
122             return (joeq.ClassLib.Common.Interface)c.newInstance();
123         } catch (java.lang.ClassNotFoundException x) {
124             if (jq.IsBootstrapping) {
125                 System.err.println("Cannot find class library interface "+s+": "+x);
126                 System.err.println("Please check the version of your virtual machine.");
127             }
128         } catch (java.lang.InstantiationException x) {
129             System.err.println("Cannot instantiate class library interface "+s+": "+x);
130         } catch (java.lang.IllegalAccessException x) {
131             System.err.println("Cannot access class library interface "+s+": "+x);
132         }
133         return null;
134     }
135 
136     public static final jq_Class _class = (jq_Class)PrimordialClassLoader.loader.getOrCreateBSType("Ljoeq/ClassLib/ClassLibInterface;");
137     
138     public static /*final*/ boolean TRACE = false;
139     
140     // utility functions
141     public static jq_NameAndDesc convertClassLibNameAndDesc(jq_Class k, jq_NameAndDesc t) {
142         Utf8 d = convertClassLibDesc(t.getDesc());
143         Utf8 n = t.getName();
144         if (k.getDesc().toString().endsWith("/java/lang/Object;")) {
145             // trim initial "_", if it exists.
146             String s = n.toString();
147             if (s.charAt(0) == '_') {
148                 n = Utf8.get(s.substring(1));
149                 if (TRACE) Debug.writeln("special case for java.lang.Object: "+n+" "+d);
150                 return new jq_NameAndDesc(n, d);
151             }
152         }
153         if (d == t.getDesc())
154             return t;
155         else
156             return new jq_NameAndDesc(n, d);
157     }
158     
159     public static Utf8 convertClassLibDesc(Utf8 desc) {
160         return Utf8.get(convertClassLibDesc(desc.toString()));
161     }
162     
163     public static String convertClassLibDesc(String desc) {
164         int i = desc.indexOf("joeq/ClassLib/");
165         if (i != -1) {
166             for (;;) {
167                 int m = desc.indexOf(';', i+15);
168                 if (m == -1) break;
169                 int j = desc.indexOf('/', i+15);
170                 if (j == -1 || j > m) break;
171                 int k = desc.indexOf(';', j);
172                 String t = desc.substring(j+1, k).replace('/','.');
173                 try {
174                     Class.forName(t, false, ClassLibInterface.class.getClassLoader());
175                     desc = desc.substring(0, i) + desc.substring(j+1);
176                 } catch (ClassNotFoundException x) {
177                 }
178                 i = desc.indexOf("joeq/ClassLib/", i+1);
179                 if (i == -1) break;
180             }
181         }
182         return desc;
183     }
184     
185     public static jq_Member convertClassLibCPEntry(jq_Member t) {
186         jq_NameAndDesc u1 = convertClassLibNameAndDesc(t.getDeclaringClass(), t.getNameAndDesc());
187         Utf8 u2 = convertClassLibDesc(t.getDeclaringClass().getDesc());
188         if (u1 == t.getNameAndDesc() && u2 == t.getDeclaringClass().getDesc())
189             return t;
190         else {
191             jq_Class c;
192             if (u2 != t.getDeclaringClass().getDesc())
193                 c = (jq_Class)PrimordialClassLoader.getOrCreateType(t.getDeclaringClass().getClassLoader(), u2);
194             else
195                 c = t.getDeclaringClass();
196             if (t instanceof jq_InstanceField) {
197                 return c.getOrCreateInstanceField(u1);
198             } else if (t instanceof jq_StaticField) {
199                 return c.getOrCreateStaticField(u1);
200             } else if (t instanceof jq_InstanceMethod) {
201                 return c.getOrCreateInstanceMethod(u1);
202             } else if (t instanceof jq_StaticMethod) {
203                 return c.getOrCreateStaticMethod(u1);
204             } else {
205                 Assert.UNREACHABLE(); return null;
206             }
207         }
208     }
209     
210     public static jq_Reference convertClassLibCPEntry(jq_Reference t) {
211         Utf8 u = convertClassLibDesc(t.getDesc());
212         if (u == t.getDesc())
213             return t;
214         else
215             return (jq_Reference)PrimordialClassLoader.getOrCreateType(t.getClassLoader(), u);
216     }
217     
218     public static void init_zipfile_static(java.util.zip.ZipFile zf, java.lang.String s) {
219         try {
220             ClassLibInterface.DEFAULT.init_zipfile(zf, s);
221         } catch (java.io.IOException x) {
222             System.err.println("Note: cannot reopen zip file "+s);
223             try {
224                 zf.close();
225             } catch (java.io.IOException y) {}
226         }
227     }
228     
229     public static void init_inflater_static(java.util.zip.Inflater i, boolean nowrap)
230         throws java.io.IOException {
231         ClassLibInterface.DEFAULT.init_inflater(i, nowrap);
232     }
233 }