// for [databind#1301] public void testJavaTypeToString() throws Exception { TypeFactory tf = objectMapper().getTypeFactory(); String desc = tf.constructType(DataDefinition.class).toString(); assertNotNull(desc); // could try comparing exact message, but since it's informational try looser: if (!desc.contains("map type")) { fail("Description should contain 'map type', did not: " + desc); } if (!desc.contains("recursive type")) { fail("Description should contain 'recursive type', did not: " + desc); } }
/** @see jaskell.compiler.JaskellVisitor#visit(ConstructorPattern) */ public Object visit(ConstructorPattern a) { String cname = a.getConstructor().getName(); /* retrieve parameter types of constructor */ ConstructorDefinition ctor = (ConstructorDefinition) a.getConstructor().lookup(cname); if (ctor == null) throw new TypeError("Undefined constructor pattern " + a); /* type of data constructed by constructor */ TypeInstantiator ti = new TypeInstantiator(ctor.getDataType()); Type rtype = ti.instance(); Map map = ti.getMap(); TypeSubstitution ts = new TypeSubstitution(map); Iterator ittypes = ctor.getParameters().iterator(); /* retrieve type of patterns */ Iterator it = a.getSubPatterns(); while (it.hasNext()) { try { Pattern p = (Pattern) it.next(); Type actual = TypeFactory.freshBinding(); Type formal = ts.substitute((Type) ittypes.next()); /* unify both types */ p.setType(tu.unify(formal, actual, typeVariablesMap)); } catch (NoSuchElementException nex) { throw new TypeError("Wrong number of arguments to pattern " + a); } } a.setType(rtype); return a.getType(); }
protected JavaType parseType(MyTokenizer tokens) throws IllegalArgumentException { if (!tokens.hasMoreTokens()) { throw _problem(tokens, "Unexpected end-of-string"); } Class<?> base = findClass(tokens.nextToken(), tokens); // either end (ok, non generic type), or generics if (tokens.hasMoreTokens()) { String token = tokens.nextToken(); if ("<".equals(token)) { return _factory._fromParameterizedClass(base, parseTypes(tokens)); } // can be comma that separates types, or closing '>' tokens.pushBack(token); } return _factory._fromClass(base, null); }
/** @see jaskell.compiler.JaskellVisitor#visit(Application) */ public Object visit(Application a) { try { /* get type of function */ Expression fun = a.getFunction(); /* get type deduced from arguments */ LinkedList l = new LinkedList(); Iterator it = a.getArgs().iterator(); while (it.hasNext()) { Expression e = (Expression) it.next(); l.add((Type) e.visit(this)); } Type vt = TypeFactory.freshBinding(); Type ft = Types.fun(l, vt); log.finer("TypeChecker => In application " + a + ", type is " + ft); /* apply substitution on both types */ ft = subst.substitute(ft); /* try to unify function type and constructed types */ Type t = (Type) fun.visit(this); log.finer("In application, function " + fun + " :: " + t); t = subst.substitute(t); log.finer("In application, trying to unify function type " + t + " with body " + ft); /* * TODO : problem with unification of constrained types */ TypeApplication uni = (TypeApplication) tu.unify(t, ft, typeVariablesMap); /* sets type of functional expression - this allows * polymorphic functions to receive several types * in the same code */ // fun.setType(uni); /* apply arguments type to compute return type */ log.finer("Done unify application :" + uni); it = PrimitiveType.functionIterator(uni); Iterator it2 = l.iterator(); TypeApplication ut = uni; while (it2.hasNext()) { /* type of argument */ Type at = (Type) it2.next(); ut = (TypeApplication) it.next(); /* try unification */ tu.unify(((TypeApplication) ut.getDomain()).getRange(), at, new HashMap(typeVariablesMap)); } ft = subst.substitute(ft); fun.setType(ft); log.finer("Setting type of functional element [" + fun + "] to :" + ft); a.setType(ut.getRange()); return ut.getRange(); } catch (TypeError te) { if (te.getLineCol() == null) te.setLineCol(a.getTag("source")); throw te; } }
/** @see jaskell.compiler.JaskellVisitor#visit(Abstraction) */ public Object visit(Abstraction a) { try { Type t = a.getType(); if (t != null) return subst.substitute(t); log.finest("Visiting abstraction : " + a); Expression body = a.getBody(); /* duplicate bindings map to assign types to variables */ pushContext(a.getBindings()); /* create fresh type variables as type for each bound * variable */ Iterator it = namesMap.values().iterator(); LinkedList tl = new LinkedList(); while (it.hasNext()) { LocalBinding name = (LocalBinding) it.next(); Type vt = TypeFactory.freshBinding(); name.setType(vt); tl.add(vt); } Type tv = TypeFactory.freshBinding(); /* create type with all variables for function */ Type ft = Types.fun(tl, tv); log.finer("In abstraction, setting type to " + ft); a.setType(ft); /* analyze body */ Type bt = (Type) body.visit(this); /* unify return type of function with type of body */ Type ret = tu.unify(PrimitiveType.getReturnType(ft), bt, typeVariablesMap); TyvarSubstitution tys = new TyvarSubstitution(typeVariablesMap); tys.visit(a); log.finer("Done abstraction, setting type from " + ft + " to " + a.getType()); popContext(); return a.getType(); } catch (TypeError te) { if (te.getLineCol() == null) te.setLineCol(a.getTag("source")); throw te; } }
public JavaType resolveType(Type type) { return _typeFactory._constructType(type, this); }
public JavaType resolveType(Class<?> cls) { return _typeFactory._constructType(cls, this); }
protected void _resolveBindings(Type t) { if (t == null) return; Class<?> raw; if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) t; Type[] args = pt.getActualTypeArguments(); if (args != null && args.length > 0) { Class<?> rawType = (Class<?>) pt.getRawType(); TypeVariable<?>[] vars = rawType.getTypeParameters(); if (vars.length != args.length) { throw new IllegalArgumentException( "Strange parametrized type (in class " + rawType.getName() + "): number of type arguments != number of type parameters (" + args.length + " vs " + vars.length + ")"); } for (int i = 0, len = args.length; i < len; ++i) { TypeVariable<?> var = vars[i]; String name = var.getName(); if (_bindings == null) { _bindings = new LinkedHashMap<String, JavaType>(); } else { /* 24-Mar-2010, tatu: Better ensure that we do not overwrite something * collected earlier (since we descend towards super-classes): */ if (_bindings.containsKey(name)) continue; } // first: add a placeholder to prevent infinite loops _addPlaceholder(name); // then resolve type _bindings.put(name, _typeFactory._constructType(args[i], this)); } } raw = (Class<?>) pt.getRawType(); } else if (t instanceof Class<?>) { raw = (Class<?>) t; /* [JACKSON-677]: If this is an inner class then the generics are defined on the * enclosing class so we have to check there as well. We don't * need to call getEnclosingClass since anonymous classes declare * generics */ _resolveBindings(raw.getDeclaringClass()); /* 24-Mar-2010, tatu: Can not have true generics definitions, but can * have lower bounds ("<T extends BeanBase>") in declaration itself */ TypeVariable<?>[] vars = raw.getTypeParameters(); if (vars != null && vars.length > 0) { JavaType[] typeParams = null; if (_contextType != null && raw.isAssignableFrom(_contextType.getRawClass())) { typeParams = _typeFactory.findTypeParameters(_contextType, raw); } for (int i = 0; i < vars.length; i++) { TypeVariable<?> var = vars[i]; String name = var.getName(); Type varType = var.getBounds()[0]; if (varType != null) { if (_bindings == null) { _bindings = new LinkedHashMap<String, JavaType>(); } else { // and no overwriting... if (_bindings.containsKey(name)) continue; } _addPlaceholder(name); // to prevent infinite loops if (typeParams != null) { _bindings.put(name, typeParams[i]); } else { _bindings.put(name, _typeFactory._constructType(varType, this)); } } } } } else { // probably can't be any of these... so let's skip for now // if (type instanceof GenericArrayType) { // if (type instanceof TypeVariable<?>) { // if (type instanceof WildcardType) { return; } // but even if it's not a parameterized type, its super types may be: _resolveBindings(raw.getGenericSuperclass()); for (Type intType : raw.getGenericInterfaces()) { _resolveBindings(intType); } }
// for [databind#1301] public void testRecursiveType() { TypeFactory tf = TypeFactory.defaultInstance(); JavaType type = tf.constructType(HashTree.class); assertNotNull(type); }