use of kalang.core.Type in project kalang by kasonyang.
the class AstUtil method matchTypes.
/**
* @param args
* @param from
* @param target
* @return array when matched,null when not
*/
@Nullable
public static ExprNode[] matchTypes(ExprNode[] args, Type[] from, Type[] target) {
if (args == null)
return null;
if (from == null || from.length == 0) {
if (target == null || target.length == 0) {
return args;
} else {
return null;
}
}
if (from.length != target.length || from.length != args.length) {
return null;
}
if (from.length == 0)
return new ExprNode[0];
ExprNode[] newParams = new ExprNode[from.length];
for (int i = 0; i < from.length; i++) {
Type f = from[i];
Type t = target[i];
newParams[i] = matchType(f, t, args[i]);
if (newParams[i] == null)
return null;
}
return newParams;
}
use of kalang.core.Type in project kalang by kasonyang.
the class AstUtil method createScriptMainMethodIfNotExists.
public static void createScriptMainMethodIfNotExists(ClassNode clazz) {
ClassType clazzType = Types.getClassType(clazz);
MethodDescriptor[] methods = clazzType.getMethodDescriptors(null, false, false);
Type[] argTypes = new Type[] { Types.getArrayType(Types.getStringClassType()) };
MethodDescriptor mainMethod = MethodUtil.getMethodDescriptor(methods, "main", argTypes);
if (mainMethod == null) {
MethodNode m = clazz.createMethodNode(Types.VOID_TYPE, "main", Modifier.PUBLIC + Modifier.STATIC);
ParameterNode p = m.createParameter(argTypes[0], "arg");
BlockStmt body = m.getBody();
try {
NewObjectExpr newScriptExpr = new NewObjectExpr(clazzType);
ObjectInvokeExpr invokeExpr = ObjectInvokeExpr.create(newScriptExpr, "run", new ExprNode[] { new ParameterExpr(p) });
body.statements.add(new ExprStmt(invokeExpr));
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
}
}
use of kalang.core.Type in project kalang by kasonyang.
the class Ast2Java method visitMethodNode.
@Override
public String visitMethodNode(MethodNode node) {
this.method = node;
List<String> psList = new LinkedList<>();
for (ParameterNode p : node.getParameters()) {
psList.add(this.getVarStr(p));
}
// .join(",");
String ps = String.join(",", psList);
String exStr = "";
Type[] exceptionTypes = node.getExceptionTypes();
if (exceptionTypes.length > 0) {
List<String> types = new ArrayList<>(exceptionTypes.length);
for (Type et : exceptionTypes) types.add(et.getName());
// .join(",");
exStr = "throws " + String.join(",", types);
}
String mname = node.getName();
String typeStr = "";
int mdf = node.getModifier();
if (mname.equals("<init>")) {
mdf = mdf & (~Modifier.STATIC);
Objects.requireNonNull(cls, "cls must be not null");
int lastIdx = cls.name.lastIndexOf(".");
if (lastIdx < 0) {
mname = cls.name;
} else {
mname = cls.name.substring(lastIdx + 1);
}
} else {
typeStr = node.getType().getName();
}
c(visitModifier(mdf) + " " + typeStr + " " + mname + "(" + ps + ")" + " " + exStr);
if (node.getBody() != null) {
c(visit(node.getBody()));
} else {
c(";");
}
return null;
}
use of kalang.core.Type in project kalang by kasonyang.
the class Ast2Java method toJavaString.
private String toJavaString(ConstExpr ce) {
Object v = ce.getValue();
Type t = ce.getType();
if (v instanceof String) {
return "\"" + v + "\"";
} else if (Types.CHAR_TYPE.equals(t)) {
return "'" + v + "'";
} else if (Types.NULL_TYPE.equals(t)) {
return "null";
} else {
return String.valueOf(v);
}
}
Aggregations