View Javadoc

1   // Comparisons.java, created Sun Feb  8 16:32:41 PST 2004 by gback
2   // Copyright (C) 2003 Godmar Back <gback@stanford.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.Util;
5   
6   import java.lang.reflect.Field;
7   import java.lang.reflect.Modifier;
8   
9   /***
10   * Helper application to create proxy classes that encapsulate an object's
11   * package-accessible fields and make them public fields. 
12   * Currently used to create for Compiler.Analysis.IPA.PAProxy.
13   */
14  public class MakeProxy {
15      public static final String suffix = "Proxy";
16  
17      public static void main(String []av) throws Exception {
18          String fqname = av[0];
19          int b = fqname.lastIndexOf('.')+1;
20          String cname = fqname.substring(b);
21          String pkg = fqname.substring(0, b-1);
22          String pcname = cname + suffix;
23          System.out.println("// Public proxy class for " + fqname);
24          System.out.println("// Generated via " + MakeProxy.class.getName());
25          System.out.println("package " + pkg + ";\n");
26          System.out.println("public class " + pcname + " {");
27          System.out.println("  public " + pcname + "(" + cname + " that) {");
28          StringBuffer sb = new StringBuffer();
29          Class clazz = Class.forName(fqname);
30          while (clazz != null) {
31              Field [] ff = clazz.getDeclaredFields();
32              for (int i = 0; i < ff.length; i++) {
33                  Field f = ff[i];
34                  if (f.getName().startsWith("class$"))
35                      continue;
36                  boolean isPrivate = (f.getModifiers() & Modifier.PRIVATE) != 0;
37                  if (isPrivate)
38                      continue;
39                  System.out.println("    this." + f.getName() + " = that." + f.getName() + ";");
40                  boolean isStatic = (f.getModifiers() & Modifier.STATIC) != 0;
41                  String typeName;
42                  Class c = f.getType();
43                  if (f.getType().isArray()) {
44                      StringBuffer sb2 = new StringBuffer();
45                      while (c.isArray()) {
46                          sb2.append("[]");
47                          c = c.getComponentType();
48                      }
49                      typeName = c.getName() + sb2.toString();
50                  } else {
51                      typeName = c.getName();
52                  }
53                  sb.append("  public " + (isStatic ? "static " : "") + typeName.replace('$', '.') 
54                          + " " + f.getName() + ";\n");
55              }
56              clazz = clazz.getSuperclass();
57          }
58          System.out.println("  }");
59          System.out.print(sb.toString());
60          System.out.println("}");
61      }
62  }