aosp12/external/auto/factory
hcl 7be3fd486c init from android-12.1.0_r8 2023-01-09 17:11:35 +08:00
..
src init from android-12.1.0_r8 2023-01-09 17:11:35 +08:00
Android.bp init from android-12.1.0_r8 2023-01-09 17:11:35 +08:00
README.md init from android-12.1.0_r8 2023-01-09 17:11:35 +08:00
pom.xml init from android-12.1.0_r8 2023-01-09 17:11:35 +08:00

README.md

AutoFactory

A source code generator for JSR-330-compatible factories.

AutoWhat‽

Java is full of factories. They're mechanical, repetitive, typically untested and sometimes the source of subtle bugs. Sounds like a job for robots!

AutoFactory generates factories that can be used on their own or with JSR-330-compatible dependency injectors 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.

Save time. Save code. Save sanity.

Example

Say you have:

@AutoFactory
final class SomeClass {
  private final String providedDepA;
  private final String depB;

  SomeClass(@Provided @AQualifier String providedDepA, String depB) {
    this.providedDepA = providedDepA;
    this.depB = depB;
  }

  // …
}

AutoFactory will generate:

import javax.annotation.Generated;
import javax.inject.Inject;
import javax.inject.Provider;

@Generated(value = "com.google.auto.factory.processor.AutoFactoryProcessor")
final class SomeClassFactory {
  private final Provider<String> providedDepAProvider;
  
  @Inject SomeClassFactory(
      @AQualifier Provider<String> providedDepAProvider) {
    this.providedDepAProvider = providedDepAProvider;
  }
  
  SomeClass create(String depB) {
    return new SomeClass(providedDepAProvider.get(), depB);
  }
}

NOTE: AutoFactory only supports JSR-330 @Qualifier annotations. Older, framework-specific annotations from Guice, Spring, etc are not supported (though these all support JSR-330)

Download

In order to activate code generation you will need to include auto-factory-${version}.jar in your build at compile time.

In a Maven project, one would include the auto-factory artifact as an "optional" dependency:

<dependencies>
  <dependency>
    <groupId>com.google.auto.factory</groupId>
    <artifactId>auto-factory</artifactId>
    <version>${version}</version>
    <optional>true</optional>
  </dependency>
</dependencies>

License

Copyright 2013 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.