use of kalang.core.ObjectType in project kalang by kasonyang.
the class Ast2Class method visitMethodNode.
@Override
public Object visitMethodNode(MethodNode node) {
int access = node.getModifier();
md = classWriter.visitMethod(access, internalName(node.getName()), getMethodDescriptor(node), methodSignature(node), internalName(node.getExceptionTypes()));
if (node.getType() instanceof ObjectType) {
annotationNullable(md, (ObjectType) node.getType());
}
annotation(md, node.getAnnotations());
this.methodStartLabel = new Label();
this.methodEndLabel = new Label();
if (AstUtil.isStatic(node.getModifier())) {
varIdCounter = 0;
} else {
varIdCounter = 1;
}
BlockStmt body = node.getBody();
ParameterNode[] parameters = node.getParameters();
for (int i = 0; i < parameters.length; i++) {
ParameterNode p = parameters[i];
visit(p);
if (p.getType() instanceof ObjectType) {
md.visitParameterAnnotation(i, getClassDescriptor(getNullableAnnotation((ObjectType) p.getType())), true).visitEnd();
}
}
md.visitLabel(methodStartLabel);
if (body != null) {
visit(body);
if (node.getType().equals(VOID_TYPE)) {
md.visitInsn(RETURN);
}
md.visitLabel(methodEndLabel);
try {
md.visitMaxs(0, 0);
} catch (Exception ex) {
ex.printStackTrace(System.err);
// throw new RuntimeException("exception when visit method:" + node.name, ex);
}
}
md.visitEnd();
return null;
}
use of kalang.core.ObjectType in project kalang by kasonyang.
the class Ast2Class method classSignature.
@Nullable
private String classSignature(ClassNode c) {
GenericType[] genericTypes = c.getGenericTypes();
if (genericTypes == null || genericTypes.length == 0) {
return null;
}
String gnrTypeStr = "";
for (GenericType t : genericTypes) {
gnrTypeStr += t.getName() + ":" + "Ljava/lang/Object;";
}
String superTypeStr = "";
if (c.getSuperType() != null)
superTypeStr += typeSignature(c.getSuperType());
for (ObjectType itf : c.getInterfaces()) {
superTypeStr += typeSignature(itf);
}
return "<" + gnrTypeStr + ">" + superTypeStr;
}
use of kalang.core.ObjectType 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.ObjectType in project kalang by kasonyang.
the class AstBuilder method onAssign.
private void onAssign(ExprNode to, ExprNode expr) {
removeOverrideType(to);
if (to instanceof VarExpr) {
((VarExpr) to).removeOverrideType();
} else if (to instanceof ParameterExpr) {
((ParameterExpr) to).removeOverrideType();
}
VarObject key = getOverrideTypeKey(to);
if (key != null) {
Type toType = to.getType();
if (toType instanceof ObjectType) {
Type type = expr.getType();
int ns;
if (Types.NULL_TYPE.equals(type)) {
ns = NULLSTATE_MUST_NULL;
} else if (type instanceof ObjectType) {
ns = getNullState(((ObjectType) type).getNullable());
} else {
throw Exceptions.unexceptedValue(type);
}
nullState.put(key, ns);
}
}
}
use of kalang.core.ObjectType in project kalang by kasonyang.
the class AstBuilder method getObjectInvokeExpr.
@Nullable
private ExprNode getObjectInvokeExpr(ExprNode target, String methodName, ExprNode[] args, ParserRuleContext ctx) {
if ("<init>".equals(methodName)) {
throw Exceptions.unexceptedException("Don't get constructor by this method.");
}
Type targetType = target.getType();
if (!(targetType instanceof ObjectType)) {
handleSyntaxError("class type required.", ctx);
return null;
}
ObjectType targetClassType = (ObjectType) targetType;
if (targetClassType.getNullable() == NullableKind.NULLABLE) {
handleSyntaxError("expression may be null", ctx);
return null;
}
ExprNode expr;
try {
ObjectInvokeExpr invoke = ObjectInvokeExpr.create(target, methodName, args, thisClazz);
if (invoke.getMethod().getMethodNode().getType() instanceof GenericType) {
Type invokeType = invoke.getType();
if (invokeType instanceof ObjectType) {
expr = new CastExpr(invokeType, invoke);
} else {
expr = invoke;
}
} else {
expr = invoke;
}
} catch (MethodNotFoundException ex) {
methodNotFound(ctx.start, className, methodName, args);
expr = new UnknownInvocationExpr(target, methodName, args);
} catch (AmbiguousMethodException ex) {
methodIsAmbiguous(ctx.start, ex);
return null;
}
mapAst(expr, ctx);
return expr;
}
Aggregations