├── .gitignore ├── Android_Annotation_Processor.md ├── README.md ├── build.gradle ├── command-codegen ├── .gitignore ├── build.gradle └── src │ └── main │ ├── java │ └── la │ │ └── dahuo │ │ └── command │ │ └── codegen │ │ ├── AnnotationUtil.java │ │ ├── CommandFactoryClassGen.java │ │ ├── CommandProcessor.java │ │ └── CommandRegistersGen.java │ └── resources │ └── META-INF │ └── services │ └── javax.annotation.processing.Processor ├── command ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── la │ └── dahuo │ └── command │ ├── Command.java │ ├── CommandDef.java │ ├── CommandFactory.java │ ├── CommandRepository.java │ ├── Param.java │ └── TypeConverter.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /Android_Annotation_Processor.md: -------------------------------------------------------------------------------- 1 | # Android注解处理器(Annotation Processor) 2 | 3 | 在这篇文章中, 我将阐述怎么写一个Android的注解处理器(Annotation Processor)。 4 | 5 | ## 基本概念 ## 6 | 注解处理器(Annotation Processor)是javac的一个工具,它用来在编译时扫描和处理注解(Annotation)。你可以对自定义注解,并注册相应的注解处理器。到这里,我假设你已经知道什么是注解,并且知道怎么申明的一个注解。如果你不熟悉注解,你可以在这官方文档中得到更多信息。注解处理器在Java 5开始就有了,但是从Java 6(2006年12月发布)开始才有可用的API。过了一些时间,Java世界才意识到注解处理器的强大作用,所以它到最近几年才流行起来。 7 | 8 | 一个注解的注解处理器,以Java代码(或者编译过的字节码)作为输入,生成文件(通常是.java文件)作为输出。这具体的含义什么呢?你可以生成Java代码!这些生成的Java代码是在生成的.java文件中,所以你不能修改已经存在的Java类,例如向已有的类中添加方法。这些生成的Java文件,会同其他普通的手动编写的Java源代码一样被javac编译。 9 | 10 | ## 使用注解处理器 ## 11 | 下面来讲述怎么在Android Studio中使用注解处理器. 12 | 在这里就需要介绍一个gradle插件android-apt了, 13 | 官方地址: https://bitbucket.org/hvisser/android-apt 14 | 15 | Android Studio原本是不支持注解处理器的, 但是用这个插件后, 我们就可以使用注解处理器了, 这个插件可以自动的帮你为生成的代码创建目录, 让生成的代码编译到APK里面去, 而且它还可以让最终编译出来的APK里面不包含注解处理器本身的代码, 因为这部分代码只是编译的时候需要用来生成代码, 最终运行的时候是不需要的. 16 | 17 | 使用这个插件很简单, 首先在你项目顶层的build.gradle文件中添加依赖项, 如下: 18 | 19 | ```gradle 20 | buildscript { 21 | repositories { 22 | jcenter() 23 | } 24 | dependencies { 25 | classpath 'com.android.tools.build:gradle:1.5.0' 26 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 27 | } 28 | } 29 | ``` 30 | 31 | 然后在app的build.gradle里面添加插件的引用以及需要依赖哪些库, 如下: 32 | 33 | ```gradle 34 | apply plugin: 'com.android.application' 35 | apply plugin: 'com.neenbedankt.android-apt' 36 | 37 | ... 38 | dependencies { 39 | compile fileTree(dir: 'libs', include: ['*.jar']) 40 | testCompile 'junit:junit:4.12' 41 | compile 'com.android.support:appcompat-v7:22.2.1' 42 | compile 'la.dahuo:command:1.0.0' 43 | apt 'la.dahuo:command-codegen:1.0.0' 44 | } 45 | ``` 46 | 47 | 注意上面的`apt 'la.dahuo:command-codegen:1.0.0'`, 这里表示引用一个注解处理器的库, 这个库的代码最终不会进入编译出来的APK里面. 48 | 49 | ## 使用注解 ## 50 | 下面就来介绍怎么使用注解生成代码, 上面出现的库la.dahuo:command, 51 | la.dahuo:command-codegen就是我根据命令设计模式(Command Design Pattern)写的一个注解库, 它用来让我们方便的生成代码, 看下用法: 52 | 53 | ### 定义Command ### 54 | 55 | ```java 56 | @CommandDef("commandName") 57 | public class MyCommand extends Command { 58 | 59 | @Param 60 | String paramStr; 61 | 62 | @Param("paramIntName") 63 | int paramInt; 64 | 65 | @Override 66 | public void execute() { 67 | // do something with params 68 | } 69 | } 70 | ``` 71 | 上面的代码通过@CommandDef("commandName")来标注MyCommand是一个Command类, 它对应的名字为commandName. 然后通过标注@Param来定义了两个参数, @Param不填值的话表示参数名和属性名一样, 也可以通过填入一个值来指定参数名 72 | 73 | ### 使用Command ### 74 | 75 | ```java 76 | Map params = new HashMap<>(); 77 | params.put("paramStr", "string"); 78 | params.put("paramIntName", 1); 79 | Command.parse("commandName", params).execute(); 80 | ``` 81 | 82 | ### 分析 ### 83 | 看完上面的用法, 你可能会觉得就这么多代码就可以跑了吗? 不需要再添加什么代码了? 84 | 我的答案是: 确实可以跑, 不需要添加其他代码了. 85 | 有木有觉得用起来很方便? 我们下面来分析下这是怎么做到的. 86 | 87 | 首先标注处理器会根据@Param来获取Command的结构信息, 然后用来生成CommandFactory, 生成的代码在build/generated/source/apt下面, 生成的CommandFactory如下: 88 | 89 | ```java 90 | public class MyCommandFactory implements CommandFactory { 91 | public Command newCommand(Map params) { 92 | MyCommand command = new MyCommand(); 93 | command.paramStr = TypeConverter.toString(params.get("paramStr")); 94 | command.paramInt = TypeConverter.toInt(params.get("paramIntName")); 95 | return command; 96 | } 97 | } 98 | ``` 99 | 有了CommandFactory, 我们就可以通过Map params来构造Command了. 100 | 101 | 那Command是怎么自动注册的呢? 那我们就要看另外一个生成的类了. 102 | 103 | ```java 104 | public class CommandRegisters { 105 | public static void register(Map map) { 106 | map.put("myCommand", new MyCommandFactory()); 107 | } 108 | } 109 | ``` 110 | 它根据标注@CommandDef来获取了所有Command类的信息, 然后完成所有Command的注册. 111 | 112 | 有了上面自动生成的代码, 那么解析Command就很简单了. 代码如下: 113 | 114 | ```java 115 | public static Command parse(String action, Map params) 116 | { 117 | CommandFactory factory = CommandRepository.getInstance().getFactory(action); 118 | if (factory != null) { 119 | return factory.newCommand(params); 120 | } 121 | return EmptyCommand.INSTANCE; 122 | } 123 | ``` 124 | 125 | CommandRepository类通过反射的方式调用CommandRegisters类的register方法来完成所有Command的注册. 这是我这个注解库唯一的一处使用反射的地方, Command的创建都通过CommandFactory来完成, 性能很高, 比那些通过大量反射来实现对象映射的库性能好很多(比如GSON, 可以实现json转object), 而且也不需要依赖很多代码, 依赖一个很小的库 126 | la.dahuo:command就够了, 才5K. 127 | 128 | ## 怎么创建注解处理器 ## 129 | 注解处理器最核心的就是要有一个Processor, 它继承自AbstractProcessor,它长成这个样子: 130 | 131 | ```java 132 | package com.example; 133 | 134 | public class MyProcessor extends AbstractProcessor { 135 | 136 | @Override 137 | public synchronized void init(ProcessingEnvironment env){ } 138 | 139 | @Override 140 | public boolean process(Set annoations, RoundEnvironment env) { } 141 | 142 | @Override 143 | public Set getSupportedAnnotationTypes() { } 144 | 145 | @Override 146 | public SourceVersion getSupportedSourceVersion() { } 147 | } 148 | ``` 149 | 150 | - init(ProcessingEnvironment env):每个注解处理器都必须有个空的构造方法。不过,有一个特殊的init方法,它会被注解处理器工具传入一个ProcessingEnvironment作为参数来调用。ProcessingEnvironment提供了一些有用的工具类,如Elements,Types和Filter。我们后面会用到它们。 151 | 152 | - process(Set annotations, RoundEnvironment env):这个方法可以看做每个处理器的main方法。你要在这里写下你的扫描,判断和处理注解的代码,并生成java文件。通过传入的RoundEnvironment参数,你可以查询被某个特定注解注解的元素,我们稍后会看到。 153 | 154 | - getSupportedAnnotationTypes( ):这里你需要说明这个处理器需要针对哪些注解来注册。注意返回类型是一个字符串的Set,包含了你要用这个处理器处理的注解类型的全名。 155 | 156 | - getSupportedSourceVersion( ):用于指定你使用的java版本。通常你会返回SourceVersion.latestSupported( )。 157 | 158 | 接下来你需要知道的一件事就是注解处理器在它自己的JVM中运行。是的,你没看错。javac启动一个完整的Java虚拟机来给注解处理器运行。这对你意味着什么?你可以使用你在任何其他java应用中使用的东西,比如谷歌的guava!如果你愿意,你还可以使用依赖注入工具,如dagger或任何你想要使用的库。不过不要忘了,尽管这只是个小的处理器,你依然需要考虑高效的算法和设计模式,就像是你会为任何其他Java应用所做的那样。 159 | 160 | ## 注册你的处理器 ## 161 | 你也许会问自己:”我该怎样把我的处理器注册到javac?”。你需要提供一个.jar文件。就像其他的jar文件一样,你要把编译后的处理器打包到那个文件。而且你还需要把一个特别的放在META-INF/services下的叫做javax.annotation.processing.Processor的文件打包进你的.jar文件。所以你的.jar文件内容看起来会像这个样子: 162 | 163 | ```java 164 | MyProcessor.jar 165 | - com 166 | - example 167 | - MyProcessor.class 168 | - META-INF 169 | - services 170 | - javax.annotation.processing.Processor 171 | ``` 172 | 173 | java.annotation.processing.Processor文件(打包到MyProcessor.jar中)的内容是用换行符隔开的处理器完整类名: 174 | 175 | ```java 176 | com.example.MyProcessor 177 | com.foo.OtherProcessor 178 | net.blabla.SpecialProcessor 179 | ``` 180 | 181 | 这样, 一个注解处理器的框架就好了, 完成代码后编译成jar文件, 然后像开始介绍的那样添加依赖就好了. 182 | 183 | 如果你想要使用command-lib库, 请在proguard配置文件里面添加 184 | `-keep class la.dahuo.command.CommandRegisters {*;}` 185 | 186 | 上面提到的我写的Command的注解库我已经传到Github上了, 187 | 地址: https://github.com/derron/command-lib 188 | 有兴趣的可以"查看原文"去看看. 189 | 190 | ###扫描或长按关注我们的微信技术公众号-极客联盟 191 | ![极客联盟](https://dn-cloud-disk.qbox.me/images/jklm_qr.jpg) 192 | 193 | ###“崇尚自由,推崇技术,拥抱开源” - 极客联盟:传播新技术理念,分享技术经验。 打造华中区最有影响力的技术公众号。 194 | 195 | ## 我们正在招聘APP产品经理、PHP、测试、前端、IOS、Android、Java等岗位,有意向的可以联系我们: dahuo.contact@gmail.com 196 | 197 | 198 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## How to use in Android Studio 2 | 3 | ### Add dependencies 4 | 5 | Add the following to your project's build.gradle file, in the buildScript dependency section so we can use android-apt. 6 | 7 | ```gradle 8 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 9 | ``` 10 | 11 | The app's build.gradle looks like the following. 12 | 13 | ```gradle 14 | apply plugin: 'com.android.application' 15 | apply plugin: 'com.neenbedankt.android-apt' 16 | 17 | ... 18 | dependencies { 19 | compile fileTree(dir: 'libs', include: ['*.jar']) 20 | testCompile 'junit:junit:4.12' 21 | compile 'com.android.support:appcompat-v7:22.2.1' 22 | compile 'la.dahuo:command:1.0.0' 23 | apt 'la.dahuo:command-codegen:1.0.0' 24 | } 25 | ``` 26 | 27 | ### Define Command 28 | 29 | ```java 30 | @CommandDef("commandName") 31 | public class MyCommand extends Command { 32 | 33 | @Param 34 | String paramStr; 35 | 36 | @Param("paramIntName") 37 | int paramInt; 38 | 39 | @Override 40 | public void execute() { 41 | // do something with params 42 | } 43 | } 44 | ``` 45 | 46 | ### Use Command 47 | 48 | ```java 49 | Map params = new HashMap<>(); 50 | params.put("paramStr", "string"); 51 | params.put("paramIntName", 1); 52 | Command.parse("commandName", params).execute(); 53 | ``` 54 | 55 | ### Proguard 56 | 57 | Add following line to your proguard config file. 58 | ``` 59 | -keep class la.dahuo.command.CommandRegisters {*;} 60 | ``` 61 | 62 | You can put your commands class in any where you like, all commands will be auto registered. -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4' 9 | } 10 | } 11 | 12 | allprojects { 13 | repositories { 14 | jcenter() 15 | } 16 | group = 'la.dahuo' 17 | version = '1.0.0' 18 | } 19 | 20 | subprojects { 21 | apply plugin: 'maven' 22 | apply plugin: 'java' 23 | apply plugin: 'com.jfrog.bintray' 24 | 25 | sourceCompatibility = JavaVersion.VERSION_1_7 26 | targetCompatibility = JavaVersion.VERSION_1_7 27 | 28 | // custom tasks for creating source/javadoc jars 29 | task sourcesJar(type: Jar, dependsOn: classes) { 30 | classifier = 'sources' 31 | from sourceSets.main.allSource 32 | } 33 | 34 | task javadocJar(type: Jar, dependsOn: javadoc) { 35 | classifier = 'javadoc' 36 | from javadoc.destinationDir 37 | } 38 | 39 | artifacts { 40 | archives sourcesJar, javadocJar 41 | } 42 | 43 | bintray { 44 | user = System.getenv('BINTRAY_USER'); 45 | key = System.getenv('BINTRAY_KEY'); 46 | 47 | configurations = ['archives'] //When uploading configuration files 48 | 49 | dryRun = false //Whether to run this as dry-run, without deploying 50 | publish = true //If version should be auto published after an upload 51 | pkg { 52 | repo = 'maven' 53 | name = project.group + ":" + project.name 54 | websiteUrl = 'https://github.com/derron/command-lib' 55 | issueTrackerUrl = 'https://github.com/derron/command-lib/issues' 56 | vcsUrl = 'https://github.com/derron/command-lib.git' 57 | licenses = ['Apache-2.0'] 58 | labels = ['android', 'command'] 59 | publicDownloadNumbers = true 60 | } 61 | } 62 | } -------------------------------------------------------------------------------- /command-codegen/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /command-codegen/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies { 2 | compile 'com.squareup:javapoet:1.4.0' 3 | compile project(':command') 4 | } -------------------------------------------------------------------------------- /command-codegen/src/main/java/la/dahuo/command/codegen/AnnotationUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package la.dahuo.command.codegen; 17 | 18 | import java.lang.annotation.Annotation; 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | import javax.annotation.processing.RoundEnvironment; 23 | import javax.lang.model.element.Element; 24 | 25 | class AnnotationUtil { 26 | 27 | /** 28 | * Returns only the elements that are annotated with the given class. For some reason 29 | * RoundEnvironment is returning elements annotated by other annotations. 30 | */ 31 | static List getElementsAnnotatedWith(RoundEnvironment roundEnv, 32 | Class annotationClass) { 33 | ArrayList elements = new ArrayList(); 34 | for (Element element : roundEnv.getElementsAnnotatedWith(annotationClass)) { 35 | if (element.getAnnotation(annotationClass) != null) { 36 | elements.add(element); 37 | } 38 | } 39 | return elements; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /command-codegen/src/main/java/la/dahuo/command/codegen/CommandFactoryClassGen.java: -------------------------------------------------------------------------------- 1 | package la.dahuo.command.codegen; 2 | 3 | import com.squareup.javapoet.ClassName; 4 | import com.squareup.javapoet.JavaFile; 5 | import com.squareup.javapoet.MethodSpec; 6 | import com.squareup.javapoet.ParameterizedTypeName; 7 | import com.squareup.javapoet.TypeSpec; 8 | 9 | import java.io.IOException; 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | import javax.annotation.processing.Filer; 15 | import javax.lang.model.element.Element; 16 | import javax.lang.model.element.Modifier; 17 | import javax.lang.model.element.TypeElement; 18 | import javax.lang.model.element.VariableElement; 19 | import javax.lang.model.type.TypeKind; 20 | import javax.lang.model.type.TypeMirror; 21 | import javax.lang.model.util.ElementFilter; 22 | 23 | import la.dahuo.command.Command; 24 | import la.dahuo.command.CommandDef; 25 | import la.dahuo.command.CommandFactory; 26 | import la.dahuo.command.Param; 27 | import la.dahuo.command.TypeConverter; 28 | 29 | /** 30 | * Created by dhu on 15/12/16. 31 | */ 32 | public class CommandFactoryClassGen { 33 | protected final Element mElement; 34 | 35 | private ClassName mClassName; 36 | private FactoryInfo mFactoryInfo; 37 | 38 | private static final String JAVA_LANG_STRING = "java.lang.String"; 39 | private static final String JAVA_LANG_BOOLEAN = "java.lang.Boolean"; 40 | private static final String JAVA_LANG_INTEGER = "java.lang.Integer"; 41 | private static final String JAVA_LANG_LONG = "java.lang.Long"; 42 | private static final String JAVA_LANG_FLOAT = "java.lang.Float"; 43 | private static final String JAVA_LANG_DOUBLE = "java.lang.Double"; 44 | 45 | enum ParamType { 46 | Unsupported, 47 | Boolean, 48 | Int, 49 | Long, 50 | Float, 51 | Double, 52 | String; 53 | 54 | public static ParamType parseType(TypeMirror type) { 55 | if (JAVA_LANG_STRING.equals(type.toString())) { 56 | return String; 57 | } else if (type.getKind() == TypeKind.BOOLEAN || JAVA_LANG_BOOLEAN.equals(type.toString())) { 58 | return Boolean; 59 | } else if (type.getKind() == TypeKind.INT || JAVA_LANG_INTEGER.equals(type.toString())) { 60 | return Int; 61 | } else if (type.getKind() == TypeKind.LONG || JAVA_LANG_LONG.equals(type.toString())) { 62 | return Long; 63 | } else if (type.getKind() == TypeKind.FLOAT || JAVA_LANG_FLOAT.equals(type.toString())) { 64 | return Float; 65 | } else if (type.getKind() == TypeKind.DOUBLE || JAVA_LANG_DOUBLE.equals(type.toString())) { 66 | return Double; 67 | } 68 | return Unsupported; 69 | } 70 | } 71 | static class ParamInfo { 72 | public String paramName; 73 | public String fieldName; 74 | public ParamType type; 75 | 76 | } 77 | static class FactoryInfo { 78 | public String action; 79 | public ClassName factoryName; 80 | } 81 | private List mParamInfos; 82 | public CommandFactoryClassGen(TypeElement element) { 83 | mElement = element; 84 | mClassName = ClassName.get(element); 85 | mFactoryInfo = new FactoryInfo(); 86 | mFactoryInfo.factoryName = ClassName.get(mClassName.packageName(), mClassName.simpleName() + "Factory"); 87 | String action = element.getAnnotation(CommandDef.class).value(); 88 | if (action == null || action.length() == 0) { 89 | action = element.getSimpleName().toString(); 90 | //make first letter lowercase 91 | action = action.substring(0, 1).toLowerCase() + action.substring(1); 92 | } 93 | mFactoryInfo.action = action; 94 | 95 | mParamInfos = new ArrayList<>(); 96 | List fields = ElementFilter.fieldsIn(mElement.getEnclosedElements()); 97 | for (VariableElement field : fields) { 98 | Param param = field.getAnnotation(Param.class); 99 | if (param != null) { 100 | ParamInfo paramInfo = new ParamInfo(); 101 | paramInfo.fieldName = field.getSimpleName().toString(); 102 | String paramName = param.value(); 103 | if (paramName == null || paramName.length() == 0) { 104 | paramName = paramInfo.fieldName; 105 | } 106 | paramInfo.paramName = paramName; 107 | paramInfo.type = ParamType.parseType(field.asType()); 108 | if (paramInfo.type == ParamType.Unsupported) { 109 | throw new RuntimeException("Type: " + field.asType().toString() + " Unsupported"); 110 | } 111 | mParamInfos.add(paramInfo); 112 | } 113 | } 114 | } 115 | 116 | public FactoryInfo getFactoryInfo() { 117 | return mFactoryInfo; 118 | } 119 | 120 | public void writeTo(Filer output) throws IOException { 121 | MethodSpec.Builder newCommandBuilder = MethodSpec.methodBuilder("newCommand") 122 | .addModifiers(Modifier.PUBLIC) 123 | .returns(Command.class) 124 | .addParameter(ParameterizedTypeName.get(Map.class, String.class, Object.class), "params") 125 | .addStatement("$T command = new $T()", mClassName, mClassName); 126 | for (ParamInfo paramInfo : mParamInfos) { 127 | newCommandBuilder.addStatement("command.$L = $T.to$L(params.get($S))", 128 | paramInfo.fieldName, TypeConverter.class, paramInfo.type.name(), paramInfo.paramName); 129 | } 130 | newCommandBuilder.addStatement("return command"); 131 | 132 | TypeSpec factoryClass = TypeSpec.classBuilder(mFactoryInfo.factoryName.simpleName()) 133 | .addModifiers(Modifier.PUBLIC) 134 | .addSuperinterface(CommandFactory.class) 135 | .addMethod(newCommandBuilder.build()) 136 | .build(); 137 | JavaFile javaFile = JavaFile.builder(mFactoryInfo.factoryName.packageName(), factoryClass) 138 | .build(); 139 | javaFile.writeTo(output); 140 | } 141 | 142 | } 143 | -------------------------------------------------------------------------------- /command-codegen/src/main/java/la/dahuo/command/codegen/CommandProcessor.java: -------------------------------------------------------------------------------- 1 | package la.dahuo.command.codegen; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.HashSet; 6 | import java.util.List; 7 | import java.util.Set; 8 | 9 | import javax.annotation.processing.AbstractProcessor; 10 | import javax.annotation.processing.RoundEnvironment; 11 | import javax.lang.model.SourceVersion; 12 | import javax.lang.model.element.Element; 13 | import javax.lang.model.element.TypeElement; 14 | 15 | import la.dahuo.command.CommandDef; 16 | 17 | public class CommandProcessor extends AbstractProcessor { 18 | 19 | private static boolean sCommandRegistersGenerated; 20 | @Override 21 | public Set getSupportedAnnotationTypes() { 22 | Set types = new HashSet<>(); 23 | types.add(CommandDef.class.getCanonicalName()); 24 | return types; 25 | } 26 | 27 | @Override 28 | public SourceVersion getSupportedSourceVersion() { 29 | return SourceVersion.latestSupported(); 30 | } 31 | 32 | @Override 33 | public boolean process(Set annotations, RoundEnvironment roundEnv) { 34 | List allCommandFactories = new ArrayList<>(); 35 | for (Element element : AnnotationUtil.getElementsAnnotatedWith(roundEnv, CommandDef.class)) { 36 | CommandFactoryClassGen gen = new CommandFactoryClassGen((TypeElement)element); 37 | try { 38 | gen.writeTo(processingEnv.getFiler()); 39 | allCommandFactories.add(gen.getFactoryInfo()); 40 | } catch (IOException e) { 41 | throw new RuntimeException(e); 42 | } 43 | } 44 | try { 45 | if (!sCommandRegistersGenerated) { 46 | new CommandRegistersGen(allCommandFactories).writeTo(processingEnv.getFiler()); 47 | sCommandRegistersGenerated = true; 48 | } 49 | } catch (IOException e) { 50 | throw new RuntimeException(e); 51 | } 52 | 53 | return true; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /command-codegen/src/main/java/la/dahuo/command/codegen/CommandRegistersGen.java: -------------------------------------------------------------------------------- 1 | package la.dahuo.command.codegen; 2 | 3 | import com.squareup.javapoet.JavaFile; 4 | import com.squareup.javapoet.MethodSpec; 5 | import com.squareup.javapoet.ParameterizedTypeName; 6 | import com.squareup.javapoet.TypeSpec; 7 | 8 | import java.io.IOException; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | import javax.annotation.processing.Filer; 13 | import javax.lang.model.element.Modifier; 14 | 15 | import la.dahuo.command.CommandFactory; 16 | 17 | /** 18 | * Created by dhu on 15/12/17. 19 | */ 20 | public class CommandRegistersGen { 21 | 22 | private List mFactoryInfos; 23 | public CommandRegistersGen(List factories) { 24 | mFactoryInfos = factories; 25 | } 26 | 27 | public void writeTo(Filer output) throws IOException { 28 | MethodSpec.Builder registerBuilder = MethodSpec.methodBuilder("register") 29 | .addModifiers(Modifier.PUBLIC, Modifier.STATIC) 30 | .returns(void.class) 31 | .addParameter(ParameterizedTypeName.get(Map.class, String.class, CommandFactory.class), "map"); 32 | for (CommandFactoryClassGen.FactoryInfo info : mFactoryInfos) { 33 | registerBuilder.addStatement("map.put($S, new $T())", info.action, info.factoryName); 34 | } 35 | 36 | TypeSpec registersClass = TypeSpec.classBuilder("CommandRegisters") 37 | .addModifiers(Modifier.PUBLIC) 38 | .addMethod(registerBuilder.build()) 39 | .build(); 40 | JavaFile javaFile = JavaFile.builder("la.dahuo.command", registersClass) 41 | .build(); 42 | javaFile.writeTo(output); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /command-codegen/src/main/resources/META-INF/services/javax.annotation.processing.Processor: -------------------------------------------------------------------------------- 1 | la.dahuo.command.codegen.CommandProcessor -------------------------------------------------------------------------------- /command/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /command/build.gradle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/derron/command-lib/a1caafa51a1594f60145122e8b3cf4ecb9b19def/command/build.gradle -------------------------------------------------------------------------------- /command/src/main/java/la/dahuo/command/Command.java: -------------------------------------------------------------------------------- 1 | package la.dahuo.command; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * Created by dhu on 15/12/16. 7 | */ 8 | public abstract class Command { 9 | public abstract void execute(); 10 | 11 | public static Command parse(String action, Map params) { 12 | CommandFactory factory = CommandRepository.getInstance().getFactory(action); 13 | if (factory != null) { 14 | return factory.newCommand(params); 15 | } 16 | return EmptyCommand.INSTANCE; 17 | } 18 | 19 | public static class EmptyCommand extends Command { 20 | 21 | public static final EmptyCommand INSTANCE = new EmptyCommand(); 22 | private EmptyCommand() { 23 | } 24 | 25 | @Override 26 | public void execute() { 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /command/src/main/java/la/dahuo/command/CommandDef.java: -------------------------------------------------------------------------------- 1 | package la.dahuo.command; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * Created by dhu on 15/12/16. 10 | */ 11 | @Retention(RetentionPolicy.SOURCE) 12 | @Target(ElementType.TYPE) 13 | public @interface CommandDef { 14 | String value() default ""; 15 | } 16 | -------------------------------------------------------------------------------- /command/src/main/java/la/dahuo/command/CommandFactory.java: -------------------------------------------------------------------------------- 1 | package la.dahuo.command; 2 | 3 | import java.util.Map; 4 | 5 | /** 6 | * Created by dhu on 15/12/16. 7 | */ 8 | public interface CommandFactory { 9 | Command newCommand(Map params); 10 | } 11 | -------------------------------------------------------------------------------- /command/src/main/java/la/dahuo/command/CommandRepository.java: -------------------------------------------------------------------------------- 1 | package la.dahuo.command; 2 | 3 | import java.lang.reflect.Method; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | /** 8 | * Created by dhu on 15/12/16. 9 | */ 10 | public class CommandRepository { 11 | private static CommandRepository sInstance = new CommandRepository(); 12 | private CommandRepository() { 13 | mFactories = new HashMap<>(); 14 | try { 15 | Class clazz = Class.forName("la.dahuo.command.CommandRegisters"); 16 | Method method = clazz.getMethod("register", Map.class); 17 | method.invoke(null, mFactories); 18 | } catch (Exception e) { 19 | throw new RuntimeException("There is something wrong with command codegen", e); 20 | } 21 | } 22 | 23 | public static CommandRepository getInstance() { 24 | return sInstance; 25 | } 26 | 27 | private Map mFactories; 28 | 29 | public CommandFactory getFactory(String action) { 30 | return mFactories.get(action); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /command/src/main/java/la/dahuo/command/Param.java: -------------------------------------------------------------------------------- 1 | package la.dahuo.command; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * Created by dhu on 15/12/16. 10 | */ 11 | @Target(ElementType.FIELD) 12 | @Retention(RetentionPolicy.SOURCE) 13 | public @interface Param { 14 | String value() default ""; 15 | } 16 | -------------------------------------------------------------------------------- /command/src/main/java/la/dahuo/command/TypeConverter.java: -------------------------------------------------------------------------------- 1 | package la.dahuo.command; 2 | 3 | /** 4 | * Created by dhu on 15/12/16. 5 | */ 6 | public class TypeConverter { 7 | public static String toString(Object value) { 8 | if (value == null) { 9 | return ""; 10 | } else if (value instanceof String) { 11 | return (String) value; 12 | } else { 13 | return value.toString(); 14 | } 15 | } 16 | 17 | public static boolean toBoolean(Object value) { 18 | if (value == null) { 19 | return false; 20 | } else if (value instanceof Boolean) { 21 | return ((Boolean)value).booleanValue(); 22 | } else if (value instanceof Number) { 23 | return ((Number)value).intValue() > 0; 24 | } else if (value instanceof String) { 25 | boolean result; 26 | try { 27 | result = Boolean.parseBoolean((String)value); 28 | } catch (NumberFormatException e) { 29 | result = false; 30 | } 31 | return result; 32 | } else { 33 | return false; 34 | } 35 | } 36 | 37 | public static int toInt(Object value) { 38 | if (value == null) { 39 | return 0; 40 | } else if (value instanceof Number) { 41 | return ((Number)value).intValue(); 42 | } else if (value instanceof String) { 43 | int result; 44 | try { 45 | result = Integer.parseInt((String)value); 46 | } catch (NumberFormatException e) { 47 | result = 0; 48 | } 49 | return result; 50 | } else { 51 | return 0; 52 | } 53 | } 54 | 55 | public static long toLong(Object value) { 56 | if (value == null) { 57 | return 0; 58 | } else if (value instanceof Number) { 59 | return ((Number)value).longValue(); 60 | } else if (value instanceof String) { 61 | long result; 62 | try { 63 | result = Long.parseLong((String) value); 64 | } catch (NumberFormatException e) { 65 | result = 0; 66 | } 67 | return result; 68 | } else { 69 | return 0; 70 | } 71 | } 72 | 73 | public static float toFloat(Object value) { 74 | if (value == null) { 75 | return 0; 76 | } else if (value instanceof Number) { 77 | return ((Number)value).floatValue(); 78 | } else if (value instanceof String) { 79 | float result; 80 | try { 81 | result = Float.parseFloat((String) value); 82 | } catch (NumberFormatException e) { 83 | result = 0; 84 | } 85 | return result; 86 | } else { 87 | return 0; 88 | } 89 | } 90 | 91 | public static double toDouble(Object value) { 92 | if (value == null) { 93 | return 0; 94 | } else if (value instanceof Number) { 95 | return ((Number)value).doubleValue(); 96 | } else if (value instanceof String) { 97 | double result; 98 | try { 99 | result = Double.parseDouble((String) value); 100 | } catch (NumberFormatException e) { 101 | result = 0; 102 | } 103 | return result; 104 | } else { 105 | return 0; 106 | } 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/derron/command-lib/a1caafa51a1594f60145122e8b3cf4ecb9b19def/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Oct 21 11:34:03 PDT 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':command', ':command-codegen' 2 | --------------------------------------------------------------------------------