Search in sources :

Example 1 with AstNotFoundException

use of kalang.AstNotFoundException in project kalang by kasonyang.

the class JvmClassNode method transType.

// TODO why transType could be null?
@Nullable
private Type transType(java.lang.reflect.Type t, Map<TypeVariable, GenericType> genericTypes) {
    if (t instanceof TypeVariable) {
        GenericType vt = genericTypes.get((TypeVariable) t);
        // FIXME it may be null if TypeVariable comes from method
        if (vt != null) {
            return vt;
        }
        return null;
    } else if (t instanceof java.lang.reflect.ParameterizedType) {
        java.lang.reflect.ParameterizedType pt = (java.lang.reflect.ParameterizedType) t;
        Type rawType = transType(pt.getRawType(), genericTypes);
        if (!(rawType instanceof ObjectType)) {
            return null;
        }
        java.lang.reflect.Type[] typeArgs = pt.getActualTypeArguments();
        Type[] gTypes = transType(typeArgs, genericTypes);
        if (gTypes == null) {
            return null;
        }
        return Types.getClassType(((ObjectType) rawType).getClassNode(), gTypes);
    } else if (t instanceof java.lang.reflect.WildcardType) {
        java.lang.reflect.WildcardType wt = (java.lang.reflect.WildcardType) t;
        Type[] upperBounds = transType(wt.getUpperBounds(), genericTypes);
        if (upperBounds == null) {
            return null;
        }
        Type[] lowerBounds = transType(wt.getLowerBounds(), genericTypes);
        if (lowerBounds == null) {
            return null;
        }
        return new WildcardType(upperBounds, lowerBounds);
    } else if (t instanceof GenericArrayType) {
        GenericArrayType gt = (GenericArrayType) t;
        Type ct = transType(gt.getGenericComponentType(), genericTypes);
        if (ct == null) {
            return null;
        }
        return Types.getArrayType(ct, NullableKind.NONNULL);
    } else if (t instanceof Class) {
        Class type = (Class) t;
        if (type.isPrimitive()) {
            return Types.getPrimitiveType(type.getTypeName());
        } else if (type.isArray()) {
            Type ct = transType(type.getComponentType(), genericTypes);
            if (ct == null) {
                return null;
            }
            return Types.getArrayType(ct);
        } else {
            try {
                return Types.getClassType(this.astLoader.findAst(type));
            } catch (AstNotFoundException ex) {
                throw new RuntimeException(ex);
            }
        }
    } else {
        return null;
    }
}
Also used : GenericType(kalang.core.GenericType) GenericArrayType(java.lang.reflect.GenericArrayType) ObjectType(kalang.core.ObjectType) 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) WildcardType(kalang.core.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) AstNotFoundException(kalang.AstNotFoundException) Nullable(javax.annotation.Nullable)

Example 2 with AstNotFoundException

use of kalang.AstNotFoundException in project kalang by kasonyang.

the class AstLoader method loadAst.

@Nonnull
public ClassNode loadAst(@Nonnull String className) throws AstNotFoundException {
    ClassNode ast = cachedAsts.get(className);
    if (ast != null)
        return ast;
    if (parent != null) {
        try {
            return parent.loadAst(className);
        } catch (AstNotFoundException ex) {
        }
    }
    if (className.endsWith("[]")) {
        String name = className;
        name = name.substring(0, name.length() - 2);
        ast = AstUtil.createArrayAst(loadAst(name).name);
    } else {
        ast = findAst(className);
        if (ast == null)
            throw new AstNotFoundException(className);
    }
    cachedAsts.put(className, ast);
    return ast;
}
Also used : ClassNode(kalang.ast.ClassNode) AstNotFoundException(kalang.AstNotFoundException) Nonnull(javax.annotation.Nonnull)

Aggregations

AstNotFoundException (kalang.AstNotFoundException)2 GenericArrayType (java.lang.reflect.GenericArrayType)1 TypeVariable (java.lang.reflect.TypeVariable)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 ClassNode (kalang.ast.ClassNode)1 ClassType (kalang.core.ClassType)1 GenericType (kalang.core.GenericType)1 ObjectType (kalang.core.ObjectType)1 Type (kalang.core.Type)1 WildcardType (kalang.core.WildcardType)1