Class BeanAnnotationProcessor

java.lang.Object
cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor

public class BeanAnnotationProcessor extends Object
Utilities for mapping-related tasks.
  • Method Details

    • setPropertyAccessResolver

      public static void setPropertyAccessResolver(PropertyAccessResolver resolver)
      Sets property access resolver, overriding the default one.
      Parameters:
      resolver - Resolver to set
    • isOwlClassEntity

      public static boolean isOwlClassEntity(Class<?> cls)
      Checks whether the specified class is annotated with the OWLClass annotation.
      Parameters:
      cls - The class to examine
      Returns:
      Whether it is annotated with OWLClass
    • getOwlClass

      public static String getOwlClass(Class<?> cls)
      Gets IRI of the OWL class represented by the specified Java class.
      Parameters:
      cls - Java class to examine
      Returns:
      Represented ontological class
      Throws:
      IllegalArgumentException - If the specified class is not mapped by OWLClass
    • expandIriIfNecessary

      public static String expandIriIfNecessary(String iri, Class<?> declaringClass)
      Attempts to expand the specified IRI in case it is compacted (see IdentifierUtil.isCompactIri(String)) using JOPA namespace declarations.

      If the IRI is not compact or no matching namespace is found, the original IRI is returned.

      Parameters:
      iri - IRI to expand (if necessary and possible)
      declaringClass - Class in/on which the IRI is declared. It is used as base for namespace search
      Returns:
      Expanded IRI if it was possible to expand it, original argument if not
      See Also:
    • getOwlClasses

      public static Set<String> getOwlClasses(Object object)
      Resolves ontological types of the specified object, as specified by the OWLClass annotation.
      Parameters:
      object - The object to resolve
      Returns:
      Resolved OWL types or an empty set if the object's class is not annotated with OWLClass
      See Also:
    • getOwlClasses

      public static Set<String> getOwlClasses(Class<?> cls)
      Resolves a transitive closure of ontological types of the specified class, as specified by the OWLClass annotation.

      I.e. the result contains also types mapped by the class's ancestors.

      Parameters:
      cls - The class to process
      Returns:
      Set of mapped ontological classes (possibly empty)
    • getAncestors

      public static List<Class<?>> getAncestors(Class<?> cls)
      Resolves ancestors of the specified class, up to Object.

      Ancestors are resolved using the Class.getSuperclass() method.

      Parameters:
      cls - Class whose ancestors to resolve
      Returns:
      List of ancestors, including the specified class
    • getSerializableFields

      public static List<Field> getSerializableFields(Object object)
      Returns all fields of the class of the specified object and its superclasses, which can be serialized into JSON-LD.

      For a field to be serializable, it has to be:

      • Non-static
      • Annotated with one of the following annotations: Id, OWLAnnotationProperty, OWLDataProperty, OWLObjectProperty
      • Not configured with JsonLdProperty.Access.WRITE_ONLY access
      Parameters:
      object - Object whose fields should be discovered
      Returns:
      List of discovered fields
    • getMarshallableFields

      public static List<Field> getMarshallableFields(Class<?> cls)
      Gets all fields which can be serialized or deserialized from the specified class (or its supertypes).

      is does not take into account property access configuration, just the fact that a field is:

      • Non-static
      • Annotated with one of the following annotations: Id, OWLAnnotationProperty, OWLDataProperty, OWLObjectProperty
      Parameters:
      cls - Class to check
      Returns:
      List of marshallable fields
    • mapFieldsForDeserialization

      public static Map<String,Field> mapFieldsForDeserialization(Class<?> cls)
      Creates a map of JSON-LD serializable fields, where the keys are IRIs of properties mapped by the fields.

      Identifier field is mapped to the JsonLd.ID property identifier. Ancestors of the specified class are also scanned.

      Parameters:
      cls - Class for which the mapping should be determined
      Returns:
      Mapping of OWL properties to fields
    • isPropertiesField

      public static boolean isPropertiesField(Field field)
      Checks whether the specified field is a Properties field.
      Parameters:
      field - The field to examine
      Returns:
      Whether the field has the Properties annotation
    • hasPropertiesField

      public static boolean hasPropertiesField(Class<?> cls)
      Checks whether the specified class contains a Properties field.
      Parameters:
      cls - The class to examine
      Returns:
      Whether the class has properties field
    • getPropertiesField

      public static Field getPropertiesField(Class<?> cls)
      Retrieves a field representing Properties in the specified class.
      Parameters:
      cls - The class to get properties field from
      Returns:
      Properties field
      Throws:
      IllegalArgumentException - When the specified class does not have a Properties field
    • getTypesField

      public static Optional<Field> getTypesField(Class<?> cls)
      Extracts types field from the specified class or any of its ancestors.

      This method assumes there is at most one types field in the class hierarchy.

      Parameters:
      cls - The class to scan
      Returns:
      Types field
    • hasTypesField

      public static boolean hasTypesField(Class<?> cls)
      Checks whether the specified class contains a Types field.
      Parameters:
      cls - The class to examine
      Returns:
      Whether the class has types field
    • isObjectProperty

      public static boolean isObjectProperty(Field field)
      Checks whether the specified field is mapped to an OWL object property.
      Parameters:
      field - The field to examine
      Returns:
      Whether field has a OWLObjectProperty annotation
    • isAnnotationProperty

      public static boolean isAnnotationProperty(Field field)
      Checks whether the specified field is mapped to an OWL annotation property.
      Parameters:
      field - The field to examine
      Returns:
      Whether field has a OWLAnnotationProperty annotation
    • isInstanceIdentifier

      public static boolean isInstanceIdentifier(Field field)
      Checks whether the specified field is an identifier field.
      Parameters:
      field - The field to examine
      Returns:
      Whether the field has a Id annotation
    • isTypesField

      public static boolean isTypesField(Field field)
      Checks whether the specified field is a Types field.
      Parameters:
      field - The field to examine
      Returns:
      Whether the field has a Types annotation
    • isWriteable

      public static boolean isWriteable(Field field)
      Checks whether deserialization can write into the specified field.
      Parameters:
      field - The field to examine
      Returns:
      Write access status
    • getAttributeIdentifier

      public static String getAttributeIdentifier(Field field)
      Resolves JSON-LD attribute identifier of the specified field.

      For OWL properties, this will be their IRI. For id fields it will be the JsonLd.ID string.

      Parameters:
      field - The field to resolve
      Returns:
      JSON-LD attribute identifier
    • getIdentifierField

      public static Optional<Field> getIdentifierField(Class<?> cls)
      Gets the identifier field of the specified class.

      That is, gets the field annotated with Id, even inherited.

      Parameters:
      cls - Class whose identifier field to retrieve
      Returns:
      Matching field, optionally empty
    • getInstanceIdentifier

      public static Optional<Object> getInstanceIdentifier(Object instance)
      Resolves value of the identifier attribute (i.e. annotated with Id) of the specified instance.
      Parameters:
      instance - Instance to get identifier value from
      Returns:
      Identifier value
    • getAttributeOrder

      public static String[] getAttributeOrder(Class<?> cls)
      Retrieves an array of attribute names specifying (partial) order in which they should be (de)serialized.
      Parameters:
      cls - Class whose attribute order should be retrieved
      Returns:
      Array declaring attribute order, possibly empty