Search in sources :

Example 1 with NullableKind

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

the class JvmClassNode method getDeclaredMethodNodes.

@Override
public MethodNode[] getDeclaredMethodNodes() {
    if (!this.methodsInitialized) {
        this.methodsInitialized = true;
        List<Executable> methods = new LinkedList();
        methods.addAll(Arrays.asList(clazz.getDeclaredMethods()));
        methods.addAll(Arrays.asList(clazz.getDeclaredConstructors()));
        for (Executable m : methods) {
            NullableKind nullable = getNullable(m.getAnnotations());
            Type mType;
            String mName;
            int mModifier;
            if (m instanceof Method) {
                mType = getType(((Method) m).getGenericReturnType(), getGenericTypeMap(), ((Method) m).getReturnType(), nullable);
                mName = m.getName();
                mModifier = m.getModifiers();
            } else if (m instanceof Constructor) {
                mName = "<init>";
                // getType(clz);
                mType = Types.VOID_TYPE;
                // | Modifier.STATIC;
                mModifier = m.getModifiers();
            } else {
                throw Exceptions.unexceptedValue(m);
            }
            MethodNode methodNode = createMethodNode(mType, mName, mModifier);
            for (Parameter p : m.getParameters()) {
                NullableKind pnullable = getNullable(p.getAnnotations());
                methodNode.createParameter(getType(p.getParameterizedType(), getGenericTypeMap(), p.getType(), pnullable), p.getName());
            }
            for (Class e : m.getExceptionTypes()) {
                methodNode.addExceptionType(getType(e, getGenericTypeMap(), e, NullableKind.NONNULL));
            }
        }
    }
    return super.getDeclaredMethodNodes();
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) WildcardType(kalang.core.WildcardType) ClassType(kalang.core.ClassType) MethodNode(kalang.ast.MethodNode) NullableKind(kalang.core.NullableKind) Constructor(java.lang.reflect.Constructor) Parameter(java.lang.reflect.Parameter) Method(java.lang.reflect.Method) Executable(java.lang.reflect.Executable) LinkedList(java.util.LinkedList)

Example 2 with NullableKind

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

the class AstBuilder method getVarObjectType.

private Type getVarObjectType(VarObject p) {
    Type type = this.overrideTypes.get(p);
    if (type == null)
        type = p.getType();
    // TODO handle other object type
    if (type instanceof ClassType) {
        Integer ns = nullState.get(p);
        NullableKind nullable;
        if (ns == null) {
            nullable = ((ObjectType) type).getNullable();
        } else if (ns == NULLSTATE_MUST_NONNULL) {
            nullable = NullableKind.NONNULL;
        } else if (ns == NULLSTATE_UNKNOWN) {
            nullable = NullableKind.UNKNOWN;
        } else if (ns == NULLSTATE_MUST_NULL || ns == NULLSTATE_NULLABLE) {
            nullable = NullableKind.NULLABLE;
        } else {
            throw Exceptions.unexceptedValue(ns);
        }
        return Types.getClassType((ClassType) type, nullable);
    } else {
        return type;
    }
}
Also used : 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) NullableKind(kalang.core.NullableKind) ClassType(kalang.core.ClassType)

Example 3 with NullableKind

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

the class AstBuilder method parseClassType.

@Nullable
protected ObjectType parseClassType(KalangParser.ClassTypeContext ctx) {
    NullableKind nullable = ctx.nullable == null ? NullableKind.NONNULL : NullableKind.NULLABLE;
    Token rawTypeToken = ctx.rawClass;
    List<String> classNameParts = new LinkedList();
    for (Token p : ctx.paths) {
        classNameParts.add(p.getText());
    }
    if (ctx.innerClass != null) {
        classNameParts.add(rawTypeToken.getText() + "$" + ctx.innerClass.getText());
    } else {
        classNameParts.add(rawTypeToken.getText());
    }
    String rawType = String.join(".", classNameParts);
    for (GenericType gt : thisClazz.getGenericTypes()) {
        if (rawType.equals(gt.getName()))
            return gt;
    }
    ObjectType clazzType = requireClassType(rawType, rawTypeToken);
    if (clazzType == null)
        return null;
    ClassNode clazzNode = clazzType.getClassNode();
    GenericType[] clzDeclaredGenericTypes = clazzNode.getGenericTypes();
    List<KalangParser.ParameterizedElementTypeContext> parameterTypes = ctx.parameterTypes;
    if (parameterTypes != null && !parameterTypes.isEmpty()) {
        Type[] typeArguments = new Type[parameterTypes.size()];
        if (parameterTypes != null && parameterTypes.size() > 0) {
            if (clzDeclaredGenericTypes.length != parameterTypes.size()) {
                diagnosisReporter.report(Diagnosis.Kind.ERROR, "wrong number of type arguments", ctx);
                return null;
            }
            for (int i = 0; i < typeArguments.length; i++) {
                typeArguments[i] = parseParameterizedElementType(parameterTypes.get(i));
                // TODO should return null?
                if (typeArguments[i] == null)
                    return null;
            }
        }
        return Types.getClassType(clazzType.getClassNode(), typeArguments, nullable);
    } else {
        return Types.getClassType(clazzType.getClassNode(), nullable);
    }
}
Also used : ClassNode(kalang.ast.ClassNode) GenericType(kalang.core.GenericType) NullableKind(kalang.core.NullableKind) Token(org.antlr.v4.runtime.Token) LinkedList(java.util.LinkedList) 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) Nullable(javax.annotation.Nullable)

Example 4 with NullableKind

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

the class JvmClassNode method getFields.

@Override
public FieldNode[] getFields() {
    if (!this.fieldsInitialized) {
        this.fieldsInitialized = true;
        for (Field f : clazz.getDeclaredFields()) {
            NullableKind nullable = getNullable(f.getAnnotations());
            FieldNode fn = createField(getType(f.getGenericType(), getGenericTypeMap(), f.getType(), nullable), f.getName(), f.getModifiers());
        }
    }
    return super.getFields();
}
Also used : Field(java.lang.reflect.Field) FieldNode(kalang.ast.FieldNode) NullableKind(kalang.core.NullableKind)

Aggregations

NullableKind (kalang.core.NullableKind)4 ClassType (kalang.core.ClassType)3 GenericType (kalang.core.GenericType)3 ObjectType (kalang.core.ObjectType)3 Type (kalang.core.Type)3 WildcardType (kalang.core.WildcardType)3 LinkedList (java.util.LinkedList)2 ArrayType (kalang.core.ArrayType)2 PrimitiveType (kalang.core.PrimitiveType)2 Constructor (java.lang.reflect.Constructor)1 Executable (java.lang.reflect.Executable)1 Field (java.lang.reflect.Field)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Nullable (javax.annotation.Nullable)1 ClassNode (kalang.ast.ClassNode)1 FieldNode (kalang.ast.FieldNode)1 MethodNode (kalang.ast.MethodNode)1 Token (org.antlr.v4.runtime.Token)1