home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / JDirect / sdump / SDump.java < prev   
Encoding:
Java Source  |  2000-05-04  |  1.8 KB  |  74 lines

  1. /* (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  2.  *
  3.  * This example shows how to use the Reflection api along with
  4.  * DllLib to examine the physical layout of a class declared
  5.  * with "@dll.struct".
  6.  *
  7.  */
  8.  
  9.  
  10. import com.ms.dll.*;
  11. import java.lang.reflect.*;
  12.  
  13. public class SDump
  14. {
  15.     public static void main(String args[])
  16.     {
  17.         Class c;
  18.         if (args.length == 0) {
  19.             System.err.println("Must give name of class as argument.");
  20.             return;
  21.         }
  22.  
  23.         String name = args[0];
  24.  
  25.  
  26.         if (name.length() >= 6 &&
  27.             name.substring(name.length()-6).equals(".class")) {
  28.             name = name.substring(0, name.length()-6);
  29.         }
  30.  
  31.         try {
  32.             c = Class.forName(name);
  33.         } catch (Exception e) {
  34.             System.err.println("Could not load class named \"" +
  35.                                 name +
  36.                                 "\"");
  37.             return;
  38.         }
  39.  
  40.         if (!DllLib.isStruct(c)) {
  41.             System.err.println("Class \"" +
  42.                                c.getName() +
  43.                                "\" not declared with @dll.struct.");
  44.             return;
  45.         }
  46.  
  47.         System.out.println("Layout of class \"" + c.getName() + "\"");
  48.  
  49.         Field fields[] = c.getDeclaredFields();
  50.         for (int i = 0; i < fields.length; i++) {
  51.             Field f = fields[i];
  52.             if (DllLib.isStruct(f)) {
  53.                 int offset = DllLib.offsetOf(f);
  54.                 System.out.println("+" + offset + ": " + " " + f.getName());
  55.             }
  56.         }
  57.         System.out.println();
  58.         System.out.println("  Total size = " + DllLib.sizeOf(c) + " bytes.");
  59.     }
  60. }
  61.  
  62.  
  63.  
  64. /** @dll.struct() */
  65. class SampleStruct
  66. {
  67.     int     a;
  68.     int     b;
  69.     int     c;
  70.     int     d;
  71.  
  72. }
  73.  
  74.