Search in sources :

Example 26 with ObjectType

use of kalang.core.ObjectType 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)

Example 27 with ObjectType

use of kalang.core.ObjectType 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);
}
Also used : ObjectType(kalang.core.ObjectType) MethodDescriptor(kalang.core.MethodDescriptor) MethodNotFoundException(kalang.MethodNotFoundException) ExecutableDescriptor(kalang.core.ExecutableDescriptor)

Example 28 with ObjectType

use of kalang.core.ObjectType 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);
}
Also used : ObjectType(kalang.core.ObjectType) MethodNotFoundException(kalang.MethodNotFoundException) ExecutableDescriptor(kalang.core.ExecutableDescriptor)

Example 29 with ObjectType

use of kalang.core.ObjectType in project kalang by kasonyang.

the class AstUtil method createClassNodeWithInterfaces.

public static ClassNode createClassNodeWithInterfaces(String name, @Nullable ObjectType superType, @Nullable ObjectType... interfaces) {
    ClassNode cn = new ClassNode();
    cn.name = name;
    cn.setSuperType(superType == null ? Types.getRootType() : superType);
    if (interfaces != null) {
        for (ObjectType itf : interfaces) cn.addInterface(itf);
    }
    return cn;
}
Also used : ClassNode(kalang.ast.ClassNode) ObjectType(kalang.core.ObjectType)

Example 30 with ObjectType

use of kalang.core.ObjectType in project kalang by kasonyang.

the class BoxUtil method getCastMethod.

private static int getCastMethod(Type fromType, Type toType) {
    if (toType.isAssignableFrom(fromType)) {
        return CAST_NOTHING;
    }
    if (fromType instanceof PrimitiveType && toType instanceof PrimitiveType) {
        // if ((toType) instanceof PrimitiveType) {
        if (MathType.castable(MathType.getType(fromType.getName()), MathType.getType(toType.getName()))) {
            return CAST_PRIMITIVE;
        }
    } else if (fromType instanceof PrimitiveType && toType instanceof ObjectType) {
        if (toType.equals(Types.getRootType())) {
            return CAST_PRIMITIVE_TO_OBJECT;
        }
        // if (fromType.equals(Types.NULL_TYPE)) {
        // return CAST_NOTHING;
        // }
        // //            if (toType.equals(Types.STRING_CLASS_TYPE)) {
        // //                return CAST_PRIMITIVE_TO_STRING;
        // //            }
        PrimitiveType toPriType = Types.getPrimitiveType((ObjectType) toType);
        if (toPriType == null) {
            return CAST_UNSUPPORTED;
        }
        if (toPriType.equals(fromType)) {
            return CAST_PRIMITIVE_TO_OBJECT;
        }
    } else if (fromType instanceof ObjectType && toType instanceof PrimitiveType) {
        // if() {
        ObjectType fromClassType = (ObjectType) fromType;
        PrimitiveType fromPrimitive = Types.getPrimitiveType(fromClassType);
        if (fromPrimitive == null) {
            return CAST_UNSUPPORTED;
        }
        if (fromPrimitive.equals(toType)) {
            return CAST_OBJECT_TO_PRIMITIVE;
        }
    }
    // }
    return CAST_UNSUPPORTED;
}
Also used : ObjectType(kalang.core.ObjectType) PrimitiveType(kalang.core.PrimitiveType)

Aggregations

ObjectType (kalang.core.ObjectType)32 GenericType (kalang.core.GenericType)15 ClassType (kalang.core.ClassType)14 Type (kalang.core.Type)14 ExprNode (kalang.ast.ExprNode)11 ArrayType (kalang.core.ArrayType)11 PrimitiveType (kalang.core.PrimitiveType)9 WildcardType (kalang.core.WildcardType)9 MethodNotFoundException (kalang.MethodNotFoundException)8 ClassNode (kalang.ast.ClassNode)7 Nullable (javax.annotation.Nullable)6 AmbiguousMethodException (kalang.AmbiguousMethodException)6 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)6 LinkedList (java.util.LinkedList)4 FieldNotFoundException (kalang.FieldNotFoundException)4 BlockStmt (kalang.ast.BlockStmt)4 ThisExpr (kalang.ast.ThisExpr)4 ExprStmt (kalang.ast.ExprStmt)3 InvocationExpr (kalang.ast.InvocationExpr)3 MethodNode (kalang.ast.MethodNode)3