dimanche 17 avril 2016

Java's Reflection

Correct me if I am wrong.

The instability of a class is defined as the providers of the class divided by the sum of all classes' provider

instability(A) = #providers(A)/#P

An example is

class Client {
    private Provider myProvider;

  }

I need help with my method of finding the numbers of providers

Here is my my code:

I have an array that contains all of the classes within a specific package.

private Class[] classes = getClasses(packageName);

In the function, I have a variable:

int total = 0;

The function looks at:

  1. Methods
  2. Constructors
  3. Interface
  4. Superclass

    int providers(Class a){

    int total = 0;

    // Test for Declared field
    total += a.getDeclaredFields().length;
    // Test for Interfaces
    total += a.getInterfaces().length;
    // Test for Inherietence 
    if(!a.getSuperclass().equals(Object.class))
        total += 1;
    
    ArrayList<Class> counter = new ArrayList<Class>();
    counter.addAll(counter);
    // Test for constructor 
    Constructor[] constructors = a.getDeclaredConstructors();
    
    for(int i=0; i< constructors.length; i++){
    
        Class[] params = constructors[i].getParameterTypes();
    
        for(int j=0; j<params.length; j++){
            if(params[j] instanceof Object && counter.contains(params[j])){ 
                total ++;
                counter.remove(params[j]);
            }
        }
    }
    
    counter = new ArrayList<Class>();
    counter.addAll(counter);
    
    Method methods[] = a.getDeclaredMethods();
    
    for(int i=0; i<methods.length; i++){
    
        Class[] params = methods[i].getParameterTypes();
        for(int j=0; j<params.length; j++)
            {
    
            if(params[j] instanceof Object && counter.contains(params[j])){
                total ++;
                counter.remove(params);
            }
            }
    
    }
    
    return total;
    
    

    }





Aucun commentaire:

Enregistrer un commentaire