src.org.python.util.CodegenUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jython Show documentation
Show all versions of jython Show documentation
Jython is an implementation of the high-level, dynamic, object-oriented
language Python written in 100% Pure Java, and seamlessly integrated with
the Java platform. It thus allows you to run Python on any Java platform.
/*
* Initial code taken from Charlie Nutter's org.jruby.util.CodegenUtils.
*/
package org.python.util;
import java.util.Arrays;
public class CodegenUtils {
/**
* Creates a dotted class name from a path/package name
*/
public static String c(String p) {
return p.replace('/', '.');
}
/**
* Creates a class path name, from a Class.
*/
public static String p(Class n) {
return n.getName().replace('.','/');
}
/**
* Creates a class identifier of form Labc/abc;, from a Class.
*/
public static String ci(Class n) {
if (n.isArray()) {
n = n.getComponentType();
if (n.isPrimitive()) {
if (n == Byte.TYPE) {
return "[B";
} else if (n == Boolean.TYPE) {
return "[Z";
} else if (n == Short.TYPE) {
return "[S";
} else if (n == Character.TYPE) {
return "[C";
} else if (n == Integer.TYPE) {
return "[I";
} else if (n == Float.TYPE) {
return "[F";
} else if (n == Double.TYPE) {
return "[D";
} else if (n == Long.TYPE) {
return "[J";
} else {
throw new RuntimeException("Unrecognized type in compiler: " + n.getName());
}
} else {
return "[" + ci(n);
}
} else {
if (n.isPrimitive()) {
if (n == Byte.TYPE) {
return "B";
} else if (n == Boolean.TYPE) {
return "Z";
} else if (n == Short.TYPE) {
return "S";
} else if (n == Character.TYPE) {
return "C";
} else if (n == Integer.TYPE) {
return "I";
} else if (n == Float.TYPE) {
return "F";
} else if (n == Double.TYPE) {
return "D";
} else if (n == Long.TYPE) {
return "J";
} else if (n == Void.TYPE) {
return "V";
} else {
throw new RuntimeException("Unrecognized type in compiler: " + n.getName());
}
} else {
return "L" + p(n) + ";";
}
}
}
/**
* Create a method signature from the given param types and return values
*/
public static String sig(Class retval, Class... params) {
return sigParams(params) + ci(retval);
}
public static String sig(Class retval, String descriptor, Class... params) {
return sigParams(descriptor, params) + ci(retval);
}
public static String sigParams(Class... params) {
StringBuilder signature = new StringBuilder("(");
for (int i = 0; i < params.length; i++) {
signature.append(ci(params[i]));
}
signature.append(")");
return signature.toString();
}
public static String sigParams(String descriptor, Class... params) {
StringBuilder signature = new StringBuilder("(");
signature.append(descriptor);
for (int i = 0; i < params.length; i++) {
signature.append(ci(params[i]));
}
signature.append(")");
return signature.toString();
}
public static Class[] params(Class... classes) {
return classes;
}
public static Class[] params(Class cls, int times) {
Class[] classes = new Class[times];
Arrays.fill(classes, cls);
return classes;
}
public static Class[] params(Class cls1, Class clsFill, int times) {
Class[] classes = new Class[times + 1];
Arrays.fill(classes, clsFill);
classes[0] = cls1;
return classes;
}
}