Skip to content

JoelZhu/CompileScanner

Repository files navigation

CompileScanner

Classes scanner for Android applications, which supply interfaces to get annotated classes for developers to design decoupling code.

version license build status top language last commit

✨ Overall

Deploy | Usage | Troubles | 中文版

🛠️ Deploy

To deploy the library as two steps:

  1. declare in project's build.gradle as below:
dependencies {
    classpath "io.github.joelzhu:scanner-plugin:[RELEASED_VERSION]"
}
  1. 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.

🗒️ Usage

Quick guide: Simple usage | Advanced usage: tag | Advanced usage: group | Advanced usage: default | Advanced usage: priority | Description of fields

Simple usage

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.

Advanced usage: tag

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);

Advanced usage: group

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);

Advanced usage: default

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.

Advanced usage: priority

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.

Description of fields
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

❗ Troubles

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.