View Javadoc

1   /*
2    * Created on Jul 27, 2005
3    *
4    * To change the template for this generated file go to
5    * Window>Preferences>Java>Code Generation>Code and Comments
6    */
7   package joeq.Util;
8   
9   import joeq.Class.jq_Array;
10  import joeq.Class.jq_Class;
11  import joeq.Class.jq_ClassInitializer;
12  import joeq.Class.jq_Field;
13  import joeq.Class.jq_Initializer;
14  import joeq.Class.jq_Method;
15  import joeq.Class.jq_Primitive;
16  import joeq.Class.jq_Type;
17  
18  /***
19   * Contains code for converting names of IR entities into their string 
20   * representations used by Eclipse.
21   * 
22   *  @author John Whaley
23   *  @version $Id: NameMunger.java 2453 2006-03-09 03:09:28Z mcmartin $
24   * */
25  public class NameMunger {
26      //  NQueens.<clinit>
27      // java/lang/Math/doublepow(doubledouble)
28      public static String mungeMethodName(jq_Method m) {
29          if (m == null) return "null";
30          StringBuffer sb = new StringBuffer();
31          String className = mungeTypeName(m.getDeclaringClass());
32          String returnType = mungeTypeName(m.getReturnType());
33          
34          if (m instanceof jq_ClassInitializer) {
35              return className+".<clinit>";
36          }
37          sb.append(className);
38          sb.append('/');
39          sb.append(returnType);
40          if (m instanceof jq_Initializer) {
41              
42          } else {
43              sb.append(m.getName());
44          }
45          sb.append('(');
46          jq_Type[] ps = m.getParamTypes();
47          for (int i = 0; i < ps.length; ++i) {
48              if (i == 0 && !m.isStatic()) continue;
49              jq_Type p = ps[i];
50              sb.append(mungeTypeName(p));
51          }
52          sb.append(')');
53          jq_Class[] ex = m.getThrownExceptionsTable();
54          if (ex != null) {
55              for (int i = 0; i < ex.length; ++i) {
56                  jq_Class x = ex[i];
57                  sb.append(mungeTypeName(x));
58              }
59          }
60          return sb.toString();
61      }
62      
63      // java/lang/String/count
64      // java/lang/String$1/foo
65      // java/lang/String/Iterator/n
66      public static String mungeFieldName(jq_Field t) {
67          if (t == null) return "null";
68          return mungeTypeName(t.getDeclaringClass())+"/"+t.getName();
69      }
70      
71      public static String mungeTypeName2(jq_Type t) {
72          if (t == null) return "null";
73          if (isAnonymousClass(t.toString())) return mungeTypeName(t);
74          String s = t.toString().replace('$','.');
75          return s;
76      }
77      
78      public static String mungeTypeName(jq_Type t) {
79          if (t == null) return "null";
80          if (t instanceof jq_Primitive) {
81              return ((jq_Primitive) t).getName();
82          }
83          if (t instanceof jq_Array) {
84              jq_Array a = (jq_Array) t;
85              int depth = a.getDimensionality();
86              jq_Type elementType = a.getInnermostElementType();
87              return mungeTypeName(elementType)+depth;
88          }
89          String s = t.toString();
90          s = s.replace('.', '/');
91          if (!isAnonymousClass(s))
92              s = s.replace('$', '/');
93          return s;
94      }
95      
96      public static boolean isAnonymousClass(String s) {
97          int i = s.indexOf('$');
98          if (i == -1) return false;
99          if (i+1 == s.length()) return false;
100         char c = s.charAt(i+1);
101         return Character.isDigit(c);
102     }
103     
104     public static String getJavadocSignature(jq_Method method) {
105         if (method == null) return "null";
106         String jvmSig = method.toString();
107         
108         return getJavadocSignature(jvmSig, method.getReturnType().getName());
109     }
110     
111     public static String getJavadocSignature(String jvmSig, String returnType) {
112         int spaceIdx = jvmSig.indexOf(' ');
113         String name = jvmSig.substring(0, spaceIdx);
114         String paramSig = jvmSig.substring(spaceIdx+1,jvmSig.length());
115         String[] params = DescriptorUtil.getParameters(paramSig);
116         
117         StringBuffer result = new StringBuffer();
118         if(returnType != null) {
119             result.append(returnType);
120             result.append(" ");
121         }
122         
123         result.append(name);
124         result.append("(");
125         for (int i = 0; i < params.length; i++) {
126             String param = params[i];
127             
128             result.append(param);
129             if(i < params.length-1) result.append(", ");
130         }
131         result.append(")");
132         
133         return result.toString();        
134     }
135     
136     private static void test(String sig) {
137         System.out.println(getJavadocSignature(sig, null));
138     } 
139 
140     public static void main(String[] args) {
141         test("java.util.Calendar.internalSet (II)V");
142         test("MyMockLib.MyString.substring (Ljava/lang/String;I)Ljava/lang/String;");
143     }
144 }