use of kalang.core.ExecutableDescriptor in project kalang by kasonyang.
the class Ast2Class method visitInvocationExpr.
@Override
public Object visitInvocationExpr(InvocationExpr node) {
int opc;
ExecutableDescriptor method = node.getMethod();
// = internalName(node.getMethod().classNode);
String ownerClass;
if (node instanceof StaticInvokeExpr) {
opc = INVOKESTATIC;
ownerClass = internalName(((StaticInvokeExpr) node).getInvokeClass().getReferencedClassNode());
} else if (node instanceof ObjectInvokeExpr) {
ObjectInvokeExpr oie = (ObjectInvokeExpr) node;
ObjectType targetType = (ObjectType) oie.getInvokeTarget().getType();
ownerClass = internalName(targetType);
ExprNode target = oie.getInvokeTarget();
visit(target);
if (Modifier.isPrivate(method.getModifier()) || (target instanceof SuperExpr) || method.getName().equals("<init>")) {
opc = INVOKESPECIAL;
} else {
opc = ModifierUtil.isInterface(targetType.getClassNode().modifier) ? INVOKEINTERFACE : INVOKEVIRTUAL;
}
} else {
throw Exceptions.unsupportedTypeException(node);
}
visitAll(node.getArguments());
md.visitMethodInsn(opc, ownerClass, method.getName(), getMethodDescriptor(method.getMethodNode()));
return null;
}
use of kalang.core.ExecutableDescriptor in project kalang by kasonyang.
the class StaticInvokeExpr method create.
public static StaticInvokeExpr create(ClassReference clazz, String methodName, ExprNode[] args, @Nullable ClassNode caller) throws MethodNotFoundException, AmbiguousMethodException {
ObjectType clazzType = Types.getClassType(clazz.getReferencedClassNode());
// TODO static only
// TODO what about generic static method?
MethodDescriptor[] candidates = clazzType.getMethodDescriptors(caller, true, true);
MethodSelection ms = applyMethod(Types.getClassType(clazz.getReferencedClassNode()), methodName, args, candidates);
ExecutableDescriptor md = ms.selectedMethod;
if (!AstUtil.isStatic(md.getModifier())) {
throw new MethodNotFoundException(methodName + " is not static");
}
return new StaticInvokeExpr(clazz, md, ms.appliedArguments);
}
use of kalang.core.ExecutableDescriptor in project kalang by kasonyang.
the class ObjectInvokeExpr method create.
public static ObjectInvokeExpr create(ExprNode target, String methodName, ExprNode[] args, @Nullable ClassNode caller) throws MethodNotFoundException, AmbiguousMethodException {
ObjectType targetType = (ObjectType) target.getType();
ClassNode clazz = targetType.getClassNode();
boolean recursive = !"<init>".equals(methodName);
// MethodNode[] candidates = AstUtil.listAccessibleMethods(clazz, caller , recursive);
List<ExecutableDescriptor> candidates = new LinkedList();
candidates.addAll(Arrays.asList(targetType.getMethodDescriptors(caller, recursive, true)));
candidates.addAll(Arrays.asList(targetType.getConstructorDescriptors(caller)));
MethodSelection ms = applyMethod(targetType, methodName, args, candidates.toArray(new ExecutableDescriptor[candidates.size()]));
ExecutableDescriptor md = ms.selectedMethod;
if (AstUtil.isStatic(md.getModifier())) {
throw new MethodNotFoundException(methodName + " is static");
}
return new ObjectInvokeExpr(target, ms.selectedMethod, ms.appliedArguments);
}
use of kalang.core.ExecutableDescriptor in project kalang by kasonyang.
the class AstUtil method isConstructorCallStatement.
public static boolean isConstructorCallStatement(Statement stmt) {
try {
ExprStmt exprStmt = (ExprStmt) stmt;
InvocationExpr invExpr = (InvocationExpr) exprStmt.getExpr();
ExecutableDescriptor method = invExpr.getMethod();
return method.getName().equals("<init>") && !Modifier.isStatic(method.getModifier());
} catch (ClassCastException ex) {
return false;
}
}
Aggregations