1*1c2bbba8SAndroid Build Coastguard WorkerAutoFactory 2*1c2bbba8SAndroid Build Coastguard Worker====== 3*1c2bbba8SAndroid Build Coastguard Worker 4*1c2bbba8SAndroid Build Coastguard WorkerA source code generator for JSR-330-compatible factories. 5*1c2bbba8SAndroid Build Coastguard Worker 6*1c2bbba8SAndroid Build Coastguard WorkerAutoWhat‽ 7*1c2bbba8SAndroid Build Coastguard Worker------------- 8*1c2bbba8SAndroid Build Coastguard Worker 9*1c2bbba8SAndroid Build Coastguard Worker[Java][java] is full of [factories](http://en.wikipedia.org/wiki/Factory_method_pattern). They're mechanical, repetitive, typically untested and sometimes the source of subtle bugs. _Sounds like a job for robots!_ 10*1c2bbba8SAndroid Build Coastguard Worker 11*1c2bbba8SAndroid Build Coastguard WorkerAutoFactory generates factories that can be used on their own or with [JSR-330](http://jcp.org/en/jsr/detail?id=330)-compatible [dependency injectors](http://en.wikipedia.org/wiki/Dependency_injection) from a simple annotation. Any combination of parameters can either be passed through factory methods or provided to the factory at construction time. They can implement interfaces or extend abstract classes. They're what you would have written, but without the bugs. 12*1c2bbba8SAndroid Build Coastguard Worker 13*1c2bbba8SAndroid Build Coastguard Worker[Dagger](https://dagger.dev/) users: Dagger's own 14*1c2bbba8SAndroid Build Coastguard Worker[assisted injection](https://dagger.dev/dev-guide/assisted-injection.html) is 15*1c2bbba8SAndroid Build Coastguard Workernow usually preferred to AutoFactory. 16*1c2bbba8SAndroid Build Coastguard Worker 17*1c2bbba8SAndroid Build Coastguard WorkerExample 18*1c2bbba8SAndroid Build Coastguard Worker------- 19*1c2bbba8SAndroid Build Coastguard Worker 20*1c2bbba8SAndroid Build Coastguard WorkerSay you have: 21*1c2bbba8SAndroid Build Coastguard Worker 22*1c2bbba8SAndroid Build Coastguard Worker```java 23*1c2bbba8SAndroid Build Coastguard Worker@AutoFactory 24*1c2bbba8SAndroid Build Coastguard Workerfinal class SomeClass { 25*1c2bbba8SAndroid Build Coastguard Worker private final String providedDepA; 26*1c2bbba8SAndroid Build Coastguard Worker private final String depB; 27*1c2bbba8SAndroid Build Coastguard Worker 28*1c2bbba8SAndroid Build Coastguard Worker SomeClass(@Provided @AQualifier String providedDepA, String depB) { 29*1c2bbba8SAndroid Build Coastguard Worker this.providedDepA = providedDepA; 30*1c2bbba8SAndroid Build Coastguard Worker this.depB = depB; 31*1c2bbba8SAndroid Build Coastguard Worker } 32*1c2bbba8SAndroid Build Coastguard Worker 33*1c2bbba8SAndroid Build Coastguard Worker // … 34*1c2bbba8SAndroid Build Coastguard Worker} 35*1c2bbba8SAndroid Build Coastguard Worker``` 36*1c2bbba8SAndroid Build Coastguard Worker 37*1c2bbba8SAndroid Build Coastguard WorkerAutoFactory will generate: 38*1c2bbba8SAndroid Build Coastguard Worker 39*1c2bbba8SAndroid Build Coastguard Worker```java 40*1c2bbba8SAndroid Build Coastguard Workerimport javax.annotation.Generated; 41*1c2bbba8SAndroid Build Coastguard Workerimport javax.inject.Inject; 42*1c2bbba8SAndroid Build Coastguard Workerimport javax.inject.Provider; 43*1c2bbba8SAndroid Build Coastguard Worker 44*1c2bbba8SAndroid Build Coastguard Worker@Generated(value = "com.google.auto.factory.processor.AutoFactoryProcessor") 45*1c2bbba8SAndroid Build Coastguard Workerfinal class SomeClassFactory { 46*1c2bbba8SAndroid Build Coastguard Worker private final Provider<String> providedDepAProvider; 47*1c2bbba8SAndroid Build Coastguard Worker 48*1c2bbba8SAndroid Build Coastguard Worker @Inject SomeClassFactory( 49*1c2bbba8SAndroid Build Coastguard Worker @AQualifier Provider<String> providedDepAProvider) { 50*1c2bbba8SAndroid Build Coastguard Worker this.providedDepAProvider = providedDepAProvider; 51*1c2bbba8SAndroid Build Coastguard Worker } 52*1c2bbba8SAndroid Build Coastguard Worker 53*1c2bbba8SAndroid Build Coastguard Worker SomeClass create(String depB) { 54*1c2bbba8SAndroid Build Coastguard Worker return new SomeClass(providedDepAProvider.get(), depB); 55*1c2bbba8SAndroid Build Coastguard Worker } 56*1c2bbba8SAndroid Build Coastguard Worker} 57*1c2bbba8SAndroid Build Coastguard Worker``` 58*1c2bbba8SAndroid Build Coastguard Worker 59*1c2bbba8SAndroid Build Coastguard Worker> NOTE: AutoFactory only supports JSR-330 @Qualifier annotations. Older, 60*1c2bbba8SAndroid Build Coastguard Worker> framework-specific annotations from Guice, Spring, etc are not 61*1c2bbba8SAndroid Build Coastguard Worker> supported (though these all support JSR-330) 62*1c2bbba8SAndroid Build Coastguard Worker 63*1c2bbba8SAndroid Build Coastguard WorkerMocking 64*1c2bbba8SAndroid Build Coastguard Worker------- 65*1c2bbba8SAndroid Build Coastguard Worker 66*1c2bbba8SAndroid Build Coastguard WorkerBy default, the factory class generated by AutoFactory is final, and thus cannot 67*1c2bbba8SAndroid Build Coastguard Workerbe mocked. The generated factory class can be made mockable by setting 68*1c2bbba8SAndroid Build Coastguard Worker`allowSubclasses = true`, as follows: 69*1c2bbba8SAndroid Build Coastguard Worker 70*1c2bbba8SAndroid Build Coastguard Worker```java 71*1c2bbba8SAndroid Build Coastguard Worker@AutoFactory(allowSubclasses = true) 72*1c2bbba8SAndroid Build Coastguard Workerfinal class SomeClass { 73*1c2bbba8SAndroid Build Coastguard Worker // … 74*1c2bbba8SAndroid Build Coastguard Worker} 75*1c2bbba8SAndroid Build Coastguard Worker``` 76*1c2bbba8SAndroid Build Coastguard Worker 77*1c2bbba8SAndroid Build Coastguard WorkerDownload 78*1c2bbba8SAndroid Build Coastguard Worker-------- 79*1c2bbba8SAndroid Build Coastguard Worker 80*1c2bbba8SAndroid Build Coastguard WorkerIn order to activate code generation you will need to 81*1c2bbba8SAndroid Build Coastguard Workerinclude `auto-factory-${version}.jar` in your build at 82*1c2bbba8SAndroid Build Coastguard Workercompile time. 83*1c2bbba8SAndroid Build Coastguard Worker 84*1c2bbba8SAndroid Build Coastguard WorkerIn a Maven project, one would include the `auto-factory` 85*1c2bbba8SAndroid Build Coastguard Workerartifact as an "optional" dependency: 86*1c2bbba8SAndroid Build Coastguard Worker 87*1c2bbba8SAndroid Build Coastguard Worker```xml 88*1c2bbba8SAndroid Build Coastguard Worker<dependencies> 89*1c2bbba8SAndroid Build Coastguard Worker <dependency> 90*1c2bbba8SAndroid Build Coastguard Worker <groupId>com.google.auto.factory</groupId> 91*1c2bbba8SAndroid Build Coastguard Worker <artifactId>auto-factory</artifactId> 92*1c2bbba8SAndroid Build Coastguard Worker <version>${version}</version> 93*1c2bbba8SAndroid Build Coastguard Worker <optional>true</optional> 94*1c2bbba8SAndroid Build Coastguard Worker </dependency> 95*1c2bbba8SAndroid Build Coastguard Worker</dependencies> 96*1c2bbba8SAndroid Build Coastguard Worker``` 97*1c2bbba8SAndroid Build Coastguard Worker 98*1c2bbba8SAndroid Build Coastguard Worker 99*1c2bbba8SAndroid Build Coastguard WorkerLicense 100*1c2bbba8SAndroid Build Coastguard Worker------- 101*1c2bbba8SAndroid Build Coastguard Worker 102*1c2bbba8SAndroid Build Coastguard Worker Copyright 2013 Google LLC 103*1c2bbba8SAndroid Build Coastguard Worker 104*1c2bbba8SAndroid Build Coastguard Worker Licensed under the Apache License, Version 2.0 (the "License"); 105*1c2bbba8SAndroid Build Coastguard Worker you may not use this file except in compliance with the License. 106*1c2bbba8SAndroid Build Coastguard Worker You may obtain a copy of the License at 107*1c2bbba8SAndroid Build Coastguard Worker 108*1c2bbba8SAndroid Build Coastguard Worker http://www.apache.org/licenses/LICENSE-2.0 109*1c2bbba8SAndroid Build Coastguard Worker 110*1c2bbba8SAndroid Build Coastguard Worker Unless required by applicable law or agreed to in writing, software 111*1c2bbba8SAndroid Build Coastguard Worker distributed under the License is distributed on an "AS IS" BASIS, 112*1c2bbba8SAndroid Build Coastguard Worker WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 113*1c2bbba8SAndroid Build Coastguard Worker See the License for the specific language governing permissions and 114*1c2bbba8SAndroid Build Coastguard Worker limitations under the License. 115*1c2bbba8SAndroid Build Coastguard Worker 116*1c2bbba8SAndroid Build Coastguard Worker[java]: https://en.wikipedia.org/wiki/Java_(programming_language) 117*1c2bbba8SAndroid Build Coastguard Worker 118