Search code examples
javaoopdesign-patternsreflectionobject-graph

Measure the Positional stability of a Class using reflection api


As part of a project for college we are required to " create a Java application that uses reflection to analyse an arbitrary Java Application Archive (JAR) and calculates the positional stability of each of the component classes in its object graph Recall that the Positional Stability (I) of a type can be measured by counting the number of dependencies that enter and leave that type:".

We need to measure the Efferent and Afferent couplings of each class and it components, then calculate the stability.

I am a little confused at how to calculate the Afferent and Efferent couplings. This is what I have done so far,

 for (int i = 0; i < cls.size(); i++) {

        Class cla = cls.getMyClass(i);

        Class[] interfaces = cla.getInterfaces();

        for(Class inter : interfaces){

            efferentCoup++;
        }

        Constructor[] cons = cla.getConstructors();
        Class[] conParams;

        for(Constructor c: cons){

            conParams = c.getParameterTypes();

            for(Class par: conParams){

                efferentCoup++;
            }

        }

        Field[] fields = cla.getFields();

        for(Field fie: fields ){
            efferentCoup++;
        }
}

Solution

    • Afferent Couplings:- is a measure of how many other classes use the specific class.

    To calculate this you need to introspect all packages and increment your counter everytime, that specific class is getting referenced.

    • Efferent couplings:- is a measure of how many different classes are used by the specific class

    To calculate this you need to introspect through a particular class and see how many other classes it is referencing to.

    Ideally step 1, should be good enough to calculate both couplings.