Search in sources :

Example 1 with FieldNotFoundException

use of kalang.FieldNotFoundException in project kalang by kasonyang.

the class AstBuilder method getImplicitInvokeExpr.

@Nullable
private ExprNode getImplicitInvokeExpr(String methodName, ExprNode[] args, ParserRuleContext ctx) {
    ExprNode expr = null;
    try {
        ObjectType clazzType = getThisType();
        InvocationExpr.MethodSelection ms = InvocationExpr.applyMethod(clazzType, methodName, args, clazzType.getMethodDescriptors(thisClazz, true, true));
        if (Modifier.isStatic(ms.selectedMethod.getModifier())) {
            expr = new StaticInvokeExpr(new ClassReference(thisClazz), ms.selectedMethod, ms.appliedArguments);
        } else {
            expr = new ObjectInvokeExpr(new ThisExpr(getThisType()), ms.selectedMethod, ms.appliedArguments);
        }
    } catch (MethodNotFoundException ex) {
        if (args.length == 1 && (methodName.equals("print") || methodName.equals("println"))) {
            try {
                StaticFieldExpr fieldExpr = StaticFieldExpr.create(new ClassReference(Types.requireClassType("java.lang.System").getClassNode()), "out", null);
                expr = getObjectInvokeExpr(fieldExpr, methodName, args, ctx);
            } catch (FieldNotFoundException fnfEx) {
                throw Exceptions.unexceptedException(fnfEx);
            }
        }
        if (expr == null) {
            this.methodNotFound(ctx.getStart(), className, methodName, args);
            expr = new UnknownInvocationExpr(null, methodName, args);
        }
    } catch (AmbiguousMethodException ex) {
        methodIsAmbiguous(ctx.start, ex);
        return null;
    }
    mapAst(expr, ctx);
    return expr;
}
Also used : FieldNotFoundException(kalang.FieldNotFoundException) ExprNode(kalang.ast.ExprNode) StaticInvokeExpr(kalang.ast.StaticInvokeExpr) ObjectType(kalang.core.ObjectType) StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) ClassReference(kalang.ast.ClassReference) MethodNotFoundException(kalang.MethodNotFoundException) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) InvocationExpr(kalang.ast.InvocationExpr) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) ThisExpr(kalang.ast.ThisExpr) AmbiguousMethodException(kalang.AmbiguousMethodException) Nullable(javax.annotation.Nullable)

Example 2 with FieldNotFoundException

use of kalang.FieldNotFoundException in project kalang by kasonyang.

the class AstBuilder method getObjectFieldExpr.

@Nullable
protected ExprNode getObjectFieldExpr(ExprNode expr, String fieldName, @Nullable ParserRuleContext rule) {
    ExprNode ret;
    Type type = expr.getType();
    if (!(type instanceof ObjectType)) {
        // AstBuilder.this.handleSyntaxError("unsupported type", rule==null ? ParserRuleContext.EMPTY : rule);
        return null;
    }
    ObjectType exprType = (ObjectType) type;
    if ((exprType instanceof ArrayType)) {
        return null;
    } else {
        try {
            ret = ObjectFieldExpr.create(expr, fieldName, exprType.getClassNode());
        } catch (FieldNotFoundException ex) {
            return null;
        }
    }
    if (rule != null)
        mapAst(ret, rule);
    return ret;
}
Also used : ExprNode(kalang.ast.ExprNode) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) FieldNotFoundException(kalang.FieldNotFoundException) Nullable(javax.annotation.Nullable)

Example 3 with FieldNotFoundException

use of kalang.FieldNotFoundException in project kalang by kasonyang.

the class AstBuilder method getObjectFieldLikeExpr.

protected ExprNode getObjectFieldLikeExpr(ExprNode expr, String fieldName, @Nullable ParserRuleContext rule) {
    ExprNode ret;
    Type type = expr.getType();
    if (!(type instanceof ObjectType)) {
        AstBuilder.this.handleSyntaxError("unsupported type", rule == null ? ParserRuleContext.EMPTY : rule);
        return null;
    }
    ObjectType exprType = (ObjectType) type;
    if ((exprType instanceof ArrayType) && fieldName.equals("length")) {
        ret = new ArrayLengthExpr(expr);
    } else {
        try {
            ret = ObjectFieldExpr.create(expr, fieldName, thisClazz);
        } catch (FieldNotFoundException ex) {
            this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "field not found:" + fieldName, rule);
            ret = new UnknownFieldExpr(expr, exprType.getClassNode(), fieldName);
        }
    }
    if (rule != null)
        mapAst(ret, rule);
    return ret;
}
Also used : ExprNode(kalang.ast.ExprNode) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) FieldNotFoundException(kalang.FieldNotFoundException) UnknownFieldExpr(kalang.ast.UnknownFieldExpr) ArrayLengthExpr(kalang.ast.ArrayLengthExpr)

Example 4 with FieldNotFoundException

use of kalang.FieldNotFoundException in project kalang by kasonyang.

the class AstBuilder method getStaticFieldExpr.

protected AssignableExpr getStaticFieldExpr(ClassReference clazz, String fieldName, ParserRuleContext rule) {
    AssignableExpr ret;
    try {
        ret = StaticFieldExpr.create(clazz, fieldName, thisClazz);
    } catch (FieldNotFoundException ex) {
        // ret = new UnknownFieldExpr(clazz, clazz.getReferencedClassNode(), fieldName);
        return null;
    }
    mapAst(ret, rule);
    return ret;
}
Also used : AssignableExpr(kalang.ast.AssignableExpr) FieldNotFoundException(kalang.FieldNotFoundException)

Example 5 with FieldNotFoundException

use of kalang.FieldNotFoundException in project kalang by kasonyang.

the class ObjectFieldExpr method create.

@Nonnull
public static FieldExpr create(@Nonnull ExprNode target, String fieldName, @Nullable ClassNode caller) throws FieldNotFoundException {
    Type type = target.getType();
    if (!(type instanceof ObjectType)) {
        throw new UnsupportedOperationException("unsupported type:" + type);
    }
    ObjectType classType = (ObjectType) type;
    FieldDescriptor field = getField(classType, fieldName, caller);
    if (AstUtil.isStatic(field.getModifier())) {
        throw new FieldNotFoundException(fieldName + " is static");
    }
    return new ObjectFieldExpr(target, field);
}
Also used : ObjectType(kalang.core.ObjectType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) FieldNotFoundException(kalang.FieldNotFoundException) FieldDescriptor(kalang.core.FieldDescriptor) Nonnull(javax.annotation.Nonnull)

Aggregations

FieldNotFoundException (kalang.FieldNotFoundException)5 ObjectType (kalang.core.ObjectType)4 ExprNode (kalang.ast.ExprNode)3 Type (kalang.core.Type)3 Nullable (javax.annotation.Nullable)2 ArrayType (kalang.core.ArrayType)2 ClassType (kalang.core.ClassType)2 GenericType (kalang.core.GenericType)2 PrimitiveType (kalang.core.PrimitiveType)2 WildcardType (kalang.core.WildcardType)2 Nonnull (javax.annotation.Nonnull)1 AmbiguousMethodException (kalang.AmbiguousMethodException)1 MethodNotFoundException (kalang.MethodNotFoundException)1 ArrayLengthExpr (kalang.ast.ArrayLengthExpr)1 AssignableExpr (kalang.ast.AssignableExpr)1 ClassReference (kalang.ast.ClassReference)1 InvocationExpr (kalang.ast.InvocationExpr)1 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)1 StaticFieldExpr (kalang.ast.StaticFieldExpr)1 StaticInvokeExpr (kalang.ast.StaticInvokeExpr)1