Pre-Requisite: Getting Started

Validations

Before any MISupported (JavaDoc | Source Code) class is instantiated, it must be validated as follows:

  1. Must contain MISupport (JavaDoc | Source Code) annotation
  2. MISupport (JavaDoc | Source Code) annotation must contain parentClasses attribute
  3. parentClass attribute must contain at least one Parent class
  4. Parent must not be an interface
  5. Parent must not be abstract
  6. Parent must have a public no-arguments constructor - [Read more about this here]

    An MIException (JavaDoc | Source Code) will be thrown if any one of the above condition fails.

Eager Validation (Recommended)

It is recommended to perform eager validation because all MISupported (JavaDoc | Source Code) classes will be pre-validated and in case of any errors, the program will fail early, at the time of initialization or bootstrapping. Contrary to this, in case of lazy validation, if there's a problem in one or more of the MISupported (JavaDoc | Source Code) classes configuration, then the program will fail only when MIFactory (JavaDoc | Source Code) tries to instantiate that particular problematic class.

The MIFactory (JavaDoc | Source Code) supports eager validation and can scan packages to find and pre-validate any classes that are annotated with MISupport (JavaDoc | Source Code) annotation.

You can use this one-arg constructor MIFactory(java.util.List) of MIFactory (JavaDoc | Source Code) to perform eager validation:

import com.smhumayun.mi_plus.MIFactory;
import com.smhumayun.mi_plus.testdata.Child1;

...
    //eager validation - validation happens as soon as following statement executes
    MIFactory miFactory = new MIFactory(Arrays.asList("com.smhumayun.mi_plus.testdata"));

    miFactory.newInstance(Child1.class); //validation will be skipped because it has been pre-validated above
...

All classes inside the testdata package and any sub-packages will be scanned and processed accordingly.

As soon as you try to instantiate an MIFactory (JavaDoc | Source Code) instance, it will scan of your supplied packages to find any classes that are annotated with MISupport (JavaDoc | Source Code) annotation. Once located, it will pre-validate all of them and will only return the factory instance if all goes well, else it will throw an MIException (JavaDoc | Source Code) indicating the problems it encountered during the scanning and/or validation process.

Lazy Validation

In case of lazy validation (also called on-demand validation), the validation will perform whenever newInstance() of MIFactory (JavaDoc | Source Code) is called for the first time against a given Class. Any subsequent newInstance() calls for the same Class will skip the validation process and proceed with the rest of the instantiation process.

import com.smhumayun.mi_plus.MIFactory;
import com.smhumayun.mi_plus.testdata.Child1;

...
    //lazy validation - no validation will be done in following statement
    MIFactory miFactory = new MIFactory(Arrays.asList("com.smhumayun.mi_plus.testdata"));

    miFactory.newInstance(Child1.class); //first call against Child1; validation will happen here
    miFactory.newInstance(Child1.class); //any subsequent calls after first call; validation will be skipped
...

© Syed Muhammad Humayun - smhumayun@gmail.com - www.smhumayun.com - www.linkedin.com/in/smhumayun