View Javadoc

1   // ClassName.java, created Oct 24 23:23:21 2003 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.Main;
5   
6   import java.io.DataInputStream;
7   import java.io.FileInputStream;
8   import java.io.IOException;
9   import joeq.Class.PrimordialClassLoader;
10  import joeq.Class.jq_Class;
11  import joeq.Class.jq_ClassFileConstants;
12  import joeq.Class.jq_ConstantPool;
13  
14  /***
15   * Extracts the class name from the class file(s) given on the command line.
16   * 
17   * @author jwhaley
18   * @version $Id: ClassName.java 1931 2004-09-22 22:17:47Z joewhaley $
19   */
20  public class ClassName implements jq_ClassFileConstants {
21  
22      public static void main(String[] args) throws Exception {
23          HostedVM.initialize();
24          
25          for (int i = 0; i < args.length; ++i) {
26              try {
27                  System.err.println(args[i]);
28                  DataInputStream in = new DataInputStream(new FileInputStream(args[i]));
29                  int k = in.skipBytes(8);
30                  if (k != 8) throw new IOException();
31                  int constant_pool_count = in.readUnsignedShort();
32                  jq_ConstantPool cp = new jq_ConstantPool(constant_pool_count);
33                  cp.load(in);
34                  cp.resolve(PrimordialClassLoader.loader);
35                  k = in.skipBytes(2);
36                  if (k != 2) throw new IOException();
37                  char selfindex = (char)in.readUnsignedShort();
38                  if (cp.getTag(selfindex) != CONSTANT_ResolvedClass) {
39                      System.err.println("constant pool entry "+(int)selfindex+", referred to by field this_class" +
40                                         ", is wrong type tag (expected="+CONSTANT_Class+", actual="+cp.getTag(selfindex)+")");
41                  }
42                  jq_Class t = (jq_Class) cp.get(selfindex);
43                  System.out.println(t.getJDKName());
44                  in.close();
45              } catch (Exception x) {
46                  x.printStackTrace();
47              }
48          }
49      }
50  }