@Test public void testParameterizeWithOwner() throws Exception { final Type owner = TypeUtils.parameterize(TypeUtilsTest.class, String.class); final ParameterizedType dat2Type = TypeUtils.parameterizeWithOwner(owner, That.class, String.class, String.class); assertTrue(TypeUtils.equals(getClass().getField("dat2").getGenericType(), dat2Type)); }
@Test public void testParameterizeNarrowerTypeArray() { final TypeVariable<?>[] variables = ArrayList.class.getTypeParameters(); final ParameterizedType parameterizedType = TypeUtils.parameterize(ArrayList.class, variables); final Map<TypeVariable<?>, Type> mapping = Collections.<TypeVariable<?>, Type>singletonMap(variables[0], String.class); final Type unrolled = TypeUtils.unrollVariables(mapping, parameterizedType); assertEquals(TypeUtils.parameterize(ArrayList.class, String.class), unrolled); }
@Test public void testParameterize() throws Exception { final ParameterizedType stringComparableType = TypeUtils.parameterize(Comparable.class, String.class); assertTrue(TypeUtils.equals(getClass().getField("stringComparable").getGenericType(), stringComparableType)); assertEquals("java.lang.Comparable<java.lang.String>", stringComparableType.toString()); }
@Test public void testGenericArrayType() throws Exception { final Type expected = getClass().getField("intWildcardComparable").getGenericType(); final GenericArrayType actual = TypeUtils.genericArrayType(TypeUtils.parameterize(Comparable.class, TypeUtils.wildcardType() .withUpperBounds(Integer.class).build())); assertTrue(TypeUtils.equals(expected, actual)); assertEquals("java.lang.Comparable<? extends java.lang.Integer>[]", actual.toString()); }
@Test public void testBasic() { assertTrue(TypeUtils.equals(String.class, new TypeLiteral<String>() {}.value)); assertTrue(TypeUtils.equals(TypeUtils.parameterize(List.class, String.class), new TypeLiteral<List<String>>() {}.value)); }
@Test public void testTyped() { final Typed<String> stringType = new TypeLiteral<String>() {}; assertTrue(TypeUtils.equals(String.class, stringType.getType())); final Typed<List<String>> listOfStringType = new TypeLiteral<List<String>>() {}; assertTrue(TypeUtils.equals(TypeUtils.parameterize(List.class, String.class), listOfStringType.getType())); }
/** * @param entityType The "entity-type" Json property value. * @param entityClass The entity type. */ public ExtensibleEntityJsonWriter(String entityType, Class<EntityType> entityClass) { super(); this.entityType = entityType; genericType = TypeUtils.parameterize(Enriched.class, entityClass); }
@Override public <T> List<T> query(DruidQuery druidQuery, Class<T> className) throws QueryException { try (Response response = this.queryWebTarget .request(MediaType.APPLICATION_JSON) .post(Entity.entity(druidQuery, MediaType.APPLICATION_JSON))) { if (response.getStatus() == Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) { handleInternalServerResponse(response); } return response.readEntity(new GenericType<List<T>>(TypeUtils.parameterize(List.class, className)) { }); } catch (QueryException e) { log.error("Exception while querying {}", e); throw e; } catch (Exception e) { log.error("Exception while querying {}", e); throw new QueryException(e); } }
public static Type[] guessBestTypes(Object[] params) { return Stream.of(params) .map(p -> { if (p == null) { return Object.class; } Class _clazz = p.getClass(); if (Pojo.class.isAssignableFrom(_clazz)) { return _clazz; } if (Map.class.isAssignableFrom(_clazz)) { Map map = (Map) p; return TypeUtils.parameterize(_clazz, new Type[] { guessBestClass(map.keySet()), guessBestClass(map.values()) }); } if (Collection.class.isAssignableFrom(_clazz)) { if (_clazz.equals(Arrays.asList().getClass())) { _clazz = ArrayList.class; } return TypeUtils.parameterize(_clazz, new Type[] { guessBestClass((Collection) p) }); } return _clazz; }) .toArray(Type[]::new); }
private Map<String, Serializable> getVariables(JsonNode variables, String schemaName) throws IOException { Map<String, Serializable> variable = new HashMap<>(); ParameterizedType genericType = TypeUtils.parameterize(List.class, Property.class); try (Closeable resource = ctx.wrap().with(DEFAULT_SCHEMA_NAME, schemaName).open()) { List<Property> properties = readEntity(List.class, genericType, variables); for (Property property : properties) { variable.put(property.getName().substring(schemaName.length() + 1), property.getValue()); } } return variable; }
/** * @since 10.2 */ @SuppressWarnings("unchecked") public static <T> List<T> jsonToList(Class<T> elementType, String json, RenderingContext ctx) throws IOException { Type genericType = TypeUtils.parameterize(List.class, elementType); @SuppressWarnings("rawtypes") Reader<List> reader = getService().getReader(ctx, List.class, genericType, APPLICATION_JSON_TYPE); checkMarshaller(genericType, reader); return reader.read(List.class, genericType, APPLICATION_JSON_TYPE, new ByteArrayInputStream(json.getBytes())); }
private Map<String, Serializable> getVariables(JsonNode variables, String schemaName) throws IOException { Map<String, Serializable> variable = new HashMap<>(); ParameterizedType genericType = TypeUtils.parameterize(List.class, Property.class); try (Closeable resource = ctx.wrap().with(DEFAULT_SCHEMA_NAME, schemaName).open()) { List<Property> properties = readEntity(List.class, genericType, variables); for (Property property : properties) { variable.put(property.getName().substring(schemaName.length() + 1), property.getValue()); } } return variable; }
type = ((ParameterizedType) type).getRawType(); type = TypeUtils.parameterize(((Class) type), types);
private static boolean isBigPriority(Type type) { return TypeUtils.isAssignable(type, NuxeoPrincipal.class) || TypeUtils.isAssignable(type, NuxeoGroup.class) || TypeUtils.isAssignable(type, TypeUtils.parameterize(List.class, DocumentModel.class)); }
/** * Convert the given list to json. * <p> * Specify the list element type to get the best marshaller to manage conversion. * </p> * * @param elementType The element type of the list. * @param list The list to convert. * @param ctx May be null - otherwise, use {@link CtxBuilder} to create the context. * @return the resulting json. * @since 7.2 */ public static <T> String listToJson(Class<T> elementType, List<T> list, RenderingContext ctx) throws IOException { Type genericType = TypeUtils.parameterize(List.class, elementType); @SuppressWarnings("rawtypes") Writer<List> writer = getService().getWriter(ctx, List.class, genericType, APPLICATION_JSON_TYPE); checkMarshaller(genericType, writer); ByteArrayOutputStream baos = new ByteArrayOutputStream(); writer.write(list, List.class, genericType, APPLICATION_JSON_TYPE, baos); return baos.toString(); }
@GET @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON + ";charset=UTF-8" }) @MCRCacheControl(maxAge = @MCRCacheControl.Age(time = 1, unit = TimeUnit.HOURS), sMaxAge = @MCRCacheControl.Age(time = 1, unit = TimeUnit.HOURS)) @Operation( summary = "Lists all objects in this repository", responses = @ApiResponse( content = @Content(array = @ArraySchema(schema = @Schema(implementation = MCRObjectIDDate.class)))), tags = MCRRestUtils.TAG_MYCORE_OBJECT) @XmlElementWrapper(name = "mycoreobjects") @JacksonFeatures(serializationDisable = { SerializationFeature.WRITE_DATES_AS_TIMESTAMPS }) public Response listObjects() throws IOException { Date lastModified = new Date(MCRXMLMetadataManager.instance().getLastModified()); Optional<Response> cachedResponse = MCRRestUtils.getCachedResponse(request, lastModified); if (cachedResponse.isPresent()) { return cachedResponse.get(); } List<? extends MCRObjectIDDate> idDates = MCRXMLMetadataManager.instance().listObjectDates().stream() .filter(oid -> !oid.getId().contains("_derivate_")) .collect(Collectors.toList()); Class<?> t = idDates.stream() .findAny() .map(MCRObjectIDDate::getClass) .orElse((Class) MCRObjectIDDate.class); Type type = TypeUtils.parameterize(idDates.getClass(), t); return Response.ok(new GenericEntity<List<? extends MCRObjectIDDate>>(idDates, type)) .lastModified(lastModified) .build(); }
@GET @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON + ";charset=UTF-8" }) @MCRCacheControl(maxAge = @MCRCacheControl.Age(time = 1, unit = TimeUnit.DAYS), sMaxAge = @MCRCacheControl.Age(time = 1, unit = TimeUnit.DAYS)) @Path("/{" + PARAM_MCRID + "}/versions") @Operation( summary = "Returns MCRObject with the given " + PARAM_MCRID + ".", responses = @ApiResponse(content = @Content( array = @ArraySchema(uniqueItems = true, schema = @Schema(implementation = MCRMetadataVersion.class)))), tags = MCRRestUtils.TAG_MYCORE_OBJECT) @JacksonFeatures(serializationDisable = { SerializationFeature.WRITE_DATES_AS_TIMESTAMPS }) @XmlElementWrapper(name = "versions") public Response getObjectVersions(@Parameter(example = "mir_mods_00004711") @PathParam(PARAM_MCRID) MCRObjectID id) throws IOException { long modified = MCRXMLMetadataManager.instance().getLastModified(id); if (modified < 0) { throw new NotFoundException("MCRObject " + id + " not found"); } Date lastModified = new Date(modified); Optional<Response> cachedResponse = MCRRestUtils.getCachedResponse(request, lastModified); if (cachedResponse.isPresent()) { return cachedResponse.get(); } List<MCRMetadataVersion> versions = MCRXMLMetadataManager.instance().getVersionedMetaData(id) .listVersions(); return Response.ok() .entity(new GenericEntity<>(versions, TypeUtils.parameterize(List.class, MCRMetadataVersion.class))) .lastModified(lastModified) .build(); }
/** * @since 10.3 */ private void enrichBlobProperty(JsonGenerator jg, BlobProperty property) throws IOException { Set<String> enrichers = ctx.getEnrichers("blob"); if (!enrichers.isEmpty()) { WrappedContext wrappedCtx = ctx.wrap(); OutputStreamWithJsonWriter out = new OutputStreamWithJsonWriter(jg); Enriched<BlobProperty> enriched = new Enriched<>(property); ParameterizedType genericType = TypeUtils.parameterize(Enriched.class, BlobProperty.class); for (String enricherName : enrichers) { try (Closeable ignored = wrappedCtx.with(ENTITY_ENRICHER_NAME, enricherName).open()) { Collection<Writer<Enriched>> writers = registry.getAllWriters(ctx, Enriched.class, genericType, APPLICATION_JSON_TYPE); for (Writer<Enriched> writer : writers) { writer.write(enriched, Enriched.class, genericType, APPLICATION_JSON_TYPE, out); } } } } }
ParameterizedType genericType = TypeUtils.parameterize(List.class, Property.class); List<Property> properties = readEntity(List.class, genericType, propsNode); for (Property property : properties) {