Example usage for org.apache.commons.lang3.reflect TypeUtils parameterize

List of usage examples for org.apache.commons.lang3.reflect TypeUtils parameterize

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect TypeUtils parameterize.

Prototype

public static final ParameterizedType parameterize(final Class<?> raw,
        final Map<TypeVariable<?>, Type> typeArgMappings) 

Source Link

Document

Create a parameterized type instance.

Usage

From source file:org.lambdamatic.mongodb.apt.LambdamaticAnnotationProcessorTest.java

@Test
@WithDomainClass(Foo.class)
@WithDomainClass(Bar.class)
public void shouldProcessFooClassAndGenerateQueryMetadataClass() throws ClassNotFoundException {
    // verification
    final Class<?> fooQueryClass = Class.forName("com.sample.QFoo");
    ClassAssertion.assertThat(fooQueryClass).isAbstract().isImplementing(QueryMetadata.class, Foo.class);
    // id/*ww w.ja va  2  s. c  om*/
    FieldAssertion.assertThat(fooQueryClass, "id").isParameterizedType(QueryField.class, ObjectId.class)
            .isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", EncoderUtils.MONGOBD_DOCUMENT_ID);
    // stringField: *custom name in the @DocumentField annotation*
    FieldAssertion.assertThat(fooQueryClass, "stringField").isParameterizedType(QueryField.class, String.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "stringField_");
    // transientStringField: annotated with @Transient, should not be here
    ClassAssertion.assertThat(fooQueryClass).hasNoField("transientStringField");
    // primitiveByteField: on @DocumentField annotation: should use class field name as document
    // field name
    FieldAssertion.assertThat(fooQueryClass, "primitiveByteField")
            .isParameterizedType(QueryField.class, Byte.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "primitiveByteField");
    // byteField: metadata field is primitive type instead of class wrapper
    FieldAssertion.assertThat(fooQueryClass, "byteField").isParameterizedType(QueryField.class, Byte.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "byteField");
    // primitiveShortField
    FieldAssertion.assertThat(fooQueryClass, "primitiveShortField")
            .isParameterizedType(QueryField.class, Short.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "primitiveShortField");
    // shortField: metadata field is primitive type instead of class wrapper
    FieldAssertion.assertThat(fooQueryClass, "shortField").isParameterizedType(QueryField.class, Short.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "shortField");
    // primitiveIntField
    FieldAssertion.assertThat(fooQueryClass, "primitiveIntField")
            .isParameterizedType(QueryField.class, Integer.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "primitiveIntField");
    // integerField: metadata field is primitive type instead of class wrapper
    FieldAssertion.assertThat(fooQueryClass, "integerField")
            .isParameterizedType(QueryField.class, Integer.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "integerField");
    // primitiveLongField
    FieldAssertion.assertThat(fooQueryClass, "primitiveLongField")
            .isParameterizedType(QueryField.class, Long.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "primitiveLongField");
    // longField: metadata field is primitive type instead of class wrapper
    FieldAssertion.assertThat(fooQueryClass, "longField").isParameterizedType(QueryField.class, Long.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "longField");
    // primitiveFloatField
    FieldAssertion.assertThat(fooQueryClass, "primitiveFloatField")
            .isParameterizedType(QueryField.class, Float.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "primitiveFloatField");
    // floatField: metadata field is primitive type instead of class wrapper
    FieldAssertion.assertThat(fooQueryClass, "floatField").isParameterizedType(QueryField.class, Float.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "floatField");
    // primitiveDoubleField
    FieldAssertion.assertThat(fooQueryClass, "primitiveDoubleField")
            .isParameterizedType(QueryField.class, Double.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "primitiveDoubleField");
    // doubleField: metadata field is primitive type instead of class wrapper
    FieldAssertion.assertThat(fooQueryClass, "doubleField").isParameterizedType(QueryField.class, Double.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "doubleField");
    // primitiveBooleanField
    FieldAssertion.assertThat(fooQueryClass, "primitiveBooleanField")
            .isParameterizedType(QueryField.class, Boolean.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "primitiveBooleanField");
    // booleanField: metadata field is primitive type instead of class wrapper
    FieldAssertion.assertThat(fooQueryClass, "booleanField")
            .isParameterizedType(QueryField.class, Boolean.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "booleanField");
    // primitiveCharField
    FieldAssertion.assertThat(fooQueryClass, "primitiveCharField")
            .isParameterizedType(QueryField.class, Character.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "primitiveCharField");
    // characterField: metadata field is primitive type instead of class wrapper
    FieldAssertion.assertThat(fooQueryClass, "characterField")
            .isParameterizedType(QueryField.class, Character.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "characterField");
    // location
    FieldAssertion.assertThat(fooQueryClass, "location").isType(LocationField.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "location");
    // enumFoo
    FieldAssertion.assertThat(fooQueryClass, "enumFoo").isParameterizedType(QueryField.class, EnumFoo.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "enumFoo");
    // bar
    FieldAssertion.assertThat(fooQueryClass, "bar").isType("com.sample.QBar").isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "bar");
    // barList
    FieldAssertion.assertThat(fooQueryClass, "barList")
            .isParameterizedType(QueryArray.class, ClassUtils.getClass("com.sample.QBar")).isNotFinal()
            .isNotStatic().hasAnnotation(DocumentField.class, "name", "barList");
    // barMap
    FieldAssertion.assertThat(fooQueryClass, "barMap")
            .isParameterizedType(QueryMap.class, String.class, Class.forName("com.sample.QBar")).isNotFinal()
            .isNotStatic().hasAnnotation(DocumentField.class, "name", "barMap");
    // barArray
    FieldAssertion.assertThat(fooQueryClass, "barArray")
            .isParameterizedType(QueryArray.class, ClassUtils.getClass("com.sample.QBar")).isNotFinal()
            .isNotStatic().hasAnnotation(DocumentField.class, "name", "barArray");
    // enumBarArray
    FieldAssertion.assertThat(fooQueryClass, "enumBarArray")
            .isParameterizedType(QueryArray.class,
                    TypeUtils.parameterize(QueryField.class, ClassUtils.getClass("com.sample.EnumBar")))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "enumBarArray");
    // stringArray
    FieldAssertion.assertThat(fooQueryClass, "stringArray")
            .isParameterizedType(QueryArray.class, TypeUtils.parameterize(QueryField.class, String.class))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "stringArray");
    // stringSet
    FieldAssertion.assertThat(fooQueryClass, "stringSet")
            .isParameterizedType(QueryArray.class, TypeUtils.parameterize(QueryField.class, String.class))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "stringSet");
    // stringMap
    FieldAssertion.assertThat(fooQueryClass, "stringMap")
            .isParameterizedType(QueryMap.class, String.class,
                    TypeUtils.parameterize(QueryField.class, String.class))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "stringMap");
    // bytes
    FieldAssertion.assertThat(fooQueryClass, "bytes")
            .isParameterizedType(QueryArray.class, TypeUtils.parameterize(QueryField.class, Byte.class))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "bytes");
    // bytesList
    FieldAssertion.assertThat(fooQueryClass, "bytesList")
            .isParameterizedType(QueryArray.class,
                    TypeUtils.parameterize(QueryArray.class,
                            TypeUtils.parameterize(QueryField.class, Byte.class)))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "bytesList");
}

From source file:org.lambdamatic.mongodb.apt.LambdamaticAnnotationProcessorTest.java

@Test
@WithDomainClass(Foo.class)
@WithDomainClass(Bar.class)
public void shouldProcessFooClassAndGenerateProjectionMetadataClass()
        throws URISyntaxException, ClassNotFoundException, NoSuchFieldException, SecurityException,
        IllegalArgumentException, IllegalAccessException, InstantiationException {
    // verification
    final Class<?> fooProjectionClass = Class.forName("com.sample.PFoo");
    ClassAssertion.assertThat(fooProjectionClass).isAbstract()
            .isImplementing(ProjectionMetadata.class, Foo.class).isExtending("java.lang.Object");
    // id/*from   w  w w  .jav a 2 s. c  om*/
    FieldAssertion.assertThat(fooProjectionClass, "id").isType(ProjectionField.class).isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "_id");
    // stringField: *custom name in the @DocumentField annotation*
    FieldAssertion.assertThat(fooProjectionClass, "stringField").isType(ProjectionField.class).isNotFinal()
            .isNotStatic().hasAnnotation(DocumentField.class, "name", "stringField_");
    // transientStringField: annotated with @Transient, should not be here
    ClassAssertion.assertThat(fooProjectionClass).hasNoField("transientStringField");
    // primitiveByteField
    FieldAssertion.assertThat(fooProjectionClass, "primitiveByteField").isType(ProjectionField.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "primitiveByteField");
    // primitiveShortField
    FieldAssertion.assertThat(fooProjectionClass, "primitiveShortField").isType(ProjectionField.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "primitiveShortField");
    // primitiveIntField
    FieldAssertion.assertThat(fooProjectionClass, "primitiveIntField").isType(ProjectionField.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "primitiveIntField");
    // primitiveLongField
    FieldAssertion.assertThat(fooProjectionClass, "primitiveLongField").isType(ProjectionField.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "primitiveLongField");
    // primitiveFloatField
    FieldAssertion.assertThat(fooProjectionClass, "primitiveFloatField").isType(ProjectionField.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "primitiveFloatField");
    // primitiveDoubleField
    FieldAssertion.assertThat(fooProjectionClass, "primitiveDoubleField").isType(ProjectionField.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "primitiveDoubleField");
    // primitiveBooleanField
    FieldAssertion.assertThat(fooProjectionClass, "primitiveBooleanField").isType(ProjectionField.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "primitiveBooleanField");
    // primitiveCharField
    FieldAssertion.assertThat(fooProjectionClass, "primitiveCharField").isType(ProjectionField.class)
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "primitiveCharField");
    // location
    FieldAssertion.assertThat(fooProjectionClass, "location").isType(ProjectionField.class).isNotFinal()
            .isNotStatic().hasAnnotation(DocumentField.class, "name", "location");
    // enumFoo
    FieldAssertion.assertThat(fooProjectionClass, "enumFoo").isType(ProjectionField.class).isNotFinal()
            .isNotStatic().hasAnnotation(DocumentField.class, "name", "enumFoo");
    // bar
    FieldAssertion.assertThat(fooProjectionClass, "bar").isType("com.sample.PBar").isNotFinal().isNotStatic()
            .hasAnnotation(DocumentField.class, "name", "bar");
    // embedded PBar Projection class
    final Class<?> barProjectionClass = Class.forName("com.sample.PBar");
    ClassAssertion.assertThat(barProjectionClass).isImplementing(ProjectionMetadata.class, Bar.class);
    // barList
    FieldAssertion.assertThat(fooProjectionClass, "barList")
            .isParameterizedType(ProjectionArray.class, Class.forName("com.sample.QBar")).isNotFinal()
            .isNotStatic().hasAnnotation(DocumentField.class, "name", "barList");
    // barMap
    FieldAssertion.assertThat(fooProjectionClass, "barMap")
            .isParameterizedType(ProjectionMap.class, String.class, Class.forName("com.sample.QBar"))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "barMap");
    // barArray
    FieldAssertion.assertThat(fooProjectionClass, "barArray")
            .isParameterizedType(ProjectionArray.class, Class.forName("com.sample.QBar")).isNotFinal()
            .isNotStatic().hasAnnotation(DocumentField.class, "name", "barArray");
    // enumBarArray
    FieldAssertion.assertThat(fooProjectionClass, "enumBarArray")
            .isParameterizedType(ProjectionArray.class, TypeUtils.parameterize(QueryField.class, EnumBar.class))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "enumBarArray");
    // stringArray
    FieldAssertion.assertThat(fooProjectionClass, "stringArray")
            .isParameterizedType(ProjectionArray.class, TypeUtils.parameterize(QueryField.class, String.class))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "stringArray");
    // stringList
    FieldAssertion.assertThat(fooProjectionClass, "stringList")
            .isParameterizedType(ProjectionArray.class, TypeUtils.parameterize(QueryField.class, String.class))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "stringList");
    // stringSet
    FieldAssertion.assertThat(fooProjectionClass, "stringSet")
            .isParameterizedType(ProjectionArray.class, TypeUtils.parameterize(QueryField.class, String.class))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "stringSet");
    // bytes
    FieldAssertion.assertThat(fooProjectionClass, "bytes")
            .isParameterizedType(ProjectionArray.class, TypeUtils.parameterize(QueryField.class, Byte.class))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "bytes");
    // bytesList
    FieldAssertion.assertThat(fooProjectionClass, "bytesList")
            .isParameterizedType(ProjectionArray.class,
                    TypeUtils.parameterize(QueryArray.class,
                            TypeUtils.parameterize(QueryField.class, Byte.class)))
            .isNotFinal().isNotStatic().hasAnnotation(DocumentField.class, "name", "bytesList");
}

From source file:org.lambdamatic.mongodb.apt.testutil.FieldAssertion.java

/**
 * Checks that the actual field is parameterized.
 * /*from   www .jav  a 2s  .c  o  m*/
 * @param expectedRawType the expected raw type
 * @param expectedTypeArguments the expected type arguments
 * @return this {@link FieldAssertion} for fluent linking
 */
public FieldAssertion isParameterizedType(final Class<?> expectedRawType, final Type... expectedTypeArguments) {
    isNotNull();
    if (!(actual.getGenericType() instanceof ParameterizedType)) {
        failWithMessage("Expected field <%s> to be a parameterized type but it was not", actual);
    }
    final ParameterizedType actualType = (ParameterizedType) actual.getGenericType();
    final ParameterizedType expectedParameterizedType = TypeUtils.parameterize(expectedRawType,
            expectedTypeArguments);
    if (!TypeUtils.equals(actualType, expectedParameterizedType)) {
        failWithMessage("Expected field %s.%s to be of type %s<%s> but it was %s<%s>",
                actual.getType().getName(), actual.getName(), expectedRawType, expectedTypeArguments,
                actualType.getRawType().getTypeName(), actualType.getActualTypeArguments());
    }
    return this;
}

From source file:org.nuxeo.ecm.core.io.marshallers.json.document.DocumentModelJsonReader.java

@Override
protected DocumentModel readEntity(JsonNode jn) throws IOException {

    SimpleDocumentModel simpleDoc = new SimpleDocumentModel();
    String name = getStringField(jn, "name");
    if (StringUtils.isNotBlank(name)) {
        simpleDoc.setPathInfo(null, name);
    }/*from  w  w w  .  j  a  va2  s. c om*/
    String type = getStringField(jn, "type");
    if (StringUtils.isNotBlank(type)) {
        simpleDoc.setType(type);
    }

    JsonNode propsNode = jn.get("properties");
    if (propsNode != null && !propsNode.isNull() && propsNode.isObject()) {
        ParameterizedType genericType = TypeUtils.parameterize(List.class, Property.class);
        List<Property> properties = readEntity(List.class, genericType, propsNode);
        for (Property property : properties) {
            String propertyName = property.getName();
            // handle schema with no prefix
            if (!propertyName.contains(":")) {
                propertyName = property.getField().getDeclaringType().getName() + ":" + propertyName;
            }
            simpleDoc.setPropertyValue(propertyName, property.getValue());
        }
    }

    DocumentModel doc = null;

    String uid = getStringField(jn, "uid");
    if (StringUtils.isNotBlank(uid)) {
        try (SessionWrapper wrapper = ctx.getSession(null)) {
            doc = wrapper.getSession().getDocument(new IdRef(uid));
        }
        avoidBlobUpdate(simpleDoc, doc);
        applyDirtyPropertyValues(simpleDoc, doc);
    } else if (StringUtils.isNotBlank(type)) {
        SimpleDocumentModel createdDoc = new SimpleDocumentModel();
        if (StringUtils.isNotBlank(name)) {
            createdDoc.setPathInfo(null, name);
        }
        createdDoc.setType(simpleDoc.getType());
        applyAllPropertyValues(simpleDoc, createdDoc);
        doc = createdDoc;
    } else {
        doc = simpleDoc;
    }

    return doc;
}

From source file:org.nuxeo.ecm.core.io.marshallers.json.document.DocumentModelListJsonWriterTest.java

public DocumentModelListJsonWriterTest() {
    super(DocumentModelListJsonWriter.class, List.class,
            TypeUtils.parameterize(List.class, DocumentModel.class));
}

From source file:org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter.java

/**
 * @param entityType The "entity-type" Json property value.
 * @param entityClass The entity type./* w w  w .ja v a2s . c o m*/
 */
public ExtensibleEntityJsonWriter(String entityType, Class<EntityType> entityClass) {
    super();
    this.entityType = entityType;
    genericType = TypeUtils.parameterize(Enriched.class, entityClass);
}

From source file:org.nuxeo.ecm.core.io.marshallers.json.types.DocumentTypeListJsonWriterTest.java

public DocumentTypeListJsonWriterTest() {
    super(DocumentTypeListJsonWriter.class, DocumentType.class,
            TypeUtils.parameterize(List.class, DocumentType.class));
}

From source file:org.nuxeo.ecm.core.io.marshallers.json.types.FacetListJsonWriterTest.java

public FacetListJsonWriterTest() {
    super(FacetListJsonWriter.class, List.class, TypeUtils.parameterize(List.class, CompositeType.class));
}

From source file:org.nuxeo.ecm.core.io.marshallers.json.types.SchemaListJsonWriterTest.java

public SchemaListJsonWriterTest() {
    super(SchemaListJsonWriter.class, List.class, TypeUtils.parameterize(List.class, Schema.class));
}

From source file:org.nuxeo.ecm.core.io.marshallers.json.validation.ConstraintListJsonWriterTest.java

public ConstraintListJsonWriterTest() {
    super(ConstraintListJsonWriter.class, List.class, TypeUtils.parameterize(List.class, Constraint.class));
}