├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle └── src ├── main └── java │ └── com │ └── github │ └── racc │ └── tscg │ ├── ConfigExtractor.java │ ├── ConfigExtractors.java │ ├── ListExtractor.java │ ├── ListExtractors.java │ ├── Reflector.java │ ├── TypesafeConfig.java │ ├── TypesafeConfigImpl.java │ ├── TypesafeConfigModule.java │ ├── TypesafeConfigs.java │ └── reflectors │ ├── fastclasspathscanner │ ├── FastClasspathScanningReflector.java │ ├── FieldIdentifier.java │ └── MethodIdentifier.java │ └── reflections │ └── ReflectionsReflector.java └── test ├── java └── com │ └── github │ └── racc │ └── tscg │ ├── TypesafeConfigModuleTest.java │ └── test │ ├── ConstructorInjectedPojo.java │ ├── FieldInjectedPojo.java │ ├── MethodInjectedPojo.java │ ├── NestedPojo.java │ ├── ProvidedPojo.java │ └── TestPojo.java └── resources └── conf └── test.conf /.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /build/ 3 | /.gradle/ 4 | /.settings/ 5 | /.classpath 6 | /.project 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | install: gradle -x signArchives assemble -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2015 Jason Then. 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Typesafe Config Guice 2 | Allows [Guice](https://github.com/google/guice) to @Inject configuration values into your application derived from [Typesafe Config](https://github.com/typesafehub/config). 3 | 4 | [![Build Status](https://travis-ci.org/racc/typesafeconfig-guice.svg?branch=master)](https://travis-ci.org/racc/typesafeconfig-guice) 5 | 6 | Available on [Maven Central](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22typesafeconfig-guice%22). 7 | 8 | ```xml 9 | 10 | com.github.racc 11 | typesafeconfig-guice 12 | 0.1.0 13 | 14 | ``` 15 | 16 | ### Quickstart 17 | #### Step 1: 18 | Annotate configuration injection points (Constructors, Providers, Provider methods, Setter methods, Fields) with ```@TypesafeConfig("config.path.key")``` 19 | 20 | #### Step 2: 21 | Bootstrap your application with Guice and the TypesafeConfigModule. 22 | Configuration values annotated with `@TypesafeConfig` will be scanned for on the classpath, and then bound from your supplied Typesafe Config file. 23 | ```java 24 | Config config = ConfigFactory.load("config.conf"); 25 | Injector injector = Guice.createInjector( 26 | TypesafeConfigModule.fromConfigWithPackage(config, "com.my.package"); 27 | // ... Add your other modules here 28 | ); 29 | ``` 30 | 31 | #### Step 3: 32 | Profit! 33 | 34 | ### Features 35 | - Plain Old Java Objects which follow JavaBean conventions (zero-args constructor, getters and setters) can be injected. 36 | - Supports injections of `java.time.Duration` objects with config parameters defined as "10 seconds", "10 minutes" etc. 37 | - Supports injections of `com.typesafe.config.ConfigMemorySize` objects with config parameters defined as "512k" or "2M" etc. 38 | - Supports injections of `boolean`, where the matching config parameter can be "true", "false", "yes" or "no". 39 | - Supports injections of List types of primitives: `boolean`, `int`, `double`, `long`, `string`, `java.time.Duration`, `com.typesafe.config.ConfigMemorySize` 40 | - Supports injections of `java.util.Map` 41 | - Supports injections of POJO Lists. 42 | - Extensive test coverage 43 | 44 | AUTHOR 45 | ----------- 46 | Jason Then 47 | 48 | Any comments and feedback appreciated! 49 | 50 | LICENSE 51 | ----------- 52 | Copyright 2018 Jason Then 53 | 54 | Licensed under the Apache License, Version 2.0 (the "License"); 55 | you may not use this file except in compliance with the License. 56 | You may obtain a copy of the License at 57 | 58 | http://www.apache.org/licenses/LICENSE-2.0 59 | 60 | Unless required by applicable law or agreed to in writing, software 61 | distributed under the License is distributed on an "AS IS" BASIS, 62 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 63 | See the License for the specific language governing permissions and 64 | limitations under the License. 65 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'maven' 3 | apply plugin: 'signing' 4 | 5 | repositories { 6 | mavenCentral() 7 | } 8 | 9 | group = 'com.github.racc' 10 | archivesBaseName = "typesafeconfig-guice" 11 | version = '0.1.0' 12 | 13 | task javadocJar(type: Jar, dependsOn: javadoc) { 14 | classifier = 'javadoc' 15 | from 'build/docs/javadoc' 16 | } 17 | 18 | task sourcesJar(type: Jar) { 19 | from sourceSets.main.allSource 20 | classifier = 'sources' 21 | } 22 | 23 | artifacts { 24 | archives jar 25 | archives javadocJar 26 | archives sourcesJar 27 | } 28 | 29 | signing { 30 | sign configurations.archives 31 | } 32 | 33 | def uzer = hasProperty('ossrhUsername') ? ossrhUsername : System.getenv('ossrhUsername') 34 | def password = hasProperty('ossrhPassword') ? ossrhPassword : System.getenv('ossrhPassword') 35 | 36 | uploadArchives { 37 | repositories { 38 | mavenDeployer { 39 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 40 | 41 | repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { 42 | authentication(userName: uzer, password: password) 43 | } 44 | 45 | snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { 46 | authentication(userName: uzer, password: password) 47 | } 48 | 49 | pom.project { 50 | name 'TypesafeConfigGuice' 51 | packaging 'jar' 52 | description 'TypesafeConfigGuice allows TypesafeConfig values to be injected with Guice' 53 | url 'https://github.com/racc/typesafeconfig-guice' 54 | 55 | scm { 56 | url 'scm:git@github.com:racc/typesafeconfig-guice.git' 57 | connection 'scm:git@github.com:racc/typesafeconfig-guice.git' 58 | developerConnection 'scm:git@github.com:racc/typesafeconfig-guice.git' 59 | } 60 | 61 | licenses { 62 | license { 63 | name 'The Apache Software License, Version 2.0' 64 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 65 | distribution 'repo' 66 | } 67 | } 68 | 69 | developers { 70 | developer { 71 | id 'racc' 72 | name 'Jason Then' 73 | } 74 | } 75 | } 76 | } 77 | } 78 | } 79 | 80 | dependencies { 81 | compile 'com.typesafe:config:1.3.0' 82 | compile group: 'io.github.lukehutch', name: 'fast-classpath-scanner', version: '2.18.1' 83 | compile 'org.reflections:reflections:0.9.9' 84 | compile 'com.google.inject:guice:4.0:no_aop' 85 | 86 | testCompile 'junit:junit:4.12' 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/ConfigExtractor.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg; 2 | 3 | import com.typesafe.config.Config; 4 | 5 | /** 6 | * Extracts values from a particular {@link Config}. 7 | * 8 | * @author jason 9 | */ 10 | interface ConfigExtractor { 11 | 12 | /** 13 | * @param config the {@link Config} to extract from 14 | * @param path the {@link Config} path 15 | * @return the extracted value 16 | */ 17 | public Object extractValue(Config config, String path); 18 | 19 | /** 20 | * @return the types this {@link ConfigExtractor} will extract for. 21 | */ 22 | public Class[] getMatchingClasses(); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/ConfigExtractors.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg; 2 | 3 | import java.time.Duration; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.Optional; 7 | 8 | import com.typesafe.config.Config; 9 | import com.typesafe.config.ConfigList; 10 | import com.typesafe.config.ConfigMemorySize; 11 | import com.typesafe.config.ConfigObject; 12 | import com.typesafe.config.ConfigValue; 13 | 14 | /** 15 | * An {@link Enum} of {@link ConfigExtractor}s to extract values out of a {@link Config}. 16 | * 17 | * @author jason 18 | */ 19 | enum ConfigExtractors implements ConfigExtractor { 20 | 21 | BOOLEAN(boolean.class, Boolean.class) { 22 | @Override 23 | public Object extractValue(Config config, String path) { 24 | return config.getBoolean(path); 25 | } 26 | }, 27 | BYTE(byte.class, Byte.class) { 28 | @Override 29 | public Object extractValue(Config config, String path) { 30 | return (byte) config.getInt(path); 31 | } 32 | }, 33 | SHORT(short.class, Short.class) { 34 | @Override 35 | public Object extractValue(Config config, String path) { 36 | return (short) config.getInt(path); 37 | } 38 | }, 39 | INTEGER(int.class, Integer.class) { 40 | @Override 41 | public Object extractValue(Config config, String path) { 42 | return config.getInt(path); 43 | } 44 | }, 45 | LONG(long.class, Long.class) { 46 | @Override 47 | public Object extractValue(Config config, String path) { 48 | return config.getLong(path); 49 | } 50 | }, 51 | FLOAT(float.class, Float.class) { 52 | @Override 53 | public Object extractValue(Config config, String path) { 54 | return (float) config.getDouble(path); 55 | } 56 | }, 57 | DOUBLE(double.class, Double.class) { 58 | @Override 59 | public Object extractValue(Config config, String path) { 60 | return config.getDouble(path); 61 | } 62 | }, 63 | STRING(String.class) { 64 | @Override 65 | public Object extractValue(Config config, String path) { 66 | return config.getString(path); 67 | } 68 | }, 69 | ANY_REF(Object.class) { 70 | @Override 71 | public Object extractValue(Config config, String path) { 72 | return config.getAnyRef(path); 73 | } 74 | }, 75 | CONFIG(Config.class) { 76 | @Override 77 | public Object extractValue(Config config, String path) { 78 | return config.getConfig(path); 79 | } 80 | }, 81 | CONFIG_OBJECT(ConfigObject.class) { 82 | @Override 83 | public Object extractValue(Config config, String path) { 84 | return config.getObject(path); 85 | } 86 | }, 87 | CONFIG_VALUE(ConfigValue.class) { 88 | @Override 89 | public Object extractValue(Config config, String path) { 90 | return config.getValue(path); 91 | } 92 | }, 93 | CONFIG_LIST(ConfigList.class) { 94 | @Override 95 | public Object extractValue(Config config, String path) { 96 | return config.getList(path); 97 | } 98 | }, 99 | DURATION(Duration.class) { 100 | @Override 101 | public Object extractValue(Config config, String path) { 102 | return config.getDuration(path); 103 | } 104 | }, 105 | MEMORY_SIZE(ConfigMemorySize.class) { 106 | @Override 107 | public Object extractValue(Config config, String path) { 108 | return config.getMemorySize(path); 109 | } 110 | }; 111 | 112 | private final Class[] matchingClasses; 113 | private static final Map, ConfigExtractor> EXTRACTOR_MAP = new HashMap<>(); 114 | 115 | static { 116 | for (ConfigExtractor extractor : ConfigExtractors.values()) { 117 | for (Class clazz : extractor.getMatchingClasses()) { 118 | EXTRACTOR_MAP.put(clazz, extractor); 119 | } 120 | } 121 | } 122 | 123 | private ConfigExtractors(Class... matchingClasses) { 124 | this.matchingClasses = matchingClasses; 125 | } 126 | 127 | public Class[] getMatchingClasses() { 128 | return matchingClasses; 129 | } 130 | 131 | /** 132 | * @param config the {@link Config} to extract from 133 | * @param paramClass the class 134 | * @param path 135 | * @return the extracted config value, or empty if there was no matching {@link ConfigExtractor}. 136 | */ 137 | static Optional extractConfigValue(Config config, Class paramClass, String path) { 138 | if (EXTRACTOR_MAP.containsKey(paramClass)) { 139 | return Optional.of(EXTRACTOR_MAP.get(paramClass).extractValue(config, path)); 140 | } else { 141 | return Optional.empty(); 142 | } 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/ListExtractor.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg; 2 | 3 | import java.lang.reflect.Type; 4 | import java.util.List; 5 | 6 | import com.typesafe.config.Config; 7 | 8 | /** 9 | * Extracts {@link List} values from a particular {@link Config}. 10 | * 11 | * @author jason 12 | */ 13 | public interface ListExtractor { 14 | /** 15 | * @param config the {@link Config} to extract from 16 | * @param path the {@link Config} path 17 | * @return the extracted list value 18 | */ 19 | public List extractListValue(Config config, String path); 20 | 21 | /** 22 | * @return the {@link List} type this {@link ListExtractor} extracts. 23 | */ 24 | public Type getMatchingParameterizedType(); 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/ListExtractors.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg; 2 | 3 | import java.lang.reflect.Type; 4 | import java.time.Duration; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Optional; 9 | 10 | import com.typesafe.config.Config; 11 | import com.typesafe.config.ConfigMemorySize; 12 | import com.typesafe.config.ConfigObject; 13 | 14 | public enum ListExtractors implements ListExtractor { 15 | BOOLEAN(Boolean.class) { 16 | @Override 17 | public List extractListValue(Config config, String path) { 18 | return config.getBooleanList(path); 19 | } 20 | }, 21 | INTEGER(Integer.class) { 22 | @Override 23 | public List extractListValue(Config config, String path) { 24 | return config.getIntList(path); 25 | } 26 | }, 27 | DOUBLE(Double.class) { 28 | @Override 29 | public List extractListValue(Config config, String path) { 30 | return config.getDoubleList(path); 31 | } 32 | }, 33 | LONG(Long.class) { 34 | @Override 35 | public List extractListValue(Config config, String path) { 36 | return config.getLongList(path); 37 | } 38 | }, 39 | STRING(String.class) { 40 | @Override 41 | public List extractListValue(Config config, String path) { 42 | return config.getStringList(path); 43 | } 44 | }, 45 | DURATION(Duration.class) { 46 | @Override 47 | public List extractListValue(Config config, String path) { 48 | return config.getDurationList(path); 49 | } 50 | }, 51 | MEMORY_SIZE(ConfigMemorySize.class) { 52 | @Override 53 | public List extractListValue(Config config, String path) { 54 | return config.getMemorySizeList(path); 55 | } 56 | }, 57 | OBJECT(Object.class) { 58 | @Override 59 | public List extractListValue(Config config, String path) { 60 | return config.getAnyRefList(path); 61 | } 62 | }, 63 | CONFIG(Config.class) { 64 | @Override 65 | public List extractListValue(Config config, String path) { 66 | return config.getConfigList(path); 67 | } 68 | }, 69 | CONFIG_OBJECT(ConfigObject.class) { 70 | @Override 71 | public List extractListValue(Config config, String path) { 72 | return config.getObjectList(path); 73 | } 74 | }, 75 | CONFIG_VALUE(ConfigObject.class) { 76 | @Override 77 | public List extractListValue(Config config, String path) { 78 | return config.getList(path); 79 | } 80 | } 81 | ; 82 | 83 | private final Class parameterizedTypeClass; 84 | private static final Map EXTRACTOR_MAP = new HashMap<>(); 85 | 86 | static { 87 | for (ListExtractor extractor : ListExtractors.values()) { 88 | EXTRACTOR_MAP.put(extractor.getMatchingParameterizedType(), extractor); 89 | } 90 | } 91 | 92 | private ListExtractors(Class parameterizedTypeClass) { 93 | this.parameterizedTypeClass = parameterizedTypeClass; 94 | } 95 | 96 | @Override 97 | public Type getMatchingParameterizedType() { 98 | return parameterizedTypeClass; 99 | } 100 | 101 | static Optional> extractConfigListValue(Config config, Type listType, String path) { 102 | if (EXTRACTOR_MAP.containsKey(listType)) { 103 | return Optional.of(EXTRACTOR_MAP.get(listType).extractListValue(config, path)); 104 | } else { 105 | return Optional.empty(); 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/Reflector.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg; 2 | 3 | import java.lang.reflect.Constructor; 4 | import java.lang.reflect.Field; 5 | import java.lang.reflect.Method; 6 | import java.util.Set; 7 | 8 | public interface Reflector { 9 | Set getConstructorsWithAnyParamAnnotated(Class clazz); 10 | Set getMethodsWithAnyParamAnnotated(Class clazz); 11 | Set getFieldsAnnotatedWith(Class clazz); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/TypesafeConfig.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg; 2 | 3 | import static java.lang.annotation.ElementType.FIELD; 4 | import static java.lang.annotation.ElementType.METHOD; 5 | import static java.lang.annotation.ElementType.PARAMETER; 6 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 7 | 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.Target; 10 | 11 | import com.google.inject.BindingAnnotation; 12 | import com.typesafe.config.Config; 13 | 14 | /** 15 | * A {@link BindingAnnotation} Used to annotate fields, constructors and provider methods. 16 | * 17 | * @author jason 18 | */ 19 | @BindingAnnotation 20 | @Target({ FIELD, PARAMETER, METHOD }) 21 | @Retention(RUNTIME) 22 | public @interface TypesafeConfig { 23 | 24 | /** 25 | * @return the {@link Config} path to the configuration value. 26 | */ 27 | public String value(); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/TypesafeConfigImpl.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg; 2 | 3 | import java.io.Serializable; 4 | import java.lang.annotation.Annotation; 5 | 6 | /** 7 | * Inspired in Guice's NamedImpl. 8 | */ 9 | public class TypesafeConfigImpl implements TypesafeConfig, Serializable { 10 | private final String value; 11 | 12 | public TypesafeConfigImpl(String forKeypath) { 13 | this.value = forKeypath; 14 | } 15 | 16 | @Override 17 | public String value() { 18 | return this.value; 19 | } 20 | 21 | @Override 22 | public int hashCode() { 23 | return (127 * "value".hashCode()) ^ value.hashCode(); 24 | } 25 | 26 | @Override 27 | public boolean equals(Object o) { 28 | if (!(o instanceof TypesafeConfig)) { 29 | return false; 30 | } 31 | 32 | TypesafeConfig other = (TypesafeConfig) o; 33 | return value.equals(other.value()); 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "@" + TypesafeConfig.class.getName() + "(value=" + value + ")"; 39 | } 40 | 41 | @Override 42 | public Class annotationType() { 43 | return TypesafeConfig.class; 44 | } 45 | 46 | private static final long serialVersionUID = 0; 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/TypesafeConfigModule.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg; 2 | 3 | import java.lang.reflect.Constructor; 4 | import java.lang.reflect.Field; 5 | import java.lang.reflect.Method; 6 | import java.lang.reflect.Parameter; 7 | import java.lang.reflect.ParameterizedType; 8 | import java.lang.reflect.Type; 9 | import java.util.HashSet; 10 | import java.util.List; 11 | import java.util.Map; 12 | import java.util.Optional; 13 | import java.util.Set; 14 | import java.util.stream.Collectors; 15 | 16 | import com.github.racc.tscg.reflectors.fastclasspathscanner.FastClasspathScanningReflector; 17 | import com.github.racc.tscg.reflectors.reflections.ReflectionsReflector; 18 | import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner; 19 | import org.reflections.Reflections; 20 | import org.reflections.scanners.FieldAnnotationsScanner; 21 | import org.reflections.scanners.MethodAnnotationsScanner; 22 | import org.reflections.scanners.MethodParameterScanner; 23 | import org.reflections.scanners.TypeAnnotationsScanner; 24 | import org.reflections.util.ClasspathHelper; 25 | import org.reflections.util.ConfigurationBuilder; 26 | import org.reflections.util.FilterBuilder; 27 | 28 | import com.google.inject.AbstractModule; 29 | import com.google.inject.Guice; 30 | import com.google.inject.Key; 31 | import com.google.inject.Module; 32 | import com.typesafe.config.Config; 33 | import com.typesafe.config.ConfigBeanFactory; 34 | import com.typesafe.config.ConfigObject; 35 | import com.typesafe.config.ConfigValue; 36 | import com.typesafe.config.ConfigValueType; 37 | 38 | /** 39 | * Include this {@link Module} in your {@link Guice} bootstrapping to Automagically 40 | * get bindings to your {@link TypesafeConfig} annotated configuration parameters. 41 | * 42 | * @author jason 43 | */ 44 | public class TypesafeConfigModule extends AbstractModule { 45 | 46 | private final Config config; 47 | private final Reflector reflections; 48 | private final Set boundAnnotations; 49 | 50 | private TypesafeConfigModule(Config config, Reflector reflections) { 51 | this.config = config; 52 | this.reflections = reflections; 53 | this.boundAnnotations = new HashSet(); 54 | } 55 | 56 | /** 57 | * Essentially calls fromConfigWithPackage with a package name of "" to construct a TypesafeConfigModule. 58 | * 59 | * @param config the config to use. 60 | * @return The constructed TypesafeConfigModule. 61 | * @deprecated this is deprecated as there is a known issue with scanning the classpath for annotations 62 | * in this mode, when running a packaged jar file. 63 | * use {@link #fromConfigWithPackage(Config, String)} instead and limit the packages to scan. 64 | */ 65 | @Deprecated 66 | public static TypesafeConfigModule fromConfig(Config config) { 67 | return fromConfigWithPackage(config, ""); 68 | } 69 | 70 | /** 71 | * Scans the specified packages for annotated classes, and applies Config values to them. 72 | * 73 | * @param config the Config to derive values from 74 | * @param packageNamePrefix the prefix to limit scanning to - e.g. "com.github" 75 | * @return The constructed TypesafeConfigModule. 76 | */ 77 | public static TypesafeConfigModule fromConfigWithPackage(Config config, String packageNamePrefix) { 78 | Reflections reflections = createPackageScanningReflections(packageNamePrefix); 79 | return fromConfigWithReflections(config, reflections); 80 | } 81 | 82 | /** 83 | * Scans the specified packages for annotated classes, and applies Config values to them. 84 | * 85 | * @param config the Config to derive values from 86 | * @param reflections the reflections object to use 87 | * @return The constructed TypesafeConfigModule. 88 | */ 89 | public static TypesafeConfigModule fromConfigWithReflections(Config config, Reflections reflections) { 90 | return new TypesafeConfigModule(config, new ReflectionsReflector(reflections)); 91 | } 92 | 93 | /** 94 | * Scans the specified packages for annotated classes, and applies Config values to them. 95 | * 96 | * @param config the Config to derive values from 97 | * @return The constructed TypesafeConfigModule. 98 | */ 99 | public static TypesafeConfigModule fromConfigUsingClasspathScanner(Config config, String ...scannerSpec) { 100 | return new TypesafeConfigModule(config, new FastClasspathScanningReflector(scannerSpec)); 101 | } 102 | 103 | public static Reflections createPackageScanningReflections(String packageNamePrefix){ 104 | ConfigurationBuilder configBuilder = 105 | new ConfigurationBuilder() 106 | .filterInputsBy(new FilterBuilder().includePackage(packageNamePrefix)) 107 | .setUrls(ClasspathHelper.forPackage(packageNamePrefix)) 108 | .setScanners( 109 | new TypeAnnotationsScanner(), 110 | new MethodParameterScanner(), 111 | new MethodAnnotationsScanner(), 112 | new FieldAnnotationsScanner() 113 | ); 114 | return new Reflections(configBuilder); 115 | } 116 | 117 | @SuppressWarnings({ "rawtypes"}) 118 | @Override 119 | protected void configure() { 120 | Set annotatedConstructors = reflections.getConstructorsWithAnyParamAnnotated(TypesafeConfig.class); 121 | for (Constructor c : annotatedConstructors) { 122 | Parameter[] params = c.getParameters(); 123 | bindParameters(params); 124 | } 125 | 126 | Set annotatedMethods = reflections.getMethodsWithAnyParamAnnotated(TypesafeConfig.class); 127 | for (Method m : annotatedMethods) { 128 | Parameter[] params = m.getParameters(); 129 | bindParameters(params); 130 | } 131 | 132 | Set annotatedFields = reflections.getFieldsAnnotatedWith(TypesafeConfig.class); 133 | for (Field f : annotatedFields) { 134 | TypesafeConfig annotation = f.getAnnotation(TypesafeConfig.class); 135 | bindValue(f.getType(), f.getAnnotatedType().getType(), annotation); 136 | } 137 | } 138 | 139 | private void bindParameters(Parameter[] params) { 140 | for (Parameter p : params) { 141 | if (p.isAnnotationPresent(TypesafeConfig.class)) { 142 | TypesafeConfig annotation = p.getAnnotation(TypesafeConfig.class); 143 | bindValue(p.getType(), p.getAnnotatedType().getType(), annotation); 144 | } 145 | } 146 | } 147 | 148 | private void bindValue(Class paramClass, Type paramType, TypesafeConfig annotation) { 149 | // Prevents multiple bindings on the same annotation 150 | if (!boundAnnotations.contains(annotation)) { 151 | @SuppressWarnings("unchecked") 152 | Key key = (Key) Key.get(paramType, annotation); 153 | String configPath = annotation.value(); 154 | Object configValue = getConfigValue(paramClass, paramType, configPath); 155 | bind(key).toInstance(configValue); 156 | boundAnnotations.add(annotation); 157 | } 158 | } 159 | 160 | @SuppressWarnings({ "unchecked", "rawtypes" }) 161 | private Object getConfigValue(Class paramClass, Type paramType, String path) { 162 | Optional extractedValue = ConfigExtractors.extractConfigValue(config, paramClass, path); 163 | if (extractedValue.isPresent()) { 164 | return extractedValue.get(); 165 | } 166 | 167 | ConfigValue configValue = config.getValue(path); 168 | ConfigValueType valueType = configValue.valueType(); 169 | if (valueType.equals(ConfigValueType.OBJECT) && Map.class.isAssignableFrom(paramClass)) { 170 | ConfigObject object = config.getObject(path); 171 | return object.unwrapped(); 172 | } else if (valueType.equals(ConfigValueType.OBJECT)) { 173 | Object bean = ConfigBeanFactory.create(config.getConfig(path), paramClass); 174 | return bean; 175 | } else if (valueType.equals(ConfigValueType.LIST) && List.class.isAssignableFrom(paramClass)) { 176 | Type listType = ((ParameterizedType) paramType).getActualTypeArguments()[0]; 177 | 178 | Optional> extractedListValue = 179 | ListExtractors.extractConfigListValue(config, listType, path); 180 | 181 | if (extractedListValue.isPresent()) { 182 | return extractedListValue.get(); 183 | } else { 184 | List configList = config.getConfigList(path); 185 | return configList.stream() 186 | .map(cfg -> { 187 | Object created = ConfigBeanFactory.create(cfg, (Class) listType); 188 | return created; 189 | }) 190 | .collect(Collectors.toList()); 191 | } 192 | } 193 | 194 | throw new RuntimeException("Cannot obtain config value for " + paramType + " at path: " + path); 195 | } 196 | } -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/TypesafeConfigs.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg; 2 | 3 | public class TypesafeConfigs { 4 | 5 | public static TypesafeConfig forKeypath(String keypath) { 6 | return new TypesafeConfigImpl(keypath); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/reflectors/fastclasspathscanner/FastClasspathScanningReflector.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg.reflectors.fastclasspathscanner; 2 | 3 | import com.github.racc.tscg.Reflector; 4 | import com.github.racc.tscg.TypesafeConfig; 5 | import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner; 6 | import io.github.lukehutch.fastclasspathscanner.scanner.*; 7 | 8 | import java.lang.reflect.Constructor; 9 | import java.lang.reflect.Field; 10 | import java.lang.reflect.Method; 11 | import java.util.*; 12 | 13 | public class FastClasspathScanningReflector implements Reflector { 14 | 15 | private final ScanResult scanResult; 16 | private final FastClasspathScanner scanner; 17 | 18 | public FastClasspathScanningReflector(String ...scannerSpec) { 19 | this.scanner = new FastClasspathScanner(scannerSpec).enableMethodInfo().enableFieldInfo().ignoreFieldVisibility().ignoreMethodVisibility(); 20 | this.scanResult = scanner.scan(); 21 | } 22 | 23 | @Override 24 | public Set getConstructorsWithAnyParamAnnotated(Class clazz) { 25 | Map classInfos = scanResult.getClassNameToClassInfo(); 26 | Set methodIdentifiers = new HashSet<>(); 27 | for (ClassInfo classInfo : classInfos.values()){ 28 | for (MethodInfo methodInfo : classInfo.getMethodAndConstructorInfo()) { 29 | if (methodInfo.isConstructor()) { 30 | AnnotationInfo[][] parametersAnnotationInfo = methodInfo.getParameterAnnotationInfo(); 31 | if (parametersAnnotationInfo != null) 32 | for (AnnotationInfo[] oneParametersAnnotationInfo: parametersAnnotationInfo) { 33 | for (AnnotationInfo ai : oneParametersAnnotationInfo) { 34 | if (ai.getAnnotationType() == clazz) { 35 | methodIdentifiers.add(new MethodIdentifier(methodInfo.getClassName(), methodInfo.getMethodName(), methodInfo.getParameterTypeSignatures())); 36 | } 37 | } 38 | } 39 | } 40 | } 41 | } 42 | Set constructors = new HashSet<>(methodIdentifiers.size()); 43 | for (MethodIdentifier methodIdentifier : methodIdentifiers) 44 | { 45 | Class classe = scanResult.classNameToClassRef(methodIdentifier.getClassName()); 46 | for (Constructor constructor : classe.getConstructors()) { 47 | if (methodIdentifier.matchesConstructor(classe, constructor, scanResult)){ 48 | constructors.add(constructor); 49 | } 50 | } 51 | 52 | } 53 | return constructors; 54 | } 55 | 56 | @Override 57 | public Set getMethodsWithAnyParamAnnotated(Class clazz) { 58 | Map classInfos = scanResult.getClassNameToClassInfo(); 59 | Set methodIdentifiers = new HashSet<>(); 60 | for (ClassInfo classInfo : classInfos.values()){ 61 | for (MethodInfo methodInfo : classInfo.getMethodAndConstructorInfo()) { 62 | AnnotationInfo[][] parametersAnnotationInfo = methodInfo.getParameterAnnotationInfo(); 63 | if (parametersAnnotationInfo != null) 64 | for (AnnotationInfo[] oneParametersAnnotationInfo: parametersAnnotationInfo) { 65 | for (AnnotationInfo ai : oneParametersAnnotationInfo) { 66 | if (ai.getAnnotationType() == clazz) { 67 | methodIdentifiers.add(new MethodIdentifier(methodInfo.getClassName(), methodInfo.getMethodName(), methodInfo.getParameterTypeSignatures())); 68 | } 69 | } 70 | } 71 | } 72 | } 73 | Set methods = new HashSet<>(methodIdentifiers.size()); 74 | for (MethodIdentifier methodIdentifier : methodIdentifiers) { 75 | Class classe = scanResult.classNameToClassRef(methodIdentifier.getClassName()); 76 | for (Method method : classe.getDeclaredMethods()) { 77 | if (methodIdentifier.matchesMethod(classe, method, scanResult)){ 78 | methods.add(method); 79 | } 80 | } 81 | } 82 | return methods; 83 | } 84 | 85 | @Override 86 | public Set getFieldsAnnotatedWith(Class clazz) { 87 | Map classInfos = scanResult.getClassNameToClassInfo(); 88 | Set fieldIdentifiers = new HashSet<>(); 89 | for (ClassInfo classInfo : classInfos.values()){ 90 | for (FieldInfo fieldInfo : classInfo.getFieldInfo()) { 91 | List fieldsAnnotationInfos = fieldInfo.getAnnotationInfo(); 92 | for (AnnotationInfo oneFieldAnnotation: fieldsAnnotationInfos) { 93 | if (oneFieldAnnotation.getAnnotationType() == clazz) { 94 | fieldIdentifiers.add(new FieldIdentifier(fieldInfo.getClassName(), fieldInfo.getFieldName(), fieldInfo.getType())); 95 | } 96 | 97 | } 98 | } 99 | } 100 | Set fields = new HashSet<>(fieldIdentifiers.size()); 101 | for (FieldIdentifier fieldIdentifier : fieldIdentifiers) { 102 | Class classe = scanResult.classNameToClassRef(fieldIdentifier.getClassName()); 103 | for (Field field : classe.getDeclaredFields()) { 104 | if (fieldIdentifier.matchesField(classe, field)){ 105 | fields.add(field); 106 | } 107 | } 108 | } 109 | return fields; 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/reflectors/fastclasspathscanner/FieldIdentifier.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg.reflectors.fastclasspathscanner; 2 | 3 | import java.lang.reflect.Field; 4 | 5 | public class FieldIdentifier { 6 | private final String className; 7 | private final String fieldName; 8 | private final Class fieldType; 9 | 10 | public FieldIdentifier(String className, String fieldName, Class fieldType) { 11 | this.className = className; 12 | this.fieldName = fieldName; 13 | this.fieldType = fieldType; 14 | } 15 | 16 | public String getClassName() { 17 | return className; 18 | } 19 | 20 | public String getFieldName() { 21 | return fieldName; 22 | } 23 | 24 | public Class getFieldType() { 25 | return fieldType; 26 | } 27 | 28 | public boolean matchesField(Class clazz, Field field) { 29 | return (clazz.getCanonicalName().equals(className) && field.getName().equals(fieldName) && field.getType() == fieldType); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/reflectors/fastclasspathscanner/MethodIdentifier.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg.reflectors.fastclasspathscanner; 2 | 3 | import io.github.lukehutch.fastclasspathscanner.scanner.ScanResult; 4 | import io.github.lukehutch.fastclasspathscanner.typesignature.TypeSignature; 5 | 6 | import java.lang.reflect.Constructor; 7 | import java.lang.reflect.Method; 8 | 9 | public class MethodIdentifier { 10 | private final String className; 11 | private final String methodName; 12 | private final TypeSignature[] parameterTypeSignature; 13 | 14 | public MethodIdentifier(String className, String methodName, TypeSignature[] parameterTypeSignature) { 15 | this.className = className; 16 | this.methodName = methodName; 17 | this.parameterTypeSignature = parameterTypeSignature; 18 | } 19 | 20 | public String getClassName() { 21 | return className; 22 | } 23 | 24 | public String getMethodName() { 25 | return methodName; 26 | } 27 | 28 | public TypeSignature[] getParameterTypeSignature() { 29 | return parameterTypeSignature; 30 | } 31 | 32 | public boolean matchesMethod(Class clazz, Method method, ScanResult scanResult) { 33 | if (clazz.getName().equals(className) 34 | && method.getName().equals(methodName) 35 | && method.getParameterCount() == parameterTypeSignature.length) 36 | { 37 | boolean paramsMatch = true; 38 | for (int i = 0; i < parameterTypeSignature.length; i++) { 39 | paramsMatch = paramsMatch && parameterTypeSignature[i].instantiate(scanResult) == method.getParameters()[i].getType(); 40 | } 41 | return paramsMatch; 42 | } 43 | return false; 44 | } 45 | 46 | public boolean matchesConstructor(Class clazz, Constructor constructor, ScanResult scanResult) { 47 | if (clazz.getCanonicalName().equals(className) && methodName.equals("") && constructor.getParameterCount() == parameterTypeSignature.length) 48 | { 49 | boolean paramsMatch = true; 50 | for (int i = 0; i < parameterTypeSignature.length; i++) { 51 | paramsMatch = paramsMatch && parameterTypeSignature[i].instantiate(scanResult) == constructor.getParameters()[i].getType(); 52 | } 53 | return paramsMatch; 54 | } 55 | return false; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/github/racc/tscg/reflectors/reflections/ReflectionsReflector.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg.reflectors.reflections; 2 | 3 | import com.github.racc.tscg.Reflector; 4 | import org.reflections.Reflections; 5 | 6 | import java.lang.reflect.Constructor; 7 | import java.lang.reflect.Field; 8 | import java.lang.reflect.Method; 9 | import java.util.Set; 10 | 11 | public class ReflectionsReflector implements Reflector { 12 | 13 | private final Reflections delegate; 14 | 15 | public ReflectionsReflector(Reflections delegate) { 16 | this.delegate = delegate; 17 | } 18 | 19 | @Override 20 | public Set getConstructorsWithAnyParamAnnotated(Class clazz) { 21 | return delegate.getConstructorsWithAnyParamAnnotated(clazz); 22 | } 23 | 24 | @Override 25 | public Set getMethodsWithAnyParamAnnotated(Class clazz) { 26 | return delegate.getMethodsWithAnyParamAnnotated(clazz); 27 | } 28 | 29 | @Override 30 | public Set getFieldsAnnotatedWith(Class clazz) { 31 | return delegate.getFieldsAnnotatedWith(clazz); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/com/github/racc/tscg/TypesafeConfigModuleTest.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg; 2 | 3 | 4 | import java.time.Duration; 5 | import java.time.temporal.ChronoUnit; 6 | import java.util.Arrays; 7 | import java.util.LinkedList; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | import com.google.inject.*; 12 | import org.junit.Assert; 13 | import org.junit.Before; 14 | import org.junit.Test; 15 | 16 | import com.github.racc.tscg.test.ConstructorInjectedPojo; 17 | import com.github.racc.tscg.test.FieldInjectedPojo; 18 | import com.github.racc.tscg.test.MethodInjectedPojo; 19 | import com.github.racc.tscg.test.NestedPojo; 20 | import com.github.racc.tscg.test.ProvidedPojo; 21 | import com.github.racc.tscg.test.TestPojo; 22 | import com.typesafe.config.Config; 23 | import com.typesafe.config.ConfigFactory; 24 | import com.typesafe.config.ConfigMemorySize; 25 | 26 | public class TypesafeConfigModuleTest { 27 | 28 | private List injectors = new LinkedList<>(); 29 | 30 | private Module createTestModule() { 31 | 32 | Module testModule = new AbstractModule() { 33 | @Override 34 | protected void configure() { 35 | bind(ConstructorInjectedPojo.class).asEagerSingleton(); 36 | bind(FieldInjectedPojo.class).asEagerSingleton(); 37 | bind(MethodInjectedPojo.class).asEagerSingleton(); 38 | } 39 | 40 | @Provides 41 | @Singleton 42 | ProvidedPojo providePojo( 43 | @TypesafeConfig("provided.boolean") boolean testBoolean, 44 | @TypesafeConfig("provided.yesBoolean") boolean testYesBoolean, 45 | @TypesafeConfig("provided.long") long testLong, 46 | @TypesafeConfig("provided.byte") byte testByte, 47 | @TypesafeConfig("provided.int") int testInt, 48 | @TypesafeConfig("provided.double") double testDouble, 49 | @TypesafeConfig("provided.float") float testFloat, 50 | @TypesafeConfig("provided.string") String testString, 51 | @TypesafeConfig("provided.list.boolean") List testListOfBoolean, 52 | @TypesafeConfig("provided.list.integer") List testListOfInteger, 53 | @TypesafeConfig("provided.list.double") List testListOfDouble, 54 | @TypesafeConfig("provided.list.long") List testListOfLong, 55 | @TypesafeConfig("provided.list.string") List testListOfString, 56 | @TypesafeConfig("provided.list.duration") List testListOfDuration, 57 | @TypesafeConfig("provided.list.size") List testListOfSize, 58 | @TypesafeConfig("provided.list.nested") List testListOfNested, 59 | @TypesafeConfig("provided.duration") Duration testDuration, 60 | @TypesafeConfig("provided.size") ConfigMemorySize testSize, 61 | @TypesafeConfig("provided.map") Map testMap, 62 | @TypesafeConfig("provided.map.intkey") Map testMapIntkey, 63 | @TypesafeConfig("provided.nested") NestedPojo testNestedPojo 64 | ) { 65 | return new ProvidedPojo(testBoolean, testYesBoolean, testLong, testByte, testInt, testDouble, testFloat, testString, testListOfBoolean, testListOfInteger, testListOfDouble, testListOfLong, testListOfString, testListOfDuration, testListOfSize, testListOfNested, testDuration, testSize, testMap, testMapIntkey, testNestedPojo); 66 | } 67 | 68 | }; 69 | return testModule; 70 | } 71 | @Before 72 | public void setup() { 73 | Config testConf = ConfigFactory.load("conf/test.conf"); 74 | 75 | 76 | injectors.add(Guice.createInjector( 77 | TypesafeConfigModule.fromConfigWithPackage(testConf, "com.github.racc"), 78 | createTestModule() 79 | )); 80 | injectors.add(Guice.createInjector( 81 | TypesafeConfigModule.fromConfigUsingClasspathScanner(testConf, "com.github.racc"), 82 | createTestModule() 83 | )); 84 | } 85 | 86 | @Test 87 | public void canInjectPojoViaConstructor() { 88 | for (Injector injector : injectors) { 89 | ConstructorInjectedPojo pojo = injector.getInstance(ConstructorInjectedPojo.class); 90 | assertPojoIsCorrect(pojo); 91 | } 92 | } 93 | 94 | @Test 95 | public void canInjectPojoViaFields() { 96 | for (Injector injector : injectors) { 97 | FieldInjectedPojo pojo = injector.getInstance(FieldInjectedPojo.class); 98 | assertPojoIsCorrect(pojo); 99 | } 100 | } 101 | 102 | @Test 103 | public void canInjectPojoViaMethods() { 104 | for (Injector injector : injectors) { 105 | MethodInjectedPojo pojo = injector.getInstance(MethodInjectedPojo.class); 106 | assertPojoIsCorrect(pojo); 107 | } 108 | } 109 | 110 | @Test 111 | public void canGetProvidedPojo() { 112 | for (Injector injector : injectors) { 113 | ProvidedPojo pojo = injector.getInstance(ProvidedPojo.class); 114 | assertPojoIsCorrect(pojo); 115 | } 116 | } 117 | 118 | @Test 119 | public void canGetFromInjector() { 120 | for (Injector injector : injectors) { 121 | Assert.assertTrue(injector.getInstance(Key.get(Boolean.class, TypesafeConfigs.forKeypath("provided.boolean")))); 122 | Assert.assertTrue(injector.getInstance(Key.get(Boolean.class, TypesafeConfigs.forKeypath("provided.yesBoolean")))); 123 | Assert.assertEquals(12345679123L, (long)injector.getInstance(Key.get(Long.class, TypesafeConfigs.forKeypath("provided.long")))); 124 | Assert.assertEquals(1, (int)injector.getInstance(Key.get(Integer.class, TypesafeConfigs.forKeypath("provided.int")))); 125 | Assert.assertEquals(123, (byte)injector.getInstance(Key.get(Byte.class, TypesafeConfigs.forKeypath("provided.byte")))); 126 | Assert.assertEquals(2.0, (float)injector.getInstance(Key.get(Float.class, TypesafeConfigs.forKeypath("provided.float"))), 0.001); 127 | Assert.assertEquals(2.0d, (double)injector.getInstance(Key.get(Double.class, TypesafeConfigs.forKeypath("provided.double"))), 0.001d); 128 | Assert.assertEquals("test", injector.getInstance(Key.get(String.class, TypesafeConfigs.forKeypath("provided.string")))); 129 | Assert.assertEquals(Duration.of(10, ChronoUnit.SECONDS), injector.getInstance(Key.get(Duration.class, TypesafeConfigs.forKeypath("provided.duration")))); 130 | Assert.assertEquals(ConfigMemorySize.ofBytes(524288), injector.getInstance(Key.get(ConfigMemorySize.class, TypesafeConfigs.forKeypath("provided.size")))); 131 | 132 | NestedPojo nestedListPojo = injector.getInstance(Key.get(new TypeLiteral>(){}, TypesafeConfigs.forKeypath("provided.list.nested"))).get(0); 133 | Assert.assertEquals(3, nestedListPojo.getNestInt()); 134 | 135 | Map testMap = injector.getInstance(Key.get(new TypeLiteral>(){}, TypesafeConfigs.forKeypath("provided.map"))); 136 | Assert.assertEquals(1, testMap.get("one").intValue()); 137 | 138 | Map testMapIntkey = injector.getInstance(Key.get(new TypeLiteral>(){}, TypesafeConfigs.forKeypath("provided.map.intkey"))); 139 | Assert.assertEquals("one", testMapIntkey.get("1")); 140 | 141 | Assert.assertEquals(Arrays.asList(true, false, true), injector.getInstance(Key.get(new TypeLiteral>(){}, TypesafeConfigs.forKeypath("provided.list.boolean")))); 142 | Assert.assertEquals(Arrays.asList(1, 2, 3), injector.getInstance(Key.get(new TypeLiteral>(){}, TypesafeConfigs.forKeypath("provided.list.integer")))); 143 | Assert.assertEquals(Arrays.asList(1.1, 2.2, 3.3), injector.getInstance(Key.get(new TypeLiteral>(){}, TypesafeConfigs.forKeypath("provided.list.double")))); 144 | Assert.assertEquals(Arrays.asList(12345679121L, 12345679122L, 12345679123L), injector.getInstance(Key.get(new TypeLiteral>(){}, TypesafeConfigs.forKeypath("provided.list.long")))); 145 | Assert.assertEquals(Arrays.asList("a", "b", "c"), injector.getInstance(Key.get(new TypeLiteral>(){}, TypesafeConfigs.forKeypath("provided.list.string")))); 146 | Assert.assertEquals(Arrays.asList(Duration.of(1, ChronoUnit.SECONDS), Duration.of(2, ChronoUnit.SECONDS), Duration.of(3, ChronoUnit.SECONDS)), injector.getInstance(Key.get(new TypeLiteral>(){}, TypesafeConfigs.forKeypath("provided.list.duration")))); 147 | Assert.assertEquals(Arrays.asList(ConfigMemorySize.ofBytes(524288), ConfigMemorySize.ofBytes(1048576), ConfigMemorySize.ofBytes(1073741824)), injector.getInstance(Key.get(new TypeLiteral>(){}, TypesafeConfigs.forKeypath("provided.list.size")))); 148 | } 149 | } 150 | 151 | private void assertPojoIsCorrect(TestPojo pojo) { 152 | Assert.assertTrue(pojo.isTestBoolean()); 153 | Assert.assertTrue(pojo.isTestYesBoolean()); 154 | Assert.assertEquals(12345679123l, pojo.getTestLong()); 155 | Assert.assertEquals(1, pojo.getTestInt()); 156 | Assert.assertEquals(123, pojo.getTestByte()); 157 | Assert.assertEquals(2.0, pojo.getTestDouble(), 0.001); 158 | Assert.assertEquals(2.0f, pojo.getTestFloat(), 0.001); 159 | Assert.assertEquals("test", pojo.getTestString()); 160 | Assert.assertEquals(Duration.of(10, ChronoUnit.SECONDS), pojo.getTestDuration()); 161 | Assert.assertEquals(ConfigMemorySize.ofBytes(524288), pojo.getTestSize()); 162 | 163 | NestedPojo nestedListPojo = pojo.getTestListOfNested().get(0); 164 | Assert.assertEquals(3, nestedListPojo.getNestInt()); 165 | 166 | Map testMap = pojo.getTestMap(); 167 | Assert.assertEquals(1, testMap.get("one").intValue()); 168 | 169 | Map testMapIntkey = pojo.getTestMapIntkey(); 170 | Assert.assertEquals("one", testMapIntkey.get("1")); 171 | 172 | Assert.assertEquals(Arrays.asList(true, false, true), pojo.getTestListOfBoolean()); 173 | Assert.assertEquals(Arrays.asList(1, 2, 3), pojo.getTestListOfInteger()); 174 | Assert.assertEquals(Arrays.asList(1.1, 2.2, 3.3), pojo.getTestListOfDouble()); 175 | Assert.assertEquals(Arrays.asList(12345679121L, 12345679122L, 12345679123L), pojo.getTestListOfLong()); 176 | Assert.assertEquals(Arrays.asList("a", "b", "c"), pojo.getTestListOfString()); 177 | Assert.assertEquals(Arrays.asList(Duration.of(1, ChronoUnit.SECONDS), Duration.of(2, ChronoUnit.SECONDS), Duration.of(3, ChronoUnit.SECONDS)), pojo.getTestListOfDuration()); 178 | Assert.assertEquals(Arrays.asList(ConfigMemorySize.ofBytes(524288), ConfigMemorySize.ofBytes(1048576), ConfigMemorySize.ofBytes(1073741824)), pojo.getTestListOfSize()); 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /src/test/java/com/github/racc/tscg/test/ConstructorInjectedPojo.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg.test; 2 | 3 | import java.time.Duration; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import com.github.racc.tscg.TypesafeConfig; 8 | import com.google.inject.Inject; 9 | import com.google.inject.Singleton; 10 | import com.typesafe.config.ConfigMemorySize; 11 | 12 | @Singleton 13 | public class ConstructorInjectedPojo implements TestPojo { 14 | 15 | private final boolean testBoolean; 16 | private final boolean testYesBoolean; 17 | private final long testLong; 18 | private final byte testByte; 19 | private final int testInt; 20 | private final double testDouble; 21 | private final float testFloat; 22 | private final String testString; 23 | private final List testListOfBoolean; 24 | private final List testListOfInteger; 25 | private final List testListOfDouble; 26 | private final List testListOfLong; 27 | private final List testListOfString; 28 | private final List testListOfDuration; 29 | private final List testListOfSize; 30 | private final List testListOfNested; 31 | private final Duration testDuration; 32 | private final ConfigMemorySize testSize; 33 | private final Map testMap; 34 | private final Map testMapIntkey; 35 | private final NestedPojo testNestedPojo; 36 | 37 | @Inject 38 | public ConstructorInjectedPojo( 39 | @TypesafeConfig("constructor.boolean") boolean testBoolean, 40 | @TypesafeConfig("constructor.boolean") boolean testBooleanAgain, 41 | @TypesafeConfig("constructor.yesBoolean") boolean testYesBoolean, 42 | @TypesafeConfig("constructor.long") long testLong, 43 | @TypesafeConfig("constructor.byte") byte testByte, 44 | @TypesafeConfig("constructor.int") int testInt, 45 | @TypesafeConfig("constructor.double") double testDouble, 46 | @TypesafeConfig("constructor.float") float testFloat, 47 | @TypesafeConfig("constructor.string") String testString, 48 | @TypesafeConfig("constructor.list.boolean") List testListOfBoolean, 49 | @TypesafeConfig("constructor.list.integer") List testListOfInteger, 50 | @TypesafeConfig("constructor.list.double") List testListOfDouble, 51 | @TypesafeConfig("constructor.list.long") List testListOfLong, 52 | @TypesafeConfig("constructor.list.string") List testListOfString, 53 | @TypesafeConfig("constructor.list.duration") List testListOfDuration, 54 | @TypesafeConfig("constructor.list.size") List testListOfSize, 55 | @TypesafeConfig("constructor.list.nested") List testListOfNested, 56 | @TypesafeConfig("constructor.duration") Duration testDuration, 57 | @TypesafeConfig("constructor.size") ConfigMemorySize testSize, 58 | @TypesafeConfig("constructor.map") Map testMap, 59 | @TypesafeConfig("constructor.map.intkey") Map testMapIntkey, 60 | @TypesafeConfig("constructor.nested") NestedPojo testNestedPojo 61 | ) { 62 | this.testBoolean = testBoolean; 63 | this.testYesBoolean = testYesBoolean; 64 | this.testLong = testLong; 65 | this.testByte = testByte; 66 | this.testInt = testInt; 67 | this.testDouble = testDouble; 68 | this.testFloat = testFloat; 69 | this.testString = testString; 70 | this.testListOfBoolean = testListOfBoolean; 71 | this.testListOfInteger = testListOfInteger; 72 | this.testListOfDouble = testListOfDouble; 73 | this.testListOfLong = testListOfLong; 74 | this.testListOfString = testListOfString; 75 | this.testListOfDuration = testListOfDuration; 76 | this.testListOfSize = testListOfSize; 77 | this.testListOfNested = testListOfNested; 78 | this.testDuration = testDuration; 79 | this.testSize = testSize; 80 | this.testMap = testMap; 81 | this.testMapIntkey = testMapIntkey; 82 | this.testNestedPojo = testNestedPojo; 83 | } 84 | 85 | public boolean isTestBoolean() { 86 | return testBoolean; 87 | } 88 | 89 | public boolean isTestYesBoolean() { 90 | return testYesBoolean; 91 | } 92 | 93 | public long getTestLong() { 94 | return testLong; 95 | } 96 | 97 | public byte getTestByte() { 98 | return testByte; 99 | } 100 | 101 | public int getTestInt() { 102 | return testInt; 103 | } 104 | 105 | public double getTestDouble() { 106 | return testDouble; 107 | } 108 | 109 | public float getTestFloat() { 110 | return testFloat; 111 | } 112 | 113 | public String getTestString() { 114 | return testString; 115 | } 116 | 117 | public List getTestListOfBoolean() { 118 | return testListOfBoolean; 119 | } 120 | 121 | public List getTestListOfInteger() { 122 | return testListOfInteger; 123 | } 124 | 125 | public List getTestListOfDouble() { 126 | return testListOfDouble; 127 | } 128 | 129 | public List getTestListOfLong() { 130 | return testListOfLong; 131 | } 132 | 133 | public List getTestListOfString() { 134 | return testListOfString; 135 | } 136 | 137 | public List getTestListOfDuration() { 138 | return testListOfDuration; 139 | } 140 | 141 | public List getTestListOfSize() { 142 | return testListOfSize; 143 | } 144 | 145 | public List getTestListOfNested() { 146 | return testListOfNested; 147 | } 148 | 149 | public Duration getTestDuration() { 150 | return testDuration; 151 | } 152 | 153 | public ConfigMemorySize getTestSize() { 154 | return testSize; 155 | } 156 | 157 | public Map getTestMap() { 158 | return testMap; 159 | } 160 | 161 | public Map getTestMapIntkey() { 162 | return testMapIntkey; 163 | } 164 | 165 | public NestedPojo getTestNestedPojo() { 166 | return testNestedPojo; 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /src/test/java/com/github/racc/tscg/test/FieldInjectedPojo.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg.test; 2 | 3 | import java.time.Duration; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import com.github.racc.tscg.TypesafeConfig; 8 | import com.google.inject.Inject; 9 | import com.google.inject.Singleton; 10 | import com.typesafe.config.ConfigMemorySize; 11 | 12 | @Singleton 13 | public class FieldInjectedPojo implements TestPojo { 14 | 15 | @Inject @TypesafeConfig("field.boolean") boolean testBoolean; 16 | @Inject @TypesafeConfig("field.yesBoolean") boolean testYesBoolean; 17 | @Inject @TypesafeConfig("field.long") long testLong; 18 | @Inject @TypesafeConfig("field.byte") byte testByte; 19 | @Inject @TypesafeConfig("field.int") int testInt; 20 | @Inject @TypesafeConfig("field.double") double testDouble; 21 | @Inject @TypesafeConfig("field.float") float testFloat; 22 | @Inject @TypesafeConfig("field.string") String testString; 23 | @Inject @TypesafeConfig("field.list.boolean") List testListOfBoolean; 24 | @Inject @TypesafeConfig("field.list.integer") List testListOfInteger; 25 | @Inject @TypesafeConfig("field.list.double") List testListOfDouble; 26 | @Inject @TypesafeConfig("field.list.long") List testListOfLong; 27 | @Inject @TypesafeConfig("field.list.string") List testListOfString; 28 | @Inject @TypesafeConfig("field.list.duration") List testListOfDuration; 29 | @Inject @TypesafeConfig("field.list.size") List testListOfSize; 30 | @Inject @TypesafeConfig("field.list.nested") List testListOfNested; 31 | @Inject @TypesafeConfig("field.duration") Duration testDuration; 32 | @Inject @TypesafeConfig("field.size") ConfigMemorySize testSize; 33 | @Inject @TypesafeConfig("field.map") Map testMap; 34 | @Inject @TypesafeConfig("field.map.intkey") Map testMapIntkey; 35 | @Inject @TypesafeConfig("field.nested") NestedPojo testNestedPojo; 36 | 37 | public void setTestBoolean(boolean testBoolean) { 38 | this.testBoolean = testBoolean; 39 | } 40 | 41 | public boolean isTestBoolean() { 42 | return testBoolean; 43 | } 44 | 45 | public boolean isTestYesBoolean() { 46 | return testYesBoolean; 47 | } 48 | 49 | public long getTestLong() { 50 | return testLong; 51 | } 52 | 53 | public byte getTestByte() { 54 | return testByte; 55 | } 56 | 57 | public int getTestInt() { 58 | return testInt; 59 | } 60 | 61 | public double getTestDouble() { 62 | return testDouble; 63 | } 64 | 65 | public float getTestFloat() { 66 | return testFloat; 67 | } 68 | 69 | public String getTestString() { 70 | return testString; 71 | } 72 | 73 | public List getTestListOfBoolean() { 74 | return testListOfBoolean; 75 | } 76 | 77 | public List getTestListOfInteger() { 78 | return testListOfInteger; 79 | } 80 | 81 | public List getTestListOfDouble() { 82 | return testListOfDouble; 83 | } 84 | 85 | public List getTestListOfLong() { 86 | return testListOfLong; 87 | } 88 | 89 | public List getTestListOfString() { 90 | return testListOfString; 91 | } 92 | 93 | public List getTestListOfDuration() { 94 | return testListOfDuration; 95 | } 96 | 97 | public List getTestListOfSize() { 98 | return testListOfSize; 99 | } 100 | 101 | public List getTestListOfNested() { 102 | return testListOfNested; 103 | } 104 | 105 | public Duration getTestDuration() { 106 | return testDuration; 107 | } 108 | 109 | public ConfigMemorySize getTestSize() { 110 | return testSize; 111 | } 112 | 113 | public Map getTestMap() { 114 | return testMap; 115 | } 116 | 117 | public Map getTestMapIntkey() { 118 | return testMapIntkey; 119 | } 120 | 121 | public NestedPojo getTestNestedPojo() { 122 | return testNestedPojo; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/test/java/com/github/racc/tscg/test/MethodInjectedPojo.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg.test; 2 | 3 | import java.time.Duration; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import com.github.racc.tscg.TypesafeConfig; 8 | import com.google.inject.Inject; 9 | import com.google.inject.Singleton; 10 | import com.typesafe.config.ConfigMemorySize; 11 | 12 | @Singleton 13 | public class MethodInjectedPojo implements TestPojo { 14 | 15 | private boolean testBoolean; 16 | private boolean testYesBoolean; 17 | private long testLong; 18 | private byte testByte; 19 | private int testInt; 20 | private double testDouble; 21 | private float testFloat; 22 | private String testString; 23 | private List testListOfBoolean; 24 | private List testListOfInteger; 25 | private List testListOfDouble; 26 | private List testListOfLong; 27 | private List testListOfString; 28 | private List testListOfDuration; 29 | private List testListOfSize; 30 | private List testListOfNested; 31 | private Duration testDuration; 32 | private ConfigMemorySize testSize; 33 | private Map testMap; 34 | private Map testMapIntkey; 35 | private NestedPojo testNestedPojo; 36 | 37 | @Inject 38 | public void setTestYesBoolean(@TypesafeConfig("method.boolean") boolean testYesBoolean) { 39 | this.testYesBoolean = testYesBoolean; 40 | } 41 | 42 | @Inject 43 | public void setTestLong(@TypesafeConfig("method.long") long testLong) { 44 | this.testLong = testLong; 45 | } 46 | 47 | @Inject 48 | public void setTestByte(@TypesafeConfig("method.byte") byte testByte) { 49 | this.testByte = testByte; 50 | } 51 | 52 | @Inject 53 | public void setTestInt(@TypesafeConfig("method.int") int testInt) { 54 | this.testInt = testInt; 55 | } 56 | 57 | @Inject 58 | public void setTestDouble(@TypesafeConfig("method.double") double testDouble) { 59 | this.testDouble = testDouble; 60 | } 61 | 62 | @Inject 63 | public void setTestFloat(@TypesafeConfig("method.float") float testFloat) { 64 | this.testFloat = testFloat; 65 | } 66 | 67 | @Inject 68 | public void setTestString(@TypesafeConfig("method.string") String testString) { 69 | this.testString = testString; 70 | } 71 | 72 | @Inject 73 | public void setTestListOfBoolean(@TypesafeConfig("method.list.boolean") List testListOfBoolean) { 74 | this.testListOfBoolean = testListOfBoolean; 75 | } 76 | 77 | @Inject 78 | public void setTestListOfInteger(@TypesafeConfig("method.list.integer") List testListOfInteger) { 79 | this.testListOfInteger = testListOfInteger; 80 | } 81 | 82 | @Inject 83 | public void setTestListOfDouble(@TypesafeConfig("method.list.double") List testListOfDouble) { 84 | this.testListOfDouble = testListOfDouble; 85 | } 86 | 87 | @Inject 88 | public void setTestListOfLong(@TypesafeConfig("method.list.long") List testListOfLong) { 89 | this.testListOfLong = testListOfLong; 90 | } 91 | 92 | @Inject 93 | public void setTestListOfString(@TypesafeConfig("method.list.string") List testListOfString) { 94 | this.testListOfString = testListOfString; 95 | } 96 | 97 | @Inject 98 | public void setTestListOfDuration(@TypesafeConfig("method.list.duration") List testListOfDuration) { 99 | this.testListOfDuration = testListOfDuration; 100 | } 101 | 102 | @Inject 103 | public void setTestListOfSize(@TypesafeConfig("method.list.size") List testListOfSize) { 104 | this.testListOfSize = testListOfSize; 105 | } 106 | 107 | @Inject 108 | public void setTestListOfNested(@TypesafeConfig("method.list.nested") List testListOfNested) { 109 | this.testListOfNested = testListOfNested; 110 | } 111 | 112 | @Inject 113 | public void setTestDuration(@TypesafeConfig("method.duration") Duration testDuration) { 114 | this.testDuration = testDuration; 115 | } 116 | 117 | @Inject 118 | public void setTestSize(@TypesafeConfig("method.size") ConfigMemorySize testSize) { 119 | this.testSize = testSize; 120 | } 121 | 122 | @Inject 123 | public void setTestMap(@TypesafeConfig("method.map") Map testMap) { 124 | this.testMap = testMap; 125 | } 126 | 127 | @Inject 128 | public void setTestMapIntkey(@TypesafeConfig("method.map.intkey") Map testMapIntkey) { 129 | this.testMapIntkey = testMapIntkey; 130 | } 131 | 132 | @Inject 133 | public void setTestNestedPojo(@TypesafeConfig("method.nested") NestedPojo testNestedPojo) { 134 | this.testNestedPojo = testNestedPojo; 135 | } 136 | 137 | @Inject 138 | public void setTestBoolean(@TypesafeConfig("method.yesBoolean") boolean testBoolean) { 139 | this.testBoolean = testBoolean; 140 | } 141 | 142 | public boolean isTestBoolean() { 143 | return testBoolean; 144 | } 145 | 146 | public boolean isTestYesBoolean() { 147 | return testYesBoolean; 148 | } 149 | 150 | public long getTestLong() { 151 | return testLong; 152 | } 153 | 154 | public byte getTestByte() { 155 | return testByte; 156 | } 157 | 158 | public int getTestInt() { 159 | return testInt; 160 | } 161 | 162 | public double getTestDouble() { 163 | return testDouble; 164 | } 165 | 166 | public float getTestFloat() { 167 | return testFloat; 168 | } 169 | 170 | public String getTestString() { 171 | return testString; 172 | } 173 | 174 | public List getTestListOfBoolean() { 175 | return testListOfBoolean; 176 | } 177 | 178 | public List getTestListOfInteger() { 179 | return testListOfInteger; 180 | } 181 | 182 | public List getTestListOfDouble() { 183 | return testListOfDouble; 184 | } 185 | 186 | public List getTestListOfLong() { 187 | return testListOfLong; 188 | } 189 | 190 | public List getTestListOfString() { 191 | return testListOfString; 192 | } 193 | 194 | public List getTestListOfDuration() { 195 | return testListOfDuration; 196 | } 197 | 198 | public List getTestListOfSize() { 199 | return testListOfSize; 200 | } 201 | 202 | public List getTestListOfNested() { 203 | return testListOfNested; 204 | } 205 | 206 | public Duration getTestDuration() { 207 | return testDuration; 208 | } 209 | 210 | public ConfigMemorySize getTestSize() { 211 | return testSize; 212 | } 213 | 214 | public Map getTestMap() { 215 | return testMap; 216 | } 217 | 218 | public Map getTestMapIntkey() { 219 | return testMapIntkey; 220 | } 221 | 222 | public NestedPojo getTestNestedPojo() { 223 | return testNestedPojo; 224 | } 225 | } 226 | -------------------------------------------------------------------------------- /src/test/java/com/github/racc/tscg/test/NestedPojo.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg.test; 2 | 3 | 4 | public class NestedPojo { 5 | private int nestInt; 6 | private double nestDouble; 7 | private String nestString; 8 | 9 | public NestedPojo() {} 10 | 11 | public NestedPojo( 12 | int testInt, 13 | double testDouble, 14 | String testString 15 | ) { 16 | this.setNestInt(testInt); 17 | this.setNestDouble(testDouble); 18 | this.setNestString(testString); 19 | } 20 | 21 | public int getNestInt() { 22 | return nestInt; 23 | } 24 | 25 | public void setNestInt(int nestInt) { 26 | this.nestInt = nestInt; 27 | } 28 | 29 | public double getNestDouble() { 30 | return nestDouble; 31 | } 32 | 33 | public void setNestDouble(double nestDouble) { 34 | this.nestDouble = nestDouble; 35 | } 36 | 37 | public String getNestString() { 38 | return nestString; 39 | } 40 | 41 | public void setNestString(String nestString) { 42 | this.nestString = nestString; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/com/github/racc/tscg/test/ProvidedPojo.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg.test; 2 | 3 | import java.time.Duration; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import com.google.inject.Singleton; 8 | import com.typesafe.config.ConfigMemorySize; 9 | 10 | @Singleton 11 | public class ProvidedPojo implements TestPojo { 12 | 13 | private final boolean testBoolean; 14 | private final boolean testYesBoolean; 15 | private final long testLong; 16 | private final byte testByte; 17 | private final int testInt; 18 | private final double testDouble; 19 | private final float testFloat; 20 | private final String testString; 21 | private final List testListOfBoolean; 22 | private final List testListOfInteger; 23 | private final List testListOfDouble; 24 | private final List testListOfLong; 25 | private final List testListOfString; 26 | private final List testListOfDuration; 27 | private final List testListOfSize; 28 | private final List testListOfNested; 29 | private final Duration testDuration; 30 | private final ConfigMemorySize testSize; 31 | private final Map testMap; 32 | private final Map testMapIntkey; 33 | private final NestedPojo testNestedPojo; 34 | 35 | public ProvidedPojo( 36 | boolean testBoolean, 37 | boolean testYesBoolean, 38 | long testLong, 39 | byte testByte, 40 | int testInt, 41 | double testDouble, 42 | float testFloat, 43 | String testString, 44 | List testListOfBoolean, 45 | List testListOfInteger, 46 | List testListOfDouble, 47 | List testListOfLong, 48 | List testListOfString, 49 | List testListOfDuration, 50 | List testListOfSize, 51 | List testListOfNested, 52 | Duration testDuration, 53 | ConfigMemorySize testSize, 54 | Map testMap, 55 | Map testMapIntkey, 56 | NestedPojo testNestedPojo 57 | ) { 58 | this.testBoolean = testBoolean; 59 | this.testYesBoolean = testYesBoolean; 60 | this.testLong = testLong; 61 | this.testByte = testByte; 62 | this.testInt = testInt; 63 | this.testDouble = testDouble; 64 | this.testFloat = testFloat; 65 | this.testString = testString; 66 | this.testListOfBoolean = testListOfBoolean; 67 | this.testListOfInteger = testListOfInteger; 68 | this.testListOfDouble = testListOfDouble; 69 | this.testListOfLong = testListOfLong; 70 | this.testListOfString = testListOfString; 71 | this.testListOfDuration = testListOfDuration; 72 | this.testListOfSize = testListOfSize; 73 | this.testListOfNested = testListOfNested; 74 | this.testDuration = testDuration; 75 | this.testSize = testSize; 76 | this.testMap = testMap; 77 | this.testMapIntkey = testMapIntkey; 78 | this.testNestedPojo = testNestedPojo; 79 | } 80 | 81 | public boolean isTestBoolean() { 82 | return testBoolean; 83 | } 84 | 85 | public boolean isTestYesBoolean() { 86 | return testYesBoolean; 87 | } 88 | 89 | public long getTestLong() { 90 | return testLong; 91 | } 92 | 93 | public byte getTestByte() { 94 | return testByte; 95 | } 96 | 97 | public int getTestInt() { 98 | return testInt; 99 | } 100 | 101 | public double getTestDouble() { 102 | return testDouble; 103 | } 104 | 105 | public float getTestFloat() { 106 | return testFloat; 107 | } 108 | 109 | public String getTestString() { 110 | return testString; 111 | } 112 | 113 | public List getTestListOfBoolean() { 114 | return testListOfBoolean; 115 | } 116 | 117 | public List getTestListOfInteger() { 118 | return testListOfInteger; 119 | } 120 | 121 | public List getTestListOfDouble() { 122 | return testListOfDouble; 123 | } 124 | 125 | public List getTestListOfLong() { 126 | return testListOfLong; 127 | } 128 | 129 | public List getTestListOfString() { 130 | return testListOfString; 131 | } 132 | 133 | public List getTestListOfDuration() { 134 | return testListOfDuration; 135 | } 136 | 137 | public List getTestListOfSize() { 138 | return testListOfSize; 139 | } 140 | 141 | public List getTestListOfNested() { 142 | return testListOfNested; 143 | } 144 | 145 | public Duration getTestDuration() { 146 | return testDuration; 147 | } 148 | 149 | public ConfigMemorySize getTestSize() { 150 | return testSize; 151 | } 152 | 153 | public Map getTestMap() { 154 | return testMap; 155 | } 156 | 157 | public Map getTestMapIntkey() { 158 | return testMapIntkey; 159 | } 160 | 161 | public NestedPojo getTestNestedPojo() { 162 | return testNestedPojo; 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /src/test/java/com/github/racc/tscg/test/TestPojo.java: -------------------------------------------------------------------------------- 1 | package com.github.racc.tscg.test; 2 | 3 | import java.time.Duration; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import com.typesafe.config.ConfigMemorySize; 8 | 9 | public interface TestPojo { 10 | 11 | public boolean isTestBoolean(); 12 | 13 | public boolean isTestYesBoolean(); 14 | 15 | public long getTestLong(); 16 | 17 | public byte getTestByte(); 18 | 19 | public int getTestInt(); 20 | 21 | public double getTestDouble(); 22 | 23 | public float getTestFloat(); 24 | 25 | public String getTestString(); 26 | 27 | public List getTestListOfBoolean(); 28 | 29 | public List getTestListOfInteger(); 30 | 31 | public List getTestListOfDouble(); 32 | 33 | public List getTestListOfLong(); 34 | 35 | public List getTestListOfString(); 36 | 37 | public List getTestListOfDuration(); 38 | 39 | public List getTestListOfSize(); 40 | 41 | public List getTestListOfNested(); 42 | 43 | public Duration getTestDuration(); 44 | 45 | public ConfigMemorySize getTestSize(); 46 | 47 | public Map getTestMap(); 48 | 49 | public Map getTestMapIntkey(); 50 | 51 | public NestedPojo getTestNestedPojo(); 52 | } 53 | -------------------------------------------------------------------------------- /src/test/resources/conf/test.conf: -------------------------------------------------------------------------------- 1 | test.boolean = true 2 | test.yesBoolean = yes 3 | test.long = 12345679123 4 | test.byte = 123 5 | test.int = 1 6 | test.double = 2.0 7 | test.float = 2.0 8 | test.string = test 9 | test.list.boolean = [yes, no, yes] 10 | test.list.integer = [1, 2, 3] 11 | test.list.double = [1.1, 2.2, 3.3] 12 | test.list.long = [12345679121, 12345679122, 12345679123] 13 | test.list.string = ["a", "b", "c"] 14 | test.list.duration = [1 second, 2 seconds, 3 seconds] 15 | test.list.size = [512k, 1M, 1G] 16 | test.duration = 10 seconds 17 | test.size = 512k 18 | 19 | test.map = { 20 | one = 1, 21 | two = 2, 22 | three = 3 23 | } 24 | 25 | test.map.intkey = { 26 | 1 = one, 27 | 2 = two, 28 | 3 = three 29 | } 30 | 31 | test.nested = { 32 | nestInt = 3 33 | nestDouble = 4 34 | nestString = "five" 35 | } 36 | 37 | test.list.nested = [ 38 | { 39 | nestInt = 3 40 | nestDouble = 4 41 | nestString = "five" 42 | }, 43 | { 44 | nestInt = 4 45 | nestDouble = 5 46 | nestString = "six" 47 | }, 48 | { 49 | nestInt = 5 50 | nestDouble = 6 51 | nestString = "seven" 52 | } 53 | ] 54 | 55 | provided = ${test} 56 | constructor = ${test} 57 | field = ${test} 58 | method = ${test} 59 | --------------------------------------------------------------------------------