Classes scanner for Android applications, which supply interfaces to get annotated classes for developers to design decoupling code.
Deploy | Usage | Troubles | 中文版
To deploy the library as two steps:
- declare in project's
build.gradle
as below:
dependencies {
classpath "io.github.joelzhu:scanner-plugin:[RELEASED_VERSION]"
}
- declare in application's
build.gradle
as below:
plugins {
id 'com.joelzhu.scanner'
}
dependencies {
implementation "io.github.joelzhu:scanner-runtime:[RELEASED_VERSION]"
}
There's no obvious differences of deploying between using Java and using Kotlin.
Quick guide: Simple usage | Advanced usage: tag | Advanced usage: group | Advanced usage: default | Advanced usage: priority | Description of fields
For simple usage, coding as below:
@CompileScan
public class ExampleClass implements IExample {}
And you have many classes which annotated with CompileScan
as above, then, you don't have to call them one by one, you can acquire those classes esaily. Call the method Scanner.getAnnotatedClasses();
or Scanner.getAnnotatedInstances(IExample.class);
, and you will got the classes or array of IExample
instances back.
If you want to mark each class by it's own attributes, tag
may help you. For example, you have three classes as below:
@CompileScan(tag = "A")
public class ExampleAClass implements IExample {}
@CompileScan(tag = "B")
public class ExampleBClass implements IExample {}
@CompileScan(tag = "C")
public class ExampleCClass implements IExample {}
You may have ExampleAClass
easily by calling like this: final IExample[] examples = Scanner.getAnnotatedInstances(new Options.Builder().tag("A").create(), IExample.class);
Consider only tag
is not enough, we supply you another one: group
. If you have two classes, which both belonging to Life
, but you have to get different classes in different cases, you can differ them with group
, like:
@CompileScan(tag = "Life", group = "Work")
public class Work implements ILife {}
@CompileScan(tag = "Life", group = "Rest)
public class Sleep implements ILife {}
And you can get classes which belonging to work
by calling as final ILife[] works = Scanner.getAnnotatedInstances(new Options.Builder().tag("Life").group("Work").create(), ILife.class);
You can use the CompileScanner
as a compilation Proxy. For example, you have a class A as below:
@CompileScan(tag = "ProxySample", isDefault = true)
public class ProxyA implements IProxy {
@Override
public String printName() {
return "I'm A.";
}
}
and you have another class B:
@CompileScan(tag = "ProxySample")
public class ProxyB implements IProxy {
@Override
public String printName() {
return "I'm B.";
}
}
If you call the result like:
final String tag = "ProxySample";
final IProxy[] proxys = Scanner.getAnnotatedInstances(new Options.Builder(tag).create(), IProxy.class);
you will got the instance array of IProxy.class
, as the proxys
above.
And when you calling the IProxy
's printName()
, like: proxys[0].printName();
, you will got the I'm B.
returned back.
This may help you implement pattern - Proxy easily.
You can set priority of the classes with the same tag
.
For example: If class First.class
has the higher priority of the class Second.class
, you can annotated them as below:
@CompileScan(priority = 1)
public class First {}
@CompileScan(priority = 2)
public class Second {}
And you will got the array, which put the First.class
above the Second.class
.
Lager number has lower priority than the small one.
Fields | Description |
---|---|
tag | Mark the annotated classes with different tags |
group | Mark the same tagged classes with different groups |
isDefault | Differ the classes with default one or not |
priority | Indicate the priority of each classes with the same tag |
For more examples, see the code in the project. Java Example | Kotlin Example | Multi Modules Usage Example
If you got compile exceptions or something unexcepted, add the options below in application's build.gradle
to open the compile log.
android {
}
Scanner {
enableLog = true
}
dependencies {
}
Then, when the problem occured, paste the full compile log into an new issue, and assign to me, i will try my best to resolve the problem as fast as you wish.