It seems in an Activity, for example, the onCreate() method, it does not make much difference if I have the #Override annotation or not. They both work. (As long as I call super.onCreate() inside the callback , it will call the parent class' method.)
Can someone tell me why we need to have the #Override annotation for life-cycle callbacks in Activity ?
I ask this because I tested without #Override annotation, my app still running successfully.
This is more like a good development practice.
If by mistake you want to override a method that doesn't exist in the super class (or interfaces implemented) you'll get an error.
Think that you want to override "onCreate" but you misspell it and write "onCreatee". With that annotation, you'll get an error. Without it, you'd end up spend a lot time trying to understand why your initialization method was not working properly.
The #Override annotation is used just to tell the compiler that we are overriding a method in our code. This is used for safety purposes to let the compiler know the aim of our function (i.e. to override) and if we are overloading a function by any chance, the compiler will return an error. Hence with this annotation, we can detect overloading mistakes easily.
Some already mentioned that it is very useful to catch potential bugs with wrongly spelled names.
I would like to add that it
also shows which methods are new methods specific to your class and which are inherited from the parent class (or interface). It might not sound like much, but I personally find it very useful.
So you should always use #Override and configure your IDE to flag an error if you forget it.
Related
I have tried doing a search for this but I fear I may not be wording what I want to do very well.
Currently, we have about a hundred action classes in our application with each determining if a user has access to it. I would like to make a class that can figure out the calling method, what permissions are required for it, and if the user has those permissions. Unfortunately, I don't really know how to even get started with this as each class may have slightly different requirements.
I'm happy to add more explanation if needed but as I said, I'm not sure I'm wording what I'm trying to do very well so if anyone has a better way of putting it that gets me some google results or a link to a related question here that's already been answered, I know I'd appreciate it.
current permissions checks look like below. This is a simple implementation, there are usually multiple profile checks in one if block.
If (scc.getUser().getCurrentProfile().getSystemAdmin() != 1) {
logIllegalAccess(log);
break;
}
IMHO the most elegant solution would make use of annotation processing. The idea is that you would annotate action classes with a custom annotation, something like:
#RequiredPermission(Permissions.SYSADM)
class ActionA {
public ActionA newInstance() {
return new ActionA_Gen(new ActionA());
}
private ActionA() {...}
...
}
Action classes would have to have a newInstance() method to be used to create instances instead of calling new. The method would create an instance of a class by the same name with _Gen extension. This class would have one method for each method in the original action class, which would perform a permission check and call the corresponding method in the original class instance that was passed to its constructor.
The _Gen class would be generated by an annotation processor.
Note that by using reflection it might be possible to move the newInstance() method in a common superclass.
There is a pattern which is widely used in my current project:
private Collection<Converter<T>> converters = new HashSet<>();
#Inject
private void init(#Any Instance<Converter<T>> converters) {
for (Converter<T> converter : converters) {
this.converters.add(converter);
}
}
This way I can create as many converters as I want and they are automatically injected to my bean.
My problem is now with testing: the converters collection is used in my code, but Junit doesn't call the init(..) method and I need to call it to set the mocked converters.
I could make the method protected, but I don't feel OK with it because I would be changing the visibility scope of the method.
I could also call the method using reflection, but this also doesn't feel right.
This brings me to the conclusion that this code could be improved to be more testable.
Is there anyway I change this code so the testability is improved but the references are still automatically injected?
Just go ahead and make it 'public' or 'protected'.
You are not actually gaining any protection from someone changing the collection post-instantiation this way (you've just made it a little more awkward), so you don't lose anything by exposing that method (in fact I'd argue you make your class slightly better, because than you let people chose how they want to construct, rather than forcing a use of injection/reflection).
If you did want to fully prevent post-instantiation modification, than you're going to have to go to a 'final' variable anyway, with an unmodifiable collection type and change to constructor injection, but I don't get the impression that this is what you want to do.
Thing is: if you can't "trust" the people who can write code within your "package" ... I guess having "private" on a method doesn't really help you anyway. Because if people want to mess up, and they can write code in your package, they will find ways to mess up anyway.
Meaning: if you drop the "private" on your method, yes it becomes package-visible. But you can place a javadoc on it that says: "Don't call directly; used for unit test/auto-wiring only" or something like that.
There is a class called CellSignalStrength that does not have a default constructor.
To be able to use SignalStrength, I have to make a class that extracts it, but I can't because when I try to do that, it prints out the error:
No default contstructor available for SignalStrength.
Also, there is another class called CellSignalStrengthGsm(same problem), but that class extends SignalStrength class, but how?
I've done some research on this, and i couldn't find anything, but this guide is only offering the signal strength of neighboring cell-sites to me, none of them is the one providing me with the signal. That solution is offering no actual signal strength.
Why doesn't the Android documentation have at least one example for how to use the class?
I really need help on this, I'm stuck :/
The only reason I can think of why you can't extend the CellSignalStrength class is because its constructor is protected CellSignalStrength(). Meaning only other classes in the same package may extend it.
The Android developers probably had good reason to do so. My suggestion would be to rethink what you're trying to do and figure out another solution. Perhaps using an instance of one of the classes that sub-classes CellSignalStrength:
Note: These classes are all final, so don't try to extend them.
CellSignalStrengthCdma
CellSignalStrengthGsm
CellSignalStrengthLte
CellSignalStrengthWcdma
I have an issue where one of my subscribed methods does not get called upon a post of the correct event type unless that subscribed method is used (called) elsewhere.
Here is some relevant information about the code:
A method of one of my classes is annotated with #Subscribe.
By stepping through the code with the debugger, I find that under my specific circumstance, the class has no methods annotated with #Subscribe.
Unless I call the method directly at some point in time (doesn't matter when, or even if it actually gets called at runtime) elsewhere, my post does not work.
The IDE (Android Studio) notifies me that the "method is never used"
I can certainly call the method in a block of code that I am confident will never fire, but this is obviously terrible practice, and defeats the purpose of this post/subscribe paradigm.
Or I can make the method static, but I'd rather not because I use member variables inside of it.
Any solutions to why this is occuring even though Otto's example uses a similar pattern
Turns out it was a ProGuard issue. Fixed it by adding the following lines:
-keepclassmembers class ** {
#com.squareup.otto.Subscribe public *;
#com.squareup.otto.Produce public *;
}
I hope this is not a duplicate, because I didn't find answer an for this. Only something refering to c++ destructors, but I know nothing about that. Anyway I'm used to override my methods on my own bud to speed up process I now go trought eclipse source-override/implement methods and I don't understand why eclipse automaticly paste
super.method(event);
into my code,Why eclipse does this for me? Does it actually do anything?I already call that method anyway,why it have to be there twice i dont understand.
example of what i mean:
someJlabel.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseEntered(e); // <--
}
});
I already know that if i don't have it there everything works ok and if I do also.So if anyone can explain this to me i would be really happy its a small thing bud I want to know whats going on.
super.mouseEntered(e); is call to method mouseEntered() as it's defined in the parent class of MouseAdapter. So it's not the same method called twice.
While overriding methods you often call the super method (such as you call super in constructor) and then you can do some additional actions. If you leave just super.method() there, then it's going to behave as if you didn't override it at all.
This calls the method with the same name from the super/parent class. Generally it is up to you if you want to include that call to the parent class or not. Eclipse is just suggesting it. If that method in the parent class does nothing (e.g. has an empty body), then purely practically viewed it does not matter if you add this call or not in the derived/child class. But if the method in the parent class does something useful, then it is sometimes a good idea to include that call, as you would get the parent class behavior too. That's why Eclipse is suggesting you do it.