|
| 1 | +/* |
| 2 | + * Initial code taken from Charlie Nutter's org.jruby.util.CodegenUtils. |
| 3 | + */ |
| 4 | + |
| 5 | +package org.python.util; |
| 6 | + |
| 7 | +import java.util.Arrays; |
| 8 | + |
| 9 | +public class CodegenUtils { |
| 10 | + |
| 11 | + /** |
| 12 | + * Creates a dotted class name from a path/package name |
| 13 | + */ |
| 14 | + public static String c(String p) { |
| 15 | + return p.replace('/', '.'); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Creates a class path name, from a Class. |
| 20 | + */ |
| 21 | + public static String p(Class n) { |
| 22 | + return n.getName().replace('.','/'); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates a class identifier of form Labc/abc;, from a Class. |
| 27 | + */ |
| 28 | + public static String ci(Class n) { |
| 29 | + if (n.isArray()) { |
| 30 | + n = n.getComponentType(); |
| 31 | + if (n.isPrimitive()) { |
| 32 | + if (n == Byte.TYPE) { |
| 33 | + return "[B"; |
| 34 | + } else if (n == Boolean.TYPE) { |
| 35 | + return "[Z"; |
| 36 | + } else if (n == Short.TYPE) { |
| 37 | + return "[S"; |
| 38 | + } else if (n == Character.TYPE) { |
| 39 | + return "[C"; |
| 40 | + } else if (n == Integer.TYPE) { |
| 41 | + return "[I"; |
| 42 | + } else if (n == Float.TYPE) { |
| 43 | + return "[F"; |
| 44 | + } else if (n == Double.TYPE) { |
| 45 | + return "[D"; |
| 46 | + } else if (n == Long.TYPE) { |
| 47 | + return "[J"; |
| 48 | + } else { |
| 49 | + throw new RuntimeException("Unrecognized type in compiler: " + n.getName()); |
| 50 | + } |
| 51 | + } else { |
| 52 | + return "[" + ci(n); |
| 53 | + } |
| 54 | + } else { |
| 55 | + if (n.isPrimitive()) { |
| 56 | + if (n == Byte.TYPE) { |
| 57 | + return "B"; |
| 58 | + } else if (n == Boolean.TYPE) { |
| 59 | + return "Z"; |
| 60 | + } else if (n == Short.TYPE) { |
| 61 | + return "S"; |
| 62 | + } else if (n == Character.TYPE) { |
| 63 | + return "C"; |
| 64 | + } else if (n == Integer.TYPE) { |
| 65 | + return "I"; |
| 66 | + } else if (n == Float.TYPE) { |
| 67 | + return "F"; |
| 68 | + } else if (n == Double.TYPE) { |
| 69 | + return "D"; |
| 70 | + } else if (n == Long.TYPE) { |
| 71 | + return "J"; |
| 72 | + } else if (n == Void.TYPE) { |
| 73 | + return "V"; |
| 74 | + } else { |
| 75 | + throw new RuntimeException("Unrecognized type in compiler: " + n.getName()); |
| 76 | + } |
| 77 | + } else { |
| 78 | + return "L" + p(n) + ";"; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Create a method signature from the given param types and return values |
| 85 | + */ |
| 86 | + public static String sig(Class retval, Class... params) { |
| 87 | + return sigParams(params) + ci(retval); |
| 88 | + } |
| 89 | + |
| 90 | + public static String sig(Class retval, String descriptor, Class... params) { |
| 91 | + return sigParams(descriptor, params) + ci(retval); |
| 92 | + } |
| 93 | + |
| 94 | + public static String sigParams(Class... params) { |
| 95 | + StringBuilder signature = new StringBuilder("("); |
| 96 | + |
| 97 | + for (int i = 0; i < params.length; i++) { |
| 98 | + signature.append(ci(params[i])); |
| 99 | + } |
| 100 | + |
| 101 | + signature.append(")"); |
| 102 | + |
| 103 | + return signature.toString(); |
| 104 | + } |
| 105 | + |
| 106 | + public static String sigParams(String descriptor, Class... params) { |
| 107 | + StringBuilder signature = new StringBuilder("("); |
| 108 | + |
| 109 | + signature.append(descriptor); |
| 110 | + |
| 111 | + for (int i = 0; i < params.length; i++) { |
| 112 | + signature.append(ci(params[i])); |
| 113 | + } |
| 114 | + |
| 115 | + signature.append(")"); |
| 116 | + |
| 117 | + return signature.toString(); |
| 118 | + } |
| 119 | + |
| 120 | + public static Class[] params(Class... classes) { |
| 121 | + return classes; |
| 122 | + } |
| 123 | + |
| 124 | + public static Class[] params(Class cls, int times) { |
| 125 | + Class[] classes = new Class[times]; |
| 126 | + Arrays.fill(classes, cls); |
| 127 | + return classes; |
| 128 | + } |
| 129 | + |
| 130 | + public static Class[] params(Class cls1, Class clsFill, int times) { |
| 131 | + Class[] classes = new Class[times + 1]; |
| 132 | + Arrays.fill(classes, clsFill); |
| 133 | + classes[0] = cls1; |
| 134 | + return classes; |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments