Java Code Examples for org.apache.commons.lang3.reflect.TypeUtils#parameterize()

The following examples show how to use org.apache.commons.lang3.reflect.TypeUtils#parameterize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FreeIpaClient.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public Optional<User> userFind(String user) throws FreeIpaClientException {
    List<Object> flags = List.of(user);
    Map<String, Object> params = Map.of(
            "uid", user,
            "all", true
    );
    ParameterizedType type = TypeUtils
            .parameterize(List.class, User.class);
    List<User> foundUsers = (List<User>) invoke("user_find", flags, params, type).getResult();
    if (foundUsers.size() > 1) {
        LOGGER.error("Found more than 1 user with uid {}.", user);
    }
    if (foundUsers.isEmpty()) {
        return Optional.empty();
    } else {
        return Optional.of(foundUsers.get(0));
    }
}
 
Example 2
Source File: Utils.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private static Type doDeserializeType(String name) {
   int startIdx = name.indexOf('<');
   if(startIdx == -1) {
      return classForName(name);
   }
   else if(name.charAt(name.length() - 1) == '>') {
      Class<?> rawType = classForName(name.substring(0, startIdx));
      String [] typeNames = StringUtils.split(name.substring(startIdx+1, name.length() - 1), ',');
      Type [] typeParameters = new Type[typeNames.length];
      for(int i=0; i<typeNames.length; i++) {
         typeParameters[i] = doDeserializeType(typeNames[i]);
      }
      return TypeUtils.parameterize(rawType, typeParameters);
   }
   else {
      throw new IllegalArgumentException("Invalid class name, missing close '>': " + name);
   }
}
 
Example 3
Source File: AttributeKey.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
private static Type wrap(Type type) {
   if(type instanceof ParameterizedType) {
      ParameterizedType pType  = (ParameterizedType) type;
      // re-wrap in the internal representation of TypeUtils for equalities sake (and esp hashCode)
      type = TypeUtils.parameterize((Class<?>) pType.getRawType(), wrap(pType.getActualTypeArguments()));
   }
   return type;
}
 
Example 4
Source File: FreeIpaClient.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public List<TopologySegment> findTopologySegments(String topologySuffixCn) throws FreeIpaClientException {
    List<Object> flags = List.of(topologySuffixCn);
    Map<String, Object> params = Map.of();
    ParameterizedType type = TypeUtils
            .parameterize(List.class, TopologySegment.class);
    return (List<TopologySegment>) invoke("topologysegment_find", flags, params, type).getResult();
}
 
Example 5
Source File: FreeIpaClient.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public Set<Group> groupFindAll() throws FreeIpaClientException {
    List<Object> flags = List.of();
    Map<String, Object> params = Map.of(
            "sizelimit", 0,
            "timelimit", 0,
            "all", true
    );
    ParameterizedType type = TypeUtils
            .parameterize(Set.class, Group.class);
    return (Set<Group>) invoke("group_find", flags, params, type).getResult();
}
 
Example 6
Source File: FreeIpaClient.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public Set<IpaServer> findAllServers() throws FreeIpaClientException {
    List<Object> flags = List.of();
    Map<String, Object> params = Map.of(
            "sizelimit", 0,
            "timelimit", 0
    );
    ParameterizedType type = TypeUtils
            .parameterize(Set.class, IpaServer.class);
    return (Set<IpaServer>) invoke("server_find", flags, params, type).getResult();
}
 
Example 7
Source File: FreeIpaClient.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public Set<DnsRecord> findAllDnsRecordInZone(String dnsZoneName) throws FreeIpaClientException {
    List<Object> flags = List.of(dnsZoneName);
    Map<String, Object> params = Map.of();
    ParameterizedType type = TypeUtils
            .parameterize(Set.class, DnsRecord.class);
    return (Set<DnsRecord>) invoke("dnsrecord_find", flags, params, type).getResult();
}
 
Example 8
Source File: FreeIpaClient.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public Set<Role> findAllRole() throws FreeIpaClientException {
    List<Object> flags = List.of();
    Map<String, Object> params = Map.of(
            "sizelimit", 0,
            "timelimit", 0
    );
    ParameterizedType type = TypeUtils
            .parameterize(Set.class, Role.class);
    return (Set<Role>) invoke("role_find", flags, params, type).getResult();
}
 
Example 9
Source File: FreeIpaClient.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public Set<User> userFindAll() throws FreeIpaClientException {
    List<Object> flags = List.of();
    Map<String, Object> params = Map.of(
            "sizelimit", 0,
            "timelimit", 0,
            "all", true
    );
    ParameterizedType type = TypeUtils
            .parameterize(Set.class, User.class);
    return (Set<User>) invoke("user_find", flags, params, type).getResult();
}
 
Example 10
Source File: ReflectUtil.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/**
 * 将type中的变量参数实例化
 *
 * @param type
 * @param resolver
 *            将变量转换成实际的类的方法
 * @return
 */
public static Type parameterize(Type type,
                                Function<TypeVariable<?>, Type> resolver) {
    // 类:String
    if (type instanceof Class) {
        return type;
    }
    // 变量:E
    if (type instanceof TypeVariable) {
        return resolver.apply((TypeVariable<?>) type);
    }
    // 泛型:List<E>
    if (type instanceof ParameterizedType) {
        ParameterizedType paramType = (ParameterizedType) type;
        Type[] oriTypes = paramType.getActualTypeArguments();
        // 若参数中出现了泛型或变量则使用递归
        boolean recursion = false;
        for (Type oriType : oriTypes) {
            if (!(oriType instanceof Class)) {
                recursion = true;
                break;
            }
        }
        if (recursion) {
            Type[] actTypes = new Type[oriTypes.length];
            for (int i = 0; i < oriTypes.length; i++) {
                actTypes[i] = parameterize(oriTypes[i], resolver);
            }
            return TypeUtils.parameterize((Class<?>) paramType.getRawType(),
                    actTypes);
        } else {
            return type;
        }
    }
    throw new RuntimeException("不支持的参数类型:" + type.getClass().getName());
}
 
Example 11
Source File: FreeIpaClient.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public Set<Service> findAllService() throws FreeIpaClientException {
    List<Object> flags = List.of();
    Map<String, Object> params = Map.of(
            "sizelimit", 0,
            "timelimit", 0
    );
    ParameterizedType type = TypeUtils
            .parameterize(Set.class, Service.class);
    return (Set<Service>) invoke("service_find", flags, params, type).getResult();
}
 
Example 12
Source File: SerializerRegistrar.java    From pmd-designer with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Parses a string into a type. Returns null if it doesn't succeed.
 * FIXME Only supports parameterized types with at most one type argument.
 * Doesn't support wildcard types.
 *
 * TODO make a real parser someday
 */
private static Type parseType(String t) {
    Matcher matcher = PARAM_TYPE_MATCHER.matcher(t.replaceAll("\\s+", ""));
    if (matcher.matches()) {
        String raw = matcher.group(1);
        Type result;
        try {
            result = ClassUtils.getClass(raw);
        } catch (ClassNotFoundException e) {
            return null;
        }

        String param = matcher.group(3);
        if (StringUtils.isNotBlank(param)) {
            Type paramType = parseType(param);
            if (paramType != null) {
                result = TypeUtils.parameterize((Class) result, paramType);
            }
        }

        String arrayDims = matcher.group(4);
        if (StringUtils.isNotBlank(arrayDims)) {
            int dimensions = StringUtils.countMatches(arrayDims, '[');
            while (dimensions-- > 0) {
                result = TypeUtils.genericArrayType(result);
            }
        }

        return result;
    }
    return null;
}
 
Example 13
Source File: FreeIpaClient.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public List<TopologySuffix> findAllTopologySuffixes() throws FreeIpaClientException {
    List<Object> flags = List.of();
    Map<String, Object> params = Map.of();
    ParameterizedType type = TypeUtils
            .parameterize(List.class, TopologySuffix.class);
    return (List<TopologySuffix>) invoke("topologysuffix_find", flags, params, type).getResult();
}
 
Example 14
Source File: AttributeKey.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static <T> AttributeKey<Map<String, T>> createMapOf(String name, Class<T> containedType) {
   return new AttributeKey<Map<String, T>>(name, TypeUtils.parameterize(Map.class, String.class, containedType));
}
 
Example 15
Source File: AttributeKey.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static <T> AttributeKey<Set<T>> createSetOf(String name, Class<T> containedType) {
   return new AttributeKey<Set<T>>(name, TypeUtils.parameterize(Set.class, containedType));
}
 
Example 16
Source File: TypeMarker.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static <T> TypeMarker<Collection<T>> collectionOf(Class<T> containedType) {
   Preconditions.checkNotNull(containedType, "containedType may not be null");
   return new TypeMarker<Collection<T>>(TypeUtils.parameterize(Collection.class, containedType)) {};
}
 
Example 17
Source File: TypeMarker.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static <T> TypeMarker<Set<T>> setOf(Class<T> containedType) {
   Preconditions.checkNotNull(containedType, "containedType may not be null");
   return new TypeMarker<Set<T>>(TypeUtils.parameterize(Set.class, containedType)) {};
}
 
Example 18
Source File: TypeMarker.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
public static <T> TypeMarker<List<T>> listOf(Class<T> containedType) {
   Preconditions.checkNotNull(containedType, "containedType may not be null");
   return new TypeMarker<List<T>>(TypeUtils.parameterize(List.class, containedType)) {};
}
 
Example 19
Source File: FreeIpaClient.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public Set<Cert> findAllCert() throws FreeIpaClientException {
    ParameterizedType type = TypeUtils
            .parameterize(Set.class, Cert.class);
    return (Set<Cert>) invoke("cert_find", List.of(), Map.of(), type).getResult();
}
 
Example 20
Source File: AttributeKey.java    From arcusplatform with Apache License 2.0 2 votes vote down vote up
public static <T> AttributeKey<List<T>> createListOf(String name, Class<T> containedType) {
   return new AttributeKey<List<T>>(name, TypeUtils.parameterize(List.class, containedType));
   
}