├── .buildscript ├── deploy_snapshot.sh └── settings.xml ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.txt ├── README.md ├── checkstyle.xml ├── compiler ├── pom.xml └── src │ ├── it │ ├── default-package-injected-type │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── TestApp.java │ ├── extension-graph-setvalues │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── test │ │ │ └── TestApp.java │ ├── extension-graph │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── test │ │ │ └── TestApp.java │ ├── final-field-inject │ │ ├── invoker.properties │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestApp.java │ │ └── verify.bsh │ ├── include-non-module │ │ ├── invoker.properties │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── test │ │ │ └── TestApp.java │ ├── inject-on-class │ │ ├── invoker.properties │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestApp.java │ │ └── verify.bsh │ ├── inject-parameterized-type │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── test │ │ │ └── TestApp.java │ ├── inner-classes-complaint-injection │ │ ├── invoker.properties │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestApp.java │ │ └── verify.bsh │ ├── method-injection │ │ ├── invoker.properties │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestApp.java │ │ └── verify.bsh │ ├── missing-at-inject-constructor │ │ ├── invoker.properties │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestApp.java │ │ └── verify.bsh │ ├── module-type-validation │ │ ├── invoker.properties │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestModule.java │ │ └── verify.bsh │ ├── multiple-modules-setvalues │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── test │ │ │ └── TestApp.java │ ├── multiple-provides-methods │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestApp.java │ │ └── verify.bsh │ ├── multiple-qualifiers │ │ ├── invoker.properties │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestApp.java │ │ └── verify.bsh │ ├── private-inject │ │ ├── invoker.properties │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ ├── TestApp.java │ │ │ │ └── TestFoo.java │ │ └── verify.bsh │ ├── provide-provider-or-lazy │ │ ├── invoker.properties │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestModule.java │ │ └── verify.bsh │ ├── provides-method-not-in-module │ │ ├── invoker.properties │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestApp.java │ │ └── verify.bsh │ ├── provides-method-with-throws-clause │ │ ├── invoker.properties │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestApp.java │ │ └── verify.bsh │ ├── qualifiers-on-invalid-elements-errors │ │ ├── invoker.properties │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestApp.java │ │ └── verify.bsh │ ├── qualifiers-on-invalid-elements-warnings │ │ ├── pom.xml │ │ ├── src │ │ │ └── main │ │ │ │ └── java │ │ │ │ └── test │ │ │ │ └── TestApp.java │ │ └── verify.bsh │ ├── same-provides-method-name │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── test │ │ │ └── TestApp.java │ ├── static-injected-binding-doesnt-fail-providers │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── test │ │ │ └── Test.java │ ├── uninjectable-supertype │ │ ├── pom.xml │ │ └── src │ │ │ └── main │ │ │ └── java │ │ │ └── test │ │ │ └── TestApp.java │ └── valid-use-of-qualifiers │ │ ├── pom.xml │ │ └── src │ │ └── main │ │ └── java │ │ └── test │ │ └── TestApp.java │ ├── main │ ├── java │ │ └── dagger │ │ │ └── internal │ │ │ └── codegen │ │ │ ├── AdapterJavadocs.java │ │ │ ├── GeneratorKeys.java │ │ │ ├── GraphAnalysisErrorHandler.java │ │ │ ├── GraphAnalysisInjectBinding.java │ │ │ ├── GraphAnalysisLoader.java │ │ │ ├── GraphAnalysisProcessor.java │ │ │ ├── GraphAnalysisStaticInjection.java │ │ │ ├── GraphVisualizer.java │ │ │ ├── GraphVizWriter.java │ │ │ ├── InjectAdapterProcessor.java │ │ │ ├── ModuleAdapterProcessor.java │ │ │ ├── Util.java │ │ │ └── ValidationProcessor.java │ └── resources │ │ └── META-INF │ │ └── services │ │ └── javax.annotation.processing.Processor │ └── test │ └── java │ └── dagger │ ├── internal │ └── codegen │ │ ├── DotWriterTest.java │ │ ├── GraphAnalysisLoaderTest.java │ │ └── GraphVisualizerTest.java │ ├── testing │ └── it │ │ └── BuildLogValidator.java │ └── tests │ └── integration │ ├── ProcessorTestUtils.java │ ├── codegen │ ├── GenericInjectAdapterGenerationTest.java │ ├── InjectAdapterGenerationTest.java │ └── ModuleAdapterGenerationTest.java │ ├── operation │ ├── FailureModeErrorsTest.java │ ├── PrimitiveInjectionTest.java │ └── SimpleInjectionTest.java │ └── validation │ ├── CyclicDependencyTest.java │ ├── CyclicModuleIncludesTest.java │ ├── GeneratedTypesNotReadyTest.java │ ├── LibraryModuleTest.java │ ├── ScopeAnnotationUseTest.java │ └── SimpleMissingDependencyTest.java ├── core ├── pom.xml └── src │ ├── main │ └── java │ │ └── dagger │ │ ├── Lazy.java │ │ ├── MembersInjector.java │ │ ├── Module.java │ │ ├── ObjectGraph.java │ │ ├── Provides.java │ │ └── internal │ │ ├── ArrayQueue.java │ │ ├── Binding.java │ │ ├── BindingsGroup.java │ │ ├── BuiltInBinding.java │ │ ├── FailoverLoader.java │ │ ├── Keys.java │ │ ├── LazyBinding.java │ │ ├── Linker.java │ │ ├── Loader.java │ │ ├── Memoizer.java │ │ ├── ModuleAdapter.java │ │ ├── Modules.java │ │ ├── ProblemDetector.java │ │ ├── ProvidesBinding.java │ │ ├── SetBinding.java │ │ ├── StaticInjection.java │ │ ├── ThrowingErrorHandler.java │ │ └── loaders │ │ ├── GeneratedAdapters.java │ │ ├── ReflectiveAtInjectBinding.java │ │ └── ReflectiveStaticInjection.java │ └── test │ └── java │ └── dagger │ ├── ExtensionTest.java │ ├── ExtensionWithSetBindingsTest.java │ ├── ExtensionWithStateTest.java │ ├── InjectStaticsTest.java │ ├── InjectionOfLazyTest.java │ ├── InjectionTest.java │ ├── LazyInjectionTest.java │ ├── MembersInjectorTest.java │ ├── ModuleTest.java │ ├── ProblemDetectorTest.java │ ├── SetBindingTest.java │ ├── ThreadSafetyTest.java │ ├── UnusedProviderTest.java │ └── internal │ ├── FailoverLoaderTest.java │ ├── KeysTest.java │ ├── SingletonBindingTest.java │ ├── TestingLoader.java │ └── TestingModuleAdapter.java ├── deploy_website.sh ├── examples ├── android-activity-graphs │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── dagger │ │ │ └── activitygraphs │ │ │ ├── ActivityModule.java │ │ │ ├── AndroidModule.java │ │ │ ├── DemoApplication.java │ │ │ ├── DemoBaseActivity.java │ │ │ ├── DemoBaseFragment.java │ │ │ ├── ForActivity.java │ │ │ ├── ForApplication.java │ │ │ └── ui │ │ │ ├── ActivityTitleController.java │ │ │ ├── HomeActivity.java │ │ │ └── HomeFragment.java │ │ └── res │ │ └── values │ │ └── strings.xml ├── android-simple │ ├── README.md │ ├── pom.xml │ └── src │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── dagger │ │ │ └── simple │ │ │ ├── AndroidModule.java │ │ │ ├── DemoApplication.java │ │ │ ├── DemoBaseActivity.java │ │ │ ├── DemoModule.java │ │ │ ├── ForApplication.java │ │ │ └── ui │ │ │ └── HomeActivity.java │ │ └── res │ │ └── values │ │ └── strings.xml ├── pom.xml └── simple │ ├── pom.xml │ └── src │ ├── main │ └── java │ │ └── coffee │ │ ├── CoffeeApp.java │ │ ├── CoffeeMaker.java │ │ ├── DripCoffeeModule.java │ │ ├── ElectricHeater.java │ │ ├── Heater.java │ │ ├── Pump.java │ │ ├── PumpModule.java │ │ └── Thermosiphon.java │ └── test │ └── java │ └── coffee │ └── CoffeeMakerTest.java ├── pom.xml └── website ├── index.html └── static ├── app-theme.css ├── app.css ├── bootstrap-combined.min.css ├── bootstrap.min.js ├── html5shiv.min.js ├── icon-github.png ├── icon-square.png ├── jquery-maven-artifact.min.js ├── jquery.smooth-scroll.min.js ├── logo-square.png └── prettify.js /.buildscript/deploy_snapshot.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Deploy a jar, source jar, and javadoc jar to Sonatype's snapshot repo. 4 | # 5 | # Adapted from https://coderwall.com/p/9b_lfq and 6 | # http://benlimmer.com/2013/12/26/automatically-publish-javadoc-to-gh-pages-with-travis-ci/ 7 | 8 | SLUG="square/dagger" 9 | JDK="oraclejdk7" # Dagger integration tests fail on JDK 8 :( 10 | BRANCH="master" 11 | 12 | set -e 13 | 14 | if [ "$TRAVIS_REPO_SLUG" != "$SLUG" ]; then 15 | echo "Skipping snapshot deployment: wrong repository. Expected '$SLUG' but was '$TRAVIS_REPO_SLUG'." 16 | elif [ "$TRAVIS_JDK_VERSION" != "$JDK" ]; then 17 | echo "Skipping snapshot deployment: wrong JDK. Expected '$JDK' but was '$TRAVIS_JDK_VERSION'." 18 | elif [ "$TRAVIS_PULL_REQUEST" != "false" ]; then 19 | echo "Skipping snapshot deployment: was pull request." 20 | elif [ "$TRAVIS_BRANCH" != "$BRANCH" ]; then 21 | echo "Skipping snapshot deployment: wrong branch. Expected '$BRANCH' but was '$TRAVIS_BRANCH'." 22 | else 23 | echo "Deploying snapshot..." 24 | mvn clean source:jar javadoc:jar deploy --settings=".buildscript/settings.xml" -Dmaven.test.skip=true -Dinvoker.skip=true 25 | echo "Snapshot deployed!" 26 | fi 27 | -------------------------------------------------------------------------------- /.buildscript/settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | sonatype-nexus-snapshots 5 | ${env.CI_DEPLOY_USERNAME} 6 | ${env.CI_DEPLOY_PASSWORD} 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .factorypath 3 | .project 4 | .settings 5 | eclipsebin 6 | 7 | bin 8 | gen 9 | build 10 | out 11 | lib 12 | 13 | target 14 | pom.xml.* 15 | release.properties 16 | 17 | .idea 18 | *.iml 19 | classes 20 | 21 | obj 22 | 23 | .DS_Store 24 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | 3 | android: 4 | components: 5 | - build-tools-20.0.0 6 | - android-16 7 | licenses: 8 | - android-sdk-license-5be876d5 9 | 10 | jdk: 11 | - oraclejdk7 12 | # - oraclejdk8 Dagger integration tests fail with JDK 8 :( 13 | 14 | install: mvn install clean --fail-never --quiet -DskipTests=true -Dinvoker.skip=true 15 | 16 | script: mvn verify 17 | 18 | after_success: 19 | - .buildscript/deploy_snapshot.sh 20 | 21 | env: 22 | global: 23 | - secure: "MhR0Wr+bSbdyO6a6CM2MVHLwmFZoi8ZWWncMCNSi8/xdRoykpC1HkwJjQK+HJv7j3VNbVTPNJ/yTpzJsL4JV9aTF/S28mka8GmHSmQSeQzTuNMqnE30GDbhS3S73azGHvC9/wjh1mAA0Gz/zUX/rzCYvDVZ/DmK1HppomN+P32A=" 24 | - secure: "fnHK/ei7tdcUDlQZcXWVPRgXoIFv6h0TWSzz4spgNtFlYqa47Qr4HQOLyEpWRqZHjgfR5eXD+CCI049Z73cg5oVOp1krV0aGX/wHc5lDMJuCrBZ/YoWZnDygiPzM4CvrpxRE7DjqzC0InVdbEbuECiFsI2WotLdQ6efgaxjpZCk=" 25 | 26 | branches: 27 | except: 28 | - gh-pages 29 | 30 | notifications: 31 | email: false 32 | 33 | sudo: false 34 | 35 | cache: 36 | directories: 37 | - $HOME/.m2 38 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Change Log 2 | ========== 3 | 4 | Version 1.2.5 *(2016-05-09)* 5 | ---------------------------- 6 | 7 | * Fix: Correctly emit generated code for binding parameterized types. 8 | 9 | 10 | Version 1.2.4 *(2016-05-03)* 11 | ---------------------------- 12 | 13 | * Fix: Restore static injection support to work correctly. 14 | 15 | 16 | Version 1.2.3 *(2016-05-02)* 17 | ---------------------------- 18 | 19 | * Fix: Correct detection of module base classes. This previously erroneously failed compilation 20 | on modules which extended from `Object` but were not detected as such. 21 | * Fix: Allow the use of dollar signs in processed class names. 22 | * Fix: Remove the need for `javac` to generate synthetic accessor methods for internal classes. 23 | * Fix: Error when duplicate classes are listed in `injects=` or `includes=` lists. 24 | 25 | 26 | Version 1.2.2 *(2014-07-21)* 27 | ---------------------------- 28 | 29 | * Update JavaWriter to 2.5.0. This fixes incorrectly compressing fully-qualified class names 30 | in child packages of `java.lang` (e.g., `java.lang.ref.WeakReference`). 31 | 32 | 33 | Version 1.2.1 *(2014-02-16)* 34 | ---------------------------- 35 | 36 | * Restore Java 5 compatibility. 37 | * New: Improve performance of `.plus()` with large volumes of set bindings. 38 | * Fix: Do not mask underlying exception message from binding problems when constructing a graph. 39 | 40 | 41 | Version 1.2.0 *(2013-12-13)* 42 | ---------------------------- 43 | 44 | * Numerous performance improvements in both the compiler and runtime. 45 | * Use more efficient `String` concatenation. 46 | * Module adapters are now stateless. 47 | * Use read/write locks over global locks. 48 | * Reflective constructor invocation is now cached with `Class.newInstance`. 49 | * Avoid re-linking all bindings when calling `.plus()`. 50 | * Set bindings are now unioned when calling `.plus()`. 51 | * Fix: Tolerate missing type information during compilation by deferring writing 52 | module adapters. 53 | 54 | 55 | Version 1.1.0 *(2013-08-05)* 56 | ---------------------------- 57 | 58 | * Module loading now requires code generation via the 'dagger-compiler' artifact. 59 | * Allow multiple contributions to Set binding via `Provides.Type.SET_VALUES`. 60 | * Request classloading from the classloader of the requesting object, not the current thread's 61 | context classloader. 62 | * Cache class loading at the root injector to reduce costs of loading adapters. 63 | * Fix: Primitive array types are no longer incorrectly changed to their boxed type. 64 | * Update JavaWriter to 2.1.1. 65 | 66 | 67 | Version 1.0.1 *(2013-06-03)* 68 | ---------------------------- 69 | 70 | * Explicitly forbid declaring `@Inject` on a class type (e.g., `@Inject class Foo {}`). 71 | * Update JavaWriter to 1.0.5. 72 | 73 | 74 | Version 1.0.0 *(2013-05-07)* 75 | ---------------------------- 76 | 77 | Initial release. 78 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contributing 2 | ============ 3 | 4 | If you would like to contribute code to Dagger you can do so through GitHub by 5 | forking the repository and sending a pull request. 6 | 7 | When submitting code, please make every effort to follow existing conventions 8 | and style in order to keep the code as readable as possible. 9 | 10 | Where appropriate, please provide unit tests or integration tests. Unit tests 11 | should be JUnit based tests and can use either standard JUnit assertions or 12 | FEST assertions and be added to `/src/test/java`. Changes to build-time 13 | behaviour (such as changes to code generation or graph validation) should go into 14 | small maven projects using the `maven-invoker-plugin`. Examples of this are in 15 | `core/src/it` and can include bean-shell verification scripts and other 16 | facilities provided by `maven-invoker-plugin`. 17 | 18 | Please make sure your code compiles by running `mvn clean verify` which will 19 | execute both unit and integration test phases. Additionally, consider using 20 | http://travis-ci.org to validate your branches before you even put them into 21 | pull requests. All pull requests will be validated by Travis-ci in any case 22 | and must pass before being merged. 23 | 24 | If you are adding or modifying files you may add your own copyright line, but 25 | please ensure that the form is consistent with the existing files, and please 26 | note that a Square, Inc. copyright line must appear in every copyright notice. 27 | All files are released with the Apache 2.0 license. 28 | 29 | Checkstyle failures during compilation indicate errors in your style and will 30 | be displayed in the console output of the build (including in Travis-CI output), 31 | or can be viewed in the `checkstyle-result.xml` file. 32 | 33 | Before your code can be accepted into the project you must sign the 34 | [Individual Contributor License Agreement (CLA)][1]. 35 | 36 | 37 | [1]: https://spreadsheets.google.com/spreadsheet/viewform?formkey=dDViT2xzUHAwRkI3X3k5Z0lQM091OGc6MQ&ndplr=1 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Dagger 1 2 | ======== 3 | 4 | A fast dependency injector for Android and Java. 5 | 6 | 7 | Deprecated – Please upgrade to Dagger 2 8 | --------------------------------------- 9 | 10 | Square's Dagger 1.x is deprecated in favor of [Google's Dagger 2](https://github.com/google/dagger). 11 | Please see [the migration guide](https://google.github.io/dagger/dagger-1-migration.html) for help 12 | with the upgrade. 13 | 14 | 15 | Download Dagger 1 16 | ----------------- 17 | 18 | You will need to include the `dagger-${dagger.version}.jar` in your 19 | application's runtime. In order to activate code generation you will need to 20 | include `dagger-compiler-${dagger.version}.jar` in your build at compile time. 21 | 22 | In a Maven project, one would include the runtime in the dependencies section 23 | of your `pom.xml` (replacing `${dagger.version}` with the appropriate current 24 | release), and the `dagger-compiler` artifact as an "optional" or "provided" 25 | dependency: 26 | 27 | ```xml 28 | 29 | 30 | com.squareup.dagger 31 | dagger 32 | ${dagger.version} 33 | 34 | 35 | com.squareup.dagger 36 | dagger-compiler 37 | ${dagger.version} 38 | true 39 | 40 | 41 | ``` 42 | 43 | You can also find downloadable .jars on Maven Central. You'll need 44 | [Dagger][dl-dagger], [JavaPoet][dl-javapoet], and [javax.inject][dl-inject]. 45 | 46 | Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap]. 47 | 48 | 49 | 50 | License 51 | ------- 52 | 53 | Copyright 2012 Square, Inc. 54 | 55 | Licensed under the Apache License, Version 2.0 (the "License"); 56 | you may not use this file except in compliance with the License. 57 | You may obtain a copy of the License at 58 | 59 | http://www.apache.org/licenses/LICENSE-2.0 60 | 61 | Unless required by applicable law or agreed to in writing, software 62 | distributed under the License is distributed on an "AS IS" BASIS, 63 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 64 | See the License for the specific language governing permissions and 65 | limitations under the License. 66 | 67 | 68 | 69 | [1]: http://square.github.com/dagger/ 70 | [dl-dagger]: http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.squareup.dagger%22%20a%3A%22dagger%22 71 | [dl-javapoet]: http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.squareup%22%20a%3A%22javapoet%22 72 | [dl-inject]: http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22javax.inject%22%20a%3A%22javax.inject%22 73 | [snap]: https://oss.sonatype.org/content/repositories/snapshots/ 74 | -------------------------------------------------------------------------------- /compiler/src/it/default-package-injected-type/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | default-package-injected-type 25 | HEAD-SNAPSHOT 26 | Default Package Injected Type 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/default-package-injected-type/src/main/java/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Google, Inc. 3 | * Copyright (C) 2012 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | import dagger.ObjectGraph; 19 | import dagger.Module; 20 | import javax.inject.Inject; 21 | import javax.inject.Singleton; 22 | 23 | class TestApp implements Runnable { 24 | @Inject A a; 25 | 26 | @Override public void run() { 27 | a.doit(); 28 | } 29 | 30 | public static void main(String[] args) { 31 | ObjectGraph.create(new TestModule()).get(TestApp.class).run(); 32 | } 33 | 34 | @Module(injects = { TestApp.class }) 35 | static class TestModule {} 36 | 37 | @Singleton 38 | static class A { 39 | @Inject A() {} 40 | public void doit() {}; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /compiler/src/it/extension-graph-setvalues/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | extension-graph-setvalues 25 | HEAD-SNAPSHOT 26 | Dagger Integration Test Basic 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/extension-graph-setvalues/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Google, Inc. 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import dagger.ObjectGraph; 20 | import dagger.Module; 21 | import dagger.Provides; 22 | import java.util.HashSet; 23 | import java.util.Set; 24 | import javax.inject.Inject; 25 | import javax.inject.Provider; 26 | import javax.inject.Singleton; 27 | 28 | import static dagger.Provides.Type.SET; 29 | import static dagger.Provides.Type.SET_VALUES; 30 | 31 | /** 32 | * Contributions to {@code SET_VALUES} binding do not affect Set of providers. 33 | */ 34 | class TestApp implements Runnable { 35 | @Inject Set> providers; 36 | @Inject Set strings; 37 | 38 | @Override public void run() { 39 | System.out.println(strings); 40 | } 41 | 42 | public static void main(String[] args) { 43 | ObjectGraph root = ObjectGraph.create(new RootModule()); 44 | ObjectGraph extension = root.plus(new ExtensionModule()); 45 | extension.get(TestApp.class).run(); 46 | } 47 | 48 | @Module(injects = TestApp.class) 49 | static class RootModule { 50 | @Provides Set> providers() { 51 | return new HashSet>(); 52 | } 53 | @Provides(type = SET_VALUES) Set strings() { 54 | return new HashSet(); 55 | } 56 | } 57 | 58 | @Module(addsTo = RootModule.class, injects = TestApp.class) 59 | static class ExtensionModule { 60 | @Provides(type = SET) String addToSet() { 61 | return "contributed"; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /compiler/src/it/extension-graph/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | extension-graph 25 | HEAD-SNAPSHOT 26 | Dagger Integration Test Basic 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/extension-graph/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Google, Inc. 3 | * Copyright (C) 2012 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import dagger.ObjectGraph; 20 | import dagger.Module; 21 | import javax.inject.Inject; 22 | import javax.inject.Singleton; 23 | 24 | class TestApp implements Runnable { 25 | @Inject C c; 26 | 27 | @Override public void run() { 28 | c.doit(); 29 | } 30 | 31 | public static void main(String[] args) { 32 | ObjectGraph root = ObjectGraph.create(new RootModule()); 33 | ObjectGraph extension = root.plus(new ExtensionModule()); 34 | extension.get(TestApp.class).run(); 35 | } 36 | 37 | @Module(injects = { A.class, B.class }) 38 | static class RootModule { } 39 | 40 | @Module(addsTo=RootModule.class, injects = { C.class, TestApp.class }) 41 | static class ExtensionModule { } 42 | 43 | @Singleton 44 | static class A { 45 | @Inject A() {} 46 | } 47 | 48 | static class B { 49 | @Inject A a; 50 | @Inject B() {} 51 | } 52 | 53 | static class C { 54 | @Inject A a; 55 | @Inject B b; 56 | @Inject C() {} 57 | public void doit() {}; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /compiler/src/it/final-field-inject/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure 2 | -------------------------------------------------------------------------------- /compiler/src/it/final-field-inject/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | final-field-inject 25 | HEAD-SNAPSHOT 26 | 27 | 28 | @dagger.groupId@ 29 | dagger 30 | @dagger.version@ 31 | 32 | 33 | @dagger.groupId@ 34 | dagger-compiler 35 | @dagger.version@ 36 | true 37 | 38 | 39 | 40 | 41 | 42 | maven-compiler-plugin 43 | 3.1 44 | 45 | 1.5 46 | 1.5 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /compiler/src/it/final-field-inject/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Google, Inc. 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import javax.inject.Inject; 20 | 21 | class TestApp { 22 | @Inject final Object nope; 23 | } 24 | -------------------------------------------------------------------------------- /compiler/src/it/final-field-inject/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "Can't inject a final field: test.TestApp.nope"}); 7 | -------------------------------------------------------------------------------- /compiler/src/it/include-non-module/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure -------------------------------------------------------------------------------- /compiler/src/it/include-non-module/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | include-non-module 25 | HEAD-SNAPSHOT 26 | Dagger Integration Test Basic 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/include-non-module/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test; 17 | 18 | import dagger.Module; 19 | import dagger.ObjectGraph; 20 | import dagger.Provides; 21 | import javax.inject.Inject; 22 | import java.lang.String; 23 | 24 | class TestApp { 25 | public static void main(String[] args) { 26 | TestApp app = ObjectGraph.create(new TestModule()).get(TestApp.class); 27 | } 28 | 29 | @Inject String s; 30 | 31 | @Module( 32 | injects = TestApp.class, 33 | includes = TestApp.class) 34 | static class TestModule { 35 | @Provides String provideString() { 36 | return "a"; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /compiler/src/it/inject-on-class/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure -------------------------------------------------------------------------------- /compiler/src/it/inject-on-class/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 21 | 4.0.0 22 | com.example.dagger.tests 23 | method-injection 24 | HEAD-SNAPSHOT 25 | 26 | 27 | @dagger.groupId@ 28 | dagger 29 | @dagger.version@ 30 | 31 | 32 | @dagger.groupId@ 33 | dagger-compiler 34 | @dagger.version@ 35 | true 36 | 37 | 38 | 39 | 40 | 41 | maven-compiler-plugin 42 | 3.1 43 | 44 | 1.5 45 | 1.5 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /compiler/src/it/inject-on-class/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test; 17 | 18 | import javax.inject.Inject; 19 | 20 | @Inject 21 | class TestApp { 22 | } 23 | -------------------------------------------------------------------------------- /compiler/src/it/inject-on-class/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "@Inject is not valid on a class: test.TestApp"}); 7 | -------------------------------------------------------------------------------- /compiler/src/it/inject-parameterized-type/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | inject-parameterized-type 25 | HEAD-SNAPSHOT 26 | Dagger Integration Test Basic 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/inject-parameterized-type/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test; 17 | 18 | import dagger.Module; 19 | import dagger.ObjectGraph; 20 | import dagger.Provides; 21 | import javax.inject.Inject; 22 | 23 | class TestApp { 24 | public static void main(String[] args) { 25 | Subtype subtype = ObjectGraph.create(new TestModule()).get(Subtype.class); 26 | } 27 | 28 | static class Supertype { 29 | @Inject String s; 30 | } 31 | 32 | static class Subtype extends Supertype { 33 | } 34 | 35 | @Module(injects = Subtype.class) 36 | static class TestModule { 37 | @Provides String provideString() { 38 | return "a"; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /compiler/src/it/inner-classes-complaint-injection/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure -------------------------------------------------------------------------------- /compiler/src/it/inner-classes-complaint-injection/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 21 | 4.0.0 22 | com.example.dagger.tests 23 | inner-classes-complaint-injection 24 | HEAD-SNAPSHOT 25 | Dagger Integration Test Basic 26 | 27 | 28 | @dagger.groupId@ 29 | dagger 30 | @dagger.version@ 31 | 32 | 33 | @dagger.groupId@ 34 | dagger-compiler 35 | @dagger.version@ 36 | true 37 | 38 | 39 | 40 | 41 | 42 | maven-compiler-plugin 43 | 3.1 44 | 45 | 1.5 46 | 1.5 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /compiler/src/it/inner-classes-complaint-injection/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test; 17 | 18 | import dagger.Module; 19 | 20 | import dagger.ObjectGraph; 21 | import dagger.Provides; 22 | 23 | import javax.inject.Inject; 24 | import java.lang.Override; 25 | 26 | class TestApp { 27 | 28 | class Foo { 29 | @Inject public Foo() {} 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /compiler/src/it/inner-classes-complaint-injection/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "Can't inject a non-static inner class: test.TestApp.Foo"}); 7 | -------------------------------------------------------------------------------- /compiler/src/it/method-injection/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure -------------------------------------------------------------------------------- /compiler/src/it/method-injection/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 21 | 4.0.0 22 | com.example.dagger.tests 23 | method-injection 24 | HEAD-SNAPSHOT 25 | 26 | 27 | @dagger.groupId@ 28 | dagger 29 | @dagger.version@ 30 | 31 | 32 | @dagger.groupId@ 33 | dagger-compiler 34 | @dagger.version@ 35 | true 36 | 37 | 38 | 39 | 40 | 41 | maven-compiler-plugin 42 | 3.1 43 | 44 | 1.5 45 | 1.5 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /compiler/src/it/method-injection/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test; 17 | 18 | import javax.inject.Inject; 19 | 20 | class TestApp { 21 | @Inject public void doThings(Object things) { 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /compiler/src/it/method-injection/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "Method injection is not supported: test.TestApp.doThings"}); 7 | -------------------------------------------------------------------------------- /compiler/src/it/missing-at-inject-constructor/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure -------------------------------------------------------------------------------- /compiler/src/it/missing-at-inject-constructor/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | missing-at-inject-constructor 25 | HEAD-SNAPSHOT 26 | Dagger Integration Test Basic 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/missing-at-inject-constructor/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Google, Inc. 3 | * Copyright (C) 2012 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import dagger.Module; 20 | 21 | import dagger.ObjectGraph; 22 | import javax.inject.Inject; 23 | 24 | class TestApp implements Runnable { 25 | @Inject Dependency dep; 26 | 27 | @Override public void run() { 28 | dep.doit(); 29 | } 30 | 31 | public static void main(String[] args) { 32 | ObjectGraph.create(new TestModule()).get(TestApp.class).run(); 33 | } 34 | 35 | static class Dependency { 36 | // missing @Inject Dependency() {} 37 | public void doit() { throw AssertionError(); }; 38 | } 39 | 40 | @Module(injects = TestApp.class) 41 | static class TestModule { 42 | /* missing */ // @Provides Dependency a() { return new Dependency(); } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /compiler/src/it/missing-at-inject-constructor/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "No injectable members on test.TestApp.Dependency.", 7 | "required by test.TestApp for test.TestApp.TestModule"}); 8 | -------------------------------------------------------------------------------- /compiler/src/it/module-type-validation/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure -------------------------------------------------------------------------------- /compiler/src/it/module-type-validation/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 21 | 4.0.0 22 | com.example.dagger.tests 23 | module-type-validation 24 | HEAD-SNAPSHOT 25 | 26 | 27 | @dagger.groupId@ 28 | dagger 29 | @dagger.version@ 30 | 31 | 32 | @dagger.groupId@ 33 | dagger-compiler 34 | @dagger.version@ 35 | true 36 | 37 | 38 | 39 | 40 | 41 | maven-compiler-plugin 42 | 3.1 43 | 44 | 1.5 45 | 1.5 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /compiler/src/it/module-type-validation/src/main/java/test/TestModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test; 17 | 18 | import dagger.Module; 19 | 20 | @Module 21 | class ThreadModule extends Thread { 22 | } 23 | 24 | @Module 25 | enum EnumModule { 26 | } 27 | 28 | @Module 29 | interface InterfaceModule { 30 | } 31 | -------------------------------------------------------------------------------- /compiler/src/it/module-type-validation/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "Modules must not extend from other classes: test.ThreadModule"}); 7 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 8 | "Modules must be classes: test.EnumModule"}); 9 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 10 | "Modules must be classes: test.InterfaceModule"}); -------------------------------------------------------------------------------- /compiler/src/it/multiple-modules-setvalues/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | multiple-modules-setvalues 25 | HEAD-SNAPSHOT 26 | Dagger Integration Test Basic 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/multiple-modules-setvalues/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Google, Inc. 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import dagger.ObjectGraph; 20 | import dagger.Module; 21 | import dagger.Provides; 22 | import java.util.HashSet; 23 | import java.util.Set; 24 | import javax.inject.Inject; 25 | import javax.inject.Provider; 26 | import javax.inject.Singleton; 27 | 28 | import static dagger.Provides.Type.SET; 29 | import static dagger.Provides.Type.SET_VALUES; 30 | 31 | /** 32 | * Contributions to {@code SET_VALUES} binding do not affect Set of providers. 33 | */ 34 | class TestApp implements Runnable { 35 | @Inject Set> providers; 36 | @Inject Set strings; 37 | 38 | @Override public void run() { 39 | System.out.println(strings); 40 | } 41 | 42 | public static void main(String[] args) { 43 | ObjectGraph root = ObjectGraph.create(new RootModule(), new ContributingModule()); 44 | root.get(TestApp.class).run(); 45 | } 46 | 47 | @Module(injects = TestApp.class) 48 | static class RootModule { 49 | @Provides Set> providers() { 50 | return new HashSet>(); 51 | } 52 | @Provides(type = SET_VALUES) Set strings() { 53 | return new HashSet(); 54 | } 55 | } 56 | 57 | @Module(injects = TestApp.class, complete = false) 58 | static class ContributingModule { 59 | @Provides(type = SET) String addToSet() { 60 | return "contributed"; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /compiler/src/it/multiple-provides-methods/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | multiple-provides-methods 25 | HEAD-SNAPSHOT 26 | Dagger Integration Test Basic 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/multiple-provides-methods/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * Copyright (C) 2013 Google, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import javax.inject.Inject; 22 | 23 | class TestApp { 24 | static class NotInjectable { 25 | } 26 | 27 | static class InjectableSubclass extends NotInjectable { 28 | @Inject String string; 29 | @Inject Integer integer; 30 | } 31 | 32 | @Module(injects = InjectableSubclass.class) 33 | static class TestModule { 34 | @Provides String string() { 35 | return "string"; 36 | } 37 | @Provides Integer integer() { 38 | return 5; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /compiler/src/it/multiple-provides-methods/verify.bsh: -------------------------------------------------------------------------------- 1 | import java.io.File; 2 | 3 | File classes = new File(basedir, "target/classes/test/"); 4 | 5 | File moduleAdapter = new File(classes, "TestApp$TestModule$$ModuleAdapter.class"); 6 | if (!moduleAdapter.exists()) throw new Exception("No binding generated for module"); 7 | 8 | File integerBinding = new File(classes, "TestApp$TestModule$$ModuleAdapter$IntegerProvidesAdapter.class"); 9 | if (!integerBinding.exists()) throw new Exception("No binding generated for integer()"); 10 | 11 | File stringBinding = new File(classes, "TestApp$TestModule$$ModuleAdapter$StringProvidesAdapter.class"); 12 | if (!stringBinding.exists()) throw new Exception("No binding generated for string()"); 13 | -------------------------------------------------------------------------------- /compiler/src/it/multiple-qualifiers/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure 2 | -------------------------------------------------------------------------------- /compiler/src/it/multiple-qualifiers/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | multiple-qualifiers 25 | HEAD-SNAPSHOT 26 | 27 | 28 | @dagger.groupId@ 29 | dagger 30 | @dagger.version@ 31 | 32 | 33 | @dagger.groupId@ 34 | dagger-compiler 35 | @dagger.version@ 36 | true 37 | 38 | 39 | 40 | 41 | 42 | maven-compiler-plugin 43 | 3.1 44 | 45 | 1.5 46 | 1.5 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /compiler/src/it/multiple-qualifiers/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Google, Inc. 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import java.lang.annotation.Retention; 22 | import javax.inject.Inject; 23 | import javax.inject.Singleton; 24 | import javax.inject.Qualifier; 25 | 26 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 27 | 28 | class TestApp { 29 | static class TestClass1 { 30 | @Inject 31 | @MyQualifier1 32 | @MyQualifier2 33 | String field; 34 | } 35 | 36 | static class TestClass2 { 37 | String string; 38 | 39 | public TestClass2(@MyQualifier1 @MyQualifier2 String constructorParam) { 40 | this.string = string; 41 | } 42 | } 43 | 44 | @Module(injects = TestClass1.class) 45 | static class TestModule { 46 | @MyQualifier1 47 | @MyQualifier2 48 | @Provides 49 | String providesString() { 50 | return "string"; 51 | } 52 | } 53 | 54 | @Qualifier 55 | @Retention(value = RUNTIME) 56 | @interface MyQualifier1 {} 57 | 58 | @Qualifier 59 | @Retention(value = RUNTIME) 60 | @interface MyQualifier2 {} 61 | } 62 | -------------------------------------------------------------------------------- /compiler/src/it/multiple-qualifiers/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "Only one qualifier annotation is allowed per element: test.TestApp.TestClass1.field"}); 7 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 8 | "Only one qualifier annotation is allowed per element: constructorParam"}); 9 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 10 | "Only one qualifier annotation is allowed per element: test.TestApp.TestModule.providesString()"}); 11 | -------------------------------------------------------------------------------- /compiler/src/it/private-inject/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure -------------------------------------------------------------------------------- /compiler/src/it/private-inject/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 21 | 4.0.0 22 | com.example.dagger.tests 23 | private-inject 24 | HEAD-SNAPSHOT 25 | 26 | 27 | @dagger.groupId@ 28 | dagger 29 | @dagger.version@ 30 | 31 | 32 | @dagger.groupId@ 33 | dagger-compiler 34 | @dagger.version@ 35 | true 36 | 37 | 38 | 39 | 40 | 41 | maven-compiler-plugin 42 | 3.1 43 | 44 | 1.5 45 | 1.5 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /compiler/src/it/private-inject/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test; 17 | 18 | import javax.inject.Inject; 19 | 20 | class TestApp { 21 | @Inject private Object nope; 22 | } 23 | -------------------------------------------------------------------------------- /compiler/src/it/private-inject/src/main/java/test/TestFoo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test; 17 | 18 | import javax.inject.Inject; 19 | 20 | class TestFoo { 21 | @Inject private TestFoo() { 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /compiler/src/it/private-inject/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "Can't inject a private field: test.TestApp.nope"}); 7 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 8 | "Can't inject a private constructor: test.TestFoo.TestFoo()"}); 9 | -------------------------------------------------------------------------------- /compiler/src/it/provide-provider-or-lazy/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure -------------------------------------------------------------------------------- /compiler/src/it/provide-provider-or-lazy/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 21 | 4.0.0 22 | com.example.dagger.tests 23 | provide-provider-or-lazy 24 | HEAD-SNAPSHOT 25 | 26 | 27 | @dagger.groupId@ 28 | dagger 29 | @dagger.version@ 30 | 31 | 32 | @dagger.groupId@ 33 | dagger-compiler 34 | @dagger.version@ 35 | true 36 | 37 | 38 | 39 | 40 | 41 | maven-compiler-plugin 42 | 3.1 43 | 44 | 1.5 45 | 1.5 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /compiler/src/it/provide-provider-or-lazy/src/main/java/test/TestModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test; 17 | 18 | import dagger.Lazy; 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import javax.inject.Provider; 22 | 23 | @Module class TestModule { 24 | @Provides Provider provideProvider() { 25 | return null; 26 | } 27 | 28 | @Provides Provider provideRawProvider() { 29 | return null; 30 | } 31 | 32 | @Provides Lazy provideLazy() { 33 | return null; 34 | } 35 | 36 | @Provides Lazy provideRawLazy() { 37 | return null; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /compiler/src/it/provide-provider-or-lazy/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "@Provides method must not return javax.inject.Provider directly: test.TestModule.provideProvider"}); 7 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 8 | "@Provides method must not return javax.inject.Provider directly: test.TestModule.provideRawProvider"}); 9 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 10 | "@Provides method must not return dagger.Lazy directly: test.TestModule.provideLazy"}); 11 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 12 | "@Provides method must not return dagger.Lazy directly: test.TestModule.provideRawLazy"}); 13 | -------------------------------------------------------------------------------- /compiler/src/it/provides-method-not-in-module/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure 2 | -------------------------------------------------------------------------------- /compiler/src/it/provides-method-not-in-module/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | provides-method-not-in-module 25 | HEAD-SNAPSHOT 26 | 27 | 28 | @dagger.groupId@ 29 | dagger 30 | @dagger.version@ 31 | 32 | 33 | @dagger.groupId@ 34 | dagger-compiler 35 | @dagger.version@ 36 | true 37 | 38 | 39 | 40 | 41 | 42 | maven-compiler-plugin 43 | 3.1 44 | 45 | 1.5 46 | 1.5 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /compiler/src/it/provides-method-not-in-module/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Google, Inc. 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import dagger.Provides; 20 | 21 | class TestApp { 22 | 23 | @Provides 24 | String providesString() { 25 | return "string"; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /compiler/src/it/provides-method-not-in-module/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "@Provides methods must be declared in modules: test.TestApp.providesString()"}); 7 | -------------------------------------------------------------------------------- /compiler/src/it/provides-method-with-throws-clause/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure -------------------------------------------------------------------------------- /compiler/src/it/provides-method-with-throws-clause/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | provides-method-with-throws-clause 25 | HEAD-SNAPSHOT 26 | Dagger Integration Test Basic 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/provides-method-with-throws-clause/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Google, Inc. 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import dagger.Module; 20 | 21 | import dagger.ObjectGraph; 22 | import dagger.Provides; 23 | 24 | import javax.inject.Inject; 25 | import java.io.IOException; 26 | import java.lang.Override; 27 | 28 | class TestApp implements Runnable { 29 | 30 | @Inject String string; 31 | 32 | @Override public void run() { 33 | // Yay! \o/ 34 | } 35 | 36 | public static void main(String[] args) { 37 | ObjectGraph.create(new TestModule()).get(TestApp.class).run(); 38 | } 39 | 40 | @Module(injects = TestApp.class) 41 | static class TestModule { 42 | 43 | @Provides String string() throws IOException { 44 | return "string"; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /compiler/src/it/provides-method-with-throws-clause/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "@Provides methods must not have a throws clause"}); 7 | -------------------------------------------------------------------------------- /compiler/src/it/qualifiers-on-invalid-elements-errors/invoker.properties: -------------------------------------------------------------------------------- 1 | invoker.buildResult=failure 2 | -------------------------------------------------------------------------------- /compiler/src/it/qualifiers-on-invalid-elements-errors/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | qualifiers-on-invalid-elements-errors 25 | HEAD-SNAPSHOT 26 | 27 | 28 | @dagger.groupId@ 29 | dagger 30 | @dagger.version@ 31 | 32 | 33 | @dagger.groupId@ 34 | dagger-compiler 35 | @dagger.version@ 36 | true 37 | 38 | 39 | 40 | 41 | 42 | maven-compiler-plugin 43 | 3.1 44 | 45 | 1.5 46 | 1.5 47 | true 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/qualifiers-on-invalid-elements-errors/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Google, Inc. 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import java.lang.annotation.Retention; 22 | import javax.inject.Inject; 23 | import javax.inject.Qualifier; 24 | 25 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 26 | 27 | class TestApp { 28 | 29 | @MyQualifier 30 | static class TestClass1 { 31 | 32 | @MyQualifier // qualifier on non-injectable constructor 33 | public TestClass1(String constructorParam) {} 34 | } 35 | 36 | static class TestClass2 { 37 | String string; 38 | 39 | @Inject 40 | @MyQualifier // qualifier on injectable constructor 41 | public TestClass2(String injectableConstructorParam) { 42 | this.string = string; 43 | } 44 | } 45 | 46 | @Qualifier 47 | @Retention(value = RUNTIME) 48 | @interface MyQualifier {} 49 | } 50 | -------------------------------------------------------------------------------- /compiler/src/it/qualifiers-on-invalid-elements-errors/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "Qualifier annotations are only allowed on fields, methods, and parameters: test.TestApp.TestClass1"}); 7 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 8 | "Qualifier annotations are only allowed on fields, methods, and parameters: test.TestApp.TestClass1.TestClass1(java.lang.String)"}); 9 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 10 | "Qualifier annotations are only allowed on fields, methods, and parameters: test.TestApp.TestClass2.TestClass2(java.lang.String)"}); 11 | -------------------------------------------------------------------------------- /compiler/src/it/qualifiers-on-invalid-elements-warnings/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | qualifiers-on-invalid-elements-warnings 25 | HEAD-SNAPSHOT 26 | 27 | 28 | @dagger.groupId@ 29 | dagger 30 | @dagger.version@ 31 | 32 | 33 | @dagger.groupId@ 34 | dagger-compiler 35 | @dagger.version@ 36 | true 37 | 38 | 39 | 40 | 41 | 42 | maven-compiler-plugin 43 | 3.1 44 | 45 | 1.5 46 | 1.5 47 | true 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/qualifiers-on-invalid-elements-warnings/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Google, Inc. 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import java.lang.annotation.Retention; 22 | import javax.inject.Inject; 23 | import javax.inject.Qualifier; 24 | 25 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 26 | 27 | class TestApp { 28 | 29 | static class TestClass { 30 | @MyQualifier int field1; // non-injectable field 31 | 32 | @SuppressWarnings("some string other than 'qualifiers'") 33 | @MyQualifier 34 | int field2; 35 | 36 | @SuppressWarnings("qualifiers") 37 | @MyQualifier 38 | int fieldWithWarningSuppressed1; 39 | 40 | @SuppressWarnings({"foo", "qualifiers", "bar"}) 41 | @MyQualifier 42 | int fieldWithWarningSuppressed2; 43 | 44 | // qualifier on non-injectable constructor parameter 45 | public TestClass(@MyQualifier String constructorParam) {} 46 | 47 | @MyQualifier 48 | void nonProvidesMethod(@MyQualifier String methodParam) {} 49 | } 50 | 51 | @Qualifier 52 | @Retention(value = RUNTIME) 53 | @interface MyQualifier {} 54 | } 55 | -------------------------------------------------------------------------------- /compiler/src/it/qualifiers-on-invalid-elements-warnings/verify.bsh: -------------------------------------------------------------------------------- 1 | import dagger.testing.it.BuildLogValidator; 2 | import java.io.File; 3 | 4 | File buildLog = new File(basedir, "build.log"); 5 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 6 | "Dagger will ignore qualifier annotations on fields that are not annotated with @Inject: test.TestApp.TestClass.field1"}); 7 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 8 | "Dagger will ignore qualifier annotations on fields that are not annotated with @Inject: test.TestApp.TestClass.field2"}); 9 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 10 | "Dagger will ignore qualifier annotations on methods that are not @Provides methods: test.TestApp.TestClass.nonProvidesMethod(java.lang.String)"}); 11 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 12 | "Dagger will ignore qualifier annotations on parameters that are not @Inject constructor parameters or @Provides method parameters: methodParam"}); 13 | new BuildLogValidator().assertHasText(buildLog, new String[]{ 14 | "Dagger will ignore qualifier annotations on parameters that are not @Inject constructor parameters or @Provides method parameters: constructorParam"}); 15 | new BuildLogValidator().assertDoesNotHaveText(buildLog, new String[]{ 16 | "Dagger will ignore qualifier annotations on fields that are not annotated with @Inject: test.TestApp.TestClass.fieldWithWarningSuppressed1"}); 17 | new BuildLogValidator().assertDoesNotHaveText(buildLog, new String[]{ 18 | "Dagger will ignore qualifier annotations on fields that are not annotated with @Inject: test.TestApp.TestClass.fieldWithWarningSuppressed2"}); 19 | -------------------------------------------------------------------------------- /compiler/src/it/same-provides-method-name/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | same-provides-method-name 25 | HEAD-SNAPSHOT 26 | Dagger Integration Test Basic 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/same-provides-method-name/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Google, Inc. 3 | * Copyright (C) 2012 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import dagger.Module; 20 | 21 | import dagger.ObjectGraph; 22 | import dagger.Provides; 23 | 24 | import javax.inject.Inject; 25 | import java.lang.Override; 26 | 27 | class TestApp implements Runnable { 28 | 29 | @Inject Foo foo; 30 | 31 | @Override public void run() { 32 | // Yay! \o/ 33 | } 34 | 35 | public static void main(String[] args) { 36 | ObjectGraph.create(new TestModule()).get(TestApp.class).run(); 37 | } 38 | 39 | static class Foo { 40 | } 41 | 42 | static class MyFoo extends Foo { 43 | } 44 | 45 | @Module(injects = TestApp.class) 46 | static class TestModule { 47 | 48 | @Provides Foo providesFoo(MyFoo foo) { 49 | return foo; 50 | } 51 | 52 | @Provides MyFoo providesFoo() { 53 | return new MyFoo(); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /compiler/src/it/static-injected-binding-doesnt-fail-providers/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 21 | 4.0.0 22 | com.squareup.dagger.tests 23 | static-injected-binding-doesnt-fail-providers 24 | @dagger.version@ 25 | jar 26 | Dagger Integration Test Basic 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/static-injected-binding-doesnt-fail-providers/src/main/java/test/Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test; 17 | 18 | import dagger.Module; 19 | 20 | import dagger.ObjectGraph; 21 | import dagger.Provides; 22 | import javax.inject.Inject; 23 | 24 | import java.lang.Override; 25 | 26 | public class Test { 27 | 28 | public static class InjectsOneField { 29 | @Inject static String staticallyInjectedString; 30 | } 31 | 32 | @Module(staticInjections = { InjectsOneField.class }) 33 | public static class TestModule { 34 | @Provides String string() { 35 | return "string"; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /compiler/src/it/uninjectable-supertype/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | uninjectable-supertype 25 | HEAD-SNAPSHOT 26 | Dagger Integration Test Basic 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /compiler/src/it/uninjectable-supertype/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package test; 17 | 18 | import dagger.Module; 19 | import dagger.Provides; 20 | import javax.inject.Inject; 21 | 22 | class TestApp { 23 | static class NotInjectable { 24 | } 25 | 26 | static class InjectableSubclass extends NotInjectable { 27 | @Inject String string; 28 | } 29 | 30 | @Module(injects = InjectableSubclass.class) 31 | static class TestModule { 32 | @Provides String provideString() { 33 | return "string"; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /compiler/src/it/valid-use-of-qualifiers/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 22 | 4.0.0 23 | com.example.dagger.tests 24 | valid-use-ofqualifiers 25 | HEAD-SNAPSHOT 26 | Dagger Integration Test Basic 27 | 28 | 29 | @dagger.groupId@ 30 | dagger 31 | @dagger.version@ 32 | 33 | 34 | @dagger.groupId@ 35 | dagger-compiler 36 | @dagger.version@ 37 | true 38 | 39 | 40 | 41 | 42 | 43 | maven-compiler-plugin 44 | 3.1 45 | 46 | 1.5 47 | 1.5 48 | true 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /compiler/src/it/valid-use-of-qualifiers/src/main/java/test/TestApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Google, Inc. 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package test; 18 | 19 | import dagger.Module; 20 | import dagger.Provides; 21 | import java.lang.annotation.Retention; 22 | import javax.inject.Inject; 23 | import javax.inject.Qualifier; 24 | 25 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 26 | 27 | class TestApp { 28 | static class TestClass1 { 29 | @Inject @MyQualifier1 String field; 30 | } 31 | 32 | static class TestClass2 { 33 | String string; 34 | 35 | @Inject 36 | public TestClass2(@MyQualifier1 String constructorParam) { 37 | this.string = string; 38 | } 39 | } 40 | 41 | @Module(injects = TestClass1.class) 42 | static class TestModule { 43 | 44 | @Provides 45 | @MyQualifier1 46 | String providesString(@MyQualifier2 String providesMethodParam) { 47 | return providesMethodParam + "foo"; 48 | } 49 | 50 | @Provides 51 | @MyQualifier2 52 | String providesString() { 53 | return "foo"; 54 | } 55 | } 56 | @Qualifier 57 | @Retention(value = RUNTIME) 58 | @interface MyQualifier1 {} 59 | 60 | @Qualifier 61 | @Retention(value = RUNTIME) 62 | @interface MyQualifier2 {} 63 | } 64 | -------------------------------------------------------------------------------- /compiler/src/main/java/dagger/internal/codegen/GraphAnalysisErrorHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package dagger.internal.codegen; 17 | 18 | import dagger.internal.Linker; 19 | import java.util.List; 20 | import javax.annotation.processing.ProcessingEnvironment; 21 | import javax.lang.model.element.TypeElement; 22 | import javax.tools.Diagnostic; 23 | 24 | /** 25 | * A {@code Linker.ErrorHandler} which gathers errors and reports them via a processing 26 | * environment. 27 | */ 28 | final class GraphAnalysisErrorHandler implements Linker.ErrorHandler { 29 | private final ProcessingEnvironment processingEnv; 30 | private final String moduleName; 31 | 32 | GraphAnalysisErrorHandler(ProcessingEnvironment processingEnv, String moduleName) { 33 | this.processingEnv = processingEnv; 34 | this.moduleName = moduleName; 35 | } 36 | 37 | @Override public void handleErrors(List errors) { 38 | TypeElement module = processingEnv.getElementUtils().getTypeElement(moduleName); 39 | for (String error : errors) { 40 | processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, error + " for " + moduleName, 41 | module); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /compiler/src/main/java/dagger/internal/codegen/GraphAnalysisStaticInjection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package dagger.internal.codegen; 17 | 18 | import dagger.internal.Linker; 19 | import dagger.internal.StaticInjection; 20 | import javax.inject.Inject; 21 | import javax.lang.model.element.Element; 22 | 23 | import static dagger.internal.codegen.Util.isStatic; 24 | 25 | public final class GraphAnalysisStaticInjection extends StaticInjection { 26 | 27 | private final Element enclosingClass; 28 | 29 | public GraphAnalysisStaticInjection(Element enclosingClass) { 30 | this.enclosingClass = enclosingClass; 31 | } 32 | 33 | @Override public void attach(Linker linker) { 34 | for (Element enclosedElement : enclosingClass.getEnclosedElements()) { 35 | if (enclosedElement.getKind().isField() && isStatic(enclosedElement)) { 36 | Inject injectAnnotation = enclosedElement.getAnnotation(Inject.class); 37 | if (injectAnnotation != null) { 38 | String key = GeneratorKeys.get(enclosedElement.asType()); 39 | linker.requestBinding(key, enclosingClass.toString(), 40 | getClass().getClassLoader()); 41 | } 42 | } 43 | } 44 | } 45 | 46 | @Override public void inject() { 47 | throw new UnsupportedOperationException(); 48 | } 49 | } -------------------------------------------------------------------------------- /compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor: -------------------------------------------------------------------------------- 1 | dagger.internal.codegen.ValidationProcessor 2 | dagger.internal.codegen.InjectAdapterProcessor 3 | dagger.internal.codegen.ModuleAdapterProcessor 4 | dagger.internal.codegen.GraphAnalysisProcessor 5 | -------------------------------------------------------------------------------- /compiler/src/test/java/dagger/tests/integration/ProcessorTestUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Google, Inc. 3 | * Copyright (c) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.tests.integration; 18 | 19 | import dagger.internal.codegen.GraphAnalysisProcessor; 20 | import dagger.internal.codegen.InjectAdapterProcessor; 21 | import dagger.internal.codegen.ModuleAdapterProcessor; 22 | import dagger.internal.codegen.ValidationProcessor; 23 | import java.util.Arrays; 24 | import javax.annotation.processing.Processor; 25 | 26 | /** 27 | * Internal test utilities. 28 | */ 29 | public class ProcessorTestUtils { 30 | public static Iterable daggerProcessors() { 31 | return Arrays.asList( 32 | new InjectAdapterProcessor(), 33 | new ModuleAdapterProcessor(), 34 | new GraphAnalysisProcessor(), 35 | new ValidationProcessor()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /compiler/src/test/java/dagger/tests/integration/operation/FailureModeErrorsTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2014 Google, Inc. 3 | * Copyright (C) 2014 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.tests.integration.operation; 18 | 19 | import dagger.Module; 20 | import dagger.ObjectGraph; 21 | import javax.inject.Inject; 22 | import javax.inject.Qualifier; 23 | import org.junit.Test; 24 | import org.junit.runner.RunWith; 25 | import org.junit.runners.JUnit4; 26 | 27 | import static com.google.common.truth.Truth.assertThat; 28 | import static junit.framework.Assert.fail; 29 | 30 | @RunWith(JUnit4.class) 31 | public final class FailureModeErrorsTest { 32 | 33 | @Module 34 | static class CompleteModule {} 35 | 36 | static class ArrayFoo { 37 | @Inject ArrayFoo(String[] ignored) {} 38 | } 39 | 40 | @Module(injects = ArrayFoo.class, complete = false) 41 | static class ArrayFooModule {} 42 | 43 | @Test public void failOnMissingModule_arrayorgenerics() { 44 | // Generics here are crazy to try to test for, but this code path is legit regardless. 45 | try { 46 | ObjectGraph.create(new CompleteModule(), new ArrayFooModule()).get(ArrayFoo.class); 47 | fail("Should have thrown."); 48 | } catch (IllegalStateException e) { 49 | assertThat(e.getMessage()).contains( 50 | "java.lang.String[] is a generic class or an array and can only be bound with " 51 | + "concrete type parameter(s) in a @Provides method. required by class " 52 | + "dagger.tests.integration.operation.FailureModeErrorsTest$ArrayFoo"); 53 | } 54 | } 55 | 56 | @Qualifier @interface MyFoo {} 57 | 58 | static class QualifyingFoo { 59 | @Inject QualifyingFoo(@MyFoo String ignored) {} 60 | } 61 | 62 | @Module(injects = QualifyingFoo.class, complete = false) 63 | static class QualifyingFooModule {} 64 | 65 | @Test public void failOnMissingModule_qualified() { 66 | try { 67 | ObjectGraph.create(new CompleteModule(), new QualifyingFooModule()).get(QualifyingFoo.class); 68 | fail("Should have thrown."); 69 | } catch (IllegalStateException e) { 70 | assertThat(e.getMessage()).contains( 71 | "@dagger.tests.integration.operation.FailureModeErrorsTest$MyFoo()/java.lang.String " 72 | + "is a @Qualifier-annotated type and must be bound by a @Provides method. required by " 73 | + "class dagger.tests.integration.operation.FailureModeErrorsTest$QualifyingFoo"); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /compiler/src/test/java/dagger/tests/integration/operation/PrimitiveInjectionTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013 Google, Inc. 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.tests.integration.operation; 18 | 19 | import dagger.Module; 20 | import dagger.ObjectGraph; 21 | import dagger.Provides; 22 | import javax.inject.Inject; 23 | import org.junit.Test; 24 | import org.junit.runner.RunWith; 25 | import org.junit.runners.JUnit4; 26 | 27 | import static com.google.common.truth.Truth.assertThat; 28 | 29 | @RunWith(JUnit4.class) 30 | public final class PrimitiveInjectionTest { 31 | static class ArrayInjectable { 32 | @Inject byte[] byteArray; 33 | @Inject int[] integerArray; 34 | @Inject boolean[] booleanArray; 35 | @Inject char[] charArray; 36 | @Inject long[] longArray; 37 | @Inject float[] floatArray; 38 | @Inject double[] doubleArray; 39 | } 40 | 41 | @Module(injects = ArrayInjectable.class) 42 | static class PrimitiveArrayModule { 43 | @Provides byte[] byteArray() { return new byte[] { Byte.MAX_VALUE }; } 44 | @Provides int[] provideInt() { return new int[] { Integer.MAX_VALUE }; } 45 | @Provides boolean[] provideBoolean() { return new boolean[] { true }; } 46 | @Provides long[] provideLong() { return new long[] { Long.MAX_VALUE }; } 47 | @Provides char[] provideChar() { return new char[] { Character.MAX_VALUE }; } 48 | @Provides float[] provideFloat() { return new float[] { Float.MAX_VALUE }; } 49 | @Provides double[] provideDouble() { return new double[] { Double.MAX_VALUE }; } 50 | } 51 | 52 | @Test public void primitiveArrayTypesAllInjected() { 53 | ArrayInjectable result = ObjectGraph.create(PrimitiveArrayModule.class) 54 | .get(ArrayInjectable.class); 55 | assertThat(result).isNotNull(); 56 | assertThat(result.byteArray).isEqualTo(new byte[] { Byte.MAX_VALUE }); 57 | assertThat(result.integerArray).isEqualTo(new int[] { Integer.MAX_VALUE }); 58 | assertThat(result.booleanArray).isEqualTo(new boolean[] { true }); 59 | assertThat(result.charArray).isEqualTo(new char[] { Character.MAX_VALUE }); 60 | assertThat(result.longArray).isEqualTo(new long[] { Long.MAX_VALUE }); 61 | assertThat(result.floatArray).hasValuesWithin(0).of(new float[] { Float.MAX_VALUE }); 62 | assertThat(result.doubleArray).hasValuesWithin(0).of(new double[] { Double.MAX_VALUE }); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /compiler/src/test/java/dagger/tests/integration/operation/SimpleInjectionTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013 Google, Inc. 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.tests.integration.operation; 18 | 19 | import dagger.Module; 20 | import dagger.ObjectGraph; 21 | import dagger.Provides; 22 | import javax.inject.Inject; 23 | import org.junit.Test; 24 | import org.junit.runner.RunWith; 25 | import org.junit.runners.JUnit4; 26 | 27 | import static com.google.common.truth.Truth.assertThat; 28 | 29 | @RunWith(JUnit4.class) 30 | public final class SimpleInjectionTest { 31 | static abstract class AbstractFoo { 32 | @Inject String blah; 33 | } 34 | 35 | static class Foo extends AbstractFoo { } 36 | 37 | @Module(injects = Foo.class) 38 | static class FooModule { 39 | @Provides String string() { return "blah"; } 40 | } 41 | 42 | @Module(injects = Foo.class) 43 | static class ProvidingFooModule { 44 | @Provides String string() { return "blah"; } 45 | @Provides Foo foo(String blah) { 46 | Foo foo = new Foo(); 47 | foo.blah = blah; 48 | return foo; 49 | } 50 | } 51 | 52 | @Test public void memberInject_WithoutProvidesMethod() { 53 | Foo foo = new Foo(); 54 | ObjectGraph.create(FooModule.class).inject(foo); 55 | assertThat(foo.blah).isEqualTo("blah"); 56 | } 57 | 58 | @Test public void membersInject_WithProvidesMethod() { 59 | Foo foo = new Foo(); 60 | ObjectGraph.create(ProvidingFooModule.class).inject(foo); 61 | assertThat(foo.blah).isEqualTo("blah"); 62 | } 63 | 64 | @Test public void get_WithProvidesMethod() { 65 | Foo foo = ObjectGraph.create(ProvidingFooModule.class).get(Foo.class); 66 | assertThat(foo.blah).isEqualTo("blah"); 67 | } 68 | 69 | static class Bar { } 70 | 71 | @Module(injects = Bar.class) 72 | static class BarModule { 73 | } 74 | 75 | @Test public void membersInject_WithNonInjectable() { 76 | Bar bar = new Bar(); 77 | ObjectGraph.create(BarModule.class).inject(bar); 78 | } 79 | 80 | @Module(injects = Bar.class) 81 | static class ProvidingBarModule { 82 | @Provides public Bar bar() { return new Bar(); } 83 | } 84 | 85 | @Test public void membersInject_WithProvidedNonInjectable() { 86 | Bar bar = ObjectGraph.create(ProvidingBarModule.class).get(Bar.class); 87 | assertThat(bar).isNotNull(); 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /compiler/src/test/java/dagger/tests/integration/validation/CyclicDependencyTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Google, Inc. 3 | * Copyright (c) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.tests.integration.validation; 18 | 19 | import com.google.testing.compile.JavaFileObjects; 20 | import javax.tools.JavaFileObject; 21 | import org.junit.Test; 22 | import org.junit.runner.RunWith; 23 | import org.junit.runners.JUnit4; 24 | 25 | import static com.google.common.truth.Truth.assertAbout; 26 | import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource; 27 | import static dagger.tests.integration.ProcessorTestUtils.daggerProcessors; 28 | 29 | @RunWith(JUnit4.class) 30 | public class CyclicDependencyTest { 31 | 32 | @Test public void cyclicDepsWithInjectables() { 33 | JavaFileObject sourceFile = JavaFileObjects.forSourceString("CyclicDeps", "" 34 | + "import dagger.Module;\n" 35 | + "import javax.inject.Inject;\n" 36 | + "class CyclicDeps {\n" 37 | + " static class Foo {\n" 38 | + " @Inject Foo(Bar b) { }\n" 39 | + " }\n" 40 | + " static class Bar {\n" 41 | + " @Inject Bar(Blah b) { }\n" 42 | + " }\n" 43 | + " static class Blah {\n" 44 | + " @Inject Blah(Foo f) { }\n" 45 | + " }\n" 46 | + " static class EntryPoint {\n" 47 | + " @Inject Foo f;\n" 48 | + " }\n" 49 | + " @Module(injects = EntryPoint.class)\n" 50 | + " static class TestModule { }\n" 51 | + "}\n" 52 | ); 53 | 54 | assertAbout(javaSource()).that(sourceFile).processedWith(daggerProcessors()).failsToCompile() 55 | .withErrorContaining("Dependency cycle:").in(sourceFile).onLine(17); 56 | } 57 | 58 | @Test public void cyclicDepsWithProvidesMethods() { 59 | JavaFileObject sourceFile = JavaFileObjects.forSourceString("CyclicDeps", "" 60 | + "import dagger.Module;\n" 61 | + "import dagger.Provides;\n" 62 | + "class CyclicDeps {\n" 63 | + " static class A { }\n" 64 | + " static class B { }\n" 65 | + " static class C { }\n" 66 | + " static class D { }\n" 67 | + " @Module(injects = D.class)\n" 68 | + " static class CyclicModule {\n" 69 | + " @Provides A a(D d) { return null; }\n" 70 | + " @Provides B b(A a) { return null; }\n" 71 | + " @Provides C c(B b) { return null; }\n" 72 | + " @Provides D d(C c) { return null; }\n" 73 | + " }\n" 74 | + "}\n" 75 | ); 76 | 77 | assertAbout(javaSource()).that(sourceFile).processedWith(daggerProcessors()).failsToCompile() 78 | .withErrorContaining("Dependency cycle:").in(sourceFile).onLine(9); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /compiler/src/test/java/dagger/tests/integration/validation/SimpleMissingDependencyTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Google, Inc. 3 | * Copyright (c) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.tests.integration.validation; 18 | 19 | import com.google.testing.compile.JavaFileObjects; 20 | import javax.tools.JavaFileObject; 21 | import org.junit.Test; 22 | import org.junit.runner.RunWith; 23 | import org.junit.runners.JUnit4; 24 | 25 | import static com.google.common.truth.Truth.assertAbout; 26 | import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource; 27 | import static dagger.tests.integration.ProcessorTestUtils.daggerProcessors; 28 | 29 | @RunWith(JUnit4.class) 30 | public class SimpleMissingDependencyTest { 31 | 32 | @Test public void missingDependency() { 33 | JavaFileObject file = JavaFileObjects.forSourceString("MissingDep", "" 34 | + "import dagger.Module;\n" 35 | + "import javax.inject.Inject;\n" 36 | + "class MissingDep {\n" 37 | + " @Inject Dependency dep;\n" 38 | + " static interface Dependency {\n" 39 | + " void doit();\n" 40 | + " }\n" 41 | + " @Module(injects = MissingDep.class)\n" 42 | + " static class DaModule {\n" 43 | + " /* missing */ // @Provides Dependency a() { return new Dependency(); }\n" 44 | + " }\n" 45 | + "}\n" 46 | ); 47 | 48 | assertAbout(javaSource()) 49 | .that(file) 50 | .processedWith(daggerProcessors()) 51 | .failsToCompile() 52 | .withErrorContaining("MissingDep$Dependency could not be bound") 53 | .in(file).onLine(9).and() 54 | .withErrorContaining("required by MissingDep for MissingDep.DaModule") 55 | .in(file).onLine(9); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 4.0.0 19 | 20 | 21 | com.squareup.dagger 22 | dagger-parent 23 | 1.2.6-SNAPSHOT 24 | ../pom.xml 25 | 26 | 27 | dagger 28 | Dagger 29 | 30 | 31 | 32 | javax.inject 33 | javax.inject 34 | 35 | 36 | 37 | junit 38 | junit 39 | test 40 | 41 | 42 | com.google.truth 43 | truth 44 | test 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.codehaus.mojo 52 | animal-sniffer-maven-plugin 53 | 1.8 54 | 55 | 56 | sniff-api 57 | check 58 | 59 | 60 | 61 | 62 | org.codehaus.mojo.signature 63 | java15 64 | 1.0 65 | 66 | 67 | 68 | 69 | maven-javadoc-plugin 70 | 71 | dagger.internal:dagger.internal.* 72 | 73 | 74 | 75 | maven-jar-plugin 76 | 77 | 78 | 79 | test-jar 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /core/src/main/java/dagger/MembersInjector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Square, Inc. 3 | * Copyright (C) 2009 Google Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger; 18 | 19 | /** 20 | * Injects dependencies into the fields and methods on instances of type 21 | * {@code T}. Ignores the presence or absence of an injectable constructor. 22 | * 23 | * @param type to inject members of 24 | * 25 | * @author Bob Lee 26 | * @author Jesse Wilson 27 | */ 28 | public interface MembersInjector { 29 | 30 | /** 31 | * Injects dependencies into the fields and methods of {@code instance}. 32 | * Ignores the presence or absence of an injectable constructor. 33 | * 34 | *

Whenever the object graph creates an instance, it performs this 35 | * injection automatically (after first performing constructor injection), so 36 | * if you're able to let the object graph create all your objects for you, 37 | * you'll never need to use this method. 38 | * 39 | * @param instance to inject members on. May be {@code null}. 40 | */ 41 | void injectMembers(T instance); 42 | } 43 | -------------------------------------------------------------------------------- /core/src/main/java/dagger/Provides.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 Google Inc. 3 | * Copyright (C) 2012 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger; 18 | 19 | import java.lang.annotation.Documented; 20 | import java.lang.annotation.Retention; 21 | import java.lang.annotation.Target; 22 | 23 | import static java.lang.annotation.ElementType.METHOD; 24 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 25 | 26 | /** 27 | * Annotates methods of a module to create a provider method binding. The 28 | * method's return type is bound to its returned value. The object graph will 29 | * pass dependencies to the method as parameters. 30 | * 31 | * @author Bob Lee 32 | */ 33 | @Documented @Target(METHOD) @Retention(RUNTIME) 34 | public @interface Provides { 35 | /** The type of binding into which the return type of the annotated method contributes. */ 36 | enum Type { 37 | /** 38 | * The method is the only one which can produce the value for the specified return type. This 39 | * is the default behavior. 40 | */ 41 | UNIQUE, 42 | 43 | /** 44 | * The method's return type forms the generic type argument of a {@code Set}, and the 45 | * returned value is contributed to the set. The object graph will pass dependencies to the 46 | * method as parameters. The {@code Set} produced from the accumulation of values will be 47 | * immutable. 48 | */ 49 | SET, 50 | 51 | /** 52 | * Like {@link #SET}, except the method's return type is {@code Set}, where any values are 53 | * contributed to the set. An example use is to provide a default empty set binding, which is 54 | * otherwise not possible using {@link #SET}. 55 | */ 56 | SET_VALUES; 57 | } 58 | 59 | Type type() default Type.UNIQUE; 60 | } 61 | -------------------------------------------------------------------------------- /core/src/main/java/dagger/internal/BindingsGroup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * Copyright (C) 2013 Google, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.internal; 18 | 19 | import java.util.LinkedHashMap; 20 | import java.util.Map; 21 | import java.util.Map.Entry; 22 | import java.util.Set; 23 | 24 | /** 25 | * A grouping of bindings that fails when existing values are clobbered, to be used in collecting 26 | * the initial set of bindings for a graph (from provides methods). 27 | */ 28 | public abstract class BindingsGroup { 29 | private final Map> bindings = new LinkedHashMap>(); 30 | 31 | public abstract Binding contributeSetBinding(String key, SetBinding value); 32 | 33 | public Binding contributeProvidesBinding(String key, ProvidesBinding value) { 34 | return put(key, value); 35 | } 36 | 37 | protected Binding put(String key, Binding value) { 38 | Binding clobbered = bindings.put(key, value); 39 | if (clobbered != null) { 40 | bindings.put(key, clobbered); // Put things back as they were. 41 | throw new IllegalArgumentException("Duplicate:\n " + clobbered + "\n " + value); 42 | } 43 | return null; 44 | } 45 | 46 | public Binding get(String key) { 47 | return bindings.get(key); 48 | } 49 | 50 | public final Set>> entrySet() { 51 | return bindings.entrySet(); 52 | } 53 | 54 | @Override public String toString() { 55 | return getClass().getSimpleName() + bindings.toString(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /core/src/main/java/dagger/internal/BuiltInBinding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package dagger.internal; 17 | 18 | 19 | /** 20 | * Injects a Provider or a MembersInjector. 21 | */ 22 | final class BuiltInBinding extends Binding { 23 | private final String delegateKey; 24 | private final ClassLoader classLoader; 25 | private Binding delegate; 26 | 27 | public BuiltInBinding( 28 | String key, Object requiredBy, ClassLoader classLoader, String delegateKey) { 29 | super(key, null, false, requiredBy); 30 | this.classLoader = classLoader; 31 | this.delegateKey = delegateKey; 32 | } 33 | 34 | @Override public void attach(Linker linker) { 35 | delegate = linker.requestBinding(delegateKey, requiredBy, classLoader); 36 | } 37 | 38 | @Override public void injectMembers(T t) { 39 | throw new UnsupportedOperationException(); 40 | } 41 | 42 | @SuppressWarnings("unchecked") // At runtime we know 'T' is a Provider or MembersInjector. 43 | @Override public T get() { 44 | return (T) delegate; 45 | } 46 | 47 | public Binding getDelegate() { 48 | return delegate; 49 | } 50 | 51 | // public void getDependencies() not overridden. 52 | // We don't add 'delegate' because it isn't actually used by get() or injectMembers(). 53 | } 54 | -------------------------------------------------------------------------------- /core/src/main/java/dagger/internal/LazyBinding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Google, Inc. 3 | * Copyright (C) 2012 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.internal; 18 | 19 | import dagger.Lazy; 20 | 21 | /** 22 | * Injects a Lazy wrapper for a type T 23 | */ 24 | final class LazyBinding extends Binding> { 25 | 26 | final static Object NOT_PRESENT = new Object(); 27 | 28 | private final String lazyKey; 29 | private final ClassLoader loader; 30 | Binding delegate; 31 | 32 | LazyBinding(String key, Object requiredBy, ClassLoader loader, String lazyKey) { 33 | super(key, null, false, requiredBy); 34 | this.loader = loader; 35 | this.lazyKey = lazyKey; 36 | } 37 | 38 | @SuppressWarnings("unchecked") // At runtime we know it's a Binding>. 39 | @Override 40 | public void attach(Linker linker) { 41 | delegate = (Binding) linker.requestBinding(lazyKey, requiredBy, loader); 42 | } 43 | 44 | @Override public void injectMembers(Lazy t) { 45 | throw new UnsupportedOperationException(); // Injecting into a custom Lazy not supported. 46 | } 47 | 48 | @Override 49 | public Lazy get() { 50 | return new Lazy() { 51 | private volatile Object cacheValue = NOT_PRESENT; 52 | 53 | @SuppressWarnings("unchecked") // Delegate is of type T 54 | @Override 55 | public T get() { 56 | if (cacheValue == NOT_PRESENT) { 57 | synchronized (this) { 58 | if (cacheValue == NOT_PRESENT) { 59 | cacheValue = delegate.get(); 60 | } 61 | } 62 | } 63 | return (T) cacheValue; 64 | } 65 | }; 66 | } 67 | 68 | // public void getDependencies() not overridden. 69 | // We don't add 'delegate' because it isn't actually used by get() or injectMembers(). 70 | } 71 | -------------------------------------------------------------------------------- /core/src/main/java/dagger/internal/Memoizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package dagger.internal; 17 | 18 | import java.util.LinkedHashMap; 19 | import java.util.Map; 20 | import java.util.concurrent.locks.Lock; 21 | import java.util.concurrent.locks.ReadWriteLock; 22 | import java.util.concurrent.locks.ReentrantReadWriteLock; 23 | 24 | /** 25 | * Represents an operation whose results are memoized. Results returned by invocations of 26 | * {@link #create(Object)} are memoized so that the same object is returned for multiple invocations 27 | * of {@link #get(Object)} for the same key. 28 | */ 29 | abstract class Memoizer { 30 | private final Map map; 31 | private final Lock readLock; 32 | private final Lock writeLock; 33 | 34 | public Memoizer() { 35 | this.map = new LinkedHashMap(); 36 | ReadWriteLock lock = new ReentrantReadWriteLock(); 37 | this.readLock = lock.readLock(); 38 | this.writeLock = lock.writeLock(); 39 | } 40 | 41 | public final V get(K key) { 42 | if (key == null) { 43 | throw new NullPointerException("key == null"); 44 | } 45 | 46 | // check to see if we already have a value 47 | readLock.lock(); 48 | try { 49 | V value = map.get(key); 50 | if (value != null) { 51 | return value; 52 | } 53 | } finally { 54 | readLock.unlock(); 55 | } 56 | 57 | // create a new value. this may race and we might create more than one instance, but that's ok 58 | V newValue = create(key); 59 | if (newValue == null) { 60 | throw new NullPointerException("create returned null"); 61 | } 62 | 63 | // write the new value and return it 64 | writeLock.lock(); 65 | try { 66 | map.put(key, newValue); 67 | return newValue; 68 | } finally { 69 | writeLock.unlock(); 70 | } 71 | } 72 | 73 | protected abstract V create(K key); 74 | 75 | @Override public final String toString() { 76 | readLock.lock(); 77 | try { 78 | return map.toString(); 79 | } finally { 80 | readLock.unlock(); 81 | } 82 | } 83 | } -------------------------------------------------------------------------------- /core/src/main/java/dagger/internal/ModuleAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Square, Inc. 3 | * Copyright (C) 2012 Google, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.internal; 18 | 19 | 20 | /** 21 | * Extracts bindings from an {@code @Module}-annotated class. 22 | */ 23 | public abstract class ModuleAdapter { 24 | public final Class moduleClass; 25 | public final String[] injectableTypes; 26 | public final Class[] staticInjections; 27 | public final boolean overrides; 28 | public final Class[] includes; 29 | public final boolean complete; 30 | public final boolean library; 31 | 32 | protected ModuleAdapter(Class moduleClass, String[] injectableTypes, 33 | Class[] staticInjections, boolean overrides, Class[] includes, boolean complete, 34 | boolean library) { 35 | this.moduleClass = moduleClass; 36 | this.injectableTypes = injectableTypes; 37 | this.staticInjections = staticInjections; 38 | this.overrides = overrides; 39 | this.includes = includes; 40 | this.complete = complete; 41 | this.library = library; 42 | } 43 | 44 | /** 45 | * Returns bindings for the {@code @Provides} methods of {@code module}. The 46 | * returned bindings must be linked before they can be used to inject values. 47 | */ 48 | @SuppressWarnings("unused") 49 | public void getBindings(BindingsGroup map, T module) { 50 | // no-op; 51 | } 52 | 53 | /** 54 | * Returns a new instance of the module class created using a no-args 55 | * constructor. Only used when a manually-constructed module is not supplied. 56 | */ 57 | protected T newModule() { 58 | throw new UnsupportedOperationException("No no-args constructor on " + getClass().getName()); 59 | } 60 | 61 | @Override 62 | public final boolean equals(Object obj) { 63 | if (obj == this) { 64 | return true; 65 | } else if (obj instanceof ModuleAdapter) { 66 | ModuleAdapter that = (ModuleAdapter) obj; 67 | return this.moduleClass.equals(that.moduleClass); 68 | } else { 69 | return false; 70 | } 71 | } 72 | 73 | @Override 74 | public final int hashCode() { 75 | return moduleClass.hashCode(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /core/src/main/java/dagger/internal/Modules.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Square, Inc. 3 | * Copyright (C) 2012 Google, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.internal; 18 | 19 | 20 | import java.util.LinkedHashMap; 21 | import java.util.Map; 22 | 23 | /** 24 | * Static helper for organizing modules. 25 | */ 26 | public final class Modules { 27 | 28 | private Modules() { } 29 | 30 | /** 31 | * Returns a full set of module adapters, including module adapters for included 32 | * modules. 33 | */ 34 | public static Map, Object> loadModules(Loader loader, 35 | Object[] seedModulesOrClasses) { 36 | Map, Object> seedAdapters = 37 | new LinkedHashMap, Object>(seedModulesOrClasses.length); 38 | for (int i = 0; i < seedModulesOrClasses.length; i++) { 39 | if (seedModulesOrClasses[i] instanceof Class) { 40 | ModuleAdapter adapter = loader.getModuleAdapter((Class) seedModulesOrClasses[i]); 41 | seedAdapters.put(adapter, adapter.newModule()); 42 | } else { 43 | ModuleAdapter adapter = loader.getModuleAdapter(seedModulesOrClasses[i].getClass()); 44 | seedAdapters.put(adapter, seedModulesOrClasses[i]); 45 | } 46 | } 47 | 48 | // Add the adapters that we have module instances for. This way we won't 49 | // construct module objects when we have a user-supplied instance. 50 | Map, Object> result = 51 | new LinkedHashMap, Object>(seedAdapters); 52 | 53 | // Next collect included modules 54 | Map, ModuleAdapter> transitiveInclusions = 55 | new LinkedHashMap, ModuleAdapter>(); 56 | for (ModuleAdapter adapter : seedAdapters.keySet()) { 57 | collectIncludedModulesRecursively(loader, adapter, transitiveInclusions); 58 | } 59 | // and create them if necessary 60 | for (ModuleAdapter dependency : transitiveInclusions.values()) { 61 | if (!result.containsKey(dependency)) { 62 | result.put(dependency, dependency.newModule()); 63 | } 64 | } 65 | return result; 66 | } 67 | 68 | /** 69 | * Fills {@code result} with the module adapters for the includes of {@code 70 | * adapter}, and their includes recursively. 71 | */ 72 | private static void collectIncludedModulesRecursively(Loader plugin, ModuleAdapter adapter, 73 | Map, ModuleAdapter> result) { 74 | for (Class include : adapter.includes) { 75 | if (!result.containsKey(include)) { 76 | ModuleAdapter includedModuleAdapter = plugin.getModuleAdapter(include); 77 | result.put(include, includedModuleAdapter); 78 | collectIncludedModulesRecursively(plugin, includedModuleAdapter, result); 79 | } 80 | } 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /core/src/main/java/dagger/internal/ProvidesBinding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Google, Inc. 3 | * Copyright (C) 2013 Square, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.internal; 18 | 19 | /** 20 | * A {@code Binding} which delegates to a module method. 21 | */ 22 | public abstract class ProvidesBinding extends Binding { 23 | protected final String moduleClass; 24 | 25 | protected final String methodName; 26 | 27 | /** 28 | * Creates a new {@code ProvidesBinding} with the given "provides" key, a flag as to whether 29 | * this binding should be scoped, and the requiredBy object for traceability. 30 | */ 31 | public ProvidesBinding(String key, boolean singleton, String moduleClass, String methodName) { 32 | // Set requiredBy as fullMethodName to preserve older debugging meaning. 33 | super(key, null, singleton, moduleClass + "." + methodName + "()"); 34 | this.moduleClass = moduleClass; 35 | this.methodName = methodName; 36 | } 37 | 38 | /** 39 | * A provides binding is responsible for implementing storage of the module instance, and 40 | * delegation to that module instance's method. 41 | */ 42 | @Override 43 | public abstract T get(); 44 | 45 | @Override public String toString() { 46 | return getClass().getName() + "[key=" + provideKey 47 | + " method=" + moduleClass + "." + methodName + "()" + "]"; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /core/src/main/java/dagger/internal/StaticInjection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Square Inc. 3 | * Copyright (C) 2012 Google Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.internal; 18 | 19 | 20 | /** 21 | * Injects the static fields of a class. 22 | */ 23 | public abstract class StaticInjection { 24 | 25 | public abstract void attach(Linker linker); 26 | 27 | public abstract void inject(); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /core/src/main/java/dagger/internal/ThrowingErrorHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package dagger.internal; 17 | 18 | import java.util.List; 19 | 20 | /** 21 | * Handles errors by throwing an exception containing all the available errors. 22 | */ 23 | public final class ThrowingErrorHandler implements Linker.ErrorHandler { 24 | 25 | @Override public void handleErrors(List errors) { 26 | if (errors.isEmpty()) { 27 | return; 28 | } 29 | StringBuilder message = new StringBuilder(); 30 | message.append("Errors creating object graph:"); 31 | for (String error : errors) { 32 | message.append("\n ").append(error); 33 | } 34 | throw new IllegalStateException(message.toString()); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * Copyright (C) 2013 Google, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.internal.loaders; 18 | 19 | 20 | /** 21 | * A single point for API used in common by Adapters and Adapter generators 22 | */ 23 | public final class GeneratedAdapters { 24 | private static final String SEPARATOR = "$$"; 25 | public static final String INJECT_ADAPTER_SUFFIX = SEPARATOR + "InjectAdapter"; 26 | public static final String MODULE_ADAPTER_SUFFIX = SEPARATOR + "ModuleAdapter"; 27 | public static final String STATIC_INJECTION_SUFFIX = SEPARATOR + "StaticInjection"; 28 | 29 | private GeneratedAdapters() { } 30 | } 31 | -------------------------------------------------------------------------------- /core/src/main/java/dagger/internal/loaders/ReflectiveStaticInjection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package dagger.internal.loaders; 17 | 18 | import dagger.internal.Binding; 19 | import dagger.internal.Keys; 20 | import dagger.internal.Linker; 21 | import dagger.internal.StaticInjection; 22 | import java.lang.reflect.Field; 23 | import java.lang.reflect.Modifier; 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | import javax.inject.Inject; 27 | 28 | /** 29 | * Uses reflection to inject the static fields of a class. 30 | */ 31 | public final class ReflectiveStaticInjection extends StaticInjection { 32 | private final ClassLoader loader; 33 | private final Field[] fields; 34 | private Binding[] bindings; 35 | 36 | private ReflectiveStaticInjection(ClassLoader loader, Field[] fields) { 37 | this.fields = fields; 38 | this.loader = loader; 39 | } 40 | 41 | @Override public void attach(Linker linker) { 42 | bindings = new Binding[fields.length]; 43 | for (int i = 0; i < fields.length; i++) { 44 | Field field = fields[i]; 45 | String key = Keys.get(field.getGenericType(), field.getAnnotations(), field); 46 | bindings[i] = linker.requestBinding(key, field, loader); 47 | } 48 | } 49 | 50 | @Override public void inject() { 51 | try { 52 | for (int f = 0; f < fields.length; f++) { 53 | fields[f].set(null, bindings[f].get()); 54 | } 55 | } catch (IllegalAccessException e) { 56 | throw new AssertionError(e); 57 | } 58 | } 59 | 60 | public static StaticInjection create(Class injectedClass) { 61 | List fields = new ArrayList(); 62 | for (Field field : injectedClass.getDeclaredFields()) { 63 | if (Modifier.isStatic(field.getModifiers()) && field.isAnnotationPresent(Inject.class)) { 64 | field.setAccessible(true); 65 | fields.add(field); 66 | } 67 | } 68 | if (fields.isEmpty()) { 69 | throw new IllegalArgumentException("No static injections: " + injectedClass.getName()); 70 | } 71 | return new ReflectiveStaticInjection(injectedClass.getClassLoader(), 72 | fields.toArray(new Field[fields.size()])); 73 | } 74 | } -------------------------------------------------------------------------------- /core/src/test/java/dagger/ExtensionWithStateTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Google Inc. 3 | * Copyright (C) 2013 Square Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger; 18 | 19 | import dagger.internal.TestingLoader; 20 | import javax.inject.Inject; 21 | import org.junit.Test; 22 | import org.junit.runner.RunWith; 23 | import org.junit.runners.JUnit4; 24 | 25 | import static com.google.common.truth.Truth.assertThat; 26 | 27 | @RunWith(JUnit4.class) 28 | public final class ExtensionWithStateTest { 29 | static class A { } 30 | 31 | static class B { 32 | @Inject A a; 33 | } 34 | 35 | @Module( 36 | injects = A.class, // for testing 37 | complete = false 38 | ) 39 | static class RootModule { 40 | final A a; 41 | RootModule(A a) { 42 | this.a = a; 43 | } 44 | @Provides A provideA() { return a; } 45 | } 46 | 47 | @Module(addsTo = RootModule.class, injects = { B.class }) 48 | static class ExtensionModule { } 49 | 50 | @Test public void basicInjectionWithExtension() { 51 | A a = new A(); 52 | ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule(a)); 53 | assertThat(root.get(A.class)).isSameAs(a); 54 | 55 | // Extension graph behaves as the root graph would for root-ish things. 56 | ObjectGraph extension = root.plus(new ExtensionModule()); 57 | assertThat(extension.get(A.class)).isSameAs(a); 58 | assertThat(extension.get(B.class).a).isSameAs(a); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /core/src/test/java/dagger/ProblemDetectorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package dagger; 17 | 18 | import dagger.internal.TestingLoader; 19 | import javax.inject.Inject; 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | import org.junit.runners.JUnit4; 23 | 24 | import static org.junit.Assert.fail; 25 | 26 | @RunWith(JUnit4.class) 27 | public final class ProblemDetectorTest { 28 | @Test public void atInjectCircularDependenciesDetected() { 29 | class TestEntryPoint { 30 | @Inject Rock rock; 31 | } 32 | 33 | @Module(injects = TestEntryPoint.class) 34 | class TestModule { 35 | } 36 | 37 | ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); 38 | try { 39 | graph.validate(); 40 | fail(); 41 | } catch (RuntimeException expected) { 42 | } 43 | } 44 | 45 | @Test public void providesCircularDependenciesDetected() { 46 | @Module 47 | class TestModule { 48 | @Provides Integer provideInteger(String s) { 49 | throw new AssertionError(); 50 | } 51 | @Provides String provideString(Integer i) { 52 | throw new AssertionError(); 53 | } 54 | } 55 | 56 | ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); 57 | try { 58 | graph.validate(); 59 | fail(); 60 | } catch (RuntimeException expected) { 61 | } 62 | } 63 | 64 | @Test public void validateLazy() { 65 | @Module(library = true) 66 | class TestModule { 67 | @Provides Integer dependOnLazy(Lazy lazyString) { 68 | throw new AssertionError(); 69 | } 70 | @Provides String provideLazyValue() { 71 | throw new AssertionError(); 72 | } 73 | } 74 | 75 | ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); 76 | graph.validate(); 77 | } 78 | 79 | static class Rock { 80 | @Inject Scissors scissors; 81 | } 82 | 83 | static class Scissors { 84 | @Inject Paper paper; 85 | } 86 | 87 | static class Paper { 88 | @Inject Rock rock; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /core/src/test/java/dagger/internal/FailoverLoaderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Google Inc. 3 | * Copyright (C) 2013 Square Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.internal; 18 | 19 | import dagger.Module; 20 | import dagger.ObjectGraph; 21 | import dagger.Provides; 22 | import javax.inject.Inject; 23 | import org.junit.Test; 24 | import org.junit.runner.RunWith; 25 | import org.junit.runners.JUnit4; 26 | 27 | import static com.google.common.truth.Truth.assertThat; 28 | 29 | /** 30 | * A test case to deal with fall-back to reflection where the concrete type has been generated 31 | * but the parent has no {@code @Inject} annotation, and so has not been generated. 32 | */ 33 | @RunWith(JUnit4.class) 34 | public final class FailoverLoaderTest { 35 | 36 | @Module(injects = Entry$Point.class) 37 | static class TestModule { 38 | @Provides String aString() { return "a"; } 39 | } 40 | 41 | /** A reflective module that will be loaded in place of a generated module for this test. */ 42 | static final class TestModule$$ModuleAdapter extends TestingModuleAdapter { 43 | public TestModule$$ModuleAdapter() { 44 | super(TestModule.class, TestModule.class.getAnnotation(Module.class)); 45 | } 46 | } 47 | 48 | static class Entry$Point { 49 | @Inject String a; 50 | } 51 | 52 | @Test public void simpleInjectionWithUnGeneratedCode() { 53 | Entry$Point entryPoint = new Entry$Point(); 54 | ObjectGraph.create(new TestModule()).inject(entryPoint); 55 | assertThat(entryPoint.a).isEqualTo("a"); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /core/src/test/java/dagger/internal/SingletonBindingTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package dagger.internal; 18 | 19 | import org.junit.Before; 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | import org.junit.runners.JUnit4; 23 | 24 | import static com.google.common.truth.Truth.assertThat; 25 | 26 | @RunWith(JUnit4.class) 27 | public final class SingletonBindingTest { 28 | private Binding wrappedBinding; 29 | private Binding singletonBinding; 30 | 31 | @Before public void setUp() { 32 | wrappedBinding = new StringBinding(); 33 | singletonBinding = Linker.scope(wrappedBinding); 34 | } 35 | 36 | @Test public void testSingletonBindingIsSingleton() { 37 | assertThat(singletonBinding.isSingleton()).isTrue(); 38 | } 39 | 40 | // This next batch of tests validates that SingletonBinding consistently delegates to the wrapped binding for state. 41 | @Test public void testSingletonBindingDelegatesSetLinked() { 42 | singletonBinding.setLinked(); 43 | assertThat(wrappedBinding.isLinked()).isTrue(); 44 | } 45 | 46 | @Test public void testSingletonBindingDelegatesIsLinked() { 47 | wrappedBinding.setLinked(); 48 | assertThat(singletonBinding.isLinked()).isTrue(); 49 | } 50 | 51 | @Test public void testSingletonBindingDelegatesSetVisiting() { 52 | singletonBinding.setVisiting(true); 53 | assertThat(wrappedBinding.isVisiting()).isTrue(); 54 | } 55 | 56 | @Test public void testSingletonBindingDelegatesIsVisiting() { 57 | wrappedBinding.setVisiting(true); 58 | assertThat(singletonBinding.isVisiting()).isTrue(); 59 | } 60 | 61 | @Test public void testSingletonBindingDelegatesSetCycleFree() { 62 | singletonBinding.setCycleFree(true); 63 | assertThat(wrappedBinding.isCycleFree()).isTrue(); 64 | } 65 | 66 | @Test public void testSingletonBindingDelegatesIsCycleFree() { 67 | wrappedBinding.setCycleFree(true); 68 | assertThat(singletonBinding.isCycleFree()).isTrue(); 69 | } 70 | 71 | private static class StringBinding extends Binding { 72 | private StringBinding() { 73 | super("dummy", "dummy", true, "dummy"); // 3rd arg true => singleton 74 | } 75 | 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /core/src/test/java/dagger/internal/TestingLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * Copyright (C) 2013 Google, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package dagger.internal; 18 | 19 | 20 | import dagger.internal.loaders.ReflectiveAtInjectBinding; 21 | import dagger.internal.loaders.ReflectiveStaticInjection; 22 | 23 | /** 24 | * A test-only loader that merely uses reflection to test internals. 25 | */ 26 | public final class TestingLoader extends Loader { 27 | 28 | @Override public ModuleAdapter getModuleAdapter(Class type) { 29 | ModuleAdapter adapter = TestingModuleAdapter.create(type); 30 | return adapter; 31 | } 32 | 33 | @Override public Binding getAtInjectBinding(String key, String className, ClassLoader ignored, 34 | boolean mustHaveInjections) { 35 | try { 36 | Class type = getClass().getClassLoader().loadClass(className); 37 | if (type.isInterface()) { 38 | return null; // Short-circuit since we can't build reflective bindings for interfaces. 39 | } 40 | return ReflectiveAtInjectBinding.create(type, mustHaveInjections); 41 | } catch (ClassNotFoundException e) { 42 | throw new TypeNotPresentException( 43 | String.format("Could not find %s needed for binding %s", className, key), e); 44 | } 45 | } 46 | 47 | @Override public StaticInjection getStaticInjection(Class injectedClass) { 48 | return ReflectiveStaticInjection.create(injectedClass); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /deploy_website.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Deploys the current Dagger website to the gh-pages branch of the GitHub 4 | # repository. To test the site locally before deploying run `jekyll --server` 5 | # in the website/ directory. 6 | 7 | set -ex 8 | 9 | REPO="git@github.com:square/dagger.git" 10 | DIR=temp-dagger-clone 11 | 12 | # Delete any existing temporary website clone 13 | rm -rf $DIR 14 | 15 | # Clone the current repo into temp folder 16 | git clone $REPO $DIR 17 | 18 | # Move working directory into temp folder 19 | cd $DIR 20 | 21 | # Checkout and track the gh-pages branch 22 | git checkout -t origin/gh-pages 23 | 24 | # Delete everything that isn't versioned (1.x, 2.x) 25 | ls | grep -E -v '^\d+\.x$' | xargs rm -rf 26 | 27 | # Copy website files from real repo 28 | cp -R ../website/* . 29 | 30 | # Stage all files in git and create a commit 31 | git add . 32 | git add -u 33 | git commit -m "Website at $(date)" 34 | 35 | # Push the new files up to GitHub 36 | git push origin gh-pages 37 | 38 | # Delete our temp folder 39 | cd .. 40 | rm -rf $DIR 41 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/README.md: -------------------------------------------------------------------------------- 1 | Example: Android Activity Graphs 2 | ================================ 3 | 4 | Building on top of the simple Android example, this example demonstrates how it is possible to 5 | create child graphs for each activity which extend from the global graph. 6 | 7 | Some of the advantages of the activity scope: 8 | 9 | * Provides the ability to inject objects which require the activity to be constructed. 10 | * Allows for the use of singletons on a per-activity basis. This is a great way to manage a 11 | resource that is shared by a bunch of fragments in an activity. 12 | * Keeps the global object graph clear of things that can be used only by activities. 13 | 14 | While this example only shows the presence of an activity scope, you should be able to see the 15 | potential for other useful scopes that can be used. For example, having a dedicated object graph 16 | for the current user session is a great way to manage data that is tied to the currently logged-in 17 | user. 18 | 19 | _Note: The app does not actually do anything when it is run. It is only to show how you can 20 | structure Dagger within an Android app_ 21 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 4.0.0 19 | 20 | 21 | com.squareup.dagger.example 22 | dagger-example-parent 23 | 1.2.6-SNAPSHOT 24 | 25 | 26 | android-activity-graphs 27 | Examples: Android - Activity Graphs 28 | apk 29 | 30 | 31 | 32 | com.squareup.dagger 33 | dagger 34 | ${project.version} 35 | 36 | 37 | com.squareup.dagger 38 | dagger-compiler 39 | ${project.version} 40 | true 41 | 42 | 43 | 44 | com.google.android 45 | android 46 | provided 47 | 48 | 49 | com.google.android 50 | support-v4 51 | 52 | 53 | 54 | 55 | 56 | 57 | com.simpligility.maven.plugins 58 | android-maven-plugin 59 | true 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | 10 | 13 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ActivityModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.activitygraphs; 17 | 18 | import android.content.Context; 19 | import com.example.dagger.activitygraphs.ui.ActivityTitleController; 20 | import com.example.dagger.activitygraphs.ui.HomeActivity; 21 | import com.example.dagger.activitygraphs.ui.HomeFragment; 22 | import dagger.Module; 23 | import dagger.Provides; 24 | import javax.inject.Singleton; 25 | 26 | /** 27 | * This module represents objects which exist only for the scope of a single activity. We can 28 | * safely create singletons using the activity instance because the entire object graph will only 29 | * ever exist inside of that activity. 30 | */ 31 | @Module( 32 | injects = { 33 | HomeActivity.class, 34 | HomeFragment.class 35 | }, 36 | addsTo = AndroidModule.class, 37 | library = true 38 | ) 39 | public class ActivityModule { 40 | private final DemoBaseActivity activity; 41 | 42 | public ActivityModule(DemoBaseActivity activity) { 43 | this.activity = activity; 44 | } 45 | 46 | /** 47 | * Allow the activity context to be injected but require that it be annotated with 48 | * {@link ForActivity @ForActivity} to explicitly differentiate it from application context. 49 | */ 50 | @Provides @Singleton @ForActivity Context provideActivityContext() { 51 | return activity; 52 | } 53 | 54 | @Provides @Singleton ActivityTitleController provideTitleController() { 55 | return new ActivityTitleController(activity); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/AndroidModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.activitygraphs; 17 | 18 | import android.content.Context; 19 | import android.location.LocationManager; 20 | import dagger.Module; 21 | import dagger.Provides; 22 | import javax.inject.Singleton; 23 | 24 | import static android.content.Context.LOCATION_SERVICE; 25 | 26 | /** 27 | * A module for Android-specific dependencies which require a {@link Context} or 28 | * {@link android.app.Application} to create. 29 | */ 30 | @Module(library = true) 31 | public class AndroidModule { 32 | private final DemoApplication application; 33 | 34 | public AndroidModule(DemoApplication application) { 35 | this.application = application; 36 | } 37 | 38 | /** 39 | * Allow the application context to be injected but require that it be annotated with 40 | * {@link ForApplication @ForApplication} to explicitly differentiate it from an activity context. 41 | */ 42 | @Provides @Singleton @ForApplication Context provideApplicationContext() { 43 | return application; 44 | } 45 | 46 | @Provides @Singleton LocationManager provideLocationManager() { 47 | return (LocationManager) application.getSystemService(LOCATION_SERVICE); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/DemoApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.activitygraphs; 17 | 18 | import android.app.Application; 19 | import dagger.ObjectGraph; 20 | import java.util.Arrays; 21 | import java.util.List; 22 | 23 | public class DemoApplication extends Application { 24 | private ObjectGraph applicationGraph; 25 | 26 | @Override public void onCreate() { 27 | super.onCreate(); 28 | 29 | applicationGraph = ObjectGraph.create(getModules().toArray()); 30 | } 31 | 32 | /** 33 | * A list of modules to use for the application graph. Subclasses can override this method to 34 | * provide additional modules provided they call {@code super.getModules()}. 35 | */ 36 | protected List getModules() { 37 | return Arrays.asList(new AndroidModule(this)); 38 | } 39 | 40 | ObjectGraph getApplicationGraph() { 41 | return applicationGraph; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/DemoBaseActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.activitygraphs; 17 | 18 | import android.os.Bundle; 19 | import android.support.v4.app.FragmentActivity; 20 | import dagger.ObjectGraph; 21 | import java.util.Arrays; 22 | import java.util.List; 23 | 24 | /** Base activity which sets up a per-activity object graph and performs injection. */ 25 | public abstract class DemoBaseActivity extends FragmentActivity { 26 | private ObjectGraph activityGraph; 27 | 28 | @Override protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | 31 | // Create the activity graph by .plus-ing our modules onto the application graph. 32 | DemoApplication application = (DemoApplication) getApplication(); 33 | activityGraph = application.getApplicationGraph().plus(getModules().toArray()); 34 | 35 | // Inject ourselves so subclasses will have dependencies fulfilled when this method returns. 36 | activityGraph.inject(this); 37 | } 38 | 39 | @Override protected void onDestroy() { 40 | // Eagerly clear the reference to the activity graph to allow it to be garbage collected as 41 | // soon as possible. 42 | activityGraph = null; 43 | 44 | super.onDestroy(); 45 | } 46 | 47 | /** 48 | * A list of modules to use for the individual activity graph. Subclasses can override this 49 | * method to provide additional modules provided they call and include the modules returned by 50 | * calling {@code super.getModules()}. 51 | */ 52 | protected List getModules() { 53 | return Arrays.asList(new ActivityModule(this)); 54 | } 55 | 56 | /** Inject the supplied {@code object} using the activity-specific graph. */ 57 | public void inject(Object object) { 58 | activityGraph.inject(object); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/DemoBaseFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.activitygraphs; 17 | 18 | import android.os.Bundle; 19 | import android.support.v4.app.Fragment; 20 | 21 | /** Base fragment which performs injection using the activity object graph of its parent. */ 22 | public class DemoBaseFragment extends Fragment { 23 | @Override public void onActivityCreated(Bundle savedInstanceState) { 24 | super.onActivityCreated(savedInstanceState); 25 | ((DemoBaseActivity) getActivity()).inject(this); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ForActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.activitygraphs; 17 | 18 | import java.lang.annotation.Retention; 19 | import javax.inject.Qualifier; 20 | 21 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 22 | 23 | @Qualifier @Retention(RUNTIME) 24 | public @interface ForActivity { 25 | } 26 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ForApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.activitygraphs; 17 | 18 | import java.lang.annotation.Retention; 19 | import javax.inject.Qualifier; 20 | 21 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 22 | 23 | @Qualifier @Retention(RUNTIME) 24 | public @interface ForApplication { 25 | } 26 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ui/ActivityTitleController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.activitygraphs.ui; 17 | 18 | import android.app.Activity; 19 | 20 | /** 21 | * A simple abstraction which provides the ability to set the title on an activity. 22 | *

23 | * Fragments should not directly modify any part of an activity outside of the view or dialog that 24 | * it creates. This class provides a way for fragments to inject a controller that will allow for 25 | * control of the activity title. While not exceedingly useful in practice, this concept could be 26 | * expanded to things like facilitating control over the action bar, dialogs, notifications, etc. 27 | */ 28 | public class ActivityTitleController { 29 | private final Activity activity; 30 | 31 | public ActivityTitleController(Activity activity) { 32 | this.activity = activity; 33 | } 34 | 35 | public void setTitle(CharSequence title) { 36 | activity.setTitle(title); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ui/HomeActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.activitygraphs.ui; 17 | 18 | import android.location.LocationManager; 19 | import android.os.Bundle; 20 | import com.example.dagger.activitygraphs.DemoBaseActivity; 21 | import javax.inject.Inject; 22 | 23 | public class HomeActivity extends DemoBaseActivity { 24 | @Inject LocationManager locationManager; 25 | 26 | @Override protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | // After the super.onCreate call returns we are guaranteed our injections are available. 29 | 30 | if (savedInstanceState == null) { 31 | getSupportFragmentManager().beginTransaction() 32 | .add(android.R.id.content, HomeFragment.newInstance()) 33 | .commit(); 34 | } 35 | 36 | // TODO do something with the injected dependencies here! 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/src/main/java/com/example/dagger/activitygraphs/ui/HomeFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.activitygraphs.ui; 17 | 18 | import android.os.Bundle; 19 | import android.view.LayoutInflater; 20 | import android.view.View; 21 | import android.view.ViewGroup; 22 | import android.widget.TextView; 23 | import com.example.dagger.activitygraphs.DemoBaseFragment; 24 | import javax.inject.Inject; 25 | 26 | import static android.view.Gravity.CENTER; 27 | 28 | public class HomeFragment extends DemoBaseFragment { 29 | public static HomeFragment newInstance() { 30 | return new HomeFragment(); 31 | } 32 | 33 | @Inject ActivityTitleController titleController; 34 | 35 | @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, 36 | Bundle savedInstanceState) { 37 | TextView tv = new TextView(getActivity()); 38 | tv.setGravity(CENTER); 39 | tv.setText("Hello, World"); 40 | return tv; 41 | } 42 | 43 | @Override public void onResume() { 44 | super.onResume(); 45 | 46 | // Fragments should not modify things outside of their own view. Use an external controller to 47 | // ask the activity to change its title. 48 | titleController.setTitle("Home Fragment"); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /examples/android-activity-graphs/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Dagger Activity Graph 4 | 5 | -------------------------------------------------------------------------------- /examples/android-simple/README.md: -------------------------------------------------------------------------------- 1 | Example: Android Simple 2 | ======================= 3 | 4 | This example demonstrates how to structure an Android application with Dagger. 5 | 6 | A custom `Application` class is used to manage a global object graph of objects. Modules are 7 | assembled with a `getModules` method on the application that can be overridden to add additional 8 | modules in development versions of your applications or in tests. 9 | 10 | Injection of activities is done automatically in a base activity. 11 | 12 | _Note: The app does not actually do anything when it is run. It is only to show how you can 13 | structure Dagger within an Android app_ 14 | -------------------------------------------------------------------------------- /examples/android-simple/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 4.0.0 19 | 20 | 21 | com.squareup.dagger.example 22 | dagger-example-parent 23 | 1.2.6-SNAPSHOT 24 | 25 | 26 | android-simple 27 | Examples: Android - Simple 28 | apk 29 | 30 | 31 | 32 | com.squareup.dagger 33 | dagger 34 | ${project.version} 35 | 36 | 37 | com.squareup.dagger 38 | dagger-compiler 39 | ${project.version} 40 | true 41 | 42 | 43 | 44 | com.google.android 45 | android 46 | provided 47 | 48 | 49 | 50 | 51 | 52 | 53 | com.simpligility.maven.plugins 54 | android-maven-plugin 55 | true 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /examples/android-simple/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | 10 | 13 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /examples/android-simple/src/main/java/com/example/dagger/simple/AndroidModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.simple; 17 | 18 | import android.content.Context; 19 | import android.location.LocationManager; 20 | import dagger.Module; 21 | import dagger.Provides; 22 | import javax.inject.Singleton; 23 | 24 | import static android.content.Context.LOCATION_SERVICE; 25 | 26 | /** 27 | * A module for Android-specific dependencies which require a {@link Context} or 28 | * {@link android.app.Application} to create. 29 | */ 30 | @Module(library = true) 31 | public class AndroidModule { 32 | private final DemoApplication application; 33 | 34 | public AndroidModule(DemoApplication application) { 35 | this.application = application; 36 | } 37 | 38 | /** 39 | * Allow the application context to be injected but require that it be annotated with 40 | * {@link ForApplication @Annotation} to explicitly differentiate it from an activity context. 41 | */ 42 | @Provides @Singleton @ForApplication Context provideApplicationContext() { 43 | return application; 44 | } 45 | 46 | @Provides @Singleton LocationManager provideLocationManager() { 47 | return (LocationManager) application.getSystemService(LOCATION_SERVICE); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /examples/android-simple/src/main/java/com/example/dagger/simple/DemoApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.simple; 17 | 18 | import android.app.Application; 19 | import dagger.ObjectGraph; 20 | import java.util.Arrays; 21 | import java.util.List; 22 | 23 | public class DemoApplication extends Application { 24 | private ObjectGraph graph; 25 | 26 | @Override public void onCreate() { 27 | super.onCreate(); 28 | 29 | graph = ObjectGraph.create(getModules().toArray()); 30 | } 31 | 32 | protected List getModules() { 33 | return Arrays.asList( 34 | new AndroidModule(this), 35 | new DemoModule() 36 | ); 37 | } 38 | 39 | public void inject(Object object) { 40 | graph.inject(object); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /examples/android-simple/src/main/java/com/example/dagger/simple/DemoBaseActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.simple; 17 | 18 | import android.app.Activity; 19 | import android.os.Bundle; 20 | 21 | public abstract class DemoBaseActivity extends Activity { 22 | @Override protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | 25 | // Perform injection so that when this call returns all dependencies will be available for use. 26 | ((DemoApplication) getApplication()).inject(this); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /examples/android-simple/src/main/java/com/example/dagger/simple/DemoModule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.simple; 17 | 18 | import com.example.dagger.simple.ui.HomeActivity; 19 | import dagger.Module; 20 | 21 | @Module( 22 | injects = HomeActivity.class, 23 | complete = false 24 | ) 25 | public class DemoModule { 26 | // TODO put your application-specific providers here! 27 | } 28 | -------------------------------------------------------------------------------- /examples/android-simple/src/main/java/com/example/dagger/simple/ForApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.simple; 17 | 18 | import java.lang.annotation.Retention; 19 | import javax.inject.Qualifier; 20 | 21 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 22 | 23 | @Qualifier @Retention(RUNTIME) 24 | public @interface ForApplication { 25 | } 26 | -------------------------------------------------------------------------------- /examples/android-simple/src/main/java/com/example/dagger/simple/ui/HomeActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.dagger.simple.ui; 17 | 18 | import android.location.LocationManager; 19 | import android.os.Bundle; 20 | import com.example.dagger.simple.DemoBaseActivity; 21 | import javax.inject.Inject; 22 | 23 | public class HomeActivity extends DemoBaseActivity { 24 | @Inject LocationManager locationManager; 25 | 26 | @Override protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | // After the super.onCreate call returns we are guaranteed our injections are available. 29 | 30 | // TODO do something with the injected dependencies here! 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /examples/android-simple/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Dagger Simple 4 | 5 | -------------------------------------------------------------------------------- /examples/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 4.0.0 20 | 21 | 22 | com.squareup.dagger 23 | dagger-parent 24 | 1.2.6-SNAPSHOT 25 | 26 | 27 | com.squareup.dagger.example 28 | dagger-example-parent 29 | pom 30 | Examples 31 | 32 | 33 | simple 34 | 35 | 36 | 37 | 38 | 39 | android 40 | 41 | true 42 | 43 | 44 | simple 45 | android-simple 46 | android-activity-graphs 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | com.google.android 56 | android 57 | 4.1.1.4 58 | 59 | 60 | com.google.android 61 | support-v4 62 | r7 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | com.simpligility.maven.plugins 72 | android-maven-plugin 73 | 4.1.1 74 | 75 | 76 | 16 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /examples/simple/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 4.0.0 19 | 20 | 21 | com.squareup.dagger.example 22 | dagger-example-parent 23 | 1.2.6-SNAPSHOT 24 | 25 | 26 | simple 27 | Examples: Simple 28 | 29 | 30 | 31 | com.squareup.dagger 32 | dagger 33 | ${project.version} 34 | 35 | 36 | com.squareup.dagger 37 | dagger-compiler 38 | ${project.version} 39 | true 40 | 41 | 42 | 43 | junit 44 | junit 45 | test 46 | 47 | 48 | org.mockito 49 | mockito-core 50 | test 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /examples/simple/src/main/java/coffee/CoffeeApp.java: -------------------------------------------------------------------------------- 1 | package coffee; 2 | 3 | import javax.inject.Inject; 4 | 5 | import dagger.ObjectGraph; 6 | 7 | public class CoffeeApp implements Runnable { 8 | @Inject CoffeeMaker coffeeMaker; 9 | 10 | @Override public void run() { 11 | coffeeMaker.brew(); 12 | } 13 | 14 | public static void main(String[] args) { 15 | ObjectGraph objectGraph = ObjectGraph.create(new DripCoffeeModule()); 16 | CoffeeApp coffeeApp = objectGraph.get(CoffeeApp.class); 17 | coffeeApp.run(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /examples/simple/src/main/java/coffee/CoffeeMaker.java: -------------------------------------------------------------------------------- 1 | package coffee; 2 | 3 | import dagger.Lazy; 4 | import javax.inject.Inject; 5 | 6 | class CoffeeMaker { 7 | @Inject Lazy heater; // Don't want to create a possibly costly heater until we need it. 8 | @Inject Pump pump; 9 | 10 | public void brew() { 11 | heater.get().on(); 12 | pump.pump(); 13 | System.out.println(" [_]P coffee! [_]P "); 14 | heater.get().off(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /examples/simple/src/main/java/coffee/DripCoffeeModule.java: -------------------------------------------------------------------------------- 1 | package coffee; 2 | 3 | import dagger.Module; 4 | import dagger.Provides; 5 | import javax.inject.Singleton; 6 | 7 | @Module( 8 | injects = CoffeeApp.class, 9 | includes = PumpModule.class 10 | ) 11 | class DripCoffeeModule { 12 | @Provides @Singleton Heater provideHeater() { 13 | return new ElectricHeater(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /examples/simple/src/main/java/coffee/ElectricHeater.java: -------------------------------------------------------------------------------- 1 | package coffee; 2 | 3 | class ElectricHeater implements Heater { 4 | boolean heating; 5 | 6 | @Override public void on() { 7 | System.out.println("~ ~ ~ heating ~ ~ ~"); 8 | this.heating = true; 9 | } 10 | 11 | @Override public void off() { 12 | this.heating = false; 13 | } 14 | 15 | @Override public boolean isHot() { 16 | return heating; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /examples/simple/src/main/java/coffee/Heater.java: -------------------------------------------------------------------------------- 1 | package coffee; 2 | 3 | interface Heater { 4 | void on(); 5 | void off(); 6 | boolean isHot(); 7 | } 8 | -------------------------------------------------------------------------------- /examples/simple/src/main/java/coffee/Pump.java: -------------------------------------------------------------------------------- 1 | package coffee; 2 | 3 | interface Pump { 4 | void pump(); 5 | } 6 | -------------------------------------------------------------------------------- /examples/simple/src/main/java/coffee/PumpModule.java: -------------------------------------------------------------------------------- 1 | package coffee; 2 | 3 | import dagger.Module; 4 | import dagger.Provides; 5 | 6 | @Module(complete = false, library = true) 7 | class PumpModule { 8 | @Provides Pump providePump(Thermosiphon pump) { 9 | return pump; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /examples/simple/src/main/java/coffee/Thermosiphon.java: -------------------------------------------------------------------------------- 1 | package coffee; 2 | 3 | import javax.inject.Inject; 4 | 5 | class Thermosiphon implements Pump { 6 | private final Heater heater; 7 | 8 | @Inject 9 | Thermosiphon(Heater heater) { 10 | this.heater = heater; 11 | } 12 | 13 | @Override public void pump() { 14 | if (heater.isHot()) { 15 | System.out.println("=> => pumping => =>"); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /examples/simple/src/test/java/coffee/CoffeeMakerTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2015 Square, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package coffee; 17 | 18 | import dagger.Module; 19 | import dagger.ObjectGraph; 20 | import dagger.Provides; 21 | import javax.inject.Inject; 22 | import javax.inject.Singleton; 23 | import org.junit.Before; 24 | import org.junit.Test; 25 | import org.junit.runner.RunWith; 26 | import org.junit.runners.JUnit4; 27 | import org.mockito.Mockito; 28 | 29 | public class CoffeeMakerTest { 30 | @Inject CoffeeMaker coffeeMaker; 31 | @Inject Heater heater; 32 | 33 | @Before public void setUp() { 34 | ObjectGraph.create(new TestModule()).inject(this); 35 | } 36 | 37 | @Module( 38 | includes = DripCoffeeModule.class, 39 | injects = CoffeeMakerTest.class, 40 | overrides = true 41 | ) 42 | static class TestModule { 43 | @Provides @Singleton Heater provideHeater() { 44 | return Mockito.mock(Heater.class); 45 | } 46 | } 47 | 48 | @Test public void testHeaterIsTurnedOnAndThenOff() { 49 | Mockito.when(heater.isHot()).thenReturn(true); 50 | coffeeMaker.brew(); 51 | Mockito.verify(heater, Mockito.times(1)).on(); 52 | Mockito.verify(heater, Mockito.times(1)).off(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /website/static/app-theme.css: -------------------------------------------------------------------------------- 1 | /* http://www.colorhexa.com/487fb9 */ 2 | 3 | /*** Primary ***/ 4 | 5 | header, 6 | #subtitle, 7 | a.dl { 8 | background-color: #487fb9; 9 | } 10 | 11 | .content-nav li.active a, 12 | .content-nav li.active a:hover { 13 | border-left-color: #487fb9; 14 | } 15 | 16 | /*** One step left on the monochromatic scale ***/ 17 | 18 | header menu li a:hover, 19 | a.dl:hover { 20 | background-color: #4072a7; 21 | } 22 | a { 23 | color: #4072a7; 24 | } 25 | 26 | /*** Three steps left on the monochromatic scale ***/ 27 | 28 | a:hover { 29 | color: #325983; 30 | } 31 | 32 | 33 | /****************************************************************\ 34 | **** Syntax highlighting styles ******************************** 35 | \****************************************************************/ 36 | 37 | .pln { color: #000; } 38 | .str { color: #396695; } 39 | .kwd { color: #666; } 40 | .com { color: #800; } 41 | .typ { color: #222; } 42 | .lit { color: #666; } 43 | .pun { color: #888; } 44 | .opn { color: #888; } 45 | .clo { color: #888; } 46 | .tag { color: #396695; } 47 | .atn { color: #606; } 48 | .atv { color: #080; } 49 | .dec { color: #606; } 50 | .var { color: #606; } 51 | .fun { color: #f00; } 52 | -------------------------------------------------------------------------------- /website/static/html5shiv.min.js: -------------------------------------------------------------------------------- 1 | /* 2 | HTML5 Shiv v3.6.2pre | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed 3 | */ 4 | (function(l,f){function m(){var a=e.elements;return"string"==typeof a?a.split(" "):a}function i(a){var b=n[a[o]];b||(b={},h++,a[o]=h,n[h]=b);return b}function p(a,b,c){b||(b=f);if(g)return b.createElement(a);c||(c=i(b));b=c.cache[a]?c.cache[a].cloneNode():r.test(a)?(c.cache[a]=c.createElem(a)).cloneNode():c.createElem(a);return b.canHaveChildren&&!s.test(a)?c.frag.appendChild(b):b}function t(a,b){if(!b.cache)b.cache={},b.createElem=a.createElement,b.createFrag=a.createDocumentFragment,b.frag=b.createFrag(); 5 | a.createElement=function(c){return!e.shivMethods?b.createElem(c):p(c,a,b)};a.createDocumentFragment=Function("h,f","return function(){var n=f.cloneNode(),c=n.createElement;h.shivMethods&&("+m().join().replace(/\w+/g,function(a){b.createElem(a);b.frag.createElement(a);return'c("'+a+'")'})+");return n}")(e,b.frag)}function q(a){a||(a=f);var b=i(a);if(e.shivCSS&&!j&&!b.hasCSS){var c,d=a;c=d.createElement("p");d=d.getElementsByTagName("head")[0]||d.documentElement;c.innerHTML="x"; 6 | c=d.insertBefore(c.lastChild,d.firstChild);b.hasCSS=!!c}g||t(a,b);return a}var k=l.html5||{},s=/^<|^(?:button|map|select|textarea|object|iframe|option|optgroup)$/i,r=/^(?:a|b|code|div|fieldset|h1|h2|h3|h4|h5|h6|i|label|li|ol|p|q|span|strong|style|table|tbody|td|th|tr|ul)$/i,j,o="_html5shiv",h=0,n={},g;(function(){try{var a=f.createElement("a");a.innerHTML="";j="hidden"in a;var b;if(!(b=1==a.childNodes.length)){f.createElement("a");var c=f.createDocumentFragment();b="undefined"==typeof c.cloneNode|| 7 | "undefined"==typeof c.createDocumentFragment||"undefined"==typeof c.createElement}g=b}catch(d){g=j=!0}})();var e={elements:k.elements||"abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video",version:"3.6.2pre",shivCSS:!1!==k.shivCSS,supportsUnknownElements:g,shivMethods:!1!==k.shivMethods,type:"default",shivDocument:q,createElement:p,createDocumentFragment:function(a,b){a||(a=f);if(g)return a.createDocumentFragment(); 8 | for(var b=b||i(a),c=b.frag.cloneNode(),d=0,e=m(),h=e.length;do2.v?-1:1});var newVersions=[];for(var i=0;i0?e.push(this):(t[r](1),o=t[r]()>0,o&&e.push(this),t[r](0))}}),e.length||this.each(function(){"BODY"===this.nodeName&&(e=[this])}),"first"===t.el&&e.length>1&&(e=[e[0]]),e};l.fn.extend({scrollable:function(l){var t=r.call(this,{dir:l});return this.pushStack(t)},firstScrollable:function(l){var t=r.call(this,{el:"first",dir:l});return this.pushStack(t)},smoothScroll:function(e){e=e||{};var o=l.extend({},l.fn.smoothScroll.defaults,e),r=l.smoothScroll.filterPath(location.pathname);return this.unbind("click.smoothscroll").bind("click.smoothscroll",function(e){var n=this,s=l(this),c=o.exclude,i=o.excludeWithin,a=0,f=0,h=!0,u={},d=location.hostname===n.hostname||!n.hostname,m=o.scrollTarget||(l.smoothScroll.filterPath(n.pathname)||r)===r,p=t(n.hash);if(o.scrollTarget||d&&m&&p){for(;h&&c.length>a;)s.is(t(c[a++]))&&(h=!1);for(;h&&i.length>f;)s.closest(i[f++]).length&&(h=!1)}else h=!1;h&&(e.preventDefault(),l.extend(u,o,{scrollTarget:o.scrollTarget||p,link:n}),l.smoothScroll(u))}),this}}),l.smoothScroll=function(t,e){var o,r,n,s,c=0,i="offset",a="scrollTop",f={},h={};"number"==typeof t?(o=l.fn.smoothScroll.defaults,n=t):(o=l.extend({},l.fn.smoothScroll.defaults,t||{}),o.scrollElement&&(i="position","static"==o.scrollElement.css("position")&&o.scrollElement.css("position","relative"))),o=l.extend({link:null},o),a="left"==o.direction?"scrollLeft":a,o.scrollElement?(r=o.scrollElement,c=r[a]()):r=l("html, body").firstScrollable(),o.beforeScroll.call(r,o),n="number"==typeof t?t:e||l(o.scrollTarget)[i]()&&l(o.scrollTarget)[i]()[o.direction]||0,f[a]=n+c+o.offset,s=o.speed,"auto"===s&&(s=f[a]||r.scrollTop(),s/=o.autoCoefficent),h={duration:s,easing:o.easing,complete:function(){o.afterScroll.call(o.link,o)}},o.step&&(h.step=o.step),r.length?r.stop().animate(f,h):o.afterScroll.call(o.link,o)},l.smoothScroll.version=e,l.smoothScroll.filterPath=function(l){return l.replace(/^\//,"").replace(/(index|default).[a-zA-Z]{3,4}$/,"").replace(/\/$/,"")},l.fn.smoothScroll.defaults=o})(jQuery); -------------------------------------------------------------------------------- /website/static/logo-square.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/square/dagger/0fa73e0ee07015f191c25b1f3fdf7048321d1610/website/static/logo-square.png --------------------------------------------------------------------------------