Skip to content
This repository has been archived by the owner on May 23, 2021. It is now read-only.

Static functions in subclasses #12

Open
smichel17 opened this issue Sep 6, 2018 · 1 comment
Open

Static functions in subclasses #12

smichel17 opened this issue Sep 6, 2018 · 1 comment

Comments

@smichel17
Copy link
Member

From the PendingIntent documentation (emphasis mine):

By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity). As such, you should be careful about how you build the PendingIntent: almost always, for example, the base Intent you supply should have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.

The current design has TransitionScheduler as an abstract class; in order to use the library, you subclass it and implement onAlarm(). This means we (the library authors) don't know the name of the subclass in order to specify it in the intent. To get around this, we find it at runtime with this.getClass():

Intent intent = new Intent(ACTION_ALARM, data, context, this.getClass());

The problem is, this.getClass() can't be used in a static context, so our schedule() and cancel() functions can't be static, either. It's not that big of a deal that we have to instantiate a TransitionScheduler in order to schedule, but it would be nice if we could just call TransitionScheduler.schedule().

Does anybody have ideas for how we might accomplish this, or should we just say what we have is good enough?

@notjuliee
Copy link
Member

Looks like you can replace this.getClass() with MethodHandles.lookup().lookupClass() for a static method in Java 7+ (Docs)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants