private static Type resolveVar(Type resolved, String varName, Type declaringType, EntityType context) {
        // get parameter index of var in declaring type
        int index = -1;
        for (int i = 0; i < declaringType.getParameters().size(); i++) {
            Type param = unwrap(declaringType.getParameters().get(i));
            if (Objects.equals(getVarName(param), varName)) {
                index = i;
            }
        }

        if (index == -1) {
            throw new IllegalStateException("Did not find type " + varName
                    + " in " + declaringType + " for " + context);
        }

        Supertype type = context.getSuperType();
        while (!type.getEntityType().equals(declaringType)) {
            type = type.getEntityType().getSuperType();
        }
        if (!type.getType().getParameters().isEmpty()) {
            return type.getType().getParameters().get(index);
        } else {
            // raw type
            return resolved;
        }
    }
Resolve type declared in declaringType for context

@param type type to be resolved
@param declaringType declaration context of type
@param context target context of type
@return resolved type
querydsl-codegen/src/main/java/com/querydsl/codegen/TypeResolver.java