Below is a function that determines if Class B uses Class A.
Currently, it tests for:
- Fields
- Superclass
- Constructors
- Methods
private boolean uses(Class<?> b, Class<?> a){
// Test for Declared field
for(Field f:b.getDeclaredFields()){
if(f.getGenericType().equals(a))
return true;
}
// Test for Inherietence
if(b.getSuperclass().getName().equals(a.getName()))
return true;
// Test for constructors
for(Constructor<?> c: b.getDeclaredConstructors()){
for(Class<?> p : c.getParameterTypes())
if(p.getName().equals(a.getName()))
return true;
}
// Test for methods
for(Method m:b.getDeclaredMethods()){
for(Class<?> p:m.getParameterTypes())
if(p.getName().equals(a.getName()))
return true;
}
return false;
}
Is there a better way to write this function?
Is the name of the function terrible?
Are there any bugs in the code?
Is my test holistic, that is, did I test for everything?
getSuperclass()
to check the inheritance is not enough, because it will return either the direct parent class ornull
in some cases.isAssignableFrom(Class)
should be used instead. – Antot Apr 23 at 22:18