Uploaded image for project: 'Sling'
  1. Sling
  2. SLING-4447

Improve JavaUseProvider to not fall back to a simple Pojo instantiation in case a Java class with @Model annotation cannot be instantiated

    XMLWordPrintableJSON

    Details

      Description

      Currently in case a Java class is a Sling Model (i.e. has the Model annotation) and cannot be instantiated (e.g. required injections not possible) Sightly falls back to instantiate those as simple Pojos.
      This is never good, since a lot of NullPointerException might happen because all injections have not been performed then.

      Therefore in the following code should be used instead

      if (modelFactory.isModelClass(resource, cls)) {
        if (modelFactory.canCreateFromAdaptable(resource, cls)) {
          obj = modelFactory.createModel(resource, cls);
        } else if (modelFactory.canCreateFromAdaptable(request, cls)) {
          obj = modelFactory.createModel(request, cls);
        } else {
          throw new IllegalStateException("Could not adapt the given Sling Model from neither resource nor request: " + cls);
        }
      }
      

      That way, exceptions would be propagated in case a Sling model cannot be instantiated and developers more easily see why the Sling Model does not work (instead of running into NullPointerExceptions in their model because it was instantiated as simple Pojo)

        Attachments

        1. SLING-4447-v01.patch
          7 kB
          Konrad Windszus

          Issue Links

            Activity

              People

              • Assignee:
                kwin Konrad Windszus
                Reporter:
                kwin Konrad Windszus
              • Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: