# Determine if Class B uses Class A ( Java Reflection)

Below is a function that determines if Class B uses Class A.

Currently, it tests for:

1. Fields
2. Superclass
3. Constructors
4. 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?

• possible bug. Using getSuperclass() to check the inheritance is not enough, because it will return either the direct parent class or null in some cases. isAssignableFrom(Class) should be used instead. – Antot Apr 23 '16 at 22:18
• "did I test for everything?" – No. Like almost all static analysis, this is basically equivalent to solving the Halting Problem, and/or Rice's Theorem. – Jörg W Mittag Apr 24 '16 at 11:17
• Hey by the way, I just noticed you have not accepted any answer. If you could do that, it would close this question and it would give both you and the one who provided that answer some positive reputation. – ZeroOne Jul 29 '16 at 11:19

Is my test holistic, that is, did I test for everything?

Currently, this is what you're testing for:

This is what you are not testing for:

Are there any bugs in the code?

Yes, there is a potential bug: getSuperclass can return null:

If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned

so you need to take care about handling that. Your current code is

if(b.getSuperclass().getName().equals(a.getName()))


which could throw a NullPointerException.

• Make sure to always use curly braces, even if it is not strictly needed here. It will make the code less error-prone for the future.
• This only uses the Reflection API. Determining whether a given class A truly uses a given class B or not would require going into the byte-code to see if there are any references through chained method calls, import statements or local variables.
• Question for b.getSuperclass(). It returns class java.lang.Object. What do you mean null?? – None Apr 23 '16 at 22:39
• @user3773246 getSuperclass() is documented to return null if this class is java.lang.Object yes. Test with System.out.println(Object.class.getSuperclass());, you'll see :). – Tunaki Apr 23 '16 at 22:40
• So write if(b.getSuperclass().equals(null)) return false; ?? – None Apr 23 '16 at 22:42
• @user3773246 No, that will throw a NullPointerException since you're calling equals on a null reference. You need to check for null with != null. – Tunaki Apr 23 '16 at 22:43
• Like previous answer said, I no longer will be comparing getName(). – None Apr 23 '16 at 22:44

First of all, your comments are completely superfluous. Consider this wisdom from Clean Code by Uncle Bob:

So when you find yourself in a position where you need to write a comment, think it through and see whether there isn’t some way to turn the tables and express yourself in code. Every time you express yourself in code, you should pat yourself on the back. Every time you write a comment, you should grimace and feel the failure of your ability of expression

In your case you can turn your code more expressive like this:

private boolean uses(Class<?> b, Class<?> a){
if (testForDeclaredFields(b, a)) return true;
if (testForInheritance(b, a)) return true;
if (testForConstructors(b, a)) return true;
if (testForMethods(b, a)) return true;
return false;
}


Second, you should write proper unit tests for your code. That's a good way of verifying your code works.

Third, you are comparing the class name strings, when you could just compare the Class objects: instead of p.getName().equals(a.getName()) you can just say p.equals(a).

And as was already discussed in the comment section of your question, your code does not cover all the uses cases. It's missing at least local variables, inherited methods, and method return types.

• Actually, it would be even better to test equality between classes with ==. It will take care of potential nulls in the process. – Tunaki Apr 23 '16 at 22:47
• How is testing for reference better? – None Apr 23 '16 at 22:48
• @user3773246 See the Stack Overflow answer I linked to. – Tunaki Apr 23 '16 at 22:49
• Luckily in this case we know that p cannot be null because all parameters must have a type. – ZeroOne Apr 23 '16 at 23:00
• This would be far better as a single line returning a || b || c ... – Software Engineer Apr 28 '16 at 1:40

So, if I have a simple class like this:

class NoName {
public void thing() {
BigDecimal bd = BigDecimal.ZERO;
System.out.println(bd.toPlainString());
}
}


Would your code be likely to discover that I'm using BigDecimal? How about System? Or, PrintStream?

Actually, you're checking if I'm using a given class in my interface, not in my implementation. I could be using the class you're searching for a thousand times and you'd still not know.

You're not only missing these local variables and uses, but you're also missing method return types, anything inherited, and generics on each class of object you've already considered including the class itself.

If you're looking for a has-a or a uses-a relationship you'll have to work a lot harder than this to get it right. You would probably have to decompile the bytecode or use the Java platform debugger architecture (JPDA).