cushon / issues-import

0 stars 0 forks source link

Calling getClass on a variable of type Class #217

Open cushon opened 9 years ago

cushon commented 9 years ago

Original issue created by fixpoint@google.com on 2013-11-22 at 02:32 AM


Have just encountered an instance of this (some details changed):

@Provides boolean provideEligibility(Class<? extends Action> action) { return actionClass.getClass().getAnnotation(IsEligible.class) != null; }

Calling getClass() on a Class can be substituted with Class.class for practically all cases with an improvement of readability.

cushon commented 9 years ago

Original comment posted by fixpoint@google.com on 2013-11-22 at 02:34 AM


Sorry, a fragment from a fix creeped into the code. It was like this:

@Provides boolean provideEligibility(Class<? extends Action> action) { return action.getClass().getAnnotation(IsEligible.class) != null; }

cushon commented 9 years ago

Original comment posted by fixpoint@google.com on 2013-11-22 at 02:40 AM


Just to clarify: calling getClass() on an instance of Class is very likely a mistake, and if it's not, the Class.class would be a better way to express that anyway.

cushon commented 9 years ago

Original comment posted by cpovirk@google.com on 2013-11-25 at 08:54 PM


I see 48 instances in Google-internal code (CL 57244074 if you're curious).

I personally like the sound of this check a lot.