Compare commits
No commits in common. "372b5be77e5b1adbc494bc00698e8d7535ccfdc6" and "240983e9ece32cac324928c15e6568d6b812ba28" have entirely different histories.
372b5be77e
...
240983e9ec
276 changed files with 32933 additions and 45283 deletions
7
.travis.yml
Normal file
7
.travis.yml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
sudo: false
|
||||||
|
language: java
|
||||||
|
jdk:
|
||||||
|
- oraclejdk8
|
||||||
|
cache:
|
||||||
|
directories:
|
||||||
|
- $HOME/.m2
|
19
README.md
19
README.md
|
@ -1,25 +1,24 @@
|
||||||
= Guice - simplified and reduced version
|
# Guice
|
||||||
|
|
||||||
This is xbib Guice, a build of Google Guice with the following differences to the
|
This is a xbib Guice, a build of Guice with the following differences to the
|
||||||
original [Google Guice Core Library](https://github.com/google/guice):
|
original [Google Guice Core Library](https://github.com/google/guice):
|
||||||
|
|
||||||
- only external dependencies are
|
- only external dependencies are
|
||||||
- javax.inject:javax.inject:1
|
- javax.inject:javax.inject:1
|
||||||
- org.xbib:jsr-305:1.0.0
|
- javax.annotation:javax.annotation-api:1.2
|
||||||
- com.google.guava:guava:30.1
|
- com.google.guava:guava:19.0
|
||||||
|
|
||||||
- the extra AOP stuff was removed (aopalliance/cglib), no intercepting any more
|
- the extra AOP stuff was removed (aopalliance/cglib), no intercepting any more
|
||||||
- removed all logging, uses of java.util.logger.* stuff and logger default binding
|
- removed all logging, uses of java.util.logger.* stuff and logger default binding
|
||||||
- removed all java.io.Serializable dependent stuff. Note: everything which relies to be serializable was removed.
|
- removed all java.io.Serializable dependent stuff
|
||||||
- reformatted source by IntelliJ and cleaned up source of @author and @since tags
|
- reformatted source by IntelliJ and cleaned up source of @author and @since tags
|
||||||
- target of compilation is Java 11, no support for Java 6/7
|
- target of compilation is Java 8, no support for Java 6/7
|
||||||
- introduced Gradle as build system
|
- introduced Gradle as build system
|
||||||
- junit tests made compatible to junit 4
|
- junit tests made compatible to junit 4.12
|
||||||
- assistedinject and multibindings extensions are included
|
- assistedinject and multibindings extension included
|
||||||
|
|
||||||
All credits belong to the original authors, especially Bob Lee http://blog.crazybob.org/
|
All credits belong to the original authors, especially Bob Lee http://blog.crazybob.org/
|
||||||
|
|
||||||
== License
|
## License
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
|
124
build.gradle
124
build.gradle
|
@ -1,51 +1,85 @@
|
||||||
plugins {
|
|
||||||
//id "checkstyle"
|
group = 'org.xbib'
|
||||||
//id "pmd"
|
version = '4.0'
|
||||||
id 'maven-publish'
|
|
||||||
id 'signing'
|
apply plugin: 'java'
|
||||||
id "io.github.gradle-nexus.publish-plugin" version "1.3.0"
|
apply plugin: 'maven'
|
||||||
//id "com.github.spotbugs" version "5.0.14"
|
|
||||||
id "org.cyclonedx.bom" version "1.7.2"
|
repositories {
|
||||||
//id "org.xbib.gradle.plugin.asciidoctor" version "3.0.0"
|
mavenCentral()
|
||||||
|
mavenLocal()
|
||||||
|
jcenter()
|
||||||
|
maven {
|
||||||
|
url "http://xbib.org/repository"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wrapper {
|
configurations {
|
||||||
gradleVersion = libs.versions.gradle.get()
|
wagon
|
||||||
distributionType = Wrapper.DistributionType.ALL
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ext {
|
|
||||||
user = 'joerg'
|
|
||||||
name = 'guice'
|
|
||||||
description = 'Guice implementation with named modules for Java 11+'
|
|
||||||
inceptionYear = '2012'
|
|
||||||
url = 'https://xbib.org/' + user + '/' + name
|
|
||||||
scmUrl = 'https://xbib.org/' + user + '/' + name
|
|
||||||
scmConnection = 'scm:git:git://xbib.org/' + user + '/' + name + '.git'
|
|
||||||
scmDeveloperConnection = 'scm:git:ssh://forgejo@xbib.org:' + user + '/' + name + '.git'
|
|
||||||
issueManagementSystem = 'Github'
|
|
||||||
issueManagementUrl = ext.scmUrl + '/issues'
|
|
||||||
licenseName = 'The Apache License, Version 2.0'
|
|
||||||
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
|
|
||||||
}
|
|
||||||
|
|
||||||
apply plugin: 'java-library'
|
|
||||||
apply from: rootProject.file('gradle/init/banner.gradle')
|
|
||||||
apply from: rootProject.file('gradle/ide/idea.gradle')
|
|
||||||
apply from: rootProject.file('gradle/repositories/maven.gradle')
|
|
||||||
apply from: rootProject.file('gradle/compile/java.gradle')
|
|
||||||
apply from: rootProject.file('gradle/test/junit5.gradle')
|
|
||||||
apply from: rootProject.file('gradle/quality/cyclonedx.gradle')
|
|
||||||
//apply from: rootProject.file('gradle/quality/spotbugs.gradle')
|
|
||||||
//apply from: rootProject.file('gradle/quality/checkstyle.gradle')
|
|
||||||
//apply from: rootProject.file('gradle/quality/pmd.gradle')
|
|
||||||
apply from: rootProject.file('gradle/publish/sonatype.gradle')
|
|
||||||
apply from: rootProject.file('gradle/publish/forgejo.gradle')
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
api libs.javax.inject
|
compile "javax.inject:javax.inject:1"
|
||||||
api libs.guava
|
compile 'javax.annotation:javax.annotation-api:1.2'
|
||||||
testImplementation libs.junit4
|
compile "com.google.guava:guava:19.0"
|
||||||
// Helper for com.google.common.testing.GcFinalization, com.google.common.testing.EqualsTester
|
testCompile "junit:junit:4.12"
|
||||||
testImplementation libs.guava.testlib
|
testCompile "org.apache.logging.log4j:log4j-slf4j-impl:2.5"
|
||||||
|
testCompile "org.apache.logging.log4j:log4j-core:2.5"
|
||||||
|
testCompile "javax.inject:javax.inject-tck:1"
|
||||||
|
testCompile "com.google.guava:guava-testlib:19.0"
|
||||||
|
wagon 'org.apache.maven.wagon:wagon-ssh-external:2.10'
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceCompatibility = 1.8
|
||||||
|
targetCompatibility = 1.8
|
||||||
|
|
||||||
|
[compileJava, compileTestJava]*.options.collect { options ->
|
||||||
|
options.encoding = 'UTF-8'
|
||||||
|
options.compilerArgs << '-Xlint:-serial,-path,-rawtypes,-unchecked'
|
||||||
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
exclude '*$*'
|
||||||
|
exclude '**/ErrorHandlingTest*'
|
||||||
|
exclude '**/OSGiContainerTest*'
|
||||||
|
exclude '**/ScopesTest*'
|
||||||
|
exclude '**/TypeConversionTest*'
|
||||||
|
testLogging {
|
||||||
|
showStandardStreams = false
|
||||||
|
exceptionFormat = 'full'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
task sourcesJar(type: Jar, dependsOn: classes) {
|
||||||
|
from sourceSets.main.allSource
|
||||||
|
classifier 'sources'
|
||||||
|
}
|
||||||
|
|
||||||
|
artifacts {
|
||||||
|
archives sourcesJar
|
||||||
|
}
|
||||||
|
|
||||||
|
uploadArchives {
|
||||||
|
repositories {
|
||||||
|
if (project.hasProperty("xbibUsername")) {
|
||||||
|
mavenDeployer {
|
||||||
|
configuration = configurations.wagon
|
||||||
|
repository(
|
||||||
|
id: 'xbib.org',
|
||||||
|
url: uri('scpexe://xbib.org/repository'),
|
||||||
|
authentication: [userName: xbibUsername, privateKey: xbibPrivateKey]
|
||||||
|
)
|
||||||
|
pom.project {
|
||||||
|
inceptionYear '2016'
|
||||||
|
licenses {
|
||||||
|
license {
|
||||||
|
name 'The Apache Software License, Version 2.0'
|
||||||
|
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
|
||||||
|
distribution 'repo'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
group = org.xbib
|
|
||||||
name = guice
|
|
||||||
version = 5.0.1.0
|
|
||||||
|
|
||||||
org.gradle.warning.mode = ALL
|
|
|
@ -1,33 +0,0 @@
|
||||||
|
|
||||||
apply plugin: 'java-library'
|
|
||||||
|
|
||||||
java {
|
|
||||||
modularity.inferModulePath.set(true)
|
|
||||||
withSourcesJar()
|
|
||||||
withJavadocJar()
|
|
||||||
}
|
|
||||||
|
|
||||||
compileJava {
|
|
||||||
sourceCompatibility = JavaVersion.VERSION_17
|
|
||||||
targetCompatibility = JavaVersion.VERSION_17
|
|
||||||
}
|
|
||||||
|
|
||||||
compileTestJava {
|
|
||||||
sourceCompatibility = JavaVersion.VERSION_17
|
|
||||||
targetCompatibility = JavaVersion.VERSION_17
|
|
||||||
}
|
|
||||||
|
|
||||||
jar {
|
|
||||||
manifest {
|
|
||||||
attributes('Implementation-Version': project.version)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.withType(JavaCompile) {
|
|
||||||
options.encoding('UTF-8')
|
|
||||||
options.compilerArgs << '-Xlint:all'
|
|
||||||
}
|
|
||||||
|
|
||||||
javadoc {
|
|
||||||
options.addStringOption('Xdoclint:none', '-quiet')
|
|
||||||
}
|
|
|
@ -1,55 +0,0 @@
|
||||||
apply plugin: 'org.xbib.gradle.plugin.asciidoctor'
|
|
||||||
|
|
||||||
configurations {
|
|
||||||
asciidoclet
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
asciidoclet "org.asciidoctor:asciidoclet:${project.property('asciidoclet.version')}"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
asciidoctor {
|
|
||||||
backends 'html5'
|
|
||||||
outputDir = file("${rootProject.projectDir}/docs")
|
|
||||||
separateOutputDirs = false
|
|
||||||
attributes 'source-highlighter': 'coderay',
|
|
||||||
idprefix: '',
|
|
||||||
idseparator: '-',
|
|
||||||
toc: 'left',
|
|
||||||
doctype: 'book',
|
|
||||||
icons: 'font',
|
|
||||||
encoding: 'utf-8',
|
|
||||||
sectlink: true,
|
|
||||||
sectanchors: true,
|
|
||||||
linkattrs: true,
|
|
||||||
imagesdir: 'img',
|
|
||||||
stylesheet: "${projectDir}/src/docs/asciidoc/css/foundation.css"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*javadoc {
|
|
||||||
options.docletpath = configurations.asciidoclet.files.asType(List)
|
|
||||||
options.doclet = 'org.asciidoctor.Asciidoclet'
|
|
||||||
//options.overview = "src/docs/asciidoclet/overview.adoc"
|
|
||||||
options.addStringOption "-base-dir", "${projectDir}"
|
|
||||||
options.addStringOption "-attribute",
|
|
||||||
"name=${project.name},version=${project.version},title-link=https://github.com/xbib/${project.name}"
|
|
||||||
configure(options) {
|
|
||||||
noTimestamp = true
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
|
|
||||||
/*javadoc {
|
|
||||||
options.docletpath = configurations.asciidoclet.files.asType(List)
|
|
||||||
options.doclet = 'org.asciidoctor.Asciidoclet'
|
|
||||||
options.overview = "${rootProject.projectDir}/src/docs/asciidoclet/overview.adoc"
|
|
||||||
options.addStringOption "-base-dir", "${projectDir}"
|
|
||||||
options.addStringOption "-attribute",
|
|
||||||
"name=${project.name},version=${project.version},title-link=https://github.com/xbib/${project.name}"
|
|
||||||
options.destinationDirectory(file("${projectDir}/docs/javadoc"))
|
|
||||||
configure(options) {
|
|
||||||
noTimestamp = true
|
|
||||||
}
|
|
||||||
}*/
|
|
|
@ -1,12 +0,0 @@
|
||||||
apply plugin: 'idea'
|
|
||||||
|
|
||||||
idea {
|
|
||||||
module {
|
|
||||||
outputDir file('build/classes/java/main')
|
|
||||||
testOutputDir file('build/classes/java/test')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
clean {
|
|
||||||
delete 'out'
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
gradle.projectsLoaded { g ->
|
|
||||||
printf "Host: %s\nOS: %s %s %s\nJVM: %s %s %s %s\nGradle: %s Groovy: %s Java: %s\n" +
|
|
||||||
"Build: group: %s name: %s version: %s\n",
|
|
||||||
InetAddress.getLocalHost(),
|
|
||||||
System.getProperty("os.name"),
|
|
||||||
System.getProperty("os.arch"),
|
|
||||||
System.getProperty("os.version"),
|
|
||||||
System.getProperty("java.version"),
|
|
||||||
System.getProperty("java.vm.version"),
|
|
||||||
System.getProperty("java.vm.vendor"),
|
|
||||||
System.getProperty("java.vm.name"),
|
|
||||||
gradle.gradleVersion, GroovySystem.getVersion(), JavaVersion.current(),
|
|
||||||
gradle.rootProject.group, gradle.rootProject.name, gradle.rootProject.version
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
if (project.hasProperty('forgeJoToken')) {
|
|
||||||
publishing {
|
|
||||||
repositories {
|
|
||||||
maven {
|
|
||||||
url 'https://xbib.org/api/packages/joerg/maven'
|
|
||||||
credentials(HttpHeaderCredentials) {
|
|
||||||
name = "Authorization"
|
|
||||||
value = "token ${project.property('forgeJoToken')}"
|
|
||||||
}
|
|
||||||
authentication {
|
|
||||||
header(HttpHeaderAuthentication)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
apply plugin: 'ivy-publish'
|
|
||||||
|
|
||||||
publishing {
|
|
||||||
repositories {
|
|
||||||
ivy {
|
|
||||||
url = "https://xbib.org/repo"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
publications {
|
|
||||||
ivy(IvyPublication) {
|
|
||||||
from components.java
|
|
||||||
descriptor {
|
|
||||||
license {
|
|
||||||
name = 'The Apache License, Version 2.0'
|
|
||||||
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
|
|
||||||
}
|
|
||||||
author {
|
|
||||||
name = 'Jörg Prante'
|
|
||||||
url = 'https://xbib.org/joerg'
|
|
||||||
}
|
|
||||||
descriptor.description {
|
|
||||||
text = rootProject.ext.description
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,51 +0,0 @@
|
||||||
|
|
||||||
publishing {
|
|
||||||
publications {
|
|
||||||
"${project.name}"(MavenPublication) {
|
|
||||||
from components.java
|
|
||||||
pom {
|
|
||||||
artifactId = project.name
|
|
||||||
name = project.name
|
|
||||||
description = rootProject.ext.description
|
|
||||||
url = rootProject.ext.url
|
|
||||||
inceptionYear = rootProject.ext.inceptionYear
|
|
||||||
packaging = 'jar'
|
|
||||||
organization {
|
|
||||||
name = 'xbib'
|
|
||||||
url = 'https://xbib.org'
|
|
||||||
}
|
|
||||||
developers {
|
|
||||||
developer {
|
|
||||||
id = 'joerg'
|
|
||||||
name = 'Jörg Prante'
|
|
||||||
email = 'joergprante@gmail.com'
|
|
||||||
url = 'https://xbib.org/joerg'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
scm {
|
|
||||||
url = rootProject.ext.scmUrl
|
|
||||||
connection = rootProject.ext.scmConnection
|
|
||||||
developerConnection = rootProject.ext.scmDeveloperConnection
|
|
||||||
}
|
|
||||||
issueManagement {
|
|
||||||
system = rootProject.ext.issueManagementSystem
|
|
||||||
url = rootProject.ext.issueManagementUrl
|
|
||||||
}
|
|
||||||
licenses {
|
|
||||||
license {
|
|
||||||
name = rootProject.ext.licenseName
|
|
||||||
url = rootProject.ext.licenseUrl
|
|
||||||
distribution = 'repo'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (project.hasProperty("signing.keyId")) {
|
|
||||||
apply plugin: 'signing'
|
|
||||||
signing {
|
|
||||||
sign publishing.publications."${project.name}"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,11 +0,0 @@
|
||||||
if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')) {
|
|
||||||
nexusPublishing {
|
|
||||||
repositories {
|
|
||||||
sonatype {
|
|
||||||
username = project.property('ossrhUsername')
|
|
||||||
password = project.property('ossrhPassword')
|
|
||||||
packageGroup = "org.xbib"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
|
|
||||||
apply plugin: 'checkstyle'
|
|
||||||
|
|
||||||
tasks.withType(Checkstyle) {
|
|
||||||
ignoreFailures = true
|
|
||||||
reports {
|
|
||||||
xml.getRequired().set(true)
|
|
||||||
html.getRequired().set(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
checkstyle {
|
|
||||||
configFile = rootProject.file('gradle/quality/checkstyle.xml')
|
|
||||||
ignoreFailures = true
|
|
||||||
showViolations = true
|
|
||||||
checkstyleMain {
|
|
||||||
source = sourceSets.main.allSource
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,333 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!DOCTYPE module PUBLIC
|
|
||||||
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
|
|
||||||
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
|
|
||||||
|
|
||||||
<!-- This is a checkstyle configuration file. For descriptions of
|
|
||||||
what the following rules do, please see the checkstyle configuration
|
|
||||||
page at http://checkstyle.sourceforge.net/config.html -->
|
|
||||||
|
|
||||||
<module name="Checker">
|
|
||||||
|
|
||||||
<module name="BeforeExecutionExclusionFileFilter">
|
|
||||||
<property name="fileNamePattern" value=".*(Example|Test|module-info)(\$.*)?"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="FileTabCharacter">
|
|
||||||
<!-- Checks that there are no tab characters in the file.
|
|
||||||
-->
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="NewlineAtEndOfFile">
|
|
||||||
<property name="lineSeparator" value="lf"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="RegexpSingleline">
|
|
||||||
<!-- Checks that FIXME is not used in comments. TODO is preferred.
|
|
||||||
-->
|
|
||||||
<property name="format" value="((//.*)|(\*.*))FIXME" />
|
|
||||||
<property name="message" value='TODO is preferred to FIXME. e.g. "TODO(johndoe): Refactor when v2 is released."' />
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="RegexpSingleline">
|
|
||||||
<!-- Checks that TODOs are named. (Actually, just that they are followed
|
|
||||||
by an open paren.)
|
|
||||||
-->
|
|
||||||
<property name="format" value="((//.*)|(\*.*))TODO[^(]" />
|
|
||||||
<property name="message" value='All TODOs should be named. e.g. "TODO(johndoe): Refactor when v2 is released."' />
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="JavadocPackage">
|
|
||||||
<!-- Checks that each Java package has a Javadoc file used for commenting.
|
|
||||||
Only allows a package-info.java, not package.html. -->
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<!-- All Java AST specific tests live under TreeWalker module. -->
|
|
||||||
<module name="TreeWalker">
|
|
||||||
|
|
||||||
<!--
|
|
||||||
|
|
||||||
IMPORT CHECKS
|
|
||||||
|
|
||||||
-->
|
|
||||||
|
|
||||||
<module name="RedundantImport">
|
|
||||||
<!-- Checks for redundant import statements. -->
|
|
||||||
<property name="severity" value="error"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="ImportOrder">
|
|
||||||
<!-- Checks for out of order import statements. -->
|
|
||||||
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
<!-- <property name="tokens" value="IMPORT, STATIC_IMPORT"/> -->
|
|
||||||
<property name="separated" value="false"/>
|
|
||||||
<property name="groups" value="*"/>
|
|
||||||
<!-- <property name="option" value="above"/> -->
|
|
||||||
<property name="sortStaticImportsAlphabetically" value="true"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="CustomImportOrder">
|
|
||||||
<!-- <property name="customImportOrderRules" value="THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/> -->
|
|
||||||
<!-- <property name="specialImportsRegExp" value="^javax\."/> -->
|
|
||||||
<!-- <property name="standardPackageRegExp" value="^java\."/> -->
|
|
||||||
<property name="sortImportsInGroupAlphabetically" value="true"/>
|
|
||||||
<property name="separateLineBetweenGroups" value="false"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<!--
|
|
||||||
|
|
||||||
JAVADOC CHECKS
|
|
||||||
|
|
||||||
-->
|
|
||||||
|
|
||||||
<!-- Checks for Javadoc comments. -->
|
|
||||||
<!-- See http://checkstyle.sf.net/config_javadoc.html -->
|
|
||||||
<module name="JavadocMethod">
|
|
||||||
<property name="accessModifiers" value="protected"/>
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
<property name="allowMissingParamTags" value="true"/>
|
|
||||||
<property name="allowMissingReturnTag" value="true"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="JavadocType">
|
|
||||||
<property name="scope" value="protected"/>
|
|
||||||
<property name="severity" value="error"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="JavadocStyle">
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<!--
|
|
||||||
|
|
||||||
NAMING CHECKS
|
|
||||||
|
|
||||||
-->
|
|
||||||
|
|
||||||
<!-- Item 38 - Adhere to generally accepted naming conventions -->
|
|
||||||
|
|
||||||
<module name="PackageName">
|
|
||||||
<!-- Validates identifiers for package names against the
|
|
||||||
supplied expression. -->
|
|
||||||
<!-- Here the default checkstyle rule restricts package name parts to
|
|
||||||
seven characters, this is not in line with common practice at Google.
|
|
||||||
-->
|
|
||||||
<property name="format" value="^[a-z]+(\.[a-z][a-z0-9]{1,})*$"/>
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="TypeNameCheck">
|
|
||||||
<!-- Validates static, final fields against the
|
|
||||||
expression "^[A-Z][a-zA-Z0-9]*$". -->
|
|
||||||
<metadata name="altname" value="TypeName"/>
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="ConstantNameCheck">
|
|
||||||
<!-- Validates non-private, static, final fields against the supplied
|
|
||||||
public/package final fields "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$". -->
|
|
||||||
<metadata name="altname" value="ConstantName"/>
|
|
||||||
<property name="applyToPublic" value="true"/>
|
|
||||||
<property name="applyToProtected" value="true"/>
|
|
||||||
<property name="applyToPackage" value="true"/>
|
|
||||||
<property name="applyToPrivate" value="false"/>
|
|
||||||
<property name="format" value="^([A-Z][A-Z0-9]*(_[A-Z0-9]+)*|FLAG_.*)$"/>
|
|
||||||
<message key="name.invalidPattern"
|
|
||||||
value="Variable ''{0}'' should be in ALL_CAPS (if it is a constant) or be private (otherwise)."/>
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="StaticVariableNameCheck">
|
|
||||||
<!-- Validates static, non-final fields against the supplied
|
|
||||||
expression "^[a-z][a-zA-Z0-9]*_?$". -->
|
|
||||||
<metadata name="altname" value="StaticVariableName"/>
|
|
||||||
<property name="applyToPublic" value="true"/>
|
|
||||||
<property name="applyToProtected" value="true"/>
|
|
||||||
<property name="applyToPackage" value="true"/>
|
|
||||||
<property name="applyToPrivate" value="true"/>
|
|
||||||
<property name="format" value="^[a-z][a-zA-Z0-9]*_?$"/>
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="MemberNameCheck">
|
|
||||||
<!-- Validates non-static members against the supplied expression. -->
|
|
||||||
<metadata name="altname" value="MemberName"/>
|
|
||||||
<property name="applyToPublic" value="true"/>
|
|
||||||
<property name="applyToProtected" value="true"/>
|
|
||||||
<property name="applyToPackage" value="true"/>
|
|
||||||
<property name="applyToPrivate" value="true"/>
|
|
||||||
<property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="MethodNameCheck">
|
|
||||||
<!-- Validates identifiers for method names. -->
|
|
||||||
<metadata name="altname" value="MethodName"/>
|
|
||||||
<property name="format" value="^[a-z][a-zA-Z0-9]*(_[a-zA-Z0-9]+)*$"/>
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="ParameterName">
|
|
||||||
<!-- Validates identifiers for method parameters against the
|
|
||||||
expression "^[a-z][a-zA-Z0-9]*$". -->
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="LocalFinalVariableName">
|
|
||||||
<!-- Validates identifiers for local final variables against the
|
|
||||||
expression "^[a-z][a-zA-Z0-9]*$". -->
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="LocalVariableName">
|
|
||||||
<!-- Validates identifiers for local variables against the
|
|
||||||
expression "^[a-z][a-zA-Z0-9]*$". -->
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
|
|
||||||
<!--
|
|
||||||
|
|
||||||
LENGTH and CODING CHECKS
|
|
||||||
|
|
||||||
-->
|
|
||||||
|
|
||||||
|
|
||||||
<module name="LeftCurly">
|
|
||||||
<!-- Checks for placement of the left curly brace ('{'). -->
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="RightCurly">
|
|
||||||
<!-- Checks right curlies on CATCH, ELSE, and TRY blocks are on
|
|
||||||
the same line. e.g., the following example is fine:
|
|
||||||
<pre>
|
|
||||||
if {
|
|
||||||
...
|
|
||||||
} else
|
|
||||||
</pre>
|
|
||||||
-->
|
|
||||||
<!-- This next example is not fine:
|
|
||||||
<pre>
|
|
||||||
if {
|
|
||||||
...
|
|
||||||
}
|
|
||||||
else
|
|
||||||
</pre>
|
|
||||||
-->
|
|
||||||
<property name="option" value="same"/>
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<!-- Checks for braces around if and else blocks -->
|
|
||||||
<module name="NeedBraces">
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
<property name="tokens" value="LITERAL_IF, LITERAL_ELSE, LITERAL_FOR, LITERAL_WHILE, LITERAL_DO"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="UpperEll">
|
|
||||||
<!-- Checks that long constants are defined with an upper ell.-->
|
|
||||||
<property name="severity" value="error"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="FallThrough">
|
|
||||||
<!-- Warn about falling through to the next case statement. Similar to
|
|
||||||
javac -Xlint:fallthrough, but the check is suppressed if a single-line comment
|
|
||||||
on the last non-blank line preceding the fallen-into case contains 'fall through' (or
|
|
||||||
some other variants which we don't publicized to promote consistency).
|
|
||||||
-->
|
|
||||||
<property name="reliefPattern"
|
|
||||||
value="fall through|Fall through|fallthru|Fallthru|falls through|Falls through|fallthrough|Fallthrough|No break|NO break|no break|continue on"/>
|
|
||||||
<property name="severity" value="error"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
|
|
||||||
<!--
|
|
||||||
|
|
||||||
MODIFIERS CHECKS
|
|
||||||
|
|
||||||
-->
|
|
||||||
|
|
||||||
<module name="ModifierOrder">
|
|
||||||
<!-- Warn if modifier order is inconsistent with JLS3 8.1.1, 8.3.1, and
|
|
||||||
8.4.3. The prescribed order is:
|
|
||||||
public, protected, private, abstract, static, final, transient, volatile,
|
|
||||||
synchronized, native, strictfp
|
|
||||||
-->
|
|
||||||
</module>
|
|
||||||
|
|
||||||
|
|
||||||
<!--
|
|
||||||
|
|
||||||
WHITESPACE CHECKS
|
|
||||||
|
|
||||||
-->
|
|
||||||
|
|
||||||
<module name="WhitespaceAround">
|
|
||||||
<!-- Checks that various tokens are surrounded by whitespace.
|
|
||||||
This includes most binary operators and keywords followed
|
|
||||||
by regular or curly braces.
|
|
||||||
-->
|
|
||||||
<property name="tokens" value="ASSIGN, BAND, BAND_ASSIGN, BOR,
|
|
||||||
BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN,
|
|
||||||
EQUAL, GE, GT, LAND, LE, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE,
|
|
||||||
LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_RETURN,
|
|
||||||
LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS,
|
|
||||||
MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION,
|
|
||||||
SL, SL_ASSIGN, SR_ASSIGN, STAR, STAR_ASSIGN"/>
|
|
||||||
<property name="severity" value="error"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="WhitespaceAfter">
|
|
||||||
<!-- Checks that commas, semicolons and typecasts are followed by
|
|
||||||
whitespace.
|
|
||||||
-->
|
|
||||||
<property name="tokens" value="COMMA, SEMI, TYPECAST"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="NoWhitespaceAfter">
|
|
||||||
<!-- Checks that there is no whitespace after various unary operators.
|
|
||||||
Linebreaks are allowed.
|
|
||||||
-->
|
|
||||||
<property name="tokens" value="BNOT, DEC, DOT, INC, LNOT, UNARY_MINUS,
|
|
||||||
UNARY_PLUS"/>
|
|
||||||
<property name="allowLineBreaks" value="true"/>
|
|
||||||
<property name="severity" value="error"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="NoWhitespaceBefore">
|
|
||||||
<!-- Checks that there is no whitespace before various unary operators.
|
|
||||||
Linebreaks are allowed.
|
|
||||||
-->
|
|
||||||
<property name="tokens" value="SEMI, DOT, POST_DEC, POST_INC"/>
|
|
||||||
<property name="allowLineBreaks" value="true"/>
|
|
||||||
<property name="severity" value="error"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="ParenPad">
|
|
||||||
<!-- Checks that there is no whitespace before close parens or after
|
|
||||||
open parens.
|
|
||||||
-->
|
|
||||||
<property name="severity" value="warning"/>
|
|
||||||
</module>
|
|
||||||
|
|
||||||
</module>
|
|
||||||
|
|
||||||
<module name="LineLength">
|
|
||||||
<!-- Checks if a line is too long. -->
|
|
||||||
<property name="max" value="${com.puppycrawl.tools.checkstyle.checks.sizes.LineLength.max}" default="128"/>
|
|
||||||
<property name="severity" value="error"/>
|
|
||||||
|
|
||||||
<!--
|
|
||||||
The default ignore pattern exempts the following elements:
|
|
||||||
- import statements
|
|
||||||
- long URLs inside comments
|
|
||||||
-->
|
|
||||||
|
|
||||||
<property name="ignorePattern"
|
|
||||||
value="${com.puppycrawl.tools.checkstyle.checks.sizes.LineLength.ignorePattern}"
|
|
||||||
default="^(package .*;\s*)|(import .*;\s*)|( *(\*|//).*https?://.*)$"/>
|
|
||||||
</module>
|
|
||||||
</module>
|
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
cyclonedxBom {
|
|
||||||
includeConfigs = [ 'runtimeClasspath' ]
|
|
||||||
skipConfigs = [ 'compileClasspath', 'testCompileClasspath' ]
|
|
||||||
projectType = "library"
|
|
||||||
schemaVersion = "1.4"
|
|
||||||
destination = file("build/reports")
|
|
||||||
outputName = "bom"
|
|
||||||
outputFormat = "json"
|
|
||||||
includeBomSerialNumber = true
|
|
||||||
componentVersion = "2.0.0"
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
|
|
||||||
apply plugin: 'pmd'
|
|
||||||
|
|
||||||
tasks.withType(Pmd) {
|
|
||||||
ignoreFailures = true
|
|
||||||
reports {
|
|
||||||
xml.getRequired().set(true)
|
|
||||||
html.getRequired().set(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pmd {
|
|
||||||
ignoreFailures = true
|
|
||||||
consoleOutput = false
|
|
||||||
toolVersion = "6.51.0"
|
|
||||||
ruleSetFiles = rootProject.files('gradle/quality/pmd/category/java/bestpractices.xml')
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,10 +0,0 @@
|
||||||
|
|
||||||
rulesets.filenames=\
|
|
||||||
category/java/bestpractices.xml,\
|
|
||||||
category/java/codestyle.xml,\
|
|
||||||
category/java/design.xml,\
|
|
||||||
category/java/documentation.xml,\
|
|
||||||
category/java/errorprone.xml,\
|
|
||||||
category/java/multithreading.xml,\
|
|
||||||
category/java/performance.xml,\
|
|
||||||
category/java/security.xml
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -1,144 +0,0 @@
|
||||||
<?xml version="1.0"?>
|
|
||||||
|
|
||||||
<ruleset name="Documentation"
|
|
||||||
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
|
|
||||||
|
|
||||||
<description>
|
|
||||||
Rules that are related to code documentation.
|
|
||||||
</description>
|
|
||||||
|
|
||||||
<rule name="CommentContent"
|
|
||||||
since="5.0"
|
|
||||||
message="Invalid words or phrases found"
|
|
||||||
class="net.sourceforge.pmd.lang.java.rule.documentation.CommentContentRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_documentation.html#commentcontent">
|
|
||||||
<description>
|
|
||||||
A rule for the politically correct... we don't want to offend anyone.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
//OMG, this is horrible, Bob is an idiot !!!
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="CommentRequired"
|
|
||||||
since="5.1"
|
|
||||||
message="Comment is required"
|
|
||||||
class="net.sourceforge.pmd.lang.java.rule.documentation.CommentRequiredRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_documentation.html#commentrequired">
|
|
||||||
<description>
|
|
||||||
Denotes whether comments are required (or unwanted) for specific language elements.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @author Jon Doe
|
|
||||||
*/
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="CommentSize"
|
|
||||||
since="5.0"
|
|
||||||
message="Comment is too large"
|
|
||||||
class="net.sourceforge.pmd.lang.java.rule.documentation.CommentSizeRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_documentation.html#commentsize">
|
|
||||||
<description>
|
|
||||||
Determines whether the dimensions of non-header comments found are within the specified limits.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* too many lines!
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="UncommentedEmptyConstructor"
|
|
||||||
language="java"
|
|
||||||
since="3.4"
|
|
||||||
message="Document empty constructor"
|
|
||||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
|
||||||
typeResolution="true"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_documentation.html#uncommentedemptyconstructor">
|
|
||||||
<description>
|
|
||||||
Uncommented Empty Constructor finds instances where a constructor does not
|
|
||||||
contain statements, but there is no comment. By explicitly commenting empty
|
|
||||||
constructors it is easier to distinguish between intentional (commented)
|
|
||||||
and unintentional empty constructors.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<properties>
|
|
||||||
<property name="xpath">
|
|
||||||
<value>
|
|
||||||
<![CDATA[
|
|
||||||
//ConstructorDeclaration[@Private='false']
|
|
||||||
[count(BlockStatement) = 0 and ($ignoreExplicitConstructorInvocation = 'true' or not(ExplicitConstructorInvocation)) and @containsComment = 'false']
|
|
||||||
[not(../Annotation/MarkerAnnotation/Name[pmd-java:typeIs('javax.inject.Inject')])]
|
|
||||||
]]>
|
|
||||||
</value>
|
|
||||||
</property>
|
|
||||||
<property name="ignoreExplicitConstructorInvocation" type="Boolean" description="Ignore explicit constructor invocation when deciding whether constructor is empty or not" value="false"/>
|
|
||||||
</properties>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
public Foo() {
|
|
||||||
// This constructor is intentionally empty. Nothing special is needed here.
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="UncommentedEmptyMethodBody"
|
|
||||||
language="java"
|
|
||||||
since="3.4"
|
|
||||||
message="Document empty method body"
|
|
||||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_documentation.html#uncommentedemptymethodbody">
|
|
||||||
<description>
|
|
||||||
Uncommented Empty Method Body finds instances where a method body does not contain
|
|
||||||
statements, but there is no comment. By explicitly commenting empty method bodies
|
|
||||||
it is easier to distinguish between intentional (commented) and unintentional
|
|
||||||
empty methods.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<properties>
|
|
||||||
<property name="xpath">
|
|
||||||
<value>
|
|
||||||
<![CDATA[
|
|
||||||
//MethodDeclaration/Block[count(BlockStatement) = 0 and @containsComment = 'false']
|
|
||||||
]]>
|
|
||||||
</value>
|
|
||||||
</property>
|
|
||||||
</properties>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
public void doSomething() {
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
</ruleset>
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,393 +0,0 @@
|
||||||
<?xml version="1.0"?>
|
|
||||||
|
|
||||||
<ruleset name="Multithreading"
|
|
||||||
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
|
|
||||||
|
|
||||||
<description>
|
|
||||||
Rules that flag issues when dealing with multiple threads of execution.
|
|
||||||
</description>
|
|
||||||
|
|
||||||
<rule name="AvoidSynchronizedAtMethodLevel"
|
|
||||||
language="java"
|
|
||||||
since="3.0"
|
|
||||||
message="Use block level rather than method level synchronization"
|
|
||||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_multithreading.html#avoidsynchronizedatmethodlevel">
|
|
||||||
<description>
|
|
||||||
Method-level synchronization can cause problems when new code is added to the method.
|
|
||||||
Block-level synchronization helps to ensure that only the code that needs synchronization
|
|
||||||
gets it.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<properties>
|
|
||||||
<property name="xpath">
|
|
||||||
<value>//MethodDeclaration[@Synchronized='true']</value>
|
|
||||||
</property>
|
|
||||||
</properties>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
public class Foo {
|
|
||||||
// Try to avoid this:
|
|
||||||
synchronized void foo() {
|
|
||||||
}
|
|
||||||
// Prefer this:
|
|
||||||
void bar() {
|
|
||||||
synchronized(this) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to avoid this for static methods:
|
|
||||||
static synchronized void fooStatic() {
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prefer this:
|
|
||||||
static void barStatic() {
|
|
||||||
synchronized(Foo.class) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="AvoidThreadGroup"
|
|
||||||
language="java"
|
|
||||||
since="3.6"
|
|
||||||
message="Avoid using java.lang.ThreadGroup; it is not thread safe"
|
|
||||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
|
||||||
typeResolution="true"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_multithreading.html#avoidthreadgroup">
|
|
||||||
<description>
|
|
||||||
Avoid using java.lang.ThreadGroup; although it is intended to be used in a threaded environment
|
|
||||||
it contains methods that are not thread-safe.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<properties>
|
|
||||||
<property name="xpath">
|
|
||||||
<value>
|
|
||||||
<![CDATA[
|
|
||||||
//AllocationExpression/ClassOrInterfaceType[pmd-java:typeIs('java.lang.ThreadGroup')]|
|
|
||||||
//PrimarySuffix[contains(@Image, 'getThreadGroup')]
|
|
||||||
]]>
|
|
||||||
</value>
|
|
||||||
</property>
|
|
||||||
</properties>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
public class Bar {
|
|
||||||
void buz() {
|
|
||||||
ThreadGroup tg = new ThreadGroup("My threadgroup");
|
|
||||||
tg = new ThreadGroup(tg, "my thread group");
|
|
||||||
tg = Thread.currentThread().getThreadGroup();
|
|
||||||
tg = System.getSecurityManager().getThreadGroup();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="AvoidUsingVolatile"
|
|
||||||
language="java"
|
|
||||||
since="4.1"
|
|
||||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
|
||||||
message="Use of modifier volatile is not recommended."
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_multithreading.html#avoidusingvolatile">
|
|
||||||
<description>
|
|
||||||
Use of the keyword 'volatile' is generally used to fine tune a Java application, and therefore, requires
|
|
||||||
a good expertise of the Java Memory Model. Moreover, its range of action is somewhat misknown. Therefore,
|
|
||||||
the volatile keyword should not be used for maintenance purpose and portability.
|
|
||||||
</description>
|
|
||||||
<priority>2</priority>
|
|
||||||
<properties>
|
|
||||||
<property name="xpath">
|
|
||||||
<value>//FieldDeclaration[contains(@Volatile,'true')]</value>
|
|
||||||
</property>
|
|
||||||
</properties>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
public class ThrDeux {
|
|
||||||
private volatile String var1; // not suggested
|
|
||||||
private String var2; // preferred
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="DoNotUseThreads"
|
|
||||||
language="java"
|
|
||||||
since="4.1"
|
|
||||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
|
||||||
message="To be compliant to J2EE, a webapp should not use any thread."
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_multithreading.html#donotusethreads">
|
|
||||||
<description>
|
|
||||||
The J2EE specification explicitly forbids the use of threads.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<properties>
|
|
||||||
<property name="xpath">
|
|
||||||
<value>//ClassOrInterfaceType[@Image = 'Thread' or @Image = 'Runnable']</value>
|
|
||||||
</property>
|
|
||||||
</properties>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
// This is not allowed
|
|
||||||
public class UsingThread extends Thread {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Neither this,
|
|
||||||
public class OtherThread implements Runnable {
|
|
||||||
// Nor this ...
|
|
||||||
public void methode() {
|
|
||||||
Runnable thread = new Thread(); thread.run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="DontCallThreadRun"
|
|
||||||
language="java"
|
|
||||||
since="4.3"
|
|
||||||
message="Don't call Thread.run() explicitly, use Thread.start()"
|
|
||||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
|
||||||
typeResolution="true"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_multithreading.html#dontcallthreadrun">
|
|
||||||
<description>
|
|
||||||
Explicitly calling Thread.run() method will execute in the caller's thread of control. Instead, call Thread.start() for the intended behavior.
|
|
||||||
</description>
|
|
||||||
<priority>4</priority>
|
|
||||||
<properties>
|
|
||||||
<property name="xpath">
|
|
||||||
<value>
|
|
||||||
<![CDATA[
|
|
||||||
//StatementExpression/PrimaryExpression
|
|
||||||
[
|
|
||||||
PrimaryPrefix
|
|
||||||
[
|
|
||||||
./Name[ends-with(@Image, '.run') or @Image = 'run']
|
|
||||||
and substring-before(Name/@Image, '.') =//VariableDeclarator/VariableDeclaratorId/@Image
|
|
||||||
[../../../Type/ReferenceType/ClassOrInterfaceType[pmd-java:typeIs('java.lang.Thread')]]
|
|
||||||
or (./AllocationExpression/ClassOrInterfaceType[pmd-java:typeIs('java.lang.Thread')]
|
|
||||||
and ../PrimarySuffix[@Image = 'run'])
|
|
||||||
]
|
|
||||||
]
|
|
||||||
]]>
|
|
||||||
</value>
|
|
||||||
</property>
|
|
||||||
</properties>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
Thread t = new Thread();
|
|
||||||
t.run(); // use t.start() instead
|
|
||||||
new Thread().run(); // same violation
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="DoubleCheckedLocking"
|
|
||||||
language="java"
|
|
||||||
since="1.04"
|
|
||||||
message="Double checked locking is not thread safe in Java."
|
|
||||||
class="net.sourceforge.pmd.lang.java.rule.multithreading.DoubleCheckedLockingRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_multithreading.html#doublecheckedlocking">
|
|
||||||
<description>
|
|
||||||
Partially created objects can be returned by the Double Checked Locking pattern when used in Java.
|
|
||||||
An optimizing JRE may assign a reference to the baz variable before it calls the constructor of the object the
|
|
||||||
reference points to.
|
|
||||||
|
|
||||||
Note: With Java 5, you can make Double checked locking work, if you declare the variable to be `volatile`.
|
|
||||||
|
|
||||||
For more details refer to: <http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-double.html>
|
|
||||||
or <http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html>
|
|
||||||
</description>
|
|
||||||
<priority>1</priority>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
public class Foo {
|
|
||||||
/*volatile */ Object baz = null; // fix for Java5 and later: volatile
|
|
||||||
Object bar() {
|
|
||||||
if (baz == null) { // baz may be non-null yet not fully created
|
|
||||||
synchronized(this) {
|
|
||||||
if (baz == null) {
|
|
||||||
baz = new Object();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return baz;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="NonThreadSafeSingleton"
|
|
||||||
since="3.4"
|
|
||||||
message="Singleton is not thread safe"
|
|
||||||
class="net.sourceforge.pmd.lang.java.rule.multithreading.NonThreadSafeSingletonRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_multithreading.html#nonthreadsafesingleton">
|
|
||||||
<description>
|
|
||||||
Non-thread safe singletons can result in bad state changes. Eliminate
|
|
||||||
static singletons if possible by instantiating the object directly. Static
|
|
||||||
singletons are usually not needed as only a single instance exists anyway.
|
|
||||||
Other possible fixes are to synchronize the entire method or to use an
|
|
||||||
[initialize-on-demand holder class](https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom).
|
|
||||||
|
|
||||||
Refrain from using the double-checked locking pattern. The Java Memory Model doesn't
|
|
||||||
guarantee it to work unless the variable is declared as `volatile`, adding an uneeded
|
|
||||||
performance penalty. [Reference](http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html)
|
|
||||||
|
|
||||||
See Effective Java, item 48.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
private static Foo foo = null;
|
|
||||||
|
|
||||||
//multiple simultaneous callers may see partially initialized objects
|
|
||||||
public static Foo getFoo() {
|
|
||||||
if (foo==null) {
|
|
||||||
foo = new Foo();
|
|
||||||
}
|
|
||||||
return foo;
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="UnsynchronizedStaticDateFormatter"
|
|
||||||
since="3.6"
|
|
||||||
deprecated="true"
|
|
||||||
message="Static DateFormatter objects should be accessed in a synchronized manner"
|
|
||||||
class="net.sourceforge.pmd.lang.java.rule.multithreading.UnsynchronizedStaticDateFormatterRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_multithreading.html#unsynchronizedstaticdateformatter">
|
|
||||||
<description>
|
|
||||||
SimpleDateFormat instances are not synchronized. Sun recommends using separate format instances
|
|
||||||
for each thread. If multiple threads must access a static formatter, the formatter must be
|
|
||||||
synchronized either on method or block level.
|
|
||||||
|
|
||||||
This rule has been deprecated in favor of the rule {% rule UnsynchronizedStaticFormatter %}.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
public class Foo {
|
|
||||||
private static final SimpleDateFormat sdf = new SimpleDateFormat();
|
|
||||||
void bar() {
|
|
||||||
sdf.format(); // poor, no thread-safety
|
|
||||||
}
|
|
||||||
synchronized void foo() {
|
|
||||||
sdf.format(); // preferred
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="UnsynchronizedStaticFormatter"
|
|
||||||
since="6.11.0"
|
|
||||||
message="Static Formatter objects should be accessed in a synchronized manner"
|
|
||||||
class="net.sourceforge.pmd.lang.java.rule.multithreading.UnsynchronizedStaticFormatterRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_multithreading.html#unsynchronizedstaticformatter">
|
|
||||||
<description>
|
|
||||||
Instances of `java.text.Format` are generally not synchronized.
|
|
||||||
Sun recommends using separate format instances for each thread.
|
|
||||||
If multiple threads must access a static formatter, the formatter must be
|
|
||||||
synchronized either on method or block level.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
public class Foo {
|
|
||||||
private static final SimpleDateFormat sdf = new SimpleDateFormat();
|
|
||||||
void bar() {
|
|
||||||
sdf.format(); // poor, no thread-safety
|
|
||||||
}
|
|
||||||
synchronized void foo() {
|
|
||||||
sdf.format(); // preferred
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="UseConcurrentHashMap"
|
|
||||||
language="java"
|
|
||||||
minimumLanguageVersion="1.5"
|
|
||||||
since="4.2.6"
|
|
||||||
message="If you run in Java5 or newer and have concurrent access, you should use the ConcurrentHashMap implementation"
|
|
||||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_multithreading.html#useconcurrenthashmap">
|
|
||||||
<description>
|
|
||||||
Since Java5 brought a new implementation of the Map designed for multi-threaded access, you can
|
|
||||||
perform efficient map reads without blocking other threads.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<properties>
|
|
||||||
<property name="xpath">
|
|
||||||
<value>
|
|
||||||
<![CDATA[
|
|
||||||
//Type[../VariableDeclarator/VariableInitializer//AllocationExpression/ClassOrInterfaceType[@Image != 'ConcurrentHashMap']]
|
|
||||||
/ReferenceType/ClassOrInterfaceType[@Image = 'Map']
|
|
||||||
]]>
|
|
||||||
</value>
|
|
||||||
</property>
|
|
||||||
</properties>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
public class ConcurrentApp {
|
|
||||||
public void getMyInstance() {
|
|
||||||
Map map1 = new HashMap(); // fine for single-threaded access
|
|
||||||
Map map2 = new ConcurrentHashMap(); // preferred for use with multiple threads
|
|
||||||
|
|
||||||
// the following case will be ignored by this rule
|
|
||||||
Map map3 = someModule.methodThatReturnMap(); // might be OK, if the returned map is already thread-safe
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="UseNotifyAllInsteadOfNotify"
|
|
||||||
language="java"
|
|
||||||
since="3.0"
|
|
||||||
message="Call Thread.notifyAll() rather than Thread.notify()"
|
|
||||||
class="net.sourceforge.pmd.lang.rule.XPathRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_multithreading.html#usenotifyallinsteadofnotify">
|
|
||||||
<description>
|
|
||||||
Thread.notify() awakens a thread monitoring the object. If more than one thread is monitoring, then only
|
|
||||||
one is chosen. The thread chosen is arbitrary; thus its usually safer to call notifyAll() instead.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<properties>
|
|
||||||
<property name="xpath">
|
|
||||||
<value>
|
|
||||||
<![CDATA[
|
|
||||||
//StatementExpression/PrimaryExpression
|
|
||||||
[PrimarySuffix/Arguments[@ArgumentCount = '0']]
|
|
||||||
[
|
|
||||||
PrimaryPrefix[
|
|
||||||
./Name[@Image='notify' or ends-with(@Image,'.notify')]
|
|
||||||
or ../PrimarySuffix/@Image='notify'
|
|
||||||
or (./AllocationExpression and ../PrimarySuffix[@Image='notify'])
|
|
||||||
]
|
|
||||||
]
|
|
||||||
]]>
|
|
||||||
</value>
|
|
||||||
</property>
|
|
||||||
</properties>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
void bar() {
|
|
||||||
x.notify();
|
|
||||||
// If many threads are monitoring x, only one (and you won't know which) will be notified.
|
|
||||||
// use instead:
|
|
||||||
x.notifyAll();
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
</ruleset>
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,65 +0,0 @@
|
||||||
<?xml version="1.0"?>
|
|
||||||
|
|
||||||
<ruleset name="Security" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
|
|
||||||
|
|
||||||
<description>
|
|
||||||
Rules that flag potential security flaws.
|
|
||||||
</description>
|
|
||||||
|
|
||||||
<rule name="HardCodedCryptoKey"
|
|
||||||
since="6.4.0"
|
|
||||||
message="Do not use hard coded encryption keys"
|
|
||||||
class="net.sourceforge.pmd.lang.java.rule.security.HardCodedCryptoKeyRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_security.html#hardcodedcryptokey">
|
|
||||||
<description>
|
|
||||||
Do not use hard coded values for cryptographic operations. Please store keys outside of source code.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
public class Foo {
|
|
||||||
void good() {
|
|
||||||
SecretKeySpec secretKeySpec = new SecretKeySpec(Properties.getKey(), "AES");
|
|
||||||
}
|
|
||||||
|
|
||||||
void bad() {
|
|
||||||
SecretKeySpec secretKeySpec = new SecretKeySpec("my secret here".getBytes(), "AES");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
<rule name="InsecureCryptoIv"
|
|
||||||
since="6.3.0"
|
|
||||||
message="Do not use hard coded initialization vector in crypto operations"
|
|
||||||
class="net.sourceforge.pmd.lang.java.rule.security.InsecureCryptoIvRule"
|
|
||||||
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_security.html#insecurecryptoiv">
|
|
||||||
<description>
|
|
||||||
Do not use hard coded initialization vector in cryptographic operations. Please use a randomly generated IV.
|
|
||||||
</description>
|
|
||||||
<priority>3</priority>
|
|
||||||
<example>
|
|
||||||
<![CDATA[
|
|
||||||
public class Foo {
|
|
||||||
void good() {
|
|
||||||
SecureRandom random = new SecureRandom();
|
|
||||||
byte iv[] = new byte[16];
|
|
||||||
random.nextBytes(bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
void bad() {
|
|
||||||
byte[] iv = new byte[] { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, };
|
|
||||||
}
|
|
||||||
|
|
||||||
void alsoBad() {
|
|
||||||
byte[] iv = "secret iv in here".getBytes();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]]>
|
|
||||||
</example>
|
|
||||||
</rule>
|
|
||||||
|
|
||||||
</ruleset>
|
|
|
@ -1,37 +0,0 @@
|
||||||
|
|
||||||
subprojects {
|
|
||||||
|
|
||||||
sonarqube {
|
|
||||||
properties {
|
|
||||||
property "sonar.projectName", "${project.group} ${project.name}"
|
|
||||||
property "sonar.sourceEncoding", "UTF-8"
|
|
||||||
property "sonar.tests", "src/test/java"
|
|
||||||
property "sonar.scm.provider", "git"
|
|
||||||
property "sonar.junit.reportsPath", "build/test-results/test/"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
tasks.withType(Pmd) {
|
|
||||||
ignoreFailures = true
|
|
||||||
reports {
|
|
||||||
xml.enabled = true
|
|
||||||
html.enabled = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
spotbugs {
|
|
||||||
effort = "max"
|
|
||||||
reportLevel = "low"
|
|
||||||
//includeFilter = file("findbugs-exclude.xml")
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.withType(com.github.spotbugs.SpotBugsTask) {
|
|
||||||
ignoreFailures = true
|
|
||||||
reports {
|
|
||||||
xml.enabled = false
|
|
||||||
html.enabled = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,15 +0,0 @@
|
||||||
|
|
||||||
apply plugin: 'com.github.spotbugs'
|
|
||||||
|
|
||||||
spotbugs {
|
|
||||||
effort = "max"
|
|
||||||
reportLevel = "low"
|
|
||||||
ignoreFailures = true
|
|
||||||
}
|
|
||||||
|
|
||||||
spotbugsMain {
|
|
||||||
reports {
|
|
||||||
xml.getRequired().set(false)
|
|
||||||
html.getRequired().set(true)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +0,0 @@
|
||||||
repositories {
|
|
||||||
mavenLocal()
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
|
@ -1,28 +0,0 @@
|
||||||
|
|
||||||
def junitVersion = project.hasProperty('junit.version')?project.property('junit.version'):'5.7.1'
|
|
||||||
def hamcrestVersion = project.hasProperty('hamcrest.version')?project.property('hamcrest.version'):'2.2'
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
|
|
||||||
testImplementation "org.junit.jupiter:junit-jupiter-params:${junitVersion}"
|
|
||||||
testImplementation "org.hamcrest:hamcrest-library:${hamcrestVersion}"
|
|
||||||
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
|
|
||||||
testRuntimeOnly "org.junit.vintage:junit-vintage-engine:${junitVersion}"
|
|
||||||
}
|
|
||||||
|
|
||||||
test {
|
|
||||||
useJUnitPlatform()
|
|
||||||
failFast = false
|
|
||||||
testLogging {
|
|
||||||
events 'STARTED', 'PASSED', 'FAILED', 'SKIPPED'
|
|
||||||
}
|
|
||||||
afterSuite { desc, result ->
|
|
||||||
if (!desc.parent) {
|
|
||||||
println "\nTest result: ${result.resultType}"
|
|
||||||
println "Test summary: ${result.testCount} tests, " +
|
|
||||||
"${result.successfulTestCount} succeeded, " +
|
|
||||||
"${result.failedTestCount} failed, " +
|
|
||||||
"${result.skippedTestCount} skipped"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
6
gradle/wrapper/gradle-wrapper.properties
vendored
6
gradle/wrapper/gradle-wrapper.properties
vendored
|
@ -1,6 +0,0 @@
|
||||||
distributionBase=GRADLE_USER_HOME
|
|
||||||
distributionPath=wrapper/dists
|
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-all.zip
|
|
||||||
networkTimeout=10000
|
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
|
||||||
zipStorePath=wrapper/dists
|
|
245
gradlew
vendored
245
gradlew
vendored
|
@ -1,245 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
#
|
|
||||||
# Copyright © 2015-2021 the original authors.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
# you may not use this file except in compliance with the License.
|
|
||||||
# You may obtain a copy of the License at
|
|
||||||
#
|
|
||||||
# https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
# See the License for the specific language governing permissions and
|
|
||||||
# limitations under the License.
|
|
||||||
#
|
|
||||||
|
|
||||||
##############################################################################
|
|
||||||
#
|
|
||||||
# Gradle start up script for POSIX generated by Gradle.
|
|
||||||
#
|
|
||||||
# Important for running:
|
|
||||||
#
|
|
||||||
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
|
||||||
# noncompliant, but you have some other compliant shell such as ksh or
|
|
||||||
# bash, then to run this script, type that shell name before the whole
|
|
||||||
# command line, like:
|
|
||||||
#
|
|
||||||
# ksh Gradle
|
|
||||||
#
|
|
||||||
# Busybox and similar reduced shells will NOT work, because this script
|
|
||||||
# requires all of these POSIX shell features:
|
|
||||||
# * functions;
|
|
||||||
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
|
||||||
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
|
||||||
# * compound commands having a testable exit status, especially «case»;
|
|
||||||
# * various built-in commands including «command», «set», and «ulimit».
|
|
||||||
#
|
|
||||||
# Important for patching:
|
|
||||||
#
|
|
||||||
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
|
||||||
# by Bash, Ksh, etc; in particular arrays are avoided.
|
|
||||||
#
|
|
||||||
# The "traditional" practice of packing multiple parameters into a
|
|
||||||
# space-separated string is a well documented source of bugs and security
|
|
||||||
# problems, so this is (mostly) avoided, by progressively accumulating
|
|
||||||
# options in "$@", and eventually passing that to Java.
|
|
||||||
#
|
|
||||||
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
|
||||||
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
|
||||||
# see the in-line comments for details.
|
|
||||||
#
|
|
||||||
# There are tweaks for specific operating systems such as AIX, CygWin,
|
|
||||||
# Darwin, MinGW, and NonStop.
|
|
||||||
#
|
|
||||||
# (3) This script is generated from the Groovy template
|
|
||||||
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
|
||||||
# within the Gradle project.
|
|
||||||
#
|
|
||||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
|
||||||
#
|
|
||||||
##############################################################################
|
|
||||||
|
|
||||||
# Attempt to set APP_HOME
|
|
||||||
|
|
||||||
# Resolve links: $0 may be a link
|
|
||||||
app_path=$0
|
|
||||||
|
|
||||||
# Need this for daisy-chained symlinks.
|
|
||||||
while
|
|
||||||
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
|
||||||
[ -h "$app_path" ]
|
|
||||||
do
|
|
||||||
ls=$( ls -ld "$app_path" )
|
|
||||||
link=${ls#*' -> '}
|
|
||||||
case $link in #(
|
|
||||||
/*) app_path=$link ;; #(
|
|
||||||
*) app_path=$APP_HOME$link ;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
# This is normally unused
|
|
||||||
# shellcheck disable=SC2034
|
|
||||||
APP_BASE_NAME=${0##*/}
|
|
||||||
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
|
|
||||||
|
|
||||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
|
||||||
MAX_FD=maximum
|
|
||||||
|
|
||||||
warn () {
|
|
||||||
echo "$*"
|
|
||||||
} >&2
|
|
||||||
|
|
||||||
die () {
|
|
||||||
echo
|
|
||||||
echo "$*"
|
|
||||||
echo
|
|
||||||
exit 1
|
|
||||||
} >&2
|
|
||||||
|
|
||||||
# OS specific support (must be 'true' or 'false').
|
|
||||||
cygwin=false
|
|
||||||
msys=false
|
|
||||||
darwin=false
|
|
||||||
nonstop=false
|
|
||||||
case "$( uname )" in #(
|
|
||||||
CYGWIN* ) cygwin=true ;; #(
|
|
||||||
Darwin* ) darwin=true ;; #(
|
|
||||||
MSYS* | MINGW* ) msys=true ;; #(
|
|
||||||
NONSTOP* ) nonstop=true ;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
|
||||||
|
|
||||||
|
|
||||||
# Determine the Java command to use to start the JVM.
|
|
||||||
if [ -n "$JAVA_HOME" ] ; then
|
|
||||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
|
||||||
# IBM's JDK on AIX uses strange locations for the executables
|
|
||||||
JAVACMD=$JAVA_HOME/jre/sh/java
|
|
||||||
else
|
|
||||||
JAVACMD=$JAVA_HOME/bin/java
|
|
||||||
fi
|
|
||||||
if [ ! -x "$JAVACMD" ] ; then
|
|
||||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
|
||||||
|
|
||||||
Please set the JAVA_HOME variable in your environment to match the
|
|
||||||
location of your Java installation."
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
JAVACMD=java
|
|
||||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
|
||||||
|
|
||||||
Please set the JAVA_HOME variable in your environment to match the
|
|
||||||
location of your Java installation."
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Increase the maximum file descriptors if we can.
|
|
||||||
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
|
||||||
case $MAX_FD in #(
|
|
||||||
max*)
|
|
||||||
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
|
||||||
# shellcheck disable=SC3045
|
|
||||||
MAX_FD=$( ulimit -H -n ) ||
|
|
||||||
warn "Could not query maximum file descriptor limit"
|
|
||||||
esac
|
|
||||||
case $MAX_FD in #(
|
|
||||||
'' | soft) :;; #(
|
|
||||||
*)
|
|
||||||
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
|
||||||
# shellcheck disable=SC3045
|
|
||||||
ulimit -n "$MAX_FD" ||
|
|
||||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
|
||||||
esac
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Collect all arguments for the java command, stacking in reverse order:
|
|
||||||
# * args from the command line
|
|
||||||
# * the main class name
|
|
||||||
# * -classpath
|
|
||||||
# * -D...appname settings
|
|
||||||
# * --module-path (only if needed)
|
|
||||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
|
||||||
|
|
||||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
|
||||||
if "$cygwin" || "$msys" ; then
|
|
||||||
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
|
||||||
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
|
||||||
|
|
||||||
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
|
||||||
|
|
||||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
|
||||||
for arg do
|
|
||||||
if
|
|
||||||
case $arg in #(
|
|
||||||
-*) false ;; # don't mess with options #(
|
|
||||||
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
|
||||||
[ -e "$t" ] ;; #(
|
|
||||||
*) false ;;
|
|
||||||
esac
|
|
||||||
then
|
|
||||||
arg=$( cygpath --path --ignore --mixed "$arg" )
|
|
||||||
fi
|
|
||||||
# Roll the args list around exactly as many times as the number of
|
|
||||||
# args, so each arg winds up back in the position where it started, but
|
|
||||||
# possibly modified.
|
|
||||||
#
|
|
||||||
# NB: a `for` loop captures its iteration list before it begins, so
|
|
||||||
# changing the positional parameters here affects neither the number of
|
|
||||||
# iterations, nor the values presented in `arg`.
|
|
||||||
shift # remove old arg
|
|
||||||
set -- "$@" "$arg" # push replacement arg
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
|
||||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
|
||||||
|
|
||||||
# Collect all arguments for the java command;
|
|
||||||
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
|
|
||||||
# shell script including quotes and variable substitutions, so put them in
|
|
||||||
# double quotes to make sure that they get re-expanded; and
|
|
||||||
# * put everything else in single quotes, so that it's not re-expanded.
|
|
||||||
|
|
||||||
set -- \
|
|
||||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
|
||||||
-classpath "$CLASSPATH" \
|
|
||||||
org.gradle.wrapper.GradleWrapperMain \
|
|
||||||
"$@"
|
|
||||||
|
|
||||||
# Stop when "xargs" is not available.
|
|
||||||
if ! command -v xargs >/dev/null 2>&1
|
|
||||||
then
|
|
||||||
die "xargs is not available"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Use "xargs" to parse quoted args.
|
|
||||||
#
|
|
||||||
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
|
||||||
#
|
|
||||||
# In Bash we could simply go:
|
|
||||||
#
|
|
||||||
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
|
||||||
# set -- "${ARGS[@]}" "$@"
|
|
||||||
#
|
|
||||||
# but POSIX shell has neither arrays nor command substitution, so instead we
|
|
||||||
# post-process each arg (as a line of input to sed) to backslash-escape any
|
|
||||||
# character that might be a shell metacharacter, then use eval to reverse
|
|
||||||
# that process (while maintaining the separation between arguments), and wrap
|
|
||||||
# the whole thing up as a single "set" statement.
|
|
||||||
#
|
|
||||||
# This will of course break if any of these variables contains a newline or
|
|
||||||
# an unmatched quote.
|
|
||||||
#
|
|
||||||
|
|
||||||
eval "set -- $(
|
|
||||||
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
|
||||||
xargs -n1 |
|
|
||||||
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
|
||||||
tr '\n' ' '
|
|
||||||
)" '"$@"'
|
|
||||||
|
|
||||||
exec "$JAVACMD" "$@"
|
|
92
gradlew.bat
vendored
92
gradlew.bat
vendored
|
@ -1,92 +0,0 @@
|
||||||
@rem
|
|
||||||
@rem Copyright 2015 the original author or authors.
|
|
||||||
@rem
|
|
||||||
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
@rem you may not use this file except in compliance with the License.
|
|
||||||
@rem You may obtain a copy of the License at
|
|
||||||
@rem
|
|
||||||
@rem https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
@rem
|
|
||||||
@rem Unless required by applicable law or agreed to in writing, software
|
|
||||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
@rem See the License for the specific language governing permissions and
|
|
||||||
@rem limitations under the License.
|
|
||||||
@rem
|
|
||||||
|
|
||||||
@if "%DEBUG%"=="" @echo off
|
|
||||||
@rem ##########################################################################
|
|
||||||
@rem
|
|
||||||
@rem Gradle startup script for Windows
|
|
||||||
@rem
|
|
||||||
@rem ##########################################################################
|
|
||||||
|
|
||||||
@rem Set local scope for the variables with windows NT shell
|
|
||||||
if "%OS%"=="Windows_NT" setlocal
|
|
||||||
|
|
||||||
set DIRNAME=%~dp0
|
|
||||||
if "%DIRNAME%"=="" set DIRNAME=.
|
|
||||||
@rem This is normally unused
|
|
||||||
set APP_BASE_NAME=%~n0
|
|
||||||
set APP_HOME=%DIRNAME%
|
|
||||||
|
|
||||||
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
|
||||||
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
|
||||||
|
|
||||||
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
|
||||||
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
|
||||||
|
|
||||||
@rem Find java.exe
|
|
||||||
if defined JAVA_HOME goto findJavaFromJavaHome
|
|
||||||
|
|
||||||
set JAVA_EXE=java.exe
|
|
||||||
%JAVA_EXE% -version >NUL 2>&1
|
|
||||||
if %ERRORLEVEL% equ 0 goto execute
|
|
||||||
|
|
||||||
echo.
|
|
||||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
|
||||||
echo.
|
|
||||||
echo Please set the JAVA_HOME variable in your environment to match the
|
|
||||||
echo location of your Java installation.
|
|
||||||
|
|
||||||
goto fail
|
|
||||||
|
|
||||||
:findJavaFromJavaHome
|
|
||||||
set JAVA_HOME=%JAVA_HOME:"=%
|
|
||||||
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
|
||||||
|
|
||||||
if exist "%JAVA_EXE%" goto execute
|
|
||||||
|
|
||||||
echo.
|
|
||||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
|
||||||
echo.
|
|
||||||
echo Please set the JAVA_HOME variable in your environment to match the
|
|
||||||
echo location of your Java installation.
|
|
||||||
|
|
||||||
goto fail
|
|
||||||
|
|
||||||
:execute
|
|
||||||
@rem Setup the command line
|
|
||||||
|
|
||||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
|
||||||
|
|
||||||
|
|
||||||
@rem Execute Gradle
|
|
||||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
|
||||||
|
|
||||||
:end
|
|
||||||
@rem End local scope for the variables with windows NT shell
|
|
||||||
if %ERRORLEVEL% equ 0 goto mainEnd
|
|
||||||
|
|
||||||
:fail
|
|
||||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
|
||||||
rem the _cmd.exe /c_ return code!
|
|
||||||
set EXIT_CODE=%ERRORLEVEL%
|
|
||||||
if %EXIT_CODE% equ 0 set EXIT_CODE=1
|
|
||||||
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
|
|
||||||
exit /b %EXIT_CODE%
|
|
||||||
|
|
||||||
:mainEnd
|
|
||||||
if "%OS%"=="Windows_NT" endlocal
|
|
||||||
|
|
||||||
:omega
|
|
|
@ -1,49 +1 @@
|
||||||
pluginManagement {
|
rootProject.name = 'guice'
|
||||||
repositories {
|
|
||||||
mavenLocal()
|
|
||||||
mavenCentral {
|
|
||||||
metadataSources {
|
|
||||||
mavenPom()
|
|
||||||
artifact()
|
|
||||||
ignoreGradleMetadataRedirection()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
gradlePluginPortal()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencyResolutionManagement {
|
|
||||||
versionCatalogs {
|
|
||||||
libs {
|
|
||||||
version('gradle', '8.1.1')
|
|
||||||
version('groovy', '3.0.10')
|
|
||||||
version('junit', '5.9.2')
|
|
||||||
library('junit-jupiter-api', 'org.junit.jupiter', 'junit-jupiter-api').versionRef('junit')
|
|
||||||
library('junit-jupiter-params', 'org.junit.jupiter', 'junit-jupiter-params').versionRef('junit')
|
|
||||||
library('junit-jupiter-engine', 'org.junit.jupiter', 'junit-jupiter-engine').versionRef('junit')
|
|
||||||
library('hamcrest', 'org.hamcrest', 'hamcrest-library').version('2.2')
|
|
||||||
library('junit4', 'junit', 'junit').version('4.13.2')
|
|
||||||
library('javax.inject', 'org.xbib', 'javax-inject').version('1')
|
|
||||||
library('guava', 'org.xbib', 'guava').version('30.1')
|
|
||||||
library('guava.testlib', 'com.google.guava', 'guava-testlib').version('30.1-jre')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
gradle.wrapper.version = 6.6.1
|
|
||||||
javax-inject.version = 1
|
|
||||||
guava.version = 30.1
|
|
||||||
# test
|
|
||||||
junit.version = 5.7.2
|
|
||||||
junit4.version = 4.13.2
|
|
||||||
log4j.version = 2.14.1
|
|
||||||
|
|
||||||
api "org.xbib:javax-inject:${project.property('javax-inject.version')}"
|
|
||||||
api "org.xbib:guava:${project.property('guava.version')}"
|
|
||||||
testImplementation "junit:junit:${project.property('junit4.version')}"
|
|
||||||
// Helper for com.google.common.testing.GcFinalization, com.google.common.testing.EqualsTester
|
|
||||||
testImplementation "com.google.guava:guava-testlib:30.1-jre"
|
|
||||||
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
|
@ -36,7 +36,6 @@ public abstract class AbstractModule implements Module {
|
||||||
|
|
||||||
Binder binder;
|
Binder binder;
|
||||||
|
|
||||||
@Override
|
|
||||||
public final synchronized void configure(Binder builder) {
|
public final synchronized void configure(Binder builder) {
|
||||||
checkState(this.binder == null, "Re-entry is not allowed.");
|
checkState(this.binder == null, "Re-entry is not allowed.");
|
||||||
|
|
||||||
|
@ -64,7 +63,8 @@ public abstract class AbstractModule implements Module {
|
||||||
/**
|
/**
|
||||||
* @see Binder#bindScope(Class, Scope)
|
* @see Binder#bindScope(Class, Scope)
|
||||||
*/
|
*/
|
||||||
protected void bindScope(Class<? extends Annotation> scopeAnnotation, Scope scope) {
|
protected void bindScope(Class<? extends Annotation> scopeAnnotation,
|
||||||
|
Scope scope) {
|
||||||
binder().bindScope(scopeAnnotation, scope);
|
binder().bindScope(scopeAnnotation, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -100,7 +100,7 @@ import java.lang.reflect.Proxy;
|
||||||
* contribute their own custom scopes for use here as well.
|
* contribute their own custom scopes for use here as well.
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* bind(new TypeLiteral<PaymentService<CreditCard>>() {})
|
* bind(new TypeLiteral<PaymentService<CreditCard>>() {})
|
||||||
* .to(CreditCardPaymentService.class);</pre>
|
* .to(CreditCardPaymentService.class);</pre>
|
||||||
*
|
*
|
||||||
* This admittedly odd construct is the way to bind a parameterized type. It
|
* This admittedly odd construct is the way to bind a parameterized type. It
|
||||||
|
@ -159,7 +159,7 @@ import java.lang.reflect.Proxy;
|
||||||
* your application.
|
* your application.
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* Constructor<T> loneCtor = getLoneCtorFromServiceImplViaReflection();
|
* Constructor<T> loneCtor = getLoneCtorFromServiceImplViaReflection();
|
||||||
* bind(ServiceImpl.class)
|
* bind(ServiceImpl.class)
|
||||||
* .toConstructor(loneCtor);</pre>
|
* .toConstructor(loneCtor);</pre>
|
||||||
*
|
*
|
||||||
|
@ -367,7 +367,7 @@ public interface Binder {
|
||||||
* their clients.
|
* their clients.
|
||||||
* @return a binder that shares its configuration with this binder.
|
* @return a binder that shares its configuration with this binder.
|
||||||
*/
|
*/
|
||||||
Binder skipSources(Class<?>... classesToSkip);
|
Binder skipSources(Class... classesToSkip);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new private child environment for bindings and other configuration. The returned
|
* Creates a new private child environment for bindings and other configuration. The returned
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package com.google.inject;
|
package com.google.inject;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.inject.internal.Messages;
|
import com.google.inject.internal.Errors;
|
||||||
import com.google.inject.spi.Message;
|
import com.google.inject.spi.Message;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -24,7 +24,7 @@ public final class ConfigurationException extends RuntimeException {
|
||||||
*/
|
*/
|
||||||
public ConfigurationException(Iterable<Message> messages) {
|
public ConfigurationException(Iterable<Message> messages) {
|
||||||
this.messages = ImmutableSet.copyOf(messages);
|
this.messages = ImmutableSet.copyOf(messages);
|
||||||
initCause(Messages.getOnlyCause(this.messages));
|
initCause(Errors.getOnlyCause(this.messages));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -59,6 +59,6 @@ public final class ConfigurationException extends RuntimeException {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getMessage() {
|
public String getMessage() {
|
||||||
return Messages.formatMessages("Guice configuration errors", messages);
|
return Errors.format("Guice configuration errors", messages);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
package com.google.inject;
|
package com.google.inject;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.inject.internal.Messages;
|
import com.google.inject.internal.Errors;
|
||||||
import com.google.inject.spi.Message;
|
import com.google.inject.spi.Message;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -12,9 +12,9 @@ import static com.google.common.base.Preconditions.checkArgument;
|
||||||
* Thrown when errors occur while creating a {@link Injector}. Includes a list of encountered
|
* Thrown when errors occur while creating a {@link Injector}. Includes a list of encountered
|
||||||
* errors. Clients should catch this exception, log it, and stop execution.
|
* errors. Clients should catch this exception, log it, and stop execution.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
|
||||||
public class CreationException extends RuntimeException {
|
public class CreationException extends RuntimeException {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 0;
|
||||||
private final ImmutableSet<Message> messages;
|
private final ImmutableSet<Message> messages;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,7 +23,7 @@ public class CreationException extends RuntimeException {
|
||||||
public CreationException(Collection<Message> messages) {
|
public CreationException(Collection<Message> messages) {
|
||||||
this.messages = ImmutableSet.copyOf(messages);
|
this.messages = ImmutableSet.copyOf(messages);
|
||||||
checkArgument(!this.messages.isEmpty());
|
checkArgument(!this.messages.isEmpty());
|
||||||
initCause(Messages.getOnlyCause(this.messages));
|
initCause(Errors.getOnlyCause(this.messages));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,6 +35,6 @@ public class CreationException extends RuntimeException {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getMessage() {
|
public String getMessage() {
|
||||||
return Messages.formatMessages("Unable to create injector, see the following errors", messages);
|
return Errors.format("Unable to create injector, see the following errors", messages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,7 +76,11 @@ public final class Guice {
|
||||||
* @throws CreationException if one or more errors occur during injector
|
* @throws CreationException if one or more errors occur during injector
|
||||||
* construction
|
* construction
|
||||||
*/
|
*/
|
||||||
public static Injector createInjector(Stage stage, Iterable<? extends Module> modules) {
|
public static Injector createInjector(Stage stage,
|
||||||
return new InternalInjectorCreator().stage(stage).addModules(modules).build();
|
Iterable<? extends Module> modules) {
|
||||||
|
return new InternalInjectorCreator()
|
||||||
|
.stage(stage)
|
||||||
|
.addModules(modules)
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package com.google.inject;
|
package com.google.inject;
|
||||||
|
|
||||||
import com.google.inject.spi.Element;
|
|
||||||
import com.google.inject.spi.InjectionPoint;
|
|
||||||
import com.google.inject.spi.TypeConverterBinding;
|
import com.google.inject.spi.TypeConverterBinding;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
|
@ -227,37 +225,4 @@ public interface Injector {
|
||||||
* <p>This method is part of the Guice SPI and is intended for use by tools and extensions.
|
* <p>This method is part of the Guice SPI and is intended for use by tools and extensions.
|
||||||
*/
|
*/
|
||||||
Set<TypeConverterBinding> getTypeConverterBindings();
|
Set<TypeConverterBinding> getTypeConverterBindings();
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the elements that make up this injector. Note that not all kinds of elements are
|
|
||||||
* returned.
|
|
||||||
*
|
|
||||||
* <p>The returned list does not include elements inherited from a {@link #getParent() parent
|
|
||||||
* injector}, should one exist.
|
|
||||||
*
|
|
||||||
* <p>The returned list is immutable; it contains only the elements that were present when {@link
|
|
||||||
* #getElements} was invoked. Subsequent calls may return a list with additional elements.
|
|
||||||
*
|
|
||||||
* <p>The returned list does not include data inherited from a {@link #getParent() parent
|
|
||||||
* injector}, should one exist.
|
|
||||||
*
|
|
||||||
* <p>This method is part of the Guice SPI and is intended for use by tools and extensions.
|
|
||||||
*/
|
|
||||||
List<Element> getElements();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the injection points created for calls to {@link #getMembersInjector} (either directly
|
|
||||||
* or indirectly, e.g. through {@link #injectMembers}.
|
|
||||||
*
|
|
||||||
* <p>This excludes any injection points from elements (which are accessible from each element via
|
|
||||||
* the SPI), unless {@link #getMembersInjector} or {@link #injectMembers} were also called for the
|
|
||||||
* same key.
|
|
||||||
*
|
|
||||||
* <p>The returned multimap does not include data inherited from a {@link #getParent() parent
|
|
||||||
* injector}, should one exist.
|
|
||||||
*
|
|
||||||
* <p>This method is part of the Guice SPI and is intended for use by tools and extensions.
|
|
||||||
*/
|
|
||||||
Map<TypeLiteral<?>, List<InjectionPoint>> getAllMembersInjectorInjectionPoints();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,24 @@
|
||||||
package com.google.inject;
|
package com.google.inject;
|
||||||
|
|
||||||
|
import com.google.common.base.Supplier;
|
||||||
|
import com.google.common.base.Suppliers;
|
||||||
|
import com.google.inject.internal.Annotations;
|
||||||
|
import com.google.inject.internal.MoreTypes;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
import static com.google.inject.internal.Annotations.generateAnnotation;
|
import static com.google.inject.internal.Annotations.generateAnnotation;
|
||||||
import static com.google.inject.internal.Annotations.isAllDefaultMethods;
|
import static com.google.inject.internal.Annotations.isAllDefaultMethods;
|
||||||
|
|
||||||
import com.google.inject.internal.Annotations;
|
|
||||||
import com.google.inject.internal.MoreTypes;
|
|
||||||
import java.lang.annotation.Annotation;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Guice uses Key objects to identify a dependency that can be resolved by the Guice {@link
|
* Binding key consisting of an injection type and an optional annotation.
|
||||||
* Injector}. A Guice key consists of an injection type and an optional annotation.
|
* Matches the type and annotation at a point of injection.
|
||||||
*
|
*
|
||||||
* <p>For example, {@code Key.get(Service.class, Transactional.class)} will match:
|
* <p>For example, {@code Key.get(Service.class, Transactional.class)} will
|
||||||
|
* match:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* {@literal @}Inject
|
* {@literal @}Inject
|
||||||
|
@ -23,13 +27,12 @@ import java.lang.reflect.Type;
|
||||||
* }
|
* }
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* <p>{@code Key} supports generic types via subclassing just like {@link TypeLiteral}.
|
* <p>{@code Key} supports generic types via subclassing just like {@link
|
||||||
|
* TypeLiteral}.
|
||||||
*
|
*
|
||||||
* <p>Keys do not differentiate between primitive types (int, char, etc.) and their corresponding
|
* <p>Keys do not differentiate between primitive types (int, char, etc.) and
|
||||||
* wrapper types (Integer, Character, etc.). Primitive types will be replaced with their wrapper
|
* their corresponding wrapper types (Integer, Character, etc.). Primitive
|
||||||
* types when keys are created.
|
* types will be replaced with their wrapper types when keys are created.
|
||||||
*
|
|
||||||
* @author crazybob@google.com (Bob Lee)
|
|
||||||
*/
|
*/
|
||||||
public class Key<T> {
|
public class Key<T> {
|
||||||
|
|
||||||
|
@ -37,37 +40,38 @@ public class Key<T> {
|
||||||
|
|
||||||
private final TypeLiteral<T> typeLiteral;
|
private final TypeLiteral<T> typeLiteral;
|
||||||
private final int hashCode;
|
private final int hashCode;
|
||||||
// This field is updated using the 'Data-Race-Ful' lazy intialization pattern
|
private final Supplier<String> toStringSupplier;
|
||||||
// See http://jeremymanson.blogspot.com/2008/12/benign-data-races-in-java.html for a detailed
|
|
||||||
// explanation.
|
|
||||||
private String toString;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new key. Derives the type from this class's type parameter.
|
* Constructs a new key. Derives the type from this class's type parameter.
|
||||||
*
|
*
|
||||||
* <p>Clients create an empty anonymous subclass. Doing so embeds the type parameter in the
|
* <p>Clients create an empty anonymous subclass. Doing so embeds the type
|
||||||
* anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
|
* parameter in the anonymous class's type hierarchy so we can reconstitute it
|
||||||
|
* at runtime despite erasure.
|
||||||
*
|
*
|
||||||
* <p>Example usage for a binding of type {@code Foo} annotated with {@code @Bar}:
|
* <p>Example usage for a binding of type {@code Foo} annotated with
|
||||||
|
* {@code @Bar}:
|
||||||
*
|
*
|
||||||
* <p>{@code new Key<Foo>(Bar.class) {}}.
|
* <p>{@code new Key<Foo>(Bar.class) {}}.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
protected Key(Class<? extends Annotation> annotationType) {
|
protected Key(Class<? extends Annotation> annotationType) {
|
||||||
this.annotationStrategy = strategyFor(annotationType);
|
this.annotationStrategy = strategyFor(annotationType);
|
||||||
this.typeLiteral =
|
this.typeLiteral = MoreTypes.canonicalizeForKey(
|
||||||
MoreTypes.canonicalizeForKey(
|
(TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()));
|
||||||
(TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()));
|
|
||||||
this.hashCode = computeHashCode();
|
this.hashCode = computeHashCode();
|
||||||
|
this.toStringSupplier = createToStringSupplier();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new key. Derives the type from this class's type parameter.
|
* Constructs a new key. Derives the type from this class's type parameter.
|
||||||
*
|
*
|
||||||
* <p>Clients create an empty anonymous subclass. Doing so embeds the type parameter in the
|
* <p>Clients create an empty anonymous subclass. Doing so embeds the type
|
||||||
* anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
|
* parameter in the anonymous class's type hierarchy so we can reconstitute it
|
||||||
|
* at runtime despite erasure.
|
||||||
*
|
*
|
||||||
* <p>Example usage for a binding of type {@code Foo} annotated with {@code @Bar}:
|
* <p>Example usage for a binding of type {@code Foo} annotated with
|
||||||
|
* {@code @Bar}:
|
||||||
*
|
*
|
||||||
* <p>{@code new Key<Foo>(new Bar()) {}}.
|
* <p>{@code new Key<Foo>(new Bar()) {}}.
|
||||||
*/
|
*/
|
||||||
|
@ -75,17 +79,18 @@ public class Key<T> {
|
||||||
protected Key(Annotation annotation) {
|
protected Key(Annotation annotation) {
|
||||||
// no usages, not test-covered
|
// no usages, not test-covered
|
||||||
this.annotationStrategy = strategyFor(annotation);
|
this.annotationStrategy = strategyFor(annotation);
|
||||||
this.typeLiteral =
|
this.typeLiteral = MoreTypes.canonicalizeForKey(
|
||||||
MoreTypes.canonicalizeForKey(
|
(TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()));
|
||||||
(TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()));
|
|
||||||
this.hashCode = computeHashCode();
|
this.hashCode = computeHashCode();
|
||||||
|
this.toStringSupplier = createToStringSupplier();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new key. Derives the type from this class's type parameter.
|
* Constructs a new key. Derives the type from this class's type parameter.
|
||||||
*
|
*
|
||||||
* <p>Clients create an empty anonymous subclass. Doing so embeds the type parameter in the
|
* <p>Clients create an empty anonymous subclass. Doing so embeds the type
|
||||||
* anonymous class's type hierarchy so we can reconstitute it at runtime despite erasure.
|
* parameter in the anonymous class's type hierarchy so we can reconstitute it
|
||||||
|
* at runtime despite erasure.
|
||||||
*
|
*
|
||||||
* <p>Example usage for a binding of type {@code Foo}:
|
* <p>Example usage for a binding of type {@code Foo}:
|
||||||
*
|
*
|
||||||
|
@ -94,51 +99,191 @@ public class Key<T> {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
protected Key() {
|
protected Key() {
|
||||||
this.annotationStrategy = NullAnnotationStrategy.INSTANCE;
|
this.annotationStrategy = NullAnnotationStrategy.INSTANCE;
|
||||||
this.typeLiteral =
|
this.typeLiteral = MoreTypes.canonicalizeForKey(
|
||||||
MoreTypes.canonicalizeForKey(
|
(TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()));
|
||||||
(TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()));
|
|
||||||
this.hashCode = computeHashCode();
|
this.hashCode = computeHashCode();
|
||||||
|
this.toStringSupplier = createToStringSupplier();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Unsafe. Constructs a key from a manually specified type. */
|
/**
|
||||||
|
* Unsafe. Constructs a key from a manually specified type.
|
||||||
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private Key(Type type, AnnotationStrategy annotationStrategy) {
|
private Key(Type type, AnnotationStrategy annotationStrategy) {
|
||||||
this.annotationStrategy = annotationStrategy;
|
this.annotationStrategy = annotationStrategy;
|
||||||
this.typeLiteral = MoreTypes.canonicalizeForKey((TypeLiteral<T>) TypeLiteral.get(type));
|
this.typeLiteral = MoreTypes.canonicalizeForKey((TypeLiteral<T>) TypeLiteral.get(type));
|
||||||
this.hashCode = computeHashCode();
|
this.hashCode = computeHashCode();
|
||||||
|
this.toStringSupplier = createToStringSupplier();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Constructs a key from a manually specified type. */
|
/**
|
||||||
|
* Constructs a key from a manually specified type.
|
||||||
|
*/
|
||||||
private Key(TypeLiteral<T> typeLiteral, AnnotationStrategy annotationStrategy) {
|
private Key(TypeLiteral<T> typeLiteral, AnnotationStrategy annotationStrategy) {
|
||||||
this.annotationStrategy = annotationStrategy;
|
this.annotationStrategy = annotationStrategy;
|
||||||
this.typeLiteral = MoreTypes.canonicalizeForKey(typeLiteral);
|
this.typeLiteral = MoreTypes.canonicalizeForKey(typeLiteral);
|
||||||
this.hashCode = computeHashCode();
|
this.hashCode = computeHashCode();
|
||||||
|
this.toStringSupplier = createToStringSupplier();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Computes the hash code for this key. */
|
/**
|
||||||
|
* Gets a key for an injection type and an annotation strategy.
|
||||||
|
*/
|
||||||
|
static <T> Key<T> get(Class<T> type,
|
||||||
|
AnnotationStrategy annotationStrategy) {
|
||||||
|
return new Key<T>(type, annotationStrategy);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a key for an injection type.
|
||||||
|
*/
|
||||||
|
public static <T> Key<T> get(Class<T> type) {
|
||||||
|
return new Key<T>(type, NullAnnotationStrategy.INSTANCE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a key for an injection type and an annotation type.
|
||||||
|
*/
|
||||||
|
public static <T> Key<T> get(Class<T> type,
|
||||||
|
Class<? extends Annotation> annotationType) {
|
||||||
|
return new Key<T>(type, strategyFor(annotationType));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a key for an injection type and an annotation.
|
||||||
|
*/
|
||||||
|
public static <T> Key<T> get(Class<T> type, Annotation annotation) {
|
||||||
|
return new Key<T>(type, strategyFor(annotation));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a key for an injection type.
|
||||||
|
*/
|
||||||
|
public static Key<?> get(Type type) {
|
||||||
|
return new Key<Object>(type, NullAnnotationStrategy.INSTANCE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a key for an injection type and an annotation type.
|
||||||
|
*/
|
||||||
|
public static Key<?> get(Type type,
|
||||||
|
Class<? extends Annotation> annotationType) {
|
||||||
|
return new Key<Object>(type, strategyFor(annotationType));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a key for an injection type and an annotation.
|
||||||
|
*/
|
||||||
|
public static Key<?> get(Type type, Annotation annotation) {
|
||||||
|
return new Key<Object>(type, strategyFor(annotation));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a key for an injection type.
|
||||||
|
*/
|
||||||
|
public static <T> Key<T> get(TypeLiteral<T> typeLiteral) {
|
||||||
|
return new Key<T>(typeLiteral, NullAnnotationStrategy.INSTANCE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a key for an injection type and an annotation type.
|
||||||
|
*/
|
||||||
|
public static <T> Key<T> get(TypeLiteral<T> typeLiteral,
|
||||||
|
Class<? extends Annotation> annotationType) {
|
||||||
|
return new Key<T>(typeLiteral, strategyFor(annotationType));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a key for an injection type and an annotation.
|
||||||
|
*/
|
||||||
|
public static <T> Key<T> get(TypeLiteral<T> typeLiteral,
|
||||||
|
Annotation annotation) {
|
||||||
|
return new Key<T>(typeLiteral, strategyFor(annotation));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the strategy for an annotation.
|
||||||
|
*/
|
||||||
|
static AnnotationStrategy strategyFor(Annotation annotation) {
|
||||||
|
checkNotNull(annotation, "annotation");
|
||||||
|
Class<? extends Annotation> annotationType = annotation.annotationType();
|
||||||
|
ensureRetainedAtRuntime(annotationType);
|
||||||
|
ensureIsBindingAnnotation(annotationType);
|
||||||
|
|
||||||
|
if (Annotations.isMarker(annotationType)) {
|
||||||
|
return new AnnotationTypeStrategy(annotationType, annotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new AnnotationInstanceStrategy(Annotations.canonicalizeIfNamed(annotation));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the strategy for an annotation type.
|
||||||
|
*/
|
||||||
|
static AnnotationStrategy strategyFor(Class<? extends Annotation> annotationType) {
|
||||||
|
annotationType = Annotations.canonicalizeIfNamed(annotationType);
|
||||||
|
if (isAllDefaultMethods(annotationType)) {
|
||||||
|
return strategyFor(generateAnnotation(annotationType));
|
||||||
|
}
|
||||||
|
|
||||||
|
checkNotNull(annotationType, "annotation type");
|
||||||
|
ensureRetainedAtRuntime(annotationType);
|
||||||
|
ensureIsBindingAnnotation(annotationType);
|
||||||
|
return new AnnotationTypeStrategy(annotationType, null);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ensureRetainedAtRuntime(
|
||||||
|
Class<? extends Annotation> annotationType) {
|
||||||
|
checkArgument(Annotations.isRetainedAtRuntime(annotationType),
|
||||||
|
"%s is not retained at runtime. Please annotate it with @Retention(RUNTIME).",
|
||||||
|
annotationType.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ensureIsBindingAnnotation(Class<? extends Annotation> annotationType) {
|
||||||
|
checkArgument(Annotations.isBindingAnnotation(annotationType),
|
||||||
|
"%s is not a binding annotation. Please annotate it with @BindingAnnotation.",
|
||||||
|
annotationType.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the hash code for this key.
|
||||||
|
*/
|
||||||
private int computeHashCode() {
|
private int computeHashCode() {
|
||||||
return typeLiteral.hashCode() * 31 + annotationStrategy.hashCode();
|
return typeLiteral.hashCode() * 31 + annotationStrategy.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gets the key type. */
|
/**
|
||||||
|
* @return a {@link Supplier} which memoizes the value for lazy initialization.
|
||||||
|
*/
|
||||||
|
private Supplier<String> createToStringSupplier() {
|
||||||
|
// The performance hit on access is acceptable since the intended use is for non-performance-
|
||||||
|
// critical applications.
|
||||||
|
return Suppliers.memoize(new Supplier<String>() {
|
||||||
|
@Override
|
||||||
|
public String get() {
|
||||||
|
return "Key[type=" + typeLiteral + ", annotation=" + annotationStrategy + "]";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the key type.
|
||||||
|
*/
|
||||||
public final TypeLiteral<T> getTypeLiteral() {
|
public final TypeLiteral<T> getTypeLiteral() {
|
||||||
return typeLiteral;
|
return typeLiteral;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gets the annotation type. Will be {@code null} if this key lacks an annotation. */
|
/**
|
||||||
|
* Gets the annotation type.
|
||||||
|
*/
|
||||||
public final Class<? extends Annotation> getAnnotationType() {
|
public final Class<? extends Annotation> getAnnotationType() {
|
||||||
return annotationStrategy.getAnnotationType();
|
return annotationStrategy.getAnnotationType();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the annotation instance if available. Will be {@code null} if this key lacks an annotation
|
* Gets the annotation.
|
||||||
* <i>or</i> the key was constructed with a {@code Class<Annotation>}.
|
|
||||||
*
|
|
||||||
* <p><b>Warning:</b> this can return null even if this key is annotated. To check whether a
|
|
||||||
* {@code Key} has an annotation use {@link #hasAnnotationType} instead.
|
|
||||||
*/
|
*/
|
||||||
// TODO(diamondm) consider deprecating this in favor of a method that ISEs if hasAnnotationType()
|
|
||||||
// is true but this would return null.
|
|
||||||
public final Annotation getAnnotation() {
|
public final Annotation getAnnotation() {
|
||||||
return annotationStrategy.getAnnotation();
|
return annotationStrategy.getAnnotation();
|
||||||
}
|
}
|
||||||
|
@ -161,7 +306,9 @@ public class Key<T> {
|
||||||
return typeLiteral.getRawType();
|
return typeLiteral.getRawType();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gets the key of this key's provider. */
|
/**
|
||||||
|
* Gets the key of this key's provider.
|
||||||
|
*/
|
||||||
Key<Provider<T>> providerKey() {
|
Key<Provider<T>> providerKey() {
|
||||||
return ofType(typeLiteral.providerType());
|
return ofType(typeLiteral.providerType());
|
||||||
}
|
}
|
||||||
|
@ -186,136 +333,73 @@ public class Key<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final String toString() {
|
public final String toString() {
|
||||||
// Note: to not introduce dangerous data races the field should only be read once in this
|
return toStringSupplier.get();
|
||||||
// method.
|
|
||||||
String local = toString;
|
|
||||||
if (local == null) {
|
|
||||||
local = "Key[type=" + typeLiteral + ", annotation=" + annotationStrategy + "]";
|
|
||||||
toString = local;
|
|
||||||
}
|
|
||||||
return local;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gets a key for an injection type and an annotation strategy. */
|
/**
|
||||||
static <T> Key<T> get(Class<T> type, AnnotationStrategy annotationStrategy) {
|
* Returns a new key of the specified type with the same annotation as this
|
||||||
|
* key.
|
||||||
|
*/
|
||||||
|
public <T> Key<T> ofType(Class<T> type) {
|
||||||
return new Key<T>(type, annotationStrategy);
|
return new Key<T>(type, annotationStrategy);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gets a key for an injection type. */
|
|
||||||
public static <T> Key<T> get(Class<T> type) {
|
|
||||||
return new Key<T>(type, NullAnnotationStrategy.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Gets a key for an injection type and an annotation type. */
|
|
||||||
public static <T> Key<T> get(Class<T> type, Class<? extends Annotation> annotationType) {
|
|
||||||
return new Key<T>(type, strategyFor(annotationType));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Gets a key for an injection type and an annotation. */
|
|
||||||
public static <T> Key<T> get(Class<T> type, Annotation annotation) {
|
|
||||||
return new Key<T>(type, strategyFor(annotation));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Gets a key for an injection type. */
|
|
||||||
public static Key<?> get(Type type) {
|
|
||||||
return new Key<>(type, NullAnnotationStrategy.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Gets a key for an injection type and an annotation type. */
|
|
||||||
public static Key<?> get(Type type, Class<? extends Annotation> annotationType) {
|
|
||||||
return new Key<>(type, strategyFor(annotationType));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Gets a key for an injection type and an annotation. */
|
|
||||||
public static Key<?> get(Type type, Annotation annotation) {
|
|
||||||
return new Key<>(type, strategyFor(annotation));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Gets a key for an injection type. */
|
|
||||||
public static <T> Key<T> get(TypeLiteral<T> typeLiteral) {
|
|
||||||
return new Key<T>(typeLiteral, NullAnnotationStrategy.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Gets a key for an injection type and an annotation type. */
|
|
||||||
public static <T> Key<T> get(
|
|
||||||
TypeLiteral<T> typeLiteral, Class<? extends Annotation> annotationType) {
|
|
||||||
return new Key<T>(typeLiteral, strategyFor(annotationType));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Gets a key for an injection type and an annotation. */
|
|
||||||
public static <T> Key<T> get(TypeLiteral<T> typeLiteral, Annotation annotation) {
|
|
||||||
return new Key<T>(typeLiteral, strategyFor(annotation));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a new key of the specified type with the same annotation as this key.
|
* Returns a new key of the specified type with the same annotation as this
|
||||||
*
|
* key.
|
||||||
* @since 3.0
|
|
||||||
*/
|
|
||||||
public <U> Key<U> ofType(Class<U> type) {
|
|
||||||
return new Key<>(type, annotationStrategy);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a new key of the specified type with the same annotation as this key.
|
|
||||||
*
|
|
||||||
* @since 3.0
|
|
||||||
*/
|
*/
|
||||||
public Key<?> ofType(Type type) {
|
public Key<?> ofType(Type type) {
|
||||||
return new Key<>(type, annotationStrategy);
|
return new Key<Object>(type, annotationStrategy);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a new key of the specified type with the same annotation as this key.
|
* Returns a new key of the specified type with the same annotation as this
|
||||||
*
|
* key.
|
||||||
* @since 3.0
|
|
||||||
*/
|
*/
|
||||||
public <U> Key<U> ofType(TypeLiteral<U> type) {
|
public <T> Key<T> ofType(TypeLiteral<T> type) {
|
||||||
return new Key<U>(type, annotationStrategy);
|
return new Key<T>(type, annotationStrategy);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a new key of the same type with the specified annotation.
|
|
||||||
*
|
|
||||||
* <p>This is equivalent to {@code Key.get(key.getTypeLiteral(), annotation)} but may be more
|
|
||||||
* convenient to use in certain cases.
|
|
||||||
*
|
|
||||||
* @since 5.0
|
|
||||||
*/
|
|
||||||
public Key<T> withAnnotation(Class<? extends Annotation> annotationType) {
|
|
||||||
return new Key<T>(typeLiteral, strategyFor(annotationType));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a new key of the same type with the specified annotation.
|
|
||||||
*
|
|
||||||
* <p>This is equivalent to {@code Key.get(key.getTypeLiteral(), annotation)} but may be more
|
|
||||||
* convenient to use in certain cases.
|
|
||||||
*
|
|
||||||
* @since 5.0
|
|
||||||
*/
|
|
||||||
public Key<T> withAnnotation(Annotation annotation) {
|
|
||||||
return new Key<T>(typeLiteral, strategyFor(annotation));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if this key has annotation attributes.
|
* Returns true if this key has annotation attributes.
|
||||||
*
|
|
||||||
* @since 3.0
|
|
||||||
*/
|
*/
|
||||||
public boolean hasAttributes() {
|
public boolean hasAttributes() {
|
||||||
return annotationStrategy.hasAttributes();
|
return annotationStrategy.hasAttributes();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns this key without annotation attributes, i.e. with only the annotation type.
|
* Returns this key without annotation attributes, i.e. with only the
|
||||||
*
|
* annotation type.
|
||||||
* @since 3.0
|
|
||||||
*/
|
*/
|
||||||
public Key<T> withoutAttributes() {
|
public Key<T> withoutAttributes() {
|
||||||
return new Key<T>(typeLiteral, annotationStrategy.withoutAttributes());
|
return new Key<T>(typeLiteral, annotationStrategy.withoutAttributes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static enum NullAnnotationStrategy implements AnnotationStrategy {
|
||||||
|
INSTANCE;
|
||||||
|
|
||||||
|
public boolean hasAttributes() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnnotationStrategy withoutAttributes() {
|
||||||
|
throw new UnsupportedOperationException("Key already has no attributes.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Annotation getAnnotation() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<? extends Annotation> getAnnotationType() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "[none]";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
interface AnnotationStrategy {
|
interface AnnotationStrategy {
|
||||||
Annotation getAnnotation();
|
Annotation getAnnotation();
|
||||||
|
|
||||||
|
@ -326,76 +410,6 @@ public class Key<T> {
|
||||||
AnnotationStrategy withoutAttributes();
|
AnnotationStrategy withoutAttributes();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gets the strategy for an annotation. */
|
|
||||||
static AnnotationStrategy strategyFor(Annotation annotation) {
|
|
||||||
checkNotNull(annotation, "annotation");
|
|
||||||
Class<? extends Annotation> annotationType = annotation.annotationType();
|
|
||||||
ensureRetainedAtRuntime(annotationType);
|
|
||||||
ensureIsBindingAnnotation(annotationType);
|
|
||||||
|
|
||||||
if (Annotations.isMarker(annotationType)) {
|
|
||||||
return new AnnotationTypeStrategy(annotationType, annotation);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new AnnotationInstanceStrategy(Annotations.canonicalizeIfNamed(annotation));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Gets the strategy for an annotation type. */
|
|
||||||
static AnnotationStrategy strategyFor(Class<? extends Annotation> annotationType) {
|
|
||||||
annotationType = Annotations.canonicalizeIfNamed(annotationType);
|
|
||||||
if (isAllDefaultMethods(annotationType)) {
|
|
||||||
return strategyFor(generateAnnotation(annotationType));
|
|
||||||
}
|
|
||||||
|
|
||||||
checkNotNull(annotationType, "annotation type");
|
|
||||||
ensureRetainedAtRuntime(annotationType);
|
|
||||||
ensureIsBindingAnnotation(annotationType);
|
|
||||||
return new AnnotationTypeStrategy(annotationType, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void ensureRetainedAtRuntime(Class<? extends Annotation> annotationType) {
|
|
||||||
checkArgument(
|
|
||||||
Annotations.isRetainedAtRuntime(annotationType),
|
|
||||||
"%s is not retained at runtime. Please annotate it with @Retention(RUNTIME).",
|
|
||||||
annotationType.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void ensureIsBindingAnnotation(Class<? extends Annotation> annotationType) {
|
|
||||||
checkArgument(
|
|
||||||
Annotations.isBindingAnnotation(annotationType),
|
|
||||||
"%s is not a binding annotation. Please annotate it with @BindingAnnotation.",
|
|
||||||
annotationType.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
static enum NullAnnotationStrategy implements AnnotationStrategy {
|
|
||||||
INSTANCE;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasAttributes() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public AnnotationStrategy withoutAttributes() {
|
|
||||||
throw new UnsupportedOperationException("Key already has no attributes.");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Annotation getAnnotation() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class<? extends Annotation> getAnnotationType() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "[none]";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// this class not test-covered
|
// this class not test-covered
|
||||||
static class AnnotationInstanceStrategy implements AnnotationStrategy {
|
static class AnnotationInstanceStrategy implements AnnotationStrategy {
|
||||||
|
|
||||||
|
@ -405,22 +419,18 @@ public class Key<T> {
|
||||||
this.annotation = checkNotNull(annotation, "annotation");
|
this.annotation = checkNotNull(annotation, "annotation");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasAttributes() {
|
public boolean hasAttributes() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public AnnotationStrategy withoutAttributes() {
|
public AnnotationStrategy withoutAttributes() {
|
||||||
return new AnnotationTypeStrategy(getAnnotationType(), annotation);
|
return new AnnotationTypeStrategy(getAnnotationType(), annotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Annotation getAnnotation() {
|
public Annotation getAnnotation() {
|
||||||
return annotation;
|
return annotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class<? extends Annotation> getAnnotationType() {
|
public Class<? extends Annotation> getAnnotationType() {
|
||||||
return annotation.annotationType();
|
return annotation.annotationType();
|
||||||
}
|
}
|
||||||
|
@ -453,27 +463,24 @@ public class Key<T> {
|
||||||
// Keep the instance around if we have it so the client can request it.
|
// Keep the instance around if we have it so the client can request it.
|
||||||
final Annotation annotation;
|
final Annotation annotation;
|
||||||
|
|
||||||
AnnotationTypeStrategy(Class<? extends Annotation> annotationType, Annotation annotation) {
|
AnnotationTypeStrategy(Class<? extends Annotation> annotationType,
|
||||||
|
Annotation annotation) {
|
||||||
this.annotationType = checkNotNull(annotationType, "annotation type");
|
this.annotationType = checkNotNull(annotationType, "annotation type");
|
||||||
this.annotation = annotation;
|
this.annotation = annotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasAttributes() {
|
public boolean hasAttributes() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public AnnotationStrategy withoutAttributes() {
|
public AnnotationStrategy withoutAttributes() {
|
||||||
throw new UnsupportedOperationException("Key already has no attributes.");
|
throw new UnsupportedOperationException("Key already has no attributes.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Annotation getAnnotation() {
|
public Annotation getAnnotation() {
|
||||||
return annotation;
|
return annotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class<? extends Annotation> getAnnotationType() {
|
public Class<? extends Annotation> getAnnotationType() {
|
||||||
return annotationType;
|
return annotationType;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
package com.google.inject;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Thrown from {@link Provider#get} when an attempt is made to access a scoped object while the
|
|
||||||
* scope in question is not currently active.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("serial")
|
|
||||||
public final class OutOfScopeException extends RuntimeException {
|
|
||||||
|
|
||||||
public OutOfScopeException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public OutOfScopeException(String message, Throwable cause) {
|
|
||||||
super(message, cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
public OutOfScopeException(Throwable cause) {
|
|
||||||
super(cause);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -31,5 +31,5 @@ public interface PrivateBinder extends Binder {
|
||||||
|
|
||||||
PrivateBinder withSource(Object source);
|
PrivateBinder withSource(Object source);
|
||||||
|
|
||||||
PrivateBinder skipSources(Class<?>... classesToSkip);
|
PrivateBinder skipSources(Class... classesToSkip);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package com.google.inject;
|
||||||
|
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
import javax.inject.Provider;
|
|
||||||
|
|
||||||
import static java.lang.annotation.ElementType.TYPE;
|
import static java.lang.annotation.ElementType.TYPE;
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
|
@ -32,6 +32,5 @@ public interface Provider<T> extends javax.inject.Provider<T> {
|
||||||
* @throws ProvisionException if an instance cannot be provided. Such exceptions include messages
|
* @throws ProvisionException if an instance cannot be provided. Such exceptions include messages
|
||||||
* and throwables to describe why provision failed.
|
* and throwables to describe why provision failed.
|
||||||
*/
|
*/
|
||||||
@Override
|
|
||||||
T get();
|
T get();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package com.google.inject;
|
package com.google.inject;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.inject.internal.Messages;
|
import com.google.inject.internal.Errors;
|
||||||
import com.google.inject.spi.Message;
|
import com.google.inject.spi.Message;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -23,7 +23,7 @@ public final class ProvisionException extends RuntimeException {
|
||||||
public ProvisionException(Iterable<Message> messages) {
|
public ProvisionException(Iterable<Message> messages) {
|
||||||
this.messages = ImmutableSet.copyOf(messages);
|
this.messages = ImmutableSet.copyOf(messages);
|
||||||
checkArgument(!this.messages.isEmpty());
|
checkArgument(!this.messages.isEmpty());
|
||||||
initCause(Messages.getOnlyCause(this.messages));
|
initCause(Errors.getOnlyCause(this.messages));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProvisionException(String message, Throwable cause) {
|
public ProvisionException(String message, Throwable cause) {
|
||||||
|
@ -44,6 +44,6 @@ public final class ProvisionException extends RuntimeException {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getMessage() {
|
public String getMessage() {
|
||||||
return Messages.formatMessages("Unable to provision, see the following errors", messages);
|
return Errors.format("Unable to provision, see the following errors", messages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,120 +0,0 @@
|
||||||
package com.google.inject;
|
|
||||||
|
|
||||||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
|
|
||||||
import static java.lang.annotation.ElementType.TYPE;
|
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
|
||||||
import java.lang.annotation.Documented;
|
|
||||||
import java.lang.annotation.Inherited;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Annotation restricting the binding of the target type to permitted sources.
|
|
||||||
*
|
|
||||||
* <p>Bindings restricted by this annotation may only be created by sources annotated with a permit
|
|
||||||
* from {@link #permits} -- otherwise, an error message including the {@link #explanation} is
|
|
||||||
* issued.
|
|
||||||
*
|
|
||||||
* <p>There are two kinds of binding source:
|
|
||||||
*
|
|
||||||
* <ol>
|
|
||||||
* <li>Module: a module is the source of a binding if it creates it (either directly, or
|
|
||||||
* indirectly by installing another module). For example: if module A creates restricted
|
|
||||||
* binding X, and module C installs module B that installs A; then all 3 modules C,B,A are
|
|
||||||
* sources of X, and it's enough for any one of them to be annotated with a permit from X's
|
|
||||||
* restriction.
|
|
||||||
* <li>Method Scanner ({@code ModuleAnnotatedMethodScanner}): If a binding was created by a
|
|
||||||
* scanner, then that scanner is also a source of the binding (in addition to the module
|
|
||||||
* sources) and a permit may be given to the scanner by annotating its class.
|
|
||||||
* </ol>
|
|
||||||
*
|
|
||||||
* <p>Bindings with qualifier annotations are restricted solely by the annotation on their qualifier
|
|
||||||
* (restrictions on the type are ignored for qualified bindings). Unqualified bindings are
|
|
||||||
* restricted by the annotation on their type.
|
|
||||||
*
|
|
||||||
* <p>This allows libraries to prevent their clients from binding their keys, similar to how
|
|
||||||
* declaring a class final prevents subtyping. For example, a library may want to prevent users from
|
|
||||||
* creating mock bindings for tests, using the {@link #explanation} - included in the error message
|
|
||||||
* - to point them to a supported testing module.
|
|
||||||
*
|
|
||||||
* <p>Example usage:
|
|
||||||
*
|
|
||||||
* <pre>{@code
|
|
||||||
* @RestrictedBindingSource.Permit
|
|
||||||
* @Retention(RetentionPolicy.RUNTIME)
|
|
||||||
* @interface NetworkPermit {}
|
|
||||||
*
|
|
||||||
* @RestrictedBindingSource(
|
|
||||||
* explanation = "Only NetworkModule can create network bindings.",
|
|
||||||
* permits = {NetworkPermit.class})
|
|
||||||
* @Qualifier
|
|
||||||
* @Retention(RetentionPolicy.RUNTIME)
|
|
||||||
* public @interface GatewayIpAdress {}
|
|
||||||
*
|
|
||||||
* @NetworkPermit
|
|
||||||
* public final class NetworkModule extends AbstractModule {
|
|
||||||
* @Provides
|
|
||||||
* @GatewayIpAdress // Allowed because the module is annotated with @NetworkPermit.
|
|
||||||
* int provideGatewayIp() { ... }
|
|
||||||
* }
|
|
||||||
* }</pre>
|
|
||||||
*
|
|
||||||
* @author vzm@google.com (Vladimir Makaric)
|
|
||||||
* @since 5.0
|
|
||||||
*/
|
|
||||||
@Inherited
|
|
||||||
@Retention(RUNTIME)
|
|
||||||
@Target(TYPE)
|
|
||||||
public @interface RestrictedBindingSource {
|
|
||||||
/**
|
|
||||||
* Explanation of why binding this target type is restricted.
|
|
||||||
*
|
|
||||||
* <p>Will appear as the error message if the target type is bound by non-allowed modules.
|
|
||||||
*/
|
|
||||||
String explanation();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Meta-annotation indicating that the target annotation is a permit for binding restricted
|
|
||||||
* bindings. Annotating a binding source (defined in top-level javadoc) with a permit gives it
|
|
||||||
* permission to bind the restricted bindings guarded by the permit (see {@link #permits}).
|
|
||||||
*
|
|
||||||
* @since 5.0
|
|
||||||
*/
|
|
||||||
@Documented
|
|
||||||
@Retention(RUNTIME)
|
|
||||||
@Target(ANNOTATION_TYPE)
|
|
||||||
public @interface Permit {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* List of {@code Permit} annotations (must be non-empty), one of which has has to be present on a
|
|
||||||
* restricted binding's source (defined in top-level javadoc).
|
|
||||||
*/
|
|
||||||
Class<? extends Annotation>[] permits();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exempt modules whose fully qualified class names match this regex.
|
|
||||||
*
|
|
||||||
* <p>If any module on the binding's module stack matches this regex, the binding is allowed (no
|
|
||||||
* permit necessary). No module is exempt by default (empty string).
|
|
||||||
*
|
|
||||||
* <p>Inteded to be used when retrofitting a binding with this restriction. When restricting an
|
|
||||||
* existing binding, it's often practical to first restrict with exemptions for existing
|
|
||||||
* violations (to prevent new violations), before updating the code in violation to use the
|
|
||||||
* permitted module(s).
|
|
||||||
*/
|
|
||||||
String exemptModules() default "";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Level of restriction. Determines how violations are handled.
|
|
||||||
*
|
|
||||||
* @since 5.0
|
|
||||||
*/
|
|
||||||
public static enum RestrictionLevel {
|
|
||||||
WARNING,
|
|
||||||
ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
RestrictionLevel restrictionLevel() default RestrictionLevel.ERROR;
|
|
||||||
}
|
|
|
@ -28,7 +28,6 @@ public class Scopes {
|
||||||
* this to "no scope" in your binding.
|
* this to "no scope" in your binding.
|
||||||
*/
|
*/
|
||||||
public static final Scope NO_SCOPE = new Scope() {
|
public static final Scope NO_SCOPE = new Scope() {
|
||||||
@Override
|
|
||||||
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
|
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
|
||||||
return unscoped;
|
return unscoped;
|
||||||
}
|
}
|
||||||
|
@ -38,26 +37,21 @@ public class Scopes {
|
||||||
return "Scopes.NO_SCOPE";
|
return "Scopes.NO_SCOPE";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private static final BindingScopingVisitor<Boolean> IS_SINGLETON_VISITOR
|
private static final BindingScopingVisitor<Boolean> IS_SINGLETON_VISITOR
|
||||||
= new BindingScopingVisitor<>() {
|
= new BindingScopingVisitor<Boolean>() {
|
||||||
@Override
|
|
||||||
public Boolean visitNoScoping() {
|
public Boolean visitNoScoping() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) {
|
public Boolean visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) {
|
||||||
return scopeAnnotation == Singleton.class
|
return scopeAnnotation == Singleton.class
|
||||||
|| scopeAnnotation == javax.inject.Singleton.class;
|
|| scopeAnnotation == javax.inject.Singleton.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean visitScope(Scope scope) {
|
public Boolean visitScope(Scope scope) {
|
||||||
return scope == Scopes.SINGLETON;
|
return scope == Scopes.SINGLETON;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean visitEagerSingleton() {
|
public Boolean visitEagerSingleton() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -113,22 +107,18 @@ public class Scopes {
|
||||||
final Class<? extends Annotation> scopeAnnotation) {
|
final Class<? extends Annotation> scopeAnnotation) {
|
||||||
do {
|
do {
|
||||||
boolean matches = binding.acceptScopingVisitor(new BindingScopingVisitor<Boolean>() {
|
boolean matches = binding.acceptScopingVisitor(new BindingScopingVisitor<Boolean>() {
|
||||||
@Override
|
|
||||||
public Boolean visitNoScoping() {
|
public Boolean visitNoScoping() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean visitScopeAnnotation(Class<? extends Annotation> visitedAnnotation) {
|
public Boolean visitScopeAnnotation(Class<? extends Annotation> visitedAnnotation) {
|
||||||
return visitedAnnotation == scopeAnnotation;
|
return visitedAnnotation == scopeAnnotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean visitScope(Scope visitedScope) {
|
public Boolean visitScope(Scope visitedScope) {
|
||||||
return visitedScope == scope;
|
return visitedScope == scope;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Boolean visitEagerSingleton() {
|
public Boolean visitEagerSingleton() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -170,7 +170,7 @@ public class TypeLiteral<T> {
|
||||||
// this implementation is made a little more complicated in an attempt to avoid object-creation
|
// this implementation is made a little more complicated in an attempt to avoid object-creation
|
||||||
while (true) {
|
while (true) {
|
||||||
if (toResolve instanceof TypeVariable) {
|
if (toResolve instanceof TypeVariable) {
|
||||||
TypeVariable<?> original = (TypeVariable) toResolve;
|
TypeVariable original = (TypeVariable) toResolve;
|
||||||
toResolve = MoreTypes.resolveTypeVariable(type, rawType, original);
|
toResolve = MoreTypes.resolveTypeVariable(type, rawType, original);
|
||||||
if (toResolve == original) {
|
if (toResolve == original) {
|
||||||
return toResolve;
|
return toResolve;
|
||||||
|
|
|
@ -5,15 +5,14 @@ import com.google.inject.Key;
|
||||||
import com.google.inject.Module;
|
import com.google.inject.Module;
|
||||||
import com.google.inject.Provider;
|
import com.google.inject.Provider;
|
||||||
import com.google.inject.TypeLiteral;
|
import com.google.inject.TypeLiteral;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.invoke.MethodHandles;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides a factory that combines the caller's arguments with injector-supplied values to
|
* Provides a factory that combines the caller's arguments with injector-supplied values to
|
||||||
* construct objects.
|
* construct objects.
|
||||||
*
|
*
|
||||||
* <h3>Defining a factory</h3>
|
* <h3>Defining a factory</h3>
|
||||||
*
|
|
||||||
* Create an interface whose methods return the constructed type, or any of its supertypes. The
|
* Create an interface whose methods return the constructed type, or any of its supertypes. The
|
||||||
* method's parameters are the arguments required to build the constructed type.
|
* method's parameters are the arguments required to build the constructed type.
|
||||||
*
|
*
|
||||||
|
@ -25,7 +24,6 @@ import java.lang.invoke.MethodHandles;
|
||||||
* or <i>newPayment</i>.
|
* or <i>newPayment</i>.
|
||||||
*
|
*
|
||||||
* <h3>Creating a type that accepts factory parameters</h3>
|
* <h3>Creating a type that accepts factory parameters</h3>
|
||||||
*
|
|
||||||
* {@code constructedType} is a concrete class with an {@literal @}{@link com.google.inject.Inject
|
* {@code constructedType} is a concrete class with an {@literal @}{@link com.google.inject.Inject
|
||||||
* Inject}-annotated constructor. In addition to injector-supplied parameters, the constructor
|
* Inject}-annotated constructor. In addition to injector-supplied parameters, the constructor
|
||||||
* should have parameters that match each of the factory method's parameters. Each factory-supplied
|
* should have parameters that match each of the factory method's parameters. Each factory-supplied
|
||||||
|
@ -44,10 +42,10 @@ import java.lang.invoke.MethodHandles;
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
*
|
||||||
* <h3>Multiple factory methods for the same type</h3>
|
* <h3>Multiple factory methods for the same type</h3>
|
||||||
*
|
|
||||||
* If the factory contains many methods that return the same type, you can create multiple
|
* If the factory contains many methods that return the same type, you can create multiple
|
||||||
* constructors in your concrete class, each constructor marked with with {@literal @}{@link
|
* constructors in your concrete class, each constructor marked with with
|
||||||
* AssistedInject}, in order to match the different parameters types of the factory methods.
|
* {@literal @}{@link AssistedInject}, in order to match the different parameters types of the
|
||||||
|
* factory methods.
|
||||||
*
|
*
|
||||||
* <pre>public interface PaymentFactory {
|
* <pre>public interface PaymentFactory {
|
||||||
* Payment create(Date startDate, Money amount);
|
* Payment create(Date startDate, Money amount);
|
||||||
|
@ -74,8 +72,8 @@ import java.lang.invoke.MethodHandles;
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
*
|
||||||
* <h3>Configuring simple factories</h3>
|
* <h3>Configuring simple factories</h3>
|
||||||
*
|
* In your {@link Module module}, install a {@code FactoryModuleBuilder} that creates the
|
||||||
* In your {@link Module module}, install a {@code FactoryModuleBuilder} that creates the factory:
|
* factory:
|
||||||
*
|
*
|
||||||
* <pre>install(new FactoryModuleBuilder()
|
* <pre>install(new FactoryModuleBuilder()
|
||||||
* .implement(Payment.class, RealPayment.class)
|
* .implement(Payment.class, RealPayment.class)
|
||||||
|
@ -85,9 +83,8 @@ import java.lang.invoke.MethodHandles;
|
||||||
* factory cannot be used until the injector has been initialized.
|
* factory cannot be used until the injector has been initialized.
|
||||||
*
|
*
|
||||||
* <h3>Configuring complex factories</h3>
|
* <h3>Configuring complex factories</h3>
|
||||||
*
|
* Factories can create an arbitrary number of objects, one per each method. Each factory
|
||||||
* Factories can create an arbitrary number of objects, one per each method. Each factory method can
|
* method can be configured using <code>.implement</code>.
|
||||||
* be configured using <code>.implement</code>.
|
|
||||||
*
|
*
|
||||||
* <pre>public interface OrderFactory {
|
* <pre>public interface OrderFactory {
|
||||||
* Payment create(Date startDate, Money amount);
|
* Payment create(Date startDate, Money amount);
|
||||||
|
@ -103,222 +100,221 @@ import java.lang.invoke.MethodHandles;
|
||||||
* // will be 'Shipment' itself, which is legal if it's not an interface.
|
* // will be 'Shipment' itself, which is legal if it's not an interface.
|
||||||
* .implement(Receipt.class, RealReceipt.class)
|
* .implement(Receipt.class, RealReceipt.class)
|
||||||
* .build(OrderFactory.class));</pre>
|
* .build(OrderFactory.class));</pre>
|
||||||
*
|
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* <h3>Using the factory</h3>
|
* <h3>Using the factory</h3>
|
||||||
*
|
* Inject your factory into your application classes. When you use the factory, your arguments
|
||||||
* Inject your factory into your application classes. When you use the factory, your arguments will
|
* will be combined with values from the injector to construct an instance.
|
||||||
* be combined with values from the injector to construct an instance.
|
|
||||||
*
|
*
|
||||||
* <pre>public class PaymentAction {
|
* <pre>public class PaymentAction {
|
||||||
* {@literal @}Inject private PaymentFactory paymentFactory;
|
* {@literal @}Inject private PaymentFactory paymentFactory;
|
||||||
*
|
*
|
||||||
* public void doPayment(Money amount) {
|
* public void doPayment(Money amount) {
|
||||||
* Payment payment = paymentFactory.create(new Date(), amount);
|
* Payment payment = paymentFactory.create(new Date(), amount);
|
||||||
* payment.apply();
|
* payment.apply();
|
||||||
* }
|
* }
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
*
|
||||||
* <h3>Making parameter types distinct</h3>
|
* <h3>Making parameter types distinct</h3>
|
||||||
*
|
* The types of the factory method's parameters must be distinct. To use multiple parameters of
|
||||||
* The types of the factory method's parameters must be distinct. To use multiple parameters of the
|
* the same type, use a named {@literal @}{@link Assisted} annotation to disambiguate the
|
||||||
* same type, use a named {@literal @}{@link Assisted} annotation to disambiguate the parameters.
|
* parameters. The names must be applied to the factory method's parameters:
|
||||||
* The names must be applied to the factory method's parameters:
|
|
||||||
*
|
*
|
||||||
* <pre>public interface PaymentFactory {
|
* <pre>public interface PaymentFactory {
|
||||||
* Payment create(
|
* Payment create(
|
||||||
* <strong>{@literal @}Assisted("startDate")</strong> Date startDate,
|
* <strong>{@literal @}Assisted("startDate")</strong> Date startDate,
|
||||||
* <strong>{@literal @}Assisted("dueDate")</strong> Date dueDate,
|
* <strong>{@literal @}Assisted("dueDate")</strong> Date dueDate,
|
||||||
* Money amount);
|
* Money amount);
|
||||||
* } </pre>
|
* } </pre>
|
||||||
*
|
*
|
||||||
* ...and to the concrete type's constructor parameters:
|
* ...and to the concrete type's constructor parameters:
|
||||||
*
|
*
|
||||||
* <pre>public class RealPayment implements Payment {
|
* <pre>public class RealPayment implements Payment {
|
||||||
* {@literal @}Inject
|
* {@literal @}Inject
|
||||||
* public RealPayment(
|
* public RealPayment(
|
||||||
* CreditService creditService,
|
* CreditService creditService,
|
||||||
* AuthService authService,
|
* AuthService authService,
|
||||||
* <strong>{@literal @}Assisted("startDate")</strong> Date startDate,
|
* <strong>{@literal @}Assisted("startDate")</strong> Date startDate,
|
||||||
* <strong>{@literal @}Assisted("dueDate")</strong> Date dueDate,
|
* <strong>{@literal @}Assisted("dueDate")</strong> Date dueDate,
|
||||||
* <strong>{@literal @}Assisted</strong> Money amount) {
|
* <strong>{@literal @}Assisted</strong> Money amount) {
|
||||||
* ...
|
* ...
|
||||||
* }
|
* }
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
*
|
||||||
* <h3>Values are created by Guice</h3>
|
* <h3>Values are created by Guice</h3>
|
||||||
*
|
|
||||||
* Returned factories use child injectors to create values. The values are eligible for method
|
* Returned factories use child injectors to create values. The values are eligible for method
|
||||||
* interception. In addition, {@literal @}{@literal Inject} members will be injected before they are
|
* interception. In addition, {@literal @}{@literal Inject} members will be injected before they are
|
||||||
* returned.
|
* returned.
|
||||||
*
|
*
|
||||||
* <h3>More configuration options</h3>
|
* <h3>More configuration options</h3>
|
||||||
*
|
|
||||||
* In addition to simply specifying an implementation class for any returned type, factories' return
|
* In addition to simply specifying an implementation class for any returned type, factories' return
|
||||||
* values can be automatic or can be configured to use annotations:
|
* values can be automatic or can be configured to use annotations:
|
||||||
*
|
* <p/>
|
||||||
* <p>If you just want to return the types specified in the factory, do not configure any
|
* If you just want to return the types specified in the factory, do not configure any
|
||||||
* implementations:
|
* implementations:
|
||||||
*
|
*
|
||||||
* <pre>public interface FruitFactory {
|
* <pre>public interface FruitFactory {
|
||||||
* Apple getApple(Color color);
|
* Apple getApple(Color color);
|
||||||
* }
|
* }
|
||||||
* ...
|
* ...
|
||||||
* protected void configure() {
|
* protected void configure() {
|
||||||
* install(new FactoryModuleBuilder().build(FruitFactory.class));
|
* install(new FactoryModuleBuilder().build(FruitFactory.class));
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
*
|
||||||
* Note that any type returned by the factory in this manner needs to be an implementation class.
|
* Note that any type returned by the factory in this manner needs to be an implementation class.
|
||||||
*
|
* <p/>
|
||||||
* <p>To return two different implementations for the same interface from your factory, use binding
|
* To return two different implementations for the same interface from your factory, use binding
|
||||||
* annotations on your return types:
|
* annotations on your return types:
|
||||||
*
|
*
|
||||||
* <pre>interface CarFactory {
|
* <pre>interface CarFactory {
|
||||||
* {@literal @}Named("fast") Car getFastCar(Color color);
|
* {@literal @}Named("fast") Car getFastCar(Color color);
|
||||||
* {@literal @}Named("clean") Car getCleanCar(Color color);
|
* {@literal @}Named("clean") Car getCleanCar(Color color);
|
||||||
* }
|
* }
|
||||||
* ...
|
* ...
|
||||||
* protected void configure() {
|
* protected void configure() {
|
||||||
* install(new FactoryModuleBuilder()
|
* install(new FactoryModuleBuilder()
|
||||||
* .implement(Car.class, Names.named("fast"), Porsche.class)
|
* .implement(Car.class, Names.named("fast"), Porsche.class)
|
||||||
* .implement(Car.class, Names.named("clean"), Prius.class)
|
* .implement(Car.class, Names.named("clean"), Prius.class)
|
||||||
* .build(CarFactory.class));
|
* .build(CarFactory.class));
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
*
|
||||||
* <h3>Implementation limitations</h3>
|
* <h3>Implementation limitations</h3>
|
||||||
*
|
* As a limitation of the implementation, it is prohibited to declare a factory method that
|
||||||
* As a limitation of the implementation, it is prohibited to declare a factory method that accepts
|
* accepts a {@code Provider} as one of its arguments.
|
||||||
* a {@code Provider} as one of its arguments.
|
|
||||||
*
|
|
||||||
* @since 3.0
|
|
||||||
* @author schmitt@google.com (Peter Schmitt)
|
|
||||||
*/
|
*/
|
||||||
public final class FactoryModuleBuilder {
|
public final class FactoryModuleBuilder {
|
||||||
|
|
||||||
private final BindingCollector bindings = new BindingCollector();
|
private final BindingCollector bindings = new BindingCollector();
|
||||||
private MethodHandles.Lookup lookups;
|
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
|
*/
|
||||||
public <T> FactoryModuleBuilder implement(Class<T> source, Class<? extends T> target) {
|
public <T> FactoryModuleBuilder implement(Class<T> source, Class<? extends T> target) {
|
||||||
return implement(source, TypeLiteral.get(target));
|
return implement(source, TypeLiteral.get(target));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
|
*/
|
||||||
public <T> FactoryModuleBuilder implement(Class<T> source, TypeLiteral<? extends T> target) {
|
public <T> FactoryModuleBuilder implement(Class<T> source, TypeLiteral<? extends T> target) {
|
||||||
return implement(TypeLiteral.get(source), target);
|
return implement(TypeLiteral.get(source), target);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
|
*/
|
||||||
public <T> FactoryModuleBuilder implement(TypeLiteral<T> source, Class<? extends T> target) {
|
public <T> FactoryModuleBuilder implement(TypeLiteral<T> source, Class<? extends T> target) {
|
||||||
return implement(source, TypeLiteral.get(target));
|
return implement(source, TypeLiteral.get(target));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
public <T> FactoryModuleBuilder implement(
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
TypeLiteral<T> source, TypeLiteral<? extends T> target) {
|
*/
|
||||||
|
public <T> FactoryModuleBuilder implement(TypeLiteral<T> source,
|
||||||
|
TypeLiteral<? extends T> target) {
|
||||||
return implement(Key.get(source), target);
|
return implement(Key.get(source), target);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
public <T> FactoryModuleBuilder implement(
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
Class<T> source, Annotation annotation, Class<? extends T> target) {
|
*/
|
||||||
|
public <T> FactoryModuleBuilder implement(Class<T> source, Annotation annotation,
|
||||||
|
Class<? extends T> target) {
|
||||||
return implement(source, annotation, TypeLiteral.get(target));
|
return implement(source, annotation, TypeLiteral.get(target));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
public <T> FactoryModuleBuilder implement(
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
Class<T> source, Annotation annotation, TypeLiteral<? extends T> target) {
|
*/
|
||||||
|
public <T> FactoryModuleBuilder implement(Class<T> source, Annotation annotation,
|
||||||
|
TypeLiteral<? extends T> target) {
|
||||||
return implement(TypeLiteral.get(source), annotation, target);
|
return implement(TypeLiteral.get(source), annotation, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
public <T> FactoryModuleBuilder implement(
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
TypeLiteral<T> source, Annotation annotation, Class<? extends T> target) {
|
*/
|
||||||
|
public <T> FactoryModuleBuilder implement(TypeLiteral<T> source, Annotation annotation,
|
||||||
|
Class<? extends T> target) {
|
||||||
return implement(source, annotation, TypeLiteral.get(target));
|
return implement(source, annotation, TypeLiteral.get(target));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
public <T> FactoryModuleBuilder implement(
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
TypeLiteral<T> source, Annotation annotation, TypeLiteral<? extends T> target) {
|
*/
|
||||||
|
public <T> FactoryModuleBuilder implement(TypeLiteral<T> source, Annotation annotation,
|
||||||
|
TypeLiteral<? extends T> target) {
|
||||||
return implement(Key.get(source, annotation), target);
|
return implement(Key.get(source, annotation), target);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
public <T> FactoryModuleBuilder implement(
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
Class<T> source, Class<? extends Annotation> annotationType, Class<? extends T> target) {
|
*/
|
||||||
|
public <T> FactoryModuleBuilder implement(Class<T> source,
|
||||||
|
Class<? extends Annotation> annotationType, Class<? extends T> target) {
|
||||||
return implement(source, annotationType, TypeLiteral.get(target));
|
return implement(source, annotationType, TypeLiteral.get(target));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
public <T> FactoryModuleBuilder implement(
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
Class<T> source,
|
*/
|
||||||
Class<? extends Annotation> annotationType,
|
public <T> FactoryModuleBuilder implement(Class<T> source,
|
||||||
TypeLiteral<? extends T> target) {
|
Class<? extends Annotation> annotationType, TypeLiteral<? extends T> target) {
|
||||||
return implement(TypeLiteral.get(source), annotationType, target);
|
return implement(TypeLiteral.get(source), annotationType, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
public <T> FactoryModuleBuilder implement(
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
TypeLiteral<T> source,
|
*/
|
||||||
Class<? extends Annotation> annotationType,
|
public <T> FactoryModuleBuilder implement(TypeLiteral<T> source,
|
||||||
Class<? extends T> target) {
|
Class<? extends Annotation> annotationType, Class<? extends T> target) {
|
||||||
return implement(source, annotationType, TypeLiteral.get(target));
|
return implement(source, annotationType, TypeLiteral.get(target));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
public <T> FactoryModuleBuilder implement(
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
TypeLiteral<T> source,
|
*/
|
||||||
Class<? extends Annotation> annotationType,
|
public <T> FactoryModuleBuilder implement(TypeLiteral<T> source,
|
||||||
TypeLiteral<? extends T> target) {
|
Class<? extends Annotation> annotationType, TypeLiteral<? extends T> target) {
|
||||||
return implement(Key.get(source, annotationType), target);
|
return implement(Key.get(source, annotationType), target);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
|
*/
|
||||||
public <T> FactoryModuleBuilder implement(Key<T> source, Class<? extends T> target) {
|
public <T> FactoryModuleBuilder implement(Key<T> source, Class<? extends T> target) {
|
||||||
return implement(source, TypeLiteral.get(target));
|
return implement(source, TypeLiteral.get(target));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
|
*/
|
||||||
public <T> FactoryModuleBuilder implement(Key<T> source, TypeLiteral<? extends T> target) {
|
public <T> FactoryModuleBuilder implement(Key<T> source, TypeLiteral<? extends T> target) {
|
||||||
bindings.addBinding(source, target);
|
bindings.addBinding(source, target);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Typically called via {@code withLookups(MethodHandles.lookup())}. Sets the MethodHandles.Lookup
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
* that the factory implementation will use to call default methods on the factory interface.
|
|
||||||
* While this is not always required, it is always OK to set it. It is required if the factory
|
|
||||||
* passed to {@link #build} is non-public and javac generated default methods while compiling it
|
|
||||||
* (which javac can sometimes do if the factory uses generic types).
|
|
||||||
*
|
|
||||||
* <p>Guice will try to work properly even if this method is not called (or called with a lookups
|
|
||||||
* that doesn't have access to the factory), but doing so requires reflection into the JDK, which
|
|
||||||
* may break at any time (and trigger unsafe access warnings).
|
|
||||||
*
|
|
||||||
* @since 5.0
|
|
||||||
*/
|
*/
|
||||||
public <T> FactoryModuleBuilder withLookups(MethodHandles.Lookup lookups) {
|
|
||||||
this.lookups = lookups;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
|
||||||
public <F> Module build(Class<F> factoryInterface) {
|
public <F> Module build(Class<F> factoryInterface) {
|
||||||
return build(TypeLiteral.get(factoryInterface));
|
return build(TypeLiteral.get(factoryInterface));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** See the factory configuration examples at {@link FactoryModuleBuilder}. */
|
/**
|
||||||
|
* See the factory configuration examples at {@link FactoryModuleBuilder}.
|
||||||
|
*/
|
||||||
public <F> Module build(TypeLiteral<F> factoryInterface) {
|
public <F> Module build(TypeLiteral<F> factoryInterface) {
|
||||||
return build(Key.get(factoryInterface));
|
return build(Key.get(factoryInterface));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public <F> Module build(final Key<F> factoryInterface) {
|
public <F> Module build(final Key<F> factoryInterface) {
|
||||||
return new AbstractModule() {
|
return new AbstractModule() {
|
||||||
@Override
|
@Override
|
||||||
protected void configure() {
|
protected void configure() {
|
||||||
Provider<F> provider = new FactoryProvider2<>(factoryInterface, bindings, lookups);
|
Provider<F> provider = new FactoryProvider2<F>(factoryInterface, bindings);
|
||||||
binder().skipSources(this.getClass()).bind(factoryInterface).toProvider(provider);
|
bind(factoryInterface).toProvider(provider);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package com.google.inject.assistedinject;
|
package com.google.inject.assistedinject;
|
||||||
|
|
||||||
import static com.google.inject.internal.Annotations.getKey;
|
|
||||||
|
|
||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
@ -18,6 +16,7 @@ import com.google.inject.internal.ErrorsException;
|
||||||
import com.google.inject.spi.Dependency;
|
import com.google.inject.spi.Dependency;
|
||||||
import com.google.inject.spi.HasDependencies;
|
import com.google.inject.spi.HasDependencies;
|
||||||
import com.google.inject.spi.Message;
|
import com.google.inject.spi.Message;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.InvocationHandler;
|
import java.lang.reflect.InvocationHandler;
|
||||||
|
@ -28,6 +27,8 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static com.google.inject.internal.Annotations.getKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <strong>Obsolete.</strong> Prefer {@link FactoryModuleBuilder} for its more concise API and
|
* <strong>Obsolete.</strong> Prefer {@link FactoryModuleBuilder} for its more concise API and
|
||||||
* additional capability.
|
* additional capability.
|
||||||
|
@ -36,25 +37,20 @@ import java.util.Set;
|
||||||
* construct objects.
|
* construct objects.
|
||||||
*
|
*
|
||||||
* <h3>Defining a factory</h3>
|
* <h3>Defining a factory</h3>
|
||||||
*
|
|
||||||
* Create an interface whose methods return the constructed type, or any of its supertypes. The
|
* Create an interface whose methods return the constructed type, or any of its supertypes. The
|
||||||
* method's parameters are the arguments required to build the constructed type.
|
* method's parameters are the arguments required to build the constructed type.
|
||||||
*
|
|
||||||
* <pre>public interface PaymentFactory {
|
* <pre>public interface PaymentFactory {
|
||||||
* Payment create(Date startDate, Money amount);
|
* Payment create(Date startDate, Money amount);
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
|
||||||
* You can name your factory methods whatever you like, such as <i>create</i>, <i>createPayment</i>
|
* You can name your factory methods whatever you like, such as <i>create</i>, <i>createPayment</i>
|
||||||
* or <i>newPayment</i>.
|
* or <i>newPayment</i>.
|
||||||
*
|
*
|
||||||
* <h3>Creating a type that accepts factory parameters</h3>
|
* <h3>Creating a type that accepts factory parameters</h3>
|
||||||
*
|
|
||||||
* {@code constructedType} is a concrete class with an {@literal @}{@link Inject}-annotated
|
* {@code constructedType} is a concrete class with an {@literal @}{@link Inject}-annotated
|
||||||
* constructor. In addition to injector-supplied parameters, the constructor should have parameters
|
* constructor. In addition to injector-supplied parameters, the constructor should have
|
||||||
* that match each of the factory method's parameters. Each factory-supplied parameter requires an
|
* parameters that match each of the factory method's parameters. Each factory-supplied parameter
|
||||||
* {@literal @}{@link Assisted} annotation. This serves to document that the parameter is not bound
|
* requires an {@literal @}{@link Assisted} annotation. This serves to document that the parameter
|
||||||
* by your application's modules.
|
* is not bound by your application's modules.
|
||||||
*
|
|
||||||
* <pre>public class RealPayment implements Payment {
|
* <pre>public class RealPayment implements Payment {
|
||||||
* {@literal @}Inject
|
* {@literal @}Inject
|
||||||
* public RealPayment(
|
* public RealPayment(
|
||||||
|
@ -65,25 +61,19 @@ import java.util.Set;
|
||||||
* ...
|
* ...
|
||||||
* }
|
* }
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
|
||||||
* Any parameter that permits a null value should also be annotated {@code @Nullable}.
|
* Any parameter that permits a null value should also be annotated {@code @Nullable}.
|
||||||
*
|
*
|
||||||
* <h3>Configuring factories</h3>
|
* <h3>Configuring factories</h3>
|
||||||
*
|
|
||||||
* In your {@link com.google.inject.Module module}, bind the factory interface to the returned
|
* In your {@link com.google.inject.Module module}, bind the factory interface to the returned
|
||||||
* factory:
|
* factory:
|
||||||
*
|
|
||||||
* <pre>bind(PaymentFactory.class).toProvider(
|
* <pre>bind(PaymentFactory.class).toProvider(
|
||||||
* FactoryProvider.newFactory(PaymentFactory.class, RealPayment.class));</pre>
|
* FactoryProvider.newFactory(PaymentFactory.class, RealPayment.class));</pre>
|
||||||
*
|
|
||||||
* As a side-effect of this binding, Guice will inject the factory to initialize it for use. The
|
* As a side-effect of this binding, Guice will inject the factory to initialize it for use. The
|
||||||
* factory cannot be used until the injector has been initialized.
|
* factory cannot be used until the injector has been initialized.
|
||||||
*
|
*
|
||||||
* <h3>Using the factory</h3>
|
* <h3>Using the factory</h3>
|
||||||
*
|
* Inject your factory into your application classes. When you use the factory, your arguments
|
||||||
* Inject your factory into your application classes. When you use the factory, your arguments will
|
* will be combined with values from the injector to construct an instance.
|
||||||
* be combined with values from the injector to construct an instance.
|
|
||||||
*
|
|
||||||
* <pre>public class PaymentAction {
|
* <pre>public class PaymentAction {
|
||||||
* {@literal @}Inject private PaymentFactory paymentFactory;
|
* {@literal @}Inject private PaymentFactory paymentFactory;
|
||||||
*
|
*
|
||||||
|
@ -94,10 +84,9 @@ import java.util.Set;
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
*
|
||||||
* <h3>Making parameter types distinct</h3>
|
* <h3>Making parameter types distinct</h3>
|
||||||
*
|
* The types of the factory method's parameters must be distinct. To use multiple parameters of
|
||||||
* The types of the factory method's parameters must be distinct. To use multiple parameters of the
|
* the same type, use a named {@literal @}{@link Assisted} annotation to disambiguate the
|
||||||
* same type, use a named {@literal @}{@link Assisted} annotation to disambiguate the parameters.
|
* parameters. The names must be applied to the factory method's parameters:
|
||||||
* The names must be applied to the factory method's parameters:
|
|
||||||
*
|
*
|
||||||
* <pre>public interface PaymentFactory {
|
* <pre>public interface PaymentFactory {
|
||||||
* Payment create(
|
* Payment create(
|
||||||
|
@ -105,9 +94,7 @@ import java.util.Set;
|
||||||
* <strong>{@literal @}Assisted("dueDate")</strong> Date dueDate,
|
* <strong>{@literal @}Assisted("dueDate")</strong> Date dueDate,
|
||||||
* Money amount);
|
* Money amount);
|
||||||
* } </pre>
|
* } </pre>
|
||||||
*
|
|
||||||
* ...and to the concrete type's constructor parameters:
|
* ...and to the concrete type's constructor parameters:
|
||||||
*
|
|
||||||
* <pre>public class RealPayment implements Payment {
|
* <pre>public class RealPayment implements Payment {
|
||||||
* {@literal @}Inject
|
* {@literal @}Inject
|
||||||
* public RealPayment(
|
* public RealPayment(
|
||||||
|
@ -121,42 +108,46 @@ import java.util.Set;
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
*
|
||||||
* <h3>Values are created by Guice</h3>
|
* <h3>Values are created by Guice</h3>
|
||||||
*
|
|
||||||
* Returned factories use child injectors to create values. The values are eligible for method
|
* Returned factories use child injectors to create values. The values are eligible for method
|
||||||
* interception. In addition, {@literal @}{@literal Inject} members will be injected before they are
|
* interception. In addition, {@literal @}{@literal Inject} members will be injected before they are
|
||||||
* returned.
|
* returned.
|
||||||
*
|
*
|
||||||
* <h3>Backwards compatibility using {@literal @}AssistedInject</h3>
|
* <h3>Backwards compatibility using {@literal @}AssistedInject</h3>
|
||||||
*
|
|
||||||
* Instead of the {@literal @}Inject annotation, you may annotate the constructed classes with
|
* Instead of the {@literal @}Inject annotation, you may annotate the constructed classes with
|
||||||
* {@literal @}{@link AssistedInject}. This triggers a limited backwards-compatability mode.
|
* {@literal @}{@link AssistedInject}. This triggers a limited backwards-compatability mode.
|
||||||
*
|
*
|
||||||
* <p>Instead of matching factory method arguments to constructor parameters using their names, the
|
* <p>Instead of matching factory method arguments to constructor parameters using their names, the
|
||||||
* <strong>parameters are matched by their order</strong>. The first factory method argument is used
|
* <strong>parameters are matched by their order</strong>. The first factory method argument is
|
||||||
* for the first {@literal @}Assisted constructor parameter, etc.. Annotation names have no effect.
|
* used for the first {@literal @}Assisted constructor parameter, etc.. Annotation names have no
|
||||||
|
* effect.
|
||||||
*
|
*
|
||||||
* <p>Returned values are <strong>not created by Guice</strong>. These types are not eligible for
|
* <p>Returned values are <strong>not created by Guice</strong>. These types are not eligible for
|
||||||
* method interception. They do receive post-construction member injection.
|
* method interception. They do receive post-construction member injection.
|
||||||
*
|
*
|
||||||
* @param <F> The factory interface
|
* @param <F> The factory interface
|
||||||
* @author jmourits@google.com (Jerome Mourits)
|
|
||||||
* @author jessewilson@google.com (Jesse Wilson)
|
|
||||||
* @author dtm@google.com (Daniel Martin)
|
|
||||||
* @deprecated use {@link FactoryModuleBuilder} instead.
|
* @deprecated use {@link FactoryModuleBuilder} instead.
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public class FactoryProvider<F> implements Provider<F>, HasDependencies {
|
public class FactoryProvider<F> implements Provider<F>, HasDependencies {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This class implements the old @AssistedInject implementation that manually matches constructors
|
* This class implements the old @AssistedInject implementation that manually matches constructors
|
||||||
* to factory methods. The new child injector implementation lives in FactoryProvider2.
|
* to factory methods. The new child injector implementation lives in FactoryProvider2.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
private Injector injector;
|
|
||||||
|
|
||||||
private final TypeLiteral<F> factoryType;
|
private final TypeLiteral<F> factoryType;
|
||||||
private final TypeLiteral<?> implementationType;
|
private final TypeLiteral<?> implementationType;
|
||||||
private final Map<Method, AssistedConstructor<?>> factoryMethodToConstructor;
|
private final Map<Method, AssistedConstructor<?>> factoryMethodToConstructor;
|
||||||
|
private Injector injector;
|
||||||
|
|
||||||
|
private FactoryProvider(TypeLiteral<F> factoryType,
|
||||||
|
TypeLiteral<?> implementationType,
|
||||||
|
Map<Method, AssistedConstructor<?>> factoryMethodToConstructor) {
|
||||||
|
this.factoryType = factoryType;
|
||||||
|
this.implementationType = implementationType;
|
||||||
|
this.factoryMethodToConstructor = factoryMethodToConstructor;
|
||||||
|
checkDeclaredExceptionsMatch();
|
||||||
|
}
|
||||||
|
|
||||||
public static <F> Provider<F> newFactory(Class<F> factoryType, Class<?> implementationType) {
|
public static <F> Provider<F> newFactory(Class<F> factoryType, Class<?> implementationType) {
|
||||||
return newFactory(TypeLiteral.get(factoryType), TypeLiteral.get(implementationType));
|
return newFactory(TypeLiteral.get(factoryType), TypeLiteral.get(implementationType));
|
||||||
|
@ -164,8 +155,8 @@ public class FactoryProvider<F> implements Provider<F>, HasDependencies {
|
||||||
|
|
||||||
public static <F> Provider<F> newFactory(
|
public static <F> Provider<F> newFactory(
|
||||||
TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) {
|
TypeLiteral<F> factoryType, TypeLiteral<?> implementationType) {
|
||||||
Map<Method, AssistedConstructor<?>> factoryMethodToConstructor =
|
Map<Method, AssistedConstructor<?>> factoryMethodToConstructor
|
||||||
createMethodMapping(factoryType, implementationType);
|
= createMethodMapping(factoryType, implementationType);
|
||||||
|
|
||||||
if (!factoryMethodToConstructor.isEmpty()) {
|
if (!factoryMethodToConstructor.isEmpty()) {
|
||||||
return new FactoryProvider<F>(factoryType, implementationType, factoryMethodToConstructor);
|
return new FactoryProvider<F>(factoryType, implementationType, factoryMethodToConstructor);
|
||||||
|
@ -179,8 +170,8 @@ public class FactoryProvider<F> implements Provider<F>, HasDependencies {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (Method method : factoryType.getRawType().getMethods()) {
|
for (Method method : factoryType.getRawType().getMethods()) {
|
||||||
Key<?> returnType =
|
Key<?> returnType = getKey(factoryType.getReturnType(method), method,
|
||||||
getKey(factoryType.getReturnType(method), method, method.getAnnotations(), errors);
|
method.getAnnotations(), errors);
|
||||||
if (!implementationKey.equals(returnType)) {
|
if (!implementationKey.equals(returnType)) {
|
||||||
collector.addBinding(returnType, implementationType);
|
collector.addBinding(returnType, implementationType);
|
||||||
}
|
}
|
||||||
|
@ -189,18 +180,83 @@ public class FactoryProvider<F> implements Provider<F>, HasDependencies {
|
||||||
throw new ConfigurationException(e.getErrors().getMessages());
|
throw new ConfigurationException(e.getErrors().getMessages());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new FactoryProvider2<F>(Key.get(factoryType), collector, /* userLookups= */ null);
|
return new FactoryProvider2<F>(Key.get(factoryType), collector);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private FactoryProvider(
|
private static Map<Method, AssistedConstructor<?>> createMethodMapping(
|
||||||
TypeLiteral<F> factoryType,
|
TypeLiteral<?> factoryType, TypeLiteral<?> implementationType) {
|
||||||
TypeLiteral<?> implementationType,
|
List<AssistedConstructor<?>> constructors = Lists.newArrayList();
|
||||||
Map<Method, AssistedConstructor<?>> factoryMethodToConstructor) {
|
|
||||||
this.factoryType = factoryType;
|
for (Constructor<?> constructor : implementationType.getRawType().getDeclaredConstructors()) {
|
||||||
this.implementationType = implementationType;
|
if (constructor.isAnnotationPresent(AssistedInject.class)) {
|
||||||
this.factoryMethodToConstructor = factoryMethodToConstructor;
|
AssistedConstructor<?> assistedConstructor = AssistedConstructor.create(
|
||||||
checkDeclaredExceptionsMatch();
|
constructor, implementationType.getParameterTypes(constructor));
|
||||||
|
constructors.add(assistedConstructor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (constructors.isEmpty()) {
|
||||||
|
return ImmutableMap.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
Method[] factoryMethods = factoryType.getRawType().getMethods();
|
||||||
|
|
||||||
|
if (constructors.size() != factoryMethods.length) {
|
||||||
|
throw newConfigurationException("Constructor mismatch: %s has %s @AssistedInject "
|
||||||
|
+ "constructors, factory %s has %s creation methods", implementationType,
|
||||||
|
constructors.size(), factoryType, factoryMethods.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<ParameterListKey, AssistedConstructor<?>> paramsToConstructor = Maps.newHashMap();
|
||||||
|
|
||||||
|
for (AssistedConstructor<?> c : constructors) {
|
||||||
|
if (paramsToConstructor.containsKey(c.getAssistedParameters())) {
|
||||||
|
throw new RuntimeException("Duplicate constructor, " + c);
|
||||||
|
}
|
||||||
|
paramsToConstructor.put(c.getAssistedParameters(), c);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<Method, AssistedConstructor<?>> result = Maps.newHashMap();
|
||||||
|
for (Method method : factoryMethods) {
|
||||||
|
if (!method.getReturnType().isAssignableFrom(implementationType.getRawType())) {
|
||||||
|
throw newConfigurationException("Return type of method %s is not assignable from %s",
|
||||||
|
method, implementationType);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Type> parameterTypes = Lists.newArrayList();
|
||||||
|
for (TypeLiteral<?> parameterType : factoryType.getParameterTypes(method)) {
|
||||||
|
parameterTypes.add(parameterType.getType());
|
||||||
|
}
|
||||||
|
ParameterListKey methodParams = new ParameterListKey(parameterTypes);
|
||||||
|
|
||||||
|
if (!paramsToConstructor.containsKey(methodParams)) {
|
||||||
|
throw newConfigurationException("%s has no @AssistInject constructor that takes the "
|
||||||
|
+ "@Assisted parameters %s in that order. @AssistInject constructors are %s",
|
||||||
|
implementationType, methodParams, paramsToConstructor.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
method.getParameterAnnotations();
|
||||||
|
for (Annotation[] parameterAnnotations : method.getParameterAnnotations()) {
|
||||||
|
for (Annotation parameterAnnotation : parameterAnnotations) {
|
||||||
|
if (parameterAnnotation.annotationType() == Assisted.class) {
|
||||||
|
throw newConfigurationException("Factory method %s has an @Assisted parameter, which "
|
||||||
|
+ "is incompatible with the deprecated @AssistedInject annotation. Please replace "
|
||||||
|
+ "@AssistedInject with @Inject on the %s constructor.",
|
||||||
|
method, implementationType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AssistedConstructor<?> matchingConstructor = paramsToConstructor.remove(methodParams);
|
||||||
|
|
||||||
|
result.put(method, matchingConstructor);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ConfigurationException newConfigurationException(String format, Object... args) {
|
||||||
|
return new ConfigurationException(ImmutableSet.of(new Message(Errors.format(format, args))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
|
@ -212,10 +268,8 @@ public class FactoryProvider<F> implements Provider<F>, HasDependencies {
|
||||||
// this is lame - we're not using the proper mechanism to add an
|
// this is lame - we're not using the proper mechanism to add an
|
||||||
// error to the injector. Throughout this class we throw exceptions
|
// error to the injector. Throughout this class we throw exceptions
|
||||||
// to add errors, which isn't really the best way in Guice
|
// to add errors, which isn't really the best way in Guice
|
||||||
throw newConfigurationException(
|
throw newConfigurationException("Parameter of type '%s' is not injectable or annotated "
|
||||||
"Parameter of type '%s' is not injectable or annotated "
|
+ "with @Assisted for Constructor '%s'", p, c);
|
||||||
+ "with @Assisted for Constructor '%s'",
|
|
||||||
p, c);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -226,10 +280,8 @@ public class FactoryProvider<F> implements Provider<F>, HasDependencies {
|
||||||
for (Class<?> constructorException : entry.getValue().getDeclaredExceptions()) {
|
for (Class<?> constructorException : entry.getValue().getDeclaredExceptions()) {
|
||||||
if (!isConstructorExceptionCompatibleWithFactoryExeception(
|
if (!isConstructorExceptionCompatibleWithFactoryExeception(
|
||||||
constructorException, entry.getKey().getExceptionTypes())) {
|
constructorException, entry.getKey().getExceptionTypes())) {
|
||||||
throw newConfigurationException(
|
throw newConfigurationException("Constructor %s declares an exception, but no compatible "
|
||||||
"Constructor %s declares an exception, but no compatible "
|
+ "exception is thrown by the factory method %s", entry.getValue(), entry.getKey());
|
||||||
+ "exception is thrown by the factory method %s",
|
|
||||||
entry.getValue(), entry.getKey());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -249,82 +301,6 @@ public class FactoryProvider<F> implements Provider<F>, HasDependencies {
|
||||||
return parameter.isBound(injector);
|
return parameter.isBound(injector);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<Method, AssistedConstructor<?>> createMethodMapping(
|
|
||||||
TypeLiteral<?> factoryType, TypeLiteral<?> implementationType) {
|
|
||||||
List<AssistedConstructor<?>> constructors = Lists.newArrayList();
|
|
||||||
|
|
||||||
for (Constructor<?> constructor : implementationType.getRawType().getDeclaredConstructors()) {
|
|
||||||
if (constructor.isAnnotationPresent(AssistedInject.class)) {
|
|
||||||
AssistedConstructor<?> assistedConstructor =
|
|
||||||
AssistedConstructor.create(
|
|
||||||
constructor, implementationType.getParameterTypes(constructor));
|
|
||||||
constructors.add(assistedConstructor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (constructors.isEmpty()) {
|
|
||||||
return ImmutableMap.of();
|
|
||||||
}
|
|
||||||
|
|
||||||
Method[] factoryMethods = factoryType.getRawType().getMethods();
|
|
||||||
|
|
||||||
if (constructors.size() != factoryMethods.length) {
|
|
||||||
throw newConfigurationException(
|
|
||||||
"Constructor mismatch: %s has %s @AssistedInject "
|
|
||||||
+ "constructors, factory %s has %s creation methods",
|
|
||||||
implementationType, constructors.size(), factoryType, factoryMethods.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<ParameterListKey, AssistedConstructor<?>> paramsToConstructor = Maps.newHashMap();
|
|
||||||
|
|
||||||
for (AssistedConstructor<?> c : constructors) {
|
|
||||||
if (paramsToConstructor.containsKey(c.getAssistedParameters())) {
|
|
||||||
throw new RuntimeException("Duplicate constructor, " + c);
|
|
||||||
}
|
|
||||||
paramsToConstructor.put(c.getAssistedParameters(), c);
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<Method, AssistedConstructor<?>> result = Maps.newHashMap();
|
|
||||||
for (Method method : factoryMethods) {
|
|
||||||
if (!method.getReturnType().isAssignableFrom(implementationType.getRawType())) {
|
|
||||||
throw newConfigurationException(
|
|
||||||
"Return type of method %s is not assignable from %s", method, implementationType);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Type> parameterTypes = Lists.newArrayList();
|
|
||||||
for (TypeLiteral<?> parameterType : factoryType.getParameterTypes(method)) {
|
|
||||||
parameterTypes.add(parameterType.getType());
|
|
||||||
}
|
|
||||||
ParameterListKey methodParams = new ParameterListKey(parameterTypes);
|
|
||||||
|
|
||||||
if (!paramsToConstructor.containsKey(methodParams)) {
|
|
||||||
throw newConfigurationException(
|
|
||||||
"%s has no @AssistInject constructor that takes the "
|
|
||||||
+ "@Assisted parameters %s in that order. @AssistInject constructors are %s",
|
|
||||||
implementationType, methodParams, paramsToConstructor.values());
|
|
||||||
}
|
|
||||||
|
|
||||||
method.getParameterAnnotations();
|
|
||||||
for (Annotation[] parameterAnnotations : method.getParameterAnnotations()) {
|
|
||||||
for (Annotation parameterAnnotation : parameterAnnotations) {
|
|
||||||
if (parameterAnnotation.annotationType() == Assisted.class) {
|
|
||||||
throw newConfigurationException(
|
|
||||||
"Factory method %s has an @Assisted parameter, which is incompatible with the"
|
|
||||||
+ " deprecated @AssistedInject annotation. Please replace @AssistedInject with"
|
|
||||||
+ " @Inject on the %s constructor.",
|
|
||||||
method, implementationType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
AssistedConstructor<?> matchingConstructor = paramsToConstructor.remove(methodParams);
|
|
||||||
|
|
||||||
result.put(method, matchingConstructor);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<Dependency<?>> getDependencies() {
|
public Set<Dependency<?>> getDependencies() {
|
||||||
List<Dependency<?>> dependencies = Lists.newArrayList();
|
List<Dependency<?>> dependencies = Lists.newArrayList();
|
||||||
for (AssistedConstructor<?> constructor : factoryMethodToConstructor.values()) {
|
for (AssistedConstructor<?> constructor : factoryMethodToConstructor.values()) {
|
||||||
|
@ -337,55 +313,51 @@ public class FactoryProvider<F> implements Provider<F>, HasDependencies {
|
||||||
return ImmutableSet.copyOf(dependencies);
|
return ImmutableSet.copyOf(dependencies);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public F get() {
|
public F get() {
|
||||||
InvocationHandler invocationHandler =
|
InvocationHandler invocationHandler = new InvocationHandler() {
|
||||||
new InvocationHandler() {
|
public Object invoke(Object proxy, Method method, Object[] creationArgs) throws Throwable {
|
||||||
@Override
|
// pass methods from Object.class to the proxy
|
||||||
public Object invoke(Object proxy, Method method, Object[] creationArgs)
|
if (method.getDeclaringClass().equals(Object.class)) {
|
||||||
throws Throwable {
|
if ("equals".equals(method.getName())) {
|
||||||
// pass methods from Object.class to the proxy
|
return proxy == creationArgs[0];
|
||||||
if (method.getDeclaringClass().equals(Object.class)) {
|
} else if ("hashCode".equals(method.getName())) {
|
||||||
if ("equals".equals(method.getName())) {
|
return System.identityHashCode(proxy);
|
||||||
return proxy == creationArgs[0];
|
} else {
|
||||||
} else if ("hashCode".equals(method.getName())) {
|
return method.invoke(this, creationArgs);
|
||||||
return System.identityHashCode(proxy);
|
|
||||||
} else {
|
|
||||||
return method.invoke(this, creationArgs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
AssistedConstructor<?> constructor = factoryMethodToConstructor.get(method);
|
|
||||||
Object[] constructorArgs = gatherArgsForConstructor(constructor, creationArgs);
|
|
||||||
Object objectToReturn = constructor.newInstance(constructorArgs);
|
|
||||||
injector.injectMembers(objectToReturn);
|
|
||||||
return objectToReturn;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Object[] gatherArgsForConstructor(
|
AssistedConstructor<?> constructor = factoryMethodToConstructor.get(method);
|
||||||
AssistedConstructor<?> constructor, Object[] factoryArgs) {
|
Object[] constructorArgs = gatherArgsForConstructor(constructor, creationArgs);
|
||||||
int numParams = constructor.getAllParameters().size();
|
Object objectToReturn = constructor.newInstance(constructorArgs);
|
||||||
int argPosition = 0;
|
injector.injectMembers(objectToReturn);
|
||||||
Object[] result = new Object[numParams];
|
return objectToReturn;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < numParams; i++) {
|
public Object[] gatherArgsForConstructor(
|
||||||
Parameter parameter = constructor.getAllParameters().get(i);
|
AssistedConstructor<?> constructor,
|
||||||
if (parameter.isProvidedByFactory()) {
|
Object[] factoryArgs) {
|
||||||
result[i] = factoryArgs[argPosition];
|
int numParams = constructor.getAllParameters().size();
|
||||||
argPosition++;
|
int argPosition = 0;
|
||||||
} else {
|
Object[] result = new Object[numParams];
|
||||||
result[i] = parameter.getValue(injector);
|
|
||||||
}
|
for (int i = 0; i < numParams; i++) {
|
||||||
}
|
Parameter parameter = constructor.getAllParameters().get(i);
|
||||||
return result;
|
if (parameter.isProvidedByFactory()) {
|
||||||
|
result[i] = factoryArgs[argPosition];
|
||||||
|
argPosition++;
|
||||||
|
} else {
|
||||||
|
result[i] = parameter.getValue(injector);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@SuppressWarnings("unchecked") // we imprecisely treat the class literal of T as a Class<T>
|
@SuppressWarnings("unchecked") // we imprecisely treat the class literal of T as a Class<T>
|
||||||
Class<F> factoryRawType = (Class<F>) (Class<?>) factoryType.getRawType();
|
Class<F> factoryRawType = (Class<F>) (Class<?>) factoryType.getRawType();
|
||||||
return factoryRawType.cast(
|
return factoryRawType.cast(Proxy.newProxyInstance(factoryRawType.getClassLoader(),
|
||||||
Proxy.newProxyInstance(
|
new Class[]{factoryRawType}, invocationHandler));
|
||||||
factoryRawType.getClassLoader(), new Class<?>[] {factoryRawType}, invocationHandler));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -402,8 +374,4 @@ public class FactoryProvider<F> implements Provider<F>, HasDependencies {
|
||||||
return factoryType.equals(other.factoryType)
|
return factoryType.equals(other.factoryType)
|
||||||
&& implementationType.equals(other.implementationType);
|
&& implementationType.equals(other.implementationType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ConfigurationException newConfigurationException(String format, Object... args) {
|
|
||||||
return new ConfigurationException(ImmutableSet.of(new Message(Errors.format(format, args))));
|
|
||||||
}
|
|
||||||
}
|
}
|
File diff suppressed because it is too large
Load diff
|
@ -51,17 +51,20 @@ public interface LinkedBindingBuilder<T> extends ScopedBindingBuilder {
|
||||||
/**
|
/**
|
||||||
* See the EDSL examples at {@link com.google.inject.Binder}.
|
* See the EDSL examples at {@link com.google.inject.Binder}.
|
||||||
*/
|
*/
|
||||||
ScopedBindingBuilder toProvider(Class<? extends javax.inject.Provider<? extends T>> providerType);
|
ScopedBindingBuilder toProvider(
|
||||||
|
Class<? extends javax.inject.Provider<? extends T>> providerType);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See the EDSL examples at {@link com.google.inject.Binder}.
|
* See the EDSL examples at {@link com.google.inject.Binder}.
|
||||||
*/
|
*/
|
||||||
ScopedBindingBuilder toProvider(TypeLiteral<? extends javax.inject.Provider<? extends T>> providerType);
|
ScopedBindingBuilder toProvider(
|
||||||
|
TypeLiteral<? extends javax.inject.Provider<? extends T>> providerType);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See the EDSL examples at {@link com.google.inject.Binder}.
|
* See the EDSL examples at {@link com.google.inject.Binder}.
|
||||||
*/
|
*/
|
||||||
ScopedBindingBuilder toProvider(Key<? extends javax.inject.Provider<? extends T>> providerKey);
|
ScopedBindingBuilder toProvider(
|
||||||
|
Key<? extends javax.inject.Provider<? extends T>> providerKey);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See the EDSL examples at {@link com.google.inject.Binder}.
|
* See the EDSL examples at {@link com.google.inject.Binder}.
|
||||||
|
@ -71,5 +74,6 @@ public interface LinkedBindingBuilder<T> extends ScopedBindingBuilder {
|
||||||
/**
|
/**
|
||||||
* See the EDSL examples at {@link com.google.inject.Binder}.
|
* See the EDSL examples at {@link com.google.inject.Binder}.
|
||||||
*/
|
*/
|
||||||
<S extends T> ScopedBindingBuilder toConstructor(Constructor<S> constructor, TypeLiteral<? extends S> type);
|
<S extends T> ScopedBindingBuilder toConstructor(
|
||||||
|
Constructor<S> constructor, TypeLiteral<? extends S> type);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,14 +18,14 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
public abstract class AbstractBindingBuilder<T> {
|
public abstract class AbstractBindingBuilder<T> {
|
||||||
|
|
||||||
public static final String IMPLEMENTATION_ALREADY_SET = "Implementation is set more than once.";
|
public static final String IMPLEMENTATION_ALREADY_SET = "Implementation is set more than once.";
|
||||||
public static final String SINGLE_INSTANCE_AND_SCOPE =
|
public static final String SINGLE_INSTANCE_AND_SCOPE
|
||||||
"Setting the scope is not permitted when binding to a single instance.";
|
= "Setting the scope is not permitted when binding to a single instance.";
|
||||||
public static final String SCOPE_ALREADY_SET = "Scope is set more than once.";
|
public static final String SCOPE_ALREADY_SET = "Scope is set more than once.";
|
||||||
public static final String BINDING_TO_NULL = "Binding to null instances is not allowed. " +
|
public static final String BINDING_TO_NULL = "Binding to null instances is not allowed. "
|
||||||
"Use toProvider(Providers.of(null)) if this is your intended behaviour.";
|
+ "Use toProvider(Providers.of(null)) if this is your intended behaviour.";
|
||||||
public static final String CONSTANT_VALUE_ALREADY_SET = "Constant value is set more than once.";
|
public static final String CONSTANT_VALUE_ALREADY_SET = "Constant value is set more than once.";
|
||||||
public static final String ANNOTATION_ALREADY_SPECIFIED =
|
public static final String ANNOTATION_ALREADY_SPECIFIED
|
||||||
"More than one annotation is specified for this binding.";
|
= "More than one annotation is specified for this binding.";
|
||||||
|
|
||||||
protected static final Key<?> NULL_KEY = Key.get(Void.class);
|
protected static final Key<?> NULL_KEY = Key.get(Void.class);
|
||||||
protected final Binder binder;
|
protected final Binder binder;
|
||||||
|
@ -37,7 +37,7 @@ public abstract class AbstractBindingBuilder<T> {
|
||||||
this.binder = binder;
|
this.binder = binder;
|
||||||
this.elements = elements;
|
this.elements = elements;
|
||||||
this.position = elements.size();
|
this.position = elements.size();
|
||||||
this.binding = new UntargettedBindingImpl<>(source, key, Scoping.UNSCOPED);
|
this.binding = new UntargettedBindingImpl<T>(source, key, Scoping.UNSCOPED);
|
||||||
elements.add(position, this.binding);
|
elements.add(position, this.binding);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +57,8 @@ public abstract class AbstractBindingBuilder<T> {
|
||||||
protected BindingImpl<T> annotatedWithInternal(Class<? extends Annotation> annotationType) {
|
protected BindingImpl<T> annotatedWithInternal(Class<? extends Annotation> annotationType) {
|
||||||
checkNotNull(annotationType, "annotationType");
|
checkNotNull(annotationType, "annotationType");
|
||||||
checkNotAnnotated();
|
checkNotAnnotated();
|
||||||
return setBinding(binding.withKey(Key.get(this.binding.getKey().getTypeLiteral(), annotationType)));
|
return setBinding(binding.withKey(
|
||||||
|
Key.get(this.binding.getKey().getTypeLiteral(), annotationType)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,7 +67,8 @@ public abstract class AbstractBindingBuilder<T> {
|
||||||
protected BindingImpl<T> annotatedWithInternal(Annotation annotation) {
|
protected BindingImpl<T> annotatedWithInternal(Annotation annotation) {
|
||||||
checkNotNull(annotation, "annotation");
|
checkNotNull(annotation, "annotation");
|
||||||
checkNotAnnotated();
|
checkNotAnnotated();
|
||||||
return setBinding(binding.withKey(Key.get(this.binding.getKey().getTypeLiteral(), annotation)));
|
return setBinding(binding.withKey(
|
||||||
|
Key.get(this.binding.getKey().getTypeLiteral(), annotation)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void in(final Class<? extends Annotation> scopeAnnotation) {
|
public void in(final Class<? extends Annotation> scopeAnnotation) {
|
||||||
|
|
|
@ -14,33 +14,34 @@ import com.google.inject.Stage;
|
||||||
import com.google.inject.TypeLiteral;
|
import com.google.inject.TypeLiteral;
|
||||||
import com.google.inject.spi.DefaultBindingTargetVisitor;
|
import com.google.inject.spi.DefaultBindingTargetVisitor;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Guarantees that processing of Binding elements happens in a sane way.
|
* Guarantees that processing of Binding elements happens in a sane way.
|
||||||
*/
|
*/
|
||||||
abstract class AbstractBindingProcessor extends AbstractProcessor {
|
abstract class AbstractBindingProcessor extends AbstractProcessor {
|
||||||
|
|
||||||
// It's unfortunate that we have to maintain a list of specific
|
// It's unfortunate that we have to maintain a blacklist of specific
|
||||||
// classes, but we can't easily block the whole package because of
|
// classes, but we can't easily block the whole package because of
|
||||||
// all our unit tests.
|
// all our unit tests.
|
||||||
private static final ImmutableSet<Class<?>> FORBIDDEN_TYPES =
|
private static final Set<Class<?>> FORBIDDEN_TYPES = ImmutableSet.<Class<?>>of(
|
||||||
ImmutableSet.<Class<?>>of(
|
AbstractModule.class,
|
||||||
AbstractModule.class,
|
Binder.class,
|
||||||
Binder.class,
|
Binding.class,
|
||||||
Binding.class,
|
Injector.class,
|
||||||
Injector.class,
|
Key.class,
|
||||||
Key.class,
|
MembersInjector.class,
|
||||||
MembersInjector.class,
|
Module.class,
|
||||||
Module.class,
|
Provider.class,
|
||||||
Provider.class,
|
Scope.class,
|
||||||
Scope.class,
|
Stage.class,
|
||||||
Stage.class,
|
TypeLiteral.class);
|
||||||
TypeLiteral.class);
|
|
||||||
|
|
||||||
protected final ProcessedBindingData processedBindingData;
|
protected final ProcessedBindingData bindingData;
|
||||||
|
|
||||||
AbstractBindingProcessor(Errors errors, ProcessedBindingData processedBindingData) {
|
AbstractBindingProcessor(Errors errors, ProcessedBindingData bindingData) {
|
||||||
super(errors);
|
super(errors);
|
||||||
this.processedBindingData = processedBindingData;
|
this.bindingData = bindingData;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <T> UntargettedBindingImpl<T> invalidBinding(
|
protected <T> UntargettedBindingImpl<T> invalidBinding(
|
||||||
|
@ -60,10 +61,10 @@ abstract class AbstractBindingProcessor extends AbstractProcessor {
|
||||||
BindingImpl<?> original = injector.getExistingBinding(key);
|
BindingImpl<?> original = injector.getExistingBinding(key);
|
||||||
if (original != null) {
|
if (original != null) {
|
||||||
// If it failed because of an explicit duplicate binding...
|
// If it failed because of an explicit duplicate binding...
|
||||||
if (injector.getBindingData().getExplicitBinding(key) != null) {
|
if (injector.state.getExplicitBinding(key) != null) {
|
||||||
try {
|
try {
|
||||||
if (!isOkayDuplicate(original, binding, injector.getBindingData())) {
|
if (!isOkayDuplicate(original, binding, injector.state)) {
|
||||||
errors.bindingAlreadySet(binding, original);
|
errors.bindingAlreadySet(key, original.getSource());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
|
@ -79,34 +80,27 @@ abstract class AbstractBindingProcessor extends AbstractProcessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
// prevent the parent from creating a JIT binding for this key
|
// prevent the parent from creating a JIT binding for this key
|
||||||
injector
|
injector.state.parent().blacklist(key, injector.state, binding.getSource());
|
||||||
.getJitBindingData()
|
injector.state.putBinding(key, binding);
|
||||||
.banKeyInParent(key, injector.getBindingData(), binding.getSource());
|
|
||||||
injector.getBindingData().putBinding(key, binding);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We tolerate duplicate bindings if one exposes the other or if the two bindings are considered
|
* We tolerate duplicate bindings if one exposes the other or if the two bindings
|
||||||
* duplicates (see {@link Bindings#areDuplicates(BindingImpl, BindingImpl)}.
|
* are considered duplicates.
|
||||||
*
|
*
|
||||||
* @param original the binding in the parent injector (candidate for an exposing binding)
|
* @param original the binding in the parent injector (candidate for an exposing binding)
|
||||||
* @param binding the binding to check (candidate for the exposed binding)
|
* @param binding the binding to check (candidate for the exposed binding)
|
||||||
*/
|
*/
|
||||||
private static boolean isOkayDuplicate(
|
private boolean isOkayDuplicate(BindingImpl<?> original, BindingImpl<?> binding, State state) {
|
||||||
BindingImpl<?> original, BindingImpl<?> binding, InjectorBindingData bindingData) {
|
|
||||||
if (original instanceof ExposedBindingImpl) {
|
if (original instanceof ExposedBindingImpl) {
|
||||||
ExposedBindingImpl<?> exposed = (ExposedBindingImpl<?>) original;
|
ExposedBindingImpl exposed = (ExposedBindingImpl) original;
|
||||||
InjectorImpl exposedFrom = (InjectorImpl) exposed.getPrivateElements().getInjector();
|
InjectorImpl exposedFrom = (InjectorImpl) exposed.getPrivateElements().getInjector();
|
||||||
return (exposedFrom == binding.getInjector());
|
return (exposedFrom == binding.getInjector());
|
||||||
} else {
|
} else {
|
||||||
original = (BindingImpl<?>) bindingData.getExplicitBindingsThisLevel().get(binding.getKey());
|
original = (BindingImpl<?>) state.getExplicitBindingsThisLevel().get(binding.getKey());
|
||||||
// If no original at this level, the original was on a parent, and we don't
|
// If no original at this level, the original was on a parent, and we don't
|
||||||
// allow deduplication between parents & children.
|
// allow deduplication between parents & children.
|
||||||
if (original == null) {
|
return original != null && original.equals(binding);
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return original.equals(binding);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,8 +110,8 @@ abstract class AbstractBindingProcessor extends AbstractProcessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processor for visiting bindings. Each overriden method that wants to actually process the
|
* Processor for visiting bindings. Each overriden method that wants to
|
||||||
* binding should call prepareBinding first.
|
* actually process the binding should call prepareBinding first.
|
||||||
*/
|
*/
|
||||||
abstract class Processor<T, V> extends DefaultBindingTargetVisitor<T, V> {
|
abstract class Processor<T, V> extends DefaultBindingTargetVisitor<T, V> {
|
||||||
final Object source;
|
final Object source;
|
||||||
|
@ -137,28 +131,16 @@ abstract class AbstractBindingProcessor extends AbstractProcessor {
|
||||||
scoping = Scoping.makeInjectable(scoping, injector, errors);
|
scoping = Scoping.makeInjectable(scoping, injector, errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected void scheduleInitialization(final BindingImpl<?> binding) {
|
||||||
* Schedule initialization of this binding to occur immediately after all bindings have been
|
bindingData.addUninitializedBinding(new Runnable() {
|
||||||
* initialially processed.
|
public void run() {
|
||||||
*/
|
try {
|
||||||
protected void scheduleInitialization(BindingImpl<?> binding) {
|
binding.getInjector().initializeBinding(binding, errors.withSource(source));
|
||||||
processedBindingData.addUninitializedBinding(() -> initializeBinding(binding));
|
} catch (ErrorsException e) {
|
||||||
}
|
errors.merge(e.getErrors());
|
||||||
|
}
|
||||||
/**
|
}
|
||||||
* Schedule initialization for this binding to occur after all other static initialization of
|
});
|
||||||
* bindings.
|
|
||||||
*/
|
|
||||||
protected void scheduleDelayedInitialization(BindingImpl<?> binding) {
|
|
||||||
processedBindingData.addDelayedUninitializedBinding(() -> initializeBinding(binding));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initializeBinding(BindingImpl<?> binding) {
|
|
||||||
try {
|
|
||||||
binding.getInjector().initializeBinding(binding, errors.withSource(source));
|
|
||||||
} catch (ErrorsException e) {
|
|
||||||
errors.merge(e.getErrors());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package com.google.inject.internal;
|
package com.google.inject.internal;
|
||||||
|
|
||||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
import com.google.common.base.Function;
|
||||||
|
|
||||||
import com.google.common.base.Joiner;
|
import com.google.common.base.Joiner;
|
||||||
import com.google.common.base.Joiner.MapJoiner;
|
import com.google.common.base.Joiner.MapJoiner;
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
@ -17,6 +16,8 @@ import com.google.inject.TypeLiteral;
|
||||||
import com.google.inject.internal.util.Classes;
|
import com.google.inject.internal.util.Classes;
|
||||||
import com.google.inject.name.Named;
|
import com.google.inject.name.Named;
|
||||||
import com.google.inject.name.Names;
|
import com.google.inject.name.Names;
|
||||||
|
|
||||||
|
import javax.inject.Qualifier;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
@ -27,16 +28,37 @@ import java.lang.reflect.Proxy;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.inject.Qualifier;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Annotation utilities.
|
* Annotation utilities.
|
||||||
*
|
*
|
||||||
* @author crazybob@google.com (Bob Lee)
|
|
||||||
*/
|
*/
|
||||||
public class Annotations {
|
public class Annotations {
|
||||||
|
|
||||||
/** Returns {@code true} if the given annotation type has no attributes. */
|
private static final MapJoiner JOINER = Joiner.on(", ").withKeyValueSeparator("=");
|
||||||
|
private static final Function<Object, String> DEEP_TO_STRING_FN = new Function<Object, String>() {
|
||||||
|
@Override
|
||||||
|
public String apply(Object arg) {
|
||||||
|
String s = Arrays.deepToString(new Object[]{arg});
|
||||||
|
return s.substring(1, s.length() - 1); // cut off brackets
|
||||||
|
}
|
||||||
|
};
|
||||||
|
private static final LoadingCache<Class<? extends Annotation>, Annotation> cache =
|
||||||
|
CacheBuilder.newBuilder().weakKeys().build(
|
||||||
|
new CacheLoader<Class<? extends Annotation>, Annotation>() {
|
||||||
|
@Override
|
||||||
|
public Annotation load(Class<? extends Annotation> input) {
|
||||||
|
return generateAnnotationImpl(input);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
private static final AnnotationChecker scopeChecker = new AnnotationChecker(
|
||||||
|
Arrays.asList(ScopeAnnotation.class, javax.inject.Scope.class));
|
||||||
|
private static final AnnotationChecker bindingAnnotationChecker = new AnnotationChecker(
|
||||||
|
Arrays.asList(BindingAnnotation.class, Qualifier.class));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code true} if the given annotation type has no attributes.
|
||||||
|
*/
|
||||||
public static boolean isMarker(Class<? extends Annotation> annotationType) {
|
public static boolean isMarker(Class<? extends Annotation> annotationType) {
|
||||||
return annotationType.getDeclaredMethods().length == 0;
|
return annotationType.getDeclaredMethods().length == 0;
|
||||||
}
|
}
|
||||||
|
@ -52,22 +74,11 @@ public class Annotations {
|
||||||
return hasMethods;
|
return hasMethods;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final LoadingCache<Class<? extends Annotation>, Annotation> cache =
|
|
||||||
CacheBuilder.newBuilder()
|
|
||||||
.weakKeys()
|
|
||||||
.build(
|
|
||||||
new CacheLoader<Class<? extends Annotation>, Annotation>() {
|
|
||||||
@Override
|
|
||||||
public Annotation load(Class<? extends Annotation> input) {
|
|
||||||
return generateAnnotationImpl(input);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates an Annotation for the annotation class. Requires that the annotation is all
|
* Generates an Annotation for the annotation class. Requires that the annotation is all
|
||||||
* optionals.
|
* optionals.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked") // Safe because generateAnnotationImpl returns T for Class<T>
|
@SuppressWarnings("unchecked")
|
||||||
public static <T extends Annotation> T generateAnnotation(Class<T> annotationType) {
|
public static <T extends Annotation> T generateAnnotation(Class<T> annotationType) {
|
||||||
Preconditions.checkState(
|
Preconditions.checkState(
|
||||||
isAllDefaultMethods(annotationType), "%s is not all default methods", annotationType);
|
isAllDefaultMethods(annotationType), "%s is not all default methods", annotationType);
|
||||||
|
@ -76,27 +87,26 @@ public class Annotations {
|
||||||
|
|
||||||
private static <T extends Annotation> T generateAnnotationImpl(final Class<T> annotationType) {
|
private static <T extends Annotation> T generateAnnotationImpl(final Class<T> annotationType) {
|
||||||
final Map<String, Object> members = resolveMembers(annotationType);
|
final Map<String, Object> members = resolveMembers(annotationType);
|
||||||
return annotationType.cast(
|
return annotationType.cast(Proxy.newProxyInstance(
|
||||||
Proxy.newProxyInstance(
|
annotationType.getClassLoader(),
|
||||||
annotationType.getClassLoader(),
|
new Class<?>[]{annotationType},
|
||||||
new Class<?>[] {annotationType},
|
new InvocationHandler() {
|
||||||
new InvocationHandler() {
|
@Override
|
||||||
@Override
|
public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
|
||||||
public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
|
String name = method.getName();
|
||||||
String name = method.getName();
|
if (name.equals("annotationType")) {
|
||||||
if (name.equals("annotationType")) {
|
return annotationType;
|
||||||
return annotationType;
|
} else if (name.equals("toString")) {
|
||||||
} else if (name.equals("toString")) {
|
return annotationToString(annotationType, members);
|
||||||
return annotationToString(annotationType, members);
|
} else if (name.equals("hashCode")) {
|
||||||
} else if (name.equals("hashCode")) {
|
return annotationHashCode(annotationType, members);
|
||||||
return annotationHashCode(annotationType, members);
|
} else if (name.equals("equals")) {
|
||||||
} else if (name.equals("equals")) {
|
return annotationEquals(annotationType, members, args[0]);
|
||||||
return annotationEquals(annotationType, members, args[0]);
|
} else {
|
||||||
} else {
|
return members.get(name);
|
||||||
return members.get(name);
|
}
|
||||||
}
|
}
|
||||||
}
|
}));
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ImmutableMap<String, Object> resolveMembers(
|
private static ImmutableMap<String, Object> resolveMembers(
|
||||||
|
@ -108,67 +118,68 @@ public class Annotations {
|
||||||
return result.build();
|
return result.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Implements {@link Annotation#equals}. */
|
/**
|
||||||
private static boolean annotationEquals(
|
* Implements {@link Annotation#equals}.
|
||||||
Class<? extends Annotation> type, Map<String, Object> members, Object other)
|
*/
|
||||||
throws Exception {
|
private static boolean annotationEquals(Class<? extends Annotation> type,
|
||||||
|
Map<String, Object> members, Object other) throws Exception {
|
||||||
if (!type.isInstance(other)) {
|
if (!type.isInstance(other)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (Method method : type.getDeclaredMethods()) {
|
for (Method method : type.getDeclaredMethods()) {
|
||||||
String name = method.getName();
|
String name = method.getName();
|
||||||
if (!Arrays.deepEquals(
|
if (!Arrays.deepEquals(
|
||||||
new Object[] {method.invoke(other)}, new Object[] {members.get(name)})) {
|
new Object[]{method.invoke(other)}, new Object[]{members.get(name)})) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Implements {@link Annotation#hashCode}. */
|
/**
|
||||||
private static int annotationHashCode(
|
* Implements {@link Annotation#hashCode}.
|
||||||
Class<? extends Annotation> type, Map<String, Object> members) throws Exception {
|
*/
|
||||||
|
private static int annotationHashCode(Class<? extends Annotation> type,
|
||||||
|
Map<String, Object> members) throws Exception {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (Method method : type.getDeclaredMethods()) {
|
for (Method method : type.getDeclaredMethods()) {
|
||||||
String name = method.getName();
|
String name = method.getName();
|
||||||
Object value = members.get(name);
|
Object value = members.get(name);
|
||||||
result += (127 * name.hashCode()) ^ (Arrays.deepHashCode(new Object[] {value}) - 31);
|
result += (127 * name.hashCode()) ^ (Arrays.deepHashCode(new Object[]{value}) - 31);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final MapJoiner JOINER = Joiner.on(", ").withKeyValueSeparator("=");
|
/**
|
||||||
|
* Implements {@link Annotation#toString}.
|
||||||
/** Implements {@link Annotation#toString}. */
|
*/
|
||||||
private static String annotationToString(
|
private static String annotationToString(Class<? extends Annotation> type,
|
||||||
Class<? extends Annotation> type, Map<String, Object> members) throws Exception {
|
Map<String, Object> members) throws Exception {
|
||||||
StringBuilder sb = new StringBuilder().append("@").append(type.getName()).append("(");
|
StringBuilder sb = new StringBuilder().append("@").append(type.getName()).append("(");
|
||||||
JOINER.appendTo(
|
JOINER.appendTo(sb, Maps.transformValues(members, DEEP_TO_STRING_FN));
|
||||||
sb,
|
|
||||||
Maps.transformValues(
|
|
||||||
members,
|
|
||||||
arg -> {
|
|
||||||
String s = Arrays.deepToString(new Object[] {arg});
|
|
||||||
return s.substring(1, s.length() - 1); // cut off brackets
|
|
||||||
}));
|
|
||||||
return sb.append(")").toString();
|
return sb.append(")").toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns true if the given annotation is retained at runtime. */
|
/**
|
||||||
|
* Returns true if the given annotation is retained at runtime.
|
||||||
|
*/
|
||||||
public static boolean isRetainedAtRuntime(Class<? extends Annotation> annotationType) {
|
public static boolean isRetainedAtRuntime(Class<? extends Annotation> annotationType) {
|
||||||
Retention retention = annotationType.getAnnotation(Retention.class);
|
Retention retention = annotationType.getAnnotation(Retention.class);
|
||||||
return retention != null && retention.value() == RetentionPolicy.RUNTIME;
|
return retention != null && retention.value() == RetentionPolicy.RUNTIME;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the scope annotation on {@code type}, or null if none is specified. */
|
/**
|
||||||
|
* Returns the scope annotation on {@code type}, or null if none is specified.
|
||||||
|
*/
|
||||||
public static Class<? extends Annotation> findScopeAnnotation(
|
public static Class<? extends Annotation> findScopeAnnotation(
|
||||||
Errors errors, Class<?> implementation) {
|
Errors errors, Class<?> implementation) {
|
||||||
return findScopeAnnotation(errors, implementation.getAnnotations());
|
return findScopeAnnotation(errors, implementation.getAnnotations());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the scoping annotation, or null if there isn't one. */
|
/**
|
||||||
public static Class<? extends Annotation> findScopeAnnotation(
|
* Returns the scoping annotation, or null if there isn't one.
|
||||||
Errors errors, Annotation[] annotations) {
|
*/
|
||||||
|
public static Class<? extends Annotation> findScopeAnnotation(Errors errors, Annotation[] annotations) {
|
||||||
Class<? extends Annotation> found = null;
|
Class<? extends Annotation> found = null;
|
||||||
|
|
||||||
for (Annotation annotation : annotations) {
|
for (Annotation annotation : annotations) {
|
||||||
|
@ -196,115 +207,13 @@ public class Annotations {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class AnnotationToStringConfig {
|
|
||||||
final boolean quote;
|
|
||||||
final boolean includeMemberName;
|
|
||||||
|
|
||||||
AnnotationToStringConfig(boolean quote, boolean includeMemberName) {
|
|
||||||
this.quote = quote;
|
|
||||||
this.includeMemberName = includeMemberName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final AnnotationToStringConfig ANNOTATION_TO_STRING_CONFIG =
|
|
||||||
determineAnnotationToStringConfig();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns {@code value}, quoted if annotation implementations quote their member values. In Java
|
|
||||||
* 9, annotations quote their string members.
|
|
||||||
*/
|
|
||||||
public static String memberValueString(String value) {
|
|
||||||
return ANNOTATION_TO_STRING_CONFIG.quote ? "\"" + value + "\"" : value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns string representation of the annotation memeber.
|
|
||||||
*
|
|
||||||
* <p>The value of the member is prefixed with `memberName=` unless the runtime omits the member
|
|
||||||
* name. The value of the member is quoted if annotation implementations quote their member values
|
|
||||||
* and the value type is String.
|
|
||||||
*
|
|
||||||
* <p>In Java 9, annotations quote their string members and in Java 15, the member name is
|
|
||||||
* omitted.
|
|
||||||
*/
|
|
||||||
public static String memberValueString(String memberName, Object value) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
boolean quote = ANNOTATION_TO_STRING_CONFIG.quote;
|
|
||||||
boolean includeMemberName = ANNOTATION_TO_STRING_CONFIG.includeMemberName;
|
|
||||||
if (includeMemberName) {
|
|
||||||
sb.append(memberName).append('=');
|
|
||||||
}
|
|
||||||
if (quote && (value instanceof String)) {
|
|
||||||
sb.append('"').append(value).append('"');
|
|
||||||
} else {
|
|
||||||
sb.append(value);
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Retention(RUNTIME)
|
|
||||||
private @interface TestAnnotation {
|
|
||||||
String value();
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestAnnotation("determineAnnotationToStringConfig")
|
|
||||||
private static AnnotationToStringConfig determineAnnotationToStringConfig() {
|
|
||||||
try {
|
|
||||||
String annotation =
|
|
||||||
Annotations.class
|
|
||||||
.getDeclaredMethod("determineAnnotationToStringConfig")
|
|
||||||
.getAnnotation(TestAnnotation.class)
|
|
||||||
.toString();
|
|
||||||
boolean quote = annotation.contains("\"determineAnnotationToStringConfig\"");
|
|
||||||
boolean includeMemberName = annotation.contains("value=");
|
|
||||||
return new AnnotationToStringConfig(quote, includeMemberName);
|
|
||||||
} catch (NoSuchMethodException e) {
|
|
||||||
throw new AssertionError(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Checks for the presence of annotations. Caches results because Android doesn't. */
|
|
||||||
static class AnnotationChecker {
|
|
||||||
private final Collection<Class<? extends Annotation>> annotationTypes;
|
|
||||||
|
|
||||||
/** Returns true if the given class has one of the desired annotations. */
|
|
||||||
private CacheLoader<Class<? extends Annotation>, Boolean> hasAnnotations =
|
|
||||||
new CacheLoader<Class<? extends Annotation>, Boolean>() {
|
|
||||||
@Override
|
|
||||||
public Boolean load(Class<? extends Annotation> annotationType) {
|
|
||||||
for (Annotation annotation : annotationType.getAnnotations()) {
|
|
||||||
if (annotationTypes.contains(annotation.annotationType())) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
final LoadingCache<Class<? extends Annotation>, Boolean> cache =
|
|
||||||
CacheBuilder.newBuilder().weakKeys().build(hasAnnotations);
|
|
||||||
|
|
||||||
/** Constructs a new checker that looks for annotations of the given types. */
|
|
||||||
AnnotationChecker(Collection<Class<? extends Annotation>> annotationTypes) {
|
|
||||||
this.annotationTypes = annotationTypes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns true if the given type has one of the desired annotations. */
|
|
||||||
boolean hasAnnotations(Class<? extends Annotation> annotated) {
|
|
||||||
return cache.getUnchecked(annotated);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final AnnotationChecker scopeChecker =
|
|
||||||
new AnnotationChecker(Arrays.asList(ScopeAnnotation.class, javax.inject.Scope.class));
|
|
||||||
|
|
||||||
public static boolean isScopeAnnotation(Class<? extends Annotation> annotationType) {
|
public static boolean isScopeAnnotation(Class<? extends Annotation> annotationType) {
|
||||||
return scopeChecker.hasAnnotations(annotationType);
|
return scopeChecker.hasAnnotations(annotationType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an error if there is a misplaced annotations on {@code type}. Scoping annotations are not
|
* Adds an error if there is a misplaced annotations on {@code type}. Scoping
|
||||||
* allowed on abstract classes or interfaces.
|
* annotations are not allowed on abstract classes or interfaces.
|
||||||
*/
|
*/
|
||||||
public static void checkForMisplacedScopeAnnotations(
|
public static void checkForMisplacedScopeAnnotations(
|
||||||
Class<?> type, Object source, Errors errors) {
|
Class<?> type, Object source, Errors errors) {
|
||||||
|
@ -320,20 +229,20 @@ public class Annotations {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: getKey/findBindingAnnotation are used by Gin which is abandoned. So changing this API
|
/**
|
||||||
// will prevent Gin users from upgrading Guice version.
|
* Gets a key for the given type, member and annotations.
|
||||||
|
*/
|
||||||
/** Gets a key for the given type, member and annotations. */
|
public static Key<?> getKey(TypeLiteral<?> type, Member member, Annotation[] annotations,
|
||||||
public static Key<?> getKey(
|
Errors errors) throws ErrorsException {
|
||||||
TypeLiteral<?> type, Member member, Annotation[] annotations, Errors errors)
|
|
||||||
throws ErrorsException {
|
|
||||||
int numErrorsBefore = errors.size();
|
int numErrorsBefore = errors.size();
|
||||||
Annotation found = findBindingAnnotation(errors, member, annotations);
|
Annotation found = findBindingAnnotation(errors, member, annotations);
|
||||||
errors.throwIfNewErrors(numErrorsBefore);
|
errors.throwIfNewErrors(numErrorsBefore);
|
||||||
return found == null ? Key.get(type) : Key.get(type, found);
|
return found == null ? Key.get(type) : Key.get(type, found);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the binding annotation on {@code member}, or null if there isn't one. */
|
/**
|
||||||
|
* Returns the binding annotation on {@code member}, or null if there isn't one.
|
||||||
|
*/
|
||||||
public static Annotation findBindingAnnotation(
|
public static Annotation findBindingAnnotation(
|
||||||
Errors errors, Member member, Annotation[] annotations) {
|
Errors errors, Member member, Annotation[] annotations) {
|
||||||
Annotation found = null;
|
Annotation found = null;
|
||||||
|
@ -352,17 +261,16 @@ public class Annotations {
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final AnnotationChecker bindingAnnotationChecker =
|
/**
|
||||||
new AnnotationChecker(Arrays.asList(BindingAnnotation.class, Qualifier.class));
|
* Returns true if annotations of the specified type are binding annotations.
|
||||||
|
*/
|
||||||
/** Returns true if annotations of the specified type are binding annotations. */
|
|
||||||
public static boolean isBindingAnnotation(Class<? extends Annotation> annotationType) {
|
public static boolean isBindingAnnotation(Class<? extends Annotation> annotationType) {
|
||||||
return bindingAnnotationChecker.hasAnnotations(annotationType);
|
return bindingAnnotationChecker.hasAnnotations(annotationType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the annotation is an instance of {@code javax.inject.Named}, canonicalizes to
|
* If the annotation is an instance of {@code javax.inject.Named}, canonicalizes to
|
||||||
* com.google.guice.name.Named. Returns the given annotation otherwise.
|
* com.google.guice.name.Named. Returns the given annotation otherwise.
|
||||||
*/
|
*/
|
||||||
public static Annotation canonicalizeIfNamed(Annotation annotation) {
|
public static Annotation canonicalizeIfNamed(Annotation annotation) {
|
||||||
if (annotation instanceof javax.inject.Named) {
|
if (annotation instanceof javax.inject.Named) {
|
||||||
|
@ -386,20 +294,41 @@ public class Annotations {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the name the binding should use. This is based on the annotation. If the annotation has
|
* Checks for the presence of annotations. Caches results because Android doesn't.
|
||||||
* an instance and is not a marker annotation, we ask the annotation for its toString. If it was a
|
|
||||||
* marker annotation or just an annotation type, we use the annotation's name. Otherwise, the name
|
|
||||||
* is the empty string.
|
|
||||||
*/
|
*/
|
||||||
public static String nameOf(Key<?> key) {
|
static class AnnotationChecker {
|
||||||
Annotation annotation = key.getAnnotation();
|
private final Collection<Class<? extends Annotation>> annotationTypes;
|
||||||
Class<? extends Annotation> annotationType = key.getAnnotationType();
|
|
||||||
if (annotation != null && !isMarker(annotationType)) {
|
/**
|
||||||
return key.getAnnotation().toString();
|
* Returns true if the given class has one of the desired annotations.
|
||||||
} else if (key.getAnnotationType() != null) {
|
*/
|
||||||
return "@" + key.getAnnotationType().getName();
|
private CacheLoader<Class<? extends Annotation>, Boolean> hasAnnotations =
|
||||||
} else {
|
new CacheLoader<Class<? extends Annotation>, Boolean>() {
|
||||||
return "";
|
public Boolean load(Class<? extends Annotation> annotationType) {
|
||||||
|
for (Annotation annotation : annotationType.getAnnotations()) {
|
||||||
|
if (annotationTypes.contains(annotation.annotationType())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
final LoadingCache<Class<? extends Annotation>, Boolean> cache = CacheBuilder.newBuilder().weakKeys()
|
||||||
|
.build(hasAnnotations);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new checker that looks for annotations of the given types.
|
||||||
|
*/
|
||||||
|
AnnotationChecker(Collection<Class<? extends Annotation>> annotationTypes) {
|
||||||
|
this.annotationTypes = annotationTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the given type has one of the desired annotations.
|
||||||
|
*/
|
||||||
|
boolean hasAnnotations(Class<? extends Annotation> annotated) {
|
||||||
|
return cache.getUnchecked(annotated);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,52 +0,0 @@
|
||||||
package com.google.inject.internal;
|
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
|
||||||
import com.google.inject.Binding;
|
|
||||||
import com.google.inject.spi.ErrorDetail;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Formatter;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/** Error reported by Guice when a key is bound at multiple places the injector. */
|
|
||||||
final class BindingAlreadySetError extends InternalErrorDetail<BindingAlreadySetError> {
|
|
||||||
private final Binding<?> binding;
|
|
||||||
private final Binding<?> original;
|
|
||||||
|
|
||||||
BindingAlreadySetError(Binding<?> binding, Binding<?> original, List<Object> sources) {
|
|
||||||
super(
|
|
||||||
ErrorId.BINDING_ALREADY_SET,
|
|
||||||
String.format("%s was bound multiple times.", Messages.convert(binding.getKey())),
|
|
||||||
sources,
|
|
||||||
null);
|
|
||||||
this.binding = binding;
|
|
||||||
this.original = original;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isMergeable(ErrorDetail<?> otherError) {
|
|
||||||
return otherError instanceof BindingAlreadySetError
|
|
||||||
&& ((BindingAlreadySetError) otherError).binding.getKey().equals(binding.getKey());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void formatDetail(List<ErrorDetail<?>> mergeableErrors, Formatter formatter) {
|
|
||||||
List<List<Object>> sourcesList = new ArrayList<>();
|
|
||||||
sourcesList.add(ImmutableList.of(original.getSource()));
|
|
||||||
sourcesList.add(ImmutableList.of(binding.getSource()));
|
|
||||||
sourcesList.addAll(
|
|
||||||
mergeableErrors.stream()
|
|
||||||
.map(e -> ((BindingAlreadySetError) e).binding.getSource())
|
|
||||||
.map(ImmutableList::of)
|
|
||||||
.collect(Collectors.toList()));
|
|
||||||
formatter.format("%n%s%n", Messages.bold("Bound at:"));
|
|
||||||
for (int i = 0; i < sourcesList.size(); i++) {
|
|
||||||
ErrorFormatter.formatSources(i + 1, sourcesList.get(i), formatter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingAlreadySetError withSources(List<Object> newSources) {
|
|
||||||
return new BindingAlreadySetError(binding, original, newSources);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -29,29 +29,24 @@ public class BindingBuilder<T> extends AbstractBindingBuilder<T>
|
||||||
super(binder, elements, source, key);
|
super(binder, elements, source, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingBuilder<T> annotatedWith(Class<? extends Annotation> annotationType) {
|
public BindingBuilder<T> annotatedWith(Class<? extends Annotation> annotationType) {
|
||||||
annotatedWithInternal(annotationType);
|
annotatedWithInternal(annotationType);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingBuilder<T> annotatedWith(Annotation annotation) {
|
public BindingBuilder<T> annotatedWith(Annotation annotation) {
|
||||||
annotatedWithInternal(annotation);
|
annotatedWithInternal(annotation);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingBuilder<T> to(Class<? extends T> implementation) {
|
public BindingBuilder<T> to(Class<? extends T> implementation) {
|
||||||
return to(Key.get(implementation));
|
return to(Key.get(implementation));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingBuilder<T> to(TypeLiteral<? extends T> implementation) {
|
public BindingBuilder<T> to(TypeLiteral<? extends T> implementation) {
|
||||||
return to(Key.get(implementation));
|
return to(Key.get(implementation));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingBuilder<T> to(Key<? extends T> linkedKey) {
|
public BindingBuilder<T> to(Key<? extends T> linkedKey) {
|
||||||
checkNotNull(linkedKey, "linkedKey");
|
checkNotNull(linkedKey, "linkedKey");
|
||||||
checkNotTargetted();
|
checkNotTargetted();
|
||||||
|
@ -61,7 +56,6 @@ public class BindingBuilder<T> extends AbstractBindingBuilder<T>
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void toInstance(T instance) {
|
public void toInstance(T instance) {
|
||||||
checkNotTargetted();
|
checkNotTargetted();
|
||||||
|
|
||||||
|
@ -85,12 +79,10 @@ public class BindingBuilder<T> extends AbstractBindingBuilder<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
|
||||||
public BindingBuilder<T> toProvider(Provider<? extends T> provider) {
|
public BindingBuilder<T> toProvider(Provider<? extends T> provider) {
|
||||||
return toProvider((javax.inject.Provider<T>) provider);
|
return toProvider((javax.inject.Provider<T>) provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingBuilder<T> toProvider(javax.inject.Provider<? extends T> provider) {
|
public BindingBuilder<T> toProvider(javax.inject.Provider<? extends T> provider) {
|
||||||
checkNotNull(provider, "provider");
|
checkNotNull(provider, "provider");
|
||||||
checkNotTargetted();
|
checkNotTargetted();
|
||||||
|
@ -110,19 +102,16 @@ public class BindingBuilder<T> extends AbstractBindingBuilder<T>
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingBuilder<T> toProvider(
|
public BindingBuilder<T> toProvider(
|
||||||
Class<? extends javax.inject.Provider<? extends T>> providerType) {
|
Class<? extends javax.inject.Provider<? extends T>> providerType) {
|
||||||
return toProvider(Key.get(providerType));
|
return toProvider(Key.get(providerType));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingBuilder<T> toProvider(
|
public BindingBuilder<T> toProvider(
|
||||||
TypeLiteral<? extends javax.inject.Provider<? extends T>> providerType) {
|
TypeLiteral<? extends javax.inject.Provider<? extends T>> providerType) {
|
||||||
return toProvider(Key.get(providerType));
|
return toProvider(Key.get(providerType));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingBuilder<T> toProvider(
|
public BindingBuilder<T> toProvider(
|
||||||
Key<? extends javax.inject.Provider<? extends T>> providerKey) {
|
Key<? extends javax.inject.Provider<? extends T>> providerKey) {
|
||||||
checkNotNull(providerKey, "providerKey");
|
checkNotNull(providerKey, "providerKey");
|
||||||
|
@ -134,12 +123,10 @@ public class BindingBuilder<T> extends AbstractBindingBuilder<T>
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> constructor) {
|
public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> constructor) {
|
||||||
return toConstructor(constructor, TypeLiteral.get(constructor.getDeclaringClass()));
|
return toConstructor(constructor, TypeLiteral.get(constructor.getDeclaringClass()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> constructor,
|
public <S extends T> ScopedBindingBuilder toConstructor(Constructor<S> constructor,
|
||||||
TypeLiteral<? extends S> type) {
|
TypeLiteral<? extends S> type) {
|
||||||
checkNotNull(constructor, "constructor");
|
checkNotNull(constructor, "constructor");
|
||||||
|
|
|
@ -34,17 +34,14 @@ public abstract class BindingImpl<T> implements Binding<T> {
|
||||||
this.scoping = scoping;
|
this.scoping = scoping;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Key<T> getKey() {
|
public Key<T> getKey() {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getSource() {
|
public Object getSource() {
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Provider<T> getProvider() {
|
public Provider<T> getProvider() {
|
||||||
if (provider == null) {
|
if (provider == null) {
|
||||||
if (injector == null) {
|
if (injector == null) {
|
||||||
|
@ -72,12 +69,10 @@ public abstract class BindingImpl<T> implements Binding<T> {
|
||||||
return this instanceof InstanceBinding;
|
return this instanceof InstanceBinding;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <V> V acceptVisitor(ElementVisitor<V> visitor) {
|
public <V> V acceptVisitor(ElementVisitor<V> visitor) {
|
||||||
return visitor.visit(this);
|
return visitor.visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <V> V acceptScopingVisitor(BindingScopingVisitor<V> visitor) {
|
public <V> V acceptScopingVisitor(BindingScopingVisitor<V> visitor) {
|
||||||
return scoping.acceptVisitor(visitor);
|
return scoping.acceptVisitor(visitor);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ import com.google.inject.spi.ProviderBinding;
|
||||||
import com.google.inject.spi.ProviderInstanceBinding;
|
import com.google.inject.spi.ProviderInstanceBinding;
|
||||||
import com.google.inject.spi.ProviderKeyBinding;
|
import com.google.inject.spi.ProviderKeyBinding;
|
||||||
import com.google.inject.spi.UntargettedBinding;
|
import com.google.inject.spi.UntargettedBinding;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,9 +25,8 @@ final class BindingProcessor extends AbstractBindingProcessor {
|
||||||
|
|
||||||
private final Initializer initializer;
|
private final Initializer initializer;
|
||||||
|
|
||||||
BindingProcessor(
|
BindingProcessor(Errors errors, Initializer initializer, ProcessedBindingData bindingData) {
|
||||||
Errors errors, Initializer initializer, ProcessedBindingData processedBindingData) {
|
super(errors, bindingData);
|
||||||
super(errors, processedBindingData);
|
|
||||||
this.initializer = initializer;
|
this.initializer = initializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,174 +49,118 @@ final class BindingProcessor extends AbstractBindingProcessor {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return command.acceptTargetVisitor(
|
return command.acceptTargetVisitor(new Processor<T, Boolean>((BindingImpl<T>) command) {
|
||||||
new Processor<T, Boolean>((BindingImpl<T>) command) {
|
@Override
|
||||||
@Override
|
public Boolean visit(ConstructorBinding<? extends T> binding) {
|
||||||
public Boolean visit(ConstructorBinding<? extends T> binding) {
|
prepareBinding();
|
||||||
prepareBinding();
|
try {
|
||||||
try {
|
ConstructorBindingImpl<T> onInjector = ConstructorBindingImpl.create(injector, key,
|
||||||
ConstructorBindingImpl<T> onInjector =
|
binding.getConstructor(), source, scoping, errors, false, false);
|
||||||
ConstructorBindingImpl.create(
|
scheduleInitialization(onInjector);
|
||||||
injector,
|
putBinding(onInjector);
|
||||||
key,
|
} catch (ErrorsException e) {
|
||||||
binding.getConstructor(),
|
errors.merge(e.getErrors());
|
||||||
source,
|
putBinding(invalidBinding(injector, key, source));
|
||||||
scoping,
|
}
|
||||||
errors,
|
return true;
|
||||||
false,
|
}
|
||||||
false);
|
|
||||||
scheduleInitialization(onInjector);
|
|
||||||
putBinding(onInjector);
|
|
||||||
} catch (ErrorsException e) {
|
|
||||||
errors.merge(e.getErrors());
|
|
||||||
putBinding(invalidBinding(injector, key, source));
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visit(InstanceBinding<? extends T> binding) {
|
public Boolean visit(InstanceBinding<? extends T> binding) {
|
||||||
prepareBinding();
|
prepareBinding();
|
||||||
Set<InjectionPoint> injectionPoints = binding.getInjectionPoints();
|
Set<InjectionPoint> injectionPoints = binding.getInjectionPoints();
|
||||||
T instance = binding.getInstance();
|
T instance = binding.getInstance();
|
||||||
@SuppressWarnings("unchecked") // safe to cast to binding<T> because
|
@SuppressWarnings("unchecked") // safe to cast to binding<T> because
|
||||||
// the processor was constructed w/ it
|
// the processor was constructed w/ it
|
||||||
Initializable<T> ref =
|
Initializable<T> ref = initializer.requestInjection(
|
||||||
initializer.requestInjection(
|
injector, instance, (Binding<T>) binding, source, injectionPoints);
|
||||||
injector, instance, (Binding<T>) binding, source, injectionPoints);
|
ConstantFactory<? extends T> factory = new ConstantFactory<T>(ref);
|
||||||
ConstantFactory<? extends T> factory = new ConstantFactory<>(ref);
|
InternalFactory<? extends T> scopedFactory
|
||||||
InternalFactory<? extends T> scopedFactory =
|
= Scoping.scope(key, injector, factory, source, scoping);
|
||||||
Scoping.scope(key, injector, factory, source, scoping);
|
putBinding(new InstanceBindingImpl<T>(injector, key, source, scopedFactory, injectionPoints,
|
||||||
putBinding(
|
instance));
|
||||||
new InstanceBindingImpl<T>(
|
return true;
|
||||||
injector, key, source, scopedFactory, injectionPoints, instance));
|
}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visit(ProviderInstanceBinding<? extends T> binding) {
|
public Boolean visit(ProviderInstanceBinding<? extends T> binding) {
|
||||||
prepareBinding();
|
prepareBinding();
|
||||||
javax.inject.Provider<? extends T> provider = binding.getUserSuppliedProvider();
|
javax.inject.Provider<? extends T> provider = binding.getUserSuppliedProvider();
|
||||||
if (provider instanceof InternalProviderInstanceBindingImpl.Factory) {
|
Set<InjectionPoint> injectionPoints = binding.getInjectionPoints();
|
||||||
@SuppressWarnings("unchecked")
|
Initializable<? extends javax.inject.Provider<? extends T>> initializable =
|
||||||
InternalProviderInstanceBindingImpl.Factory<T> asProviderMethod =
|
initializer.<javax.inject.Provider<? extends T>>requestInjection(
|
||||||
(InternalProviderInstanceBindingImpl.Factory<T>) provider;
|
injector, provider, null, source, injectionPoints);
|
||||||
return visitInternalProviderInstanceBindingFactory(asProviderMethod);
|
// always visited with Binding<T>
|
||||||
}
|
@SuppressWarnings("unchecked")
|
||||||
Set<InjectionPoint> injectionPoints = binding.getInjectionPoints();
|
InternalFactory<T> factory = new InternalFactoryToInitializableAdapter<T>(
|
||||||
Initializable<? extends javax.inject.Provider<? extends T>> initializable =
|
initializable, source,
|
||||||
initializer.<javax.inject.Provider<? extends T>>requestInjection(
|
injector.provisionListenerStore.get((ProviderInstanceBinding<T>) binding));
|
||||||
injector, provider, null, source, injectionPoints);
|
InternalFactory<? extends T> scopedFactory
|
||||||
// always visited with Binding<T>
|
= Scoping.scope(key, injector, factory, source, scoping);
|
||||||
@SuppressWarnings("unchecked")
|
putBinding(new ProviderInstanceBindingImpl<T>(injector, key, source, scopedFactory, scoping,
|
||||||
InternalFactory<T> factory =
|
provider, injectionPoints));
|
||||||
new InternalFactoryToInitializableAdapter<T>(
|
return true;
|
||||||
initializable,
|
}
|
||||||
source,
|
|
||||||
injector.provisionListenerStore.get((ProviderInstanceBinding<T>) binding));
|
|
||||||
InternalFactory<? extends T> scopedFactory =
|
|
||||||
Scoping.scope(key, injector, factory, source, scoping);
|
|
||||||
putBinding(
|
|
||||||
new ProviderInstanceBindingImpl<T>(
|
|
||||||
injector, key, source, scopedFactory, scoping, provider, injectionPoints));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visit(ProviderKeyBinding<? extends T> binding) {
|
public Boolean visit(ProviderKeyBinding<? extends T> binding) {
|
||||||
prepareBinding();
|
prepareBinding();
|
||||||
Key<? extends javax.inject.Provider<? extends T>> providerKey =
|
Key<? extends javax.inject.Provider<? extends T>> providerKey = binding.getProviderKey();
|
||||||
binding.getProviderKey();
|
// always visited with Binding<T>
|
||||||
// always visited with Binding<T>
|
@SuppressWarnings("unchecked")
|
||||||
@SuppressWarnings("unchecked")
|
BoundProviderFactory<T> boundProviderFactory = new BoundProviderFactory<T>(
|
||||||
BoundProviderFactory<T> boundProviderFactory =
|
injector, providerKey, source,
|
||||||
new BoundProviderFactory<T>(
|
injector.provisionListenerStore.get((ProviderKeyBinding<T>) binding));
|
||||||
injector,
|
bindingData.addCreationListener(boundProviderFactory);
|
||||||
providerKey,
|
InternalFactory<? extends T> scopedFactory = Scoping.scope(
|
||||||
source,
|
key, injector, (InternalFactory<? extends T>) boundProviderFactory, source, scoping);
|
||||||
injector.provisionListenerStore.get((ProviderKeyBinding<T>) binding));
|
putBinding(new LinkedProviderBindingImpl<T>(
|
||||||
processedBindingData.addCreationListener(boundProviderFactory);
|
injector, key, source, scopedFactory, scoping, providerKey));
|
||||||
InternalFactory<? extends T> scopedFactory =
|
return true;
|
||||||
Scoping.scope(
|
}
|
||||||
key,
|
|
||||||
injector,
|
|
||||||
(InternalFactory<? extends T>) boundProviderFactory,
|
|
||||||
source,
|
|
||||||
scoping);
|
|
||||||
putBinding(
|
|
||||||
new LinkedProviderBindingImpl<T>(
|
|
||||||
injector, key, source, scopedFactory, scoping, providerKey));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visit(LinkedKeyBinding<? extends T> binding) {
|
public Boolean visit(LinkedKeyBinding<? extends T> binding) {
|
||||||
prepareBinding();
|
prepareBinding();
|
||||||
Key<? extends T> linkedKey = binding.getLinkedKey();
|
Key<? extends T> linkedKey = binding.getLinkedKey();
|
||||||
if (key.equals(linkedKey)) {
|
if (key.equals(linkedKey)) {
|
||||||
// TODO: b/168656899 check for transitive recursive binding
|
errors.recursiveBinding();
|
||||||
errors.recursiveBinding(key, linkedKey);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
FactoryProxy<T> factory = new FactoryProxy<>(injector, key, linkedKey, source);
|
FactoryProxy<T> factory = new FactoryProxy<T>(injector, key, linkedKey, source);
|
||||||
processedBindingData.addCreationListener(factory);
|
bindingData.addCreationListener(factory);
|
||||||
InternalFactory<? extends T> scopedFactory =
|
InternalFactory<? extends T> scopedFactory
|
||||||
Scoping.scope(key, injector, factory, source, scoping);
|
= Scoping.scope(key, injector, factory, source, scoping);
|
||||||
putBinding(
|
putBinding(
|
||||||
new LinkedBindingImpl<T>(injector, key, source, scopedFactory, scoping, linkedKey));
|
new LinkedBindingImpl<T>(injector, key, source, scopedFactory, scoping, linkedKey));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Handle ProviderMethods specially. */
|
@Override
|
||||||
private Boolean visitInternalProviderInstanceBindingFactory(
|
public Boolean visit(UntargettedBinding<? extends T> untargetted) {
|
||||||
InternalProviderInstanceBindingImpl.Factory<T> provider) {
|
return false;
|
||||||
InternalProviderInstanceBindingImpl<T> binding =
|
}
|
||||||
new InternalProviderInstanceBindingImpl<T>(
|
|
||||||
injector,
|
|
||||||
key,
|
|
||||||
source,
|
|
||||||
provider,
|
|
||||||
Scoping.scope(key, injector, provider, source, scoping),
|
|
||||||
scoping);
|
|
||||||
switch (binding.getInitializationTiming()) {
|
|
||||||
case DELAYED:
|
|
||||||
scheduleDelayedInitialization(binding);
|
|
||||||
break;
|
|
||||||
case EAGER:
|
|
||||||
scheduleInitialization(binding);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new AssertionError();
|
|
||||||
}
|
|
||||||
putBinding(binding);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visit(UntargettedBinding<? extends T> untargetted) {
|
public Boolean visit(ExposedBinding<? extends T> binding) {
|
||||||
return false;
|
throw new IllegalArgumentException("Cannot apply a non-module element");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visit(ExposedBinding<? extends T> binding) {
|
public Boolean visit(ConvertedConstantBinding<? extends T> binding) {
|
||||||
throw new IllegalArgumentException("Cannot apply a non-module element");
|
throw new IllegalArgumentException("Cannot apply a non-module element");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visit(ConvertedConstantBinding<? extends T> binding) {
|
public Boolean visit(ProviderBinding<? extends T> binding) {
|
||||||
throw new IllegalArgumentException("Cannot apply a non-module element");
|
throw new IllegalArgumentException("Cannot apply a non-module element");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean visit(ProviderBinding<? extends T> binding) {
|
protected Boolean visitOther(Binding<? extends T> binding) {
|
||||||
throw new IllegalArgumentException("Cannot apply a non-module element");
|
throw new IllegalStateException("BindingProcessor should override all visitations");
|
||||||
}
|
}
|
||||||
|
});
|
||||||
@Override
|
|
||||||
protected Boolean visitOther(Binding<? extends T> binding) {
|
|
||||||
throw new IllegalStateException("BindingProcessor should override all visitations");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -228,14 +172,9 @@ final class BindingProcessor extends AbstractBindingProcessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> void bindExposed(PrivateElements privateElements, Key<T> key) {
|
private <T> void bindExposed(PrivateElements privateElements, Key<T> key) {
|
||||||
ExposedKeyFactory<T> exposedKeyFactory = new ExposedKeyFactory<>(key, privateElements);
|
ExposedKeyFactory<T> exposedKeyFactory = new ExposedKeyFactory<T>(key, privateElements);
|
||||||
processedBindingData.addCreationListener(exposedKeyFactory);
|
bindingData.addCreationListener(exposedKeyFactory);
|
||||||
putBinding(
|
putBinding(new ExposedBindingImpl<T>(
|
||||||
new ExposedBindingImpl<T>(
|
injector, privateElements.getExposedSource(key), key, exposedKeyFactory, privateElements));
|
||||||
injector,
|
|
||||||
privateElements.getExposedSource(key),
|
|
||||||
key,
|
|
||||||
exposedKeyFactory,
|
|
||||||
privateElements));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,17 +6,16 @@ import com.google.inject.spi.Dependency;
|
||||||
|
|
||||||
import javax.inject.Provider;
|
import javax.inject.Provider;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegates to a custom factory which is also bound in the injector.
|
* Delegates to a custom factory which is also bound in the injector.
|
||||||
*/
|
*/
|
||||||
final class BoundProviderFactory<T> extends ProviderInternalFactory<T> implements CreationListener {
|
final class BoundProviderFactory<T> extends ProviderInternalFactory<T> implements CreationListener {
|
||||||
|
|
||||||
private final Key<? extends javax.inject.Provider<? extends T>> providerKey;
|
final Key<? extends javax.inject.Provider<? extends T>> providerKey;
|
||||||
|
|
||||||
private final ProvisionListenerStackCallback<T> provisionCallback;
|
private final ProvisionListenerStackCallback<T> provisionCallback;
|
||||||
|
|
||||||
private final InjectorImpl injector;
|
private final InjectorImpl injector;
|
||||||
|
|
||||||
private InternalFactory<? extends javax.inject.Provider<? extends T>> providerFactory;
|
private InternalFactory<? extends javax.inject.Provider<? extends T>> providerFactory;
|
||||||
|
|
||||||
BoundProviderFactory(
|
BoundProviderFactory(
|
||||||
|
@ -25,12 +24,11 @@ final class BoundProviderFactory<T> extends ProviderInternalFactory<T> implement
|
||||||
Object source,
|
Object source,
|
||||||
ProvisionListenerStackCallback<T> provisionCallback) {
|
ProvisionListenerStackCallback<T> provisionCallback) {
|
||||||
super(source);
|
super(source);
|
||||||
this.provisionCallback = provisionCallback;
|
this.provisionCallback = checkNotNull(provisionCallback, "provisionCallback");
|
||||||
this.injector = injector;
|
this.injector = injector;
|
||||||
this.providerKey = providerKey;
|
this.providerKey = providerKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void notify(Errors errors) {
|
public void notify(Errors errors) {
|
||||||
try {
|
try {
|
||||||
providerFactory = injector.getInternalFactory(providerKey, errors.withSource(source), JitLimitation.NEW_OR_EXISTING_JIT);
|
providerFactory = injector.getInternalFactory(providerKey, errors.withSource(source), JitLimitation.NEW_OR_EXISTING_JIT);
|
||||||
|
@ -39,27 +37,25 @@ final class BoundProviderFactory<T> extends ProviderInternalFactory<T> implement
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public T get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked)
|
||||||
public T get(InternalContext context, Dependency<?> dependency, boolean linked)
|
throws ErrorsException {
|
||||||
throws InternalProvisionException {
|
|
||||||
context.pushState(providerKey, source);
|
context.pushState(providerKey, source);
|
||||||
try {
|
try {
|
||||||
javax.inject.Provider<? extends T> provider = providerFactory.get(context, dependency, true);
|
errors = errors.withSource(providerKey);
|
||||||
return circularGet(provider, context, dependency, provisionCallback);
|
javax.inject.Provider<? extends T> provider = providerFactory.get(errors, context, dependency, true);
|
||||||
} catch (InternalProvisionException ipe) {
|
return circularGet(provider, errors, context, dependency, provisionCallback);
|
||||||
throw ipe.addSource(providerKey);
|
|
||||||
} finally {
|
} finally {
|
||||||
context.popState();
|
context.popState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected T provision(Provider<? extends T> provider, Dependency<?> dependency,
|
protected T provision(Provider<? extends T> provider, Errors errors, Dependency<?> dependency,
|
||||||
ConstructionContext<T> constructionContext) throws InternalProvisionException {
|
ConstructionContext<T> constructionContext) throws ErrorsException {
|
||||||
try {
|
try {
|
||||||
return super.provision(provider, dependency, constructionContext);
|
return super.provision(provider, errors, dependency, constructionContext);
|
||||||
} catch (RuntimeException userException) {
|
} catch (RuntimeException userException) {
|
||||||
throw InternalProvisionException.errorInProvider(userException);
|
throw errors.errorInProvider(userException).toException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,85 +0,0 @@
|
||||||
package com.google.inject.internal;
|
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
|
||||||
import com.google.common.collect.Streams;
|
|
||||||
import com.google.inject.Key;
|
|
||||||
import com.google.inject.spi.ErrorDetail;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Formatter;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Error reported by Guice when a key is already bound in one or more child injectors or private
|
|
||||||
* modules.
|
|
||||||
*/
|
|
||||||
final class ChildBindingAlreadySetError extends InternalErrorDetail<ChildBindingAlreadySetError> {
|
|
||||||
private final Key<?> key;
|
|
||||||
private final ImmutableList<Object> existingSources;
|
|
||||||
|
|
||||||
ChildBindingAlreadySetError(Key<?> key, Iterable<Object> existingSoruces, List<Object> sources) {
|
|
||||||
super(
|
|
||||||
ErrorId.CHILD_BINDING_ALREADY_SET,
|
|
||||||
String.format(
|
|
||||||
"Unable to create binding for %s because it was already configured on one or more"
|
|
||||||
+ " child injectors or private modules.",
|
|
||||||
Messages.convert(key)),
|
|
||||||
sources,
|
|
||||||
null);
|
|
||||||
this.key = key;
|
|
||||||
// Can't use ImmutableList.toImmutableList here because of b/156759807.
|
|
||||||
this.existingSources =
|
|
||||||
ImmutableList.copyOf(
|
|
||||||
Streams.stream(existingSoruces)
|
|
||||||
.map(source -> source == null ? "" : source)
|
|
||||||
.collect(Collectors.toList()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isMergeable(ErrorDetail<?> otherError) {
|
|
||||||
return otherError instanceof ChildBindingAlreadySetError
|
|
||||||
&& ((ChildBindingAlreadySetError) otherError).key.equals(this.key);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void formatDetail(List<ErrorDetail<?>> mergeableErrors, Formatter formatter) {
|
|
||||||
formatter.format("%n%s%n", Messages.bold("Bound at:"));
|
|
||||||
int index = 1;
|
|
||||||
for (Object source : existingSources) {
|
|
||||||
formatter.format("%-2s: ", index++);
|
|
||||||
if (source.equals("")) {
|
|
||||||
formatter.format("as a just-in-time binding%n");
|
|
||||||
} else {
|
|
||||||
new SourceFormatter(source, formatter, /* omitPreposition= */ true).format();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
List<List<Object>> sourcesList = new ArrayList<>();
|
|
||||||
sourcesList.add(getSources());
|
|
||||||
mergeableErrors.forEach(error -> sourcesList.add(error.getSources()));
|
|
||||||
|
|
||||||
List<List<Object>> filteredSources =
|
|
||||||
sourcesList.stream()
|
|
||||||
.map(this::trimSource)
|
|
||||||
.filter(list -> !list.isEmpty())
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
if (!filteredSources.isEmpty()) {
|
|
||||||
formatter.format("%n%s%n", Messages.bold("Requested by:"));
|
|
||||||
for (int i = 0; i < sourcesList.size(); i++) {
|
|
||||||
ErrorFormatter.formatSources(i + 1, sourcesList.get(i), formatter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(b/151482394): Detect if the key was bound in any PrivateModule and suggest exposing the
|
|
||||||
// key in those cases.
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ChildBindingAlreadySetError withSources(List<Object> newSources) {
|
|
||||||
return new ChildBindingAlreadySetError(key, existingSources, newSources);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Omit the key itself in the source list since the information is redundant. */
|
|
||||||
private List<Object> trimSource(List<Object> sources) {
|
|
||||||
return sources.stream().filter(source -> !source.equals(this.key)).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -6,6 +6,7 @@ import com.google.inject.Key;
|
||||||
import com.google.inject.binder.AnnotatedConstantBindingBuilder;
|
import com.google.inject.binder.AnnotatedConstantBindingBuilder;
|
||||||
import com.google.inject.binder.ConstantBindingBuilder;
|
import com.google.inject.binder.ConstantBindingBuilder;
|
||||||
import com.google.inject.spi.Element;
|
import com.google.inject.spi.Element;
|
||||||
|
import com.google.inject.spi.InjectionPoint;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -23,69 +24,56 @@ public final class ConstantBindingBuilderImpl<T>
|
||||||
super(binder, elements, source, (Key<T>) NULL_KEY);
|
super(binder, elements, source, (Key<T>) NULL_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ConstantBindingBuilder annotatedWith(Class<? extends Annotation> annotationType) {
|
public ConstantBindingBuilder annotatedWith(Class<? extends Annotation> annotationType) {
|
||||||
annotatedWithInternal(annotationType);
|
annotatedWithInternal(annotationType);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ConstantBindingBuilder annotatedWith(Annotation annotation) {
|
public ConstantBindingBuilder annotatedWith(Annotation annotation) {
|
||||||
annotatedWithInternal(annotation);
|
annotatedWithInternal(annotation);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void to(final String value) {
|
public void to(final String value) {
|
||||||
toConstant(String.class, value);
|
toConstant(String.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void to(final int value) {
|
public void to(final int value) {
|
||||||
toConstant(Integer.class, value);
|
toConstant(Integer.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void to(final long value) {
|
public void to(final long value) {
|
||||||
toConstant(Long.class, value);
|
toConstant(Long.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void to(final boolean value) {
|
public void to(final boolean value) {
|
||||||
toConstant(Boolean.class, value);
|
toConstant(Boolean.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void to(final double value) {
|
public void to(final double value) {
|
||||||
toConstant(Double.class, value);
|
toConstant(Double.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void to(final float value) {
|
public void to(final float value) {
|
||||||
toConstant(Float.class, value);
|
toConstant(Float.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void to(final short value) {
|
public void to(final short value) {
|
||||||
toConstant(Short.class, value);
|
toConstant(Short.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void to(final char value) {
|
public void to(final char value) {
|
||||||
toConstant(Character.class, value);
|
toConstant(Character.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void to(final byte value) {
|
public void to(final byte value) {
|
||||||
toConstant(Byte.class, value);
|
toConstant(Byte.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void to(final Class<?> value) {
|
public void to(final Class<?> value) {
|
||||||
toConstant(Class.class, value);
|
toConstant(Class.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <E extends Enum<E>> void to(final E value) {
|
public <E extends Enum<E>> void to(final E value) {
|
||||||
toConstant(value.getDeclaringClass(), value);
|
toConstant(value.getDeclaringClass(), value);
|
||||||
}
|
}
|
||||||
|
@ -117,7 +105,7 @@ public final class ConstantBindingBuilderImpl<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
setBinding(new InstanceBindingImpl<T>(
|
setBinding(new InstanceBindingImpl<T>(
|
||||||
base.getSource(), key, base.getScoping(), ImmutableSet.of(), instanceAsT));
|
base.getSource(), key, base.getScoping(), ImmutableSet.<InjectionPoint>of(), instanceAsT));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -7,17 +7,15 @@ final class ConstantFactory<T> implements InternalFactory<T> {
|
||||||
|
|
||||||
private final Initializable<T> initializable;
|
private final Initializable<T> initializable;
|
||||||
|
|
||||||
ConstantFactory(Initializable<T> initializable) {
|
public ConstantFactory(Initializable<T> initializable) {
|
||||||
this.initializable = initializable;
|
this.initializable = initializable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public T get(Errors errors, InternalContext context, Dependency dependency, boolean linked)
|
||||||
public T get(InternalContext context, Dependency<?> dependency, boolean linked)
|
throws ErrorsException {
|
||||||
throws InternalProvisionException {
|
return initializable.get(errors);
|
||||||
return initializable.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return MoreObjects.toStringHelper(ConstantFactory.class)
|
return MoreObjects.toStringHelper(ConstantFactory.class)
|
||||||
.add("value", initializable)
|
.add("value", initializable)
|
||||||
|
|
|
@ -41,26 +41,26 @@ final class ConstructionContext<T> {
|
||||||
invocationHandlers = null;
|
invocationHandlers = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object createProxy(InjectorOptions injectorOptions,
|
public Object createProxy(Errors errors, InjectorOptions injectorOptions,
|
||||||
Class<?> expectedType) throws InternalProvisionException {
|
Class<?> expectedType) throws ErrorsException {
|
||||||
if (injectorOptions.disableCircularProxies) {
|
if (injectorOptions.disableCircularProxies) {
|
||||||
throw InternalProvisionException.circularDependenciesDisabled(expectedType);
|
throw errors.circularProxiesDisabled(expectedType).toException();
|
||||||
}
|
}
|
||||||
if (!expectedType.isInterface()) {
|
if (!expectedType.isInterface()) {
|
||||||
throw InternalProvisionException.cannotProxyClass(expectedType);
|
throw errors.cannotSatisfyCircularDependency(expectedType).toException();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (invocationHandlers == null) {
|
if (invocationHandlers == null) {
|
||||||
invocationHandlers = new ArrayList<>();
|
invocationHandlers = new ArrayList<DelegatingInvocationHandler<T>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
DelegatingInvocationHandler<T> invocationHandler = new DelegatingInvocationHandler<>();
|
DelegatingInvocationHandler<T> invocationHandler = new DelegatingInvocationHandler<T>();
|
||||||
invocationHandlers.add(invocationHandler);
|
invocationHandlers.add(invocationHandler);
|
||||||
|
|
||||||
ClassLoader classLoader = expectedType.getClass().getClassLoader() != null ?
|
ClassLoader classLoader = expectedType.getClass().getClassLoader() != null ?
|
||||||
expectedType.getClass().getClassLoader() : ClassLoader.getSystemClassLoader();
|
expectedType.getClass().getClassLoader() : ClassLoader.getSystemClassLoader();
|
||||||
return expectedType.cast(Proxy.newProxyInstance(classLoader,
|
return expectedType.cast(Proxy.newProxyInstance(classLoader,
|
||||||
new Class<?>[]{expectedType, CircularDependencyProxy.class}, invocationHandler));
|
new Class[]{expectedType, CircularDependencyProxy.class}, invocationHandler));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProxyDelegates(T delegate) {
|
public void setProxyDelegates(T delegate) {
|
||||||
|
|
|
@ -1,15 +1,11 @@
|
||||||
package com.google.inject.internal;
|
package com.google.inject.internal;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkState;
|
|
||||||
import static com.google.inject.internal.Annotations.findScopeAnnotation;
|
|
||||||
import static com.google.inject.internal.GuiceInternal.GUICE_INTERNAL;
|
|
||||||
import static com.google.inject.spi.Elements.withTrustedSource;
|
|
||||||
|
|
||||||
import com.google.common.base.MoreObjects;
|
import com.google.common.base.MoreObjects;
|
||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.inject.Binder;
|
import com.google.inject.Binder;
|
||||||
import com.google.inject.ConfigurationException;
|
import com.google.inject.ConfigurationException;
|
||||||
|
import com.google.inject.Inject;
|
||||||
import com.google.inject.Key;
|
import com.google.inject.Key;
|
||||||
import com.google.inject.TypeLiteral;
|
import com.google.inject.TypeLiteral;
|
||||||
import com.google.inject.internal.util.Classes;
|
import com.google.inject.internal.util.Classes;
|
||||||
|
@ -17,70 +13,59 @@ import com.google.inject.spi.BindingTargetVisitor;
|
||||||
import com.google.inject.spi.ConstructorBinding;
|
import com.google.inject.spi.ConstructorBinding;
|
||||||
import com.google.inject.spi.Dependency;
|
import com.google.inject.spi.Dependency;
|
||||||
import com.google.inject.spi.InjectionPoint;
|
import com.google.inject.spi.InjectionPoint;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
|
import static com.google.inject.internal.Annotations.findScopeAnnotation;
|
||||||
|
|
||||||
final class ConstructorBindingImpl<T> extends BindingImpl<T>
|
final class ConstructorBindingImpl<T> extends BindingImpl<T>
|
||||||
implements ConstructorBinding<T>, DelayedInitialize {
|
implements ConstructorBinding<T>, DelayedInitialize {
|
||||||
|
|
||||||
private final Factory<T> factory;
|
private final Factory<T> factory;
|
||||||
private final InjectionPoint constructorInjectionPoint;
|
private final InjectionPoint constructorInjectionPoint;
|
||||||
|
|
||||||
private ConstructorBindingImpl(
|
private ConstructorBindingImpl(InjectorImpl injector, Key<T> key, Object source,
|
||||||
InjectorImpl injector,
|
InternalFactory<? extends T> scopedFactory, Scoping scoping, Factory<T> factory,
|
||||||
Key<T> key,
|
InjectionPoint constructorInjectionPoint) {
|
||||||
Object source,
|
|
||||||
InternalFactory<? extends T> scopedFactory,
|
|
||||||
Scoping scoping,
|
|
||||||
Factory<T> factory,
|
|
||||||
InjectionPoint constructorInjectionPoint) {
|
|
||||||
super(injector, key, source, scopedFactory, scoping);
|
super(injector, key, source, scopedFactory, scoping);
|
||||||
this.factory = factory;
|
this.factory = factory;
|
||||||
this.constructorInjectionPoint = constructorInjectionPoint;
|
this.constructorInjectionPoint = constructorInjectionPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConstructorBindingImpl(
|
public ConstructorBindingImpl(Key<T> key, Object source, Scoping scoping,
|
||||||
Key<T> key,
|
InjectionPoint constructorInjectionPoint, Set<InjectionPoint> injectionPoints) {
|
||||||
Object source,
|
|
||||||
Scoping scoping,
|
|
||||||
InjectionPoint constructorInjectionPoint,
|
|
||||||
Set<InjectionPoint> injectionPoints) {
|
|
||||||
super(source, key, scoping);
|
super(source, key, scoping);
|
||||||
this.factory = new Factory<>(false, key);
|
this.factory = new Factory<T>(false, key);
|
||||||
ConstructionProxy<T> constructionProxy =
|
ConstructionProxy<T> constructionProxy
|
||||||
new DefaultConstructionProxyFactory<T>(constructorInjectionPoint).create();
|
= new DefaultConstructionProxyFactory<T>(constructorInjectionPoint).create();
|
||||||
this.constructorInjectionPoint = constructorInjectionPoint;
|
this.constructorInjectionPoint = constructorInjectionPoint;
|
||||||
factory.constructorInjector =
|
factory.constructorInjector = new ConstructorInjector<T>(
|
||||||
new ConstructorInjector<T>(injectionPoints, constructionProxy, null, null);
|
injectionPoints, constructionProxy, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param constructorInjector the constructor to use, or {@code null} to use the default.
|
* @param constructorInjector the constructor to use, or {@code null} to use the default.
|
||||||
* @param failIfNotLinked true if this ConstructorBindingImpl's InternalFactory should only
|
* @param failIfNotLinked true if this ConstructorBindingImpl's InternalFactory should
|
||||||
* succeed if retrieved from a linked binding
|
* only succeed if retrieved from a linked binding
|
||||||
*/
|
*/
|
||||||
static <T> ConstructorBindingImpl<T> create(
|
static <T> ConstructorBindingImpl<T> create(InjectorImpl injector, Key<T> key,
|
||||||
InjectorImpl injector,
|
InjectionPoint constructorInjector, Object source, Scoping scoping, Errors errors,
|
||||||
Key<T> key,
|
boolean failIfNotLinked, boolean failIfNotExplicit)
|
||||||
InjectionPoint constructorInjector,
|
|
||||||
Object source,
|
|
||||||
Scoping scoping,
|
|
||||||
Errors errors,
|
|
||||||
boolean failIfNotLinked,
|
|
||||||
boolean atInjectRequired)
|
|
||||||
throws ErrorsException {
|
throws ErrorsException {
|
||||||
int numErrors = errors.size();
|
int numErrors = errors.size();
|
||||||
|
|
||||||
Class<?> rawType =
|
@SuppressWarnings("unchecked") // constructorBinding guarantees type is consistent
|
||||||
constructorInjector == null
|
Class<? super T> rawType = constructorInjector == null
|
||||||
? key.getTypeLiteral().getRawType()
|
? key.getTypeLiteral().getRawType()
|
||||||
: constructorInjector.getDeclaringType().getRawType();
|
: (Class) constructorInjector.getDeclaringType().getRawType();
|
||||||
|
|
||||||
// We can't inject abstract classes.
|
// We can't inject abstract classes.
|
||||||
if (Modifier.isAbstract(rawType.getModifiers())) {
|
if (Modifier.isAbstract(rawType.getModifiers())) {
|
||||||
errors.missingImplementationWithHint(key, injector);
|
errors.missingImplementation(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error: Inner class.
|
// Error: Inner class.
|
||||||
|
@ -93,8 +78,10 @@ final class ConstructorBindingImpl<T> extends BindingImpl<T>
|
||||||
// Find a constructor annotated @Inject
|
// Find a constructor annotated @Inject
|
||||||
if (constructorInjector == null) {
|
if (constructorInjector == null) {
|
||||||
try {
|
try {
|
||||||
constructorInjector =
|
constructorInjector = InjectionPoint.forConstructorOf(key.getTypeLiteral());
|
||||||
InjectionPoint.forConstructorOf(key.getTypeLiteral(), atInjectRequired);
|
if (failIfNotExplicit && !hasAtInject((Constructor) constructorInjector.getMember())) {
|
||||||
|
errors.atInjectRequired(rawType);
|
||||||
|
}
|
||||||
} catch (ConfigurationException e) {
|
} catch (ConfigurationException e) {
|
||||||
throw errors.merge(e.getErrorMessages()).toException();
|
throw errors.merge(e.getErrorMessages()).toException();
|
||||||
}
|
}
|
||||||
|
@ -105,36 +92,47 @@ final class ConstructorBindingImpl<T> extends BindingImpl<T>
|
||||||
Class<?> annotatedType = constructorInjector.getMember().getDeclaringClass();
|
Class<?> annotatedType = constructorInjector.getMember().getDeclaringClass();
|
||||||
Class<? extends Annotation> scopeAnnotation = findScopeAnnotation(errors, annotatedType);
|
Class<? extends Annotation> scopeAnnotation = findScopeAnnotation(errors, annotatedType);
|
||||||
if (scopeAnnotation != null) {
|
if (scopeAnnotation != null) {
|
||||||
scoping =
|
scoping = Scoping.makeInjectable(Scoping.forAnnotation(scopeAnnotation),
|
||||||
Scoping.makeInjectable(
|
injector, errors.withSource(rawType));
|
||||||
Scoping.forAnnotation(scopeAnnotation), injector, errors.withSource(rawType));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
errors.throwIfNewErrors(numErrors);
|
errors.throwIfNewErrors(numErrors);
|
||||||
|
|
||||||
Factory<T> factoryFactory = new Factory<>(failIfNotLinked, key);
|
Factory<T> factoryFactory = new Factory<T>(failIfNotLinked, key);
|
||||||
InternalFactory<? extends T> scopedFactory =
|
InternalFactory<? extends T> scopedFactory
|
||||||
Scoping.scope(key, injector, factoryFactory, source, scoping);
|
= Scoping.scope(key, injector, factoryFactory, source, scoping);
|
||||||
|
|
||||||
return new ConstructorBindingImpl<T>(
|
return new ConstructorBindingImpl<T>(
|
||||||
injector, key, source, scopedFactory, scoping, factoryFactory, constructorInjector);
|
injector, key, source, scopedFactory, scoping, factoryFactory, constructorInjector);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
|
* Returns true if the inject annotation is on the constructor.
|
||||||
|
*/
|
||||||
|
private static boolean hasAtInject(Constructor cxtor) {
|
||||||
|
return cxtor.isAnnotationPresent(Inject.class)
|
||||||
|
|| cxtor.isAnnotationPresent(javax.inject.Inject.class);
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked") // the result type always agrees with the ConstructorInjector type
|
@SuppressWarnings("unchecked") // the result type always agrees with the ConstructorInjector type
|
||||||
public void initialize(InjectorImpl injector, Errors errors) throws ErrorsException {
|
public void initialize(InjectorImpl injector, Errors errors) throws ErrorsException {
|
||||||
factory.constructorInjector =
|
factory.constructorInjector =
|
||||||
(ConstructorInjector<T>) injector.constructors.get(constructorInjectionPoint, errors);
|
(ConstructorInjector<T>) injector.constructors.get(constructorInjectionPoint, errors);
|
||||||
factory.provisionCallback = injector.provisionListenerStore.get(this);
|
factory.provisionCallback =
|
||||||
|
injector.provisionListenerStore.get(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** True if this binding has been initialized and is ready for use. */
|
/**
|
||||||
|
* True if this binding has been initialized and is ready for use.
|
||||||
|
*/
|
||||||
boolean isInitialized() {
|
boolean isInitialized() {
|
||||||
return factory.constructorInjector != null;
|
return factory.constructorInjector != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns an injection point that can be used to clean up the constructor store. */
|
/**
|
||||||
|
* Returns an injection point that can be used to clean up the constructor store.
|
||||||
|
*/
|
||||||
InjectionPoint getInternalConstructor() {
|
InjectionPoint getInternalConstructor() {
|
||||||
if (factory.constructorInjector != null) {
|
if (factory.constructorInjector != null) {
|
||||||
return factory.constructorInjector.getConstructionProxy().getInjectionPoint();
|
return factory.constructorInjector.getConstructionProxy().getInjectionPoint();
|
||||||
|
@ -143,51 +141,47 @@ final class ConstructorBindingImpl<T> extends BindingImpl<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns a set of dependencies that can be iterated over to clean up stray JIT bindings. */
|
/**
|
||||||
|
* Returns a set of dependencies that can be iterated over to clean up stray JIT bindings.
|
||||||
|
*/
|
||||||
Set<Dependency<?>> getInternalDependencies() {
|
Set<Dependency<?>> getInternalDependencies() {
|
||||||
ImmutableSet.Builder<InjectionPoint> builder = ImmutableSet.builder();
|
ImmutableSet.Builder<InjectionPoint> builder = ImmutableSet.builder();
|
||||||
if (factory.constructorInjector == null) {
|
if (factory.constructorInjector == null) {
|
||||||
builder.add(constructorInjectionPoint);
|
builder.add(constructorInjectionPoint);
|
||||||
|
// If the below throws, it's OK -- we just ignore those dependencies, because no one
|
||||||
|
// could have used them anyway.
|
||||||
try {
|
try {
|
||||||
builder.addAll(
|
builder.addAll(InjectionPoint.forInstanceMethodsAndFields(constructorInjectionPoint.getDeclaringType()));
|
||||||
InjectionPoint.forInstanceMethodsAndFields(
|
|
||||||
constructorInjectionPoint.getDeclaringType()));
|
|
||||||
} catch (ConfigurationException ignored) {
|
} catch (ConfigurationException ignored) {
|
||||||
// This is OK -- we just ignore those dependencies, because no one could have used them
|
|
||||||
// anyway.
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
builder.add(getConstructor()).addAll(getInjectableMembers());
|
builder.add(getConstructor())
|
||||||
|
.addAll(getInjectableMembers());
|
||||||
}
|
}
|
||||||
|
|
||||||
return Dependency.forInjectionPoints(builder.build());
|
return Dependency.forInjectionPoints(builder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
|
public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
|
||||||
checkState(factory.constructorInjector != null, "not initialized");
|
checkState(factory.constructorInjector != null, "not initialized");
|
||||||
return visitor.visit(this);
|
return visitor.visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public InjectionPoint getConstructor() {
|
public InjectionPoint getConstructor() {
|
||||||
checkState(factory.constructorInjector != null, "Binding is not ready");
|
checkState(factory.constructorInjector != null, "Binding is not ready");
|
||||||
return factory.constructorInjector.getConstructionProxy().getInjectionPoint();
|
return factory.constructorInjector.getConstructionProxy().getInjectionPoint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<InjectionPoint> getInjectableMembers() {
|
public Set<InjectionPoint> getInjectableMembers() {
|
||||||
checkState(factory.constructorInjector != null, "Binding is not ready");
|
checkState(factory.constructorInjector != null, "Binding is not ready");
|
||||||
return factory.constructorInjector.getInjectableMembers();
|
return factory.constructorInjector.getInjectableMembers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<Dependency<?>> getDependencies() {
|
public Set<Dependency<?>> getDependencies() {
|
||||||
return Dependency.forInjectionPoints(
|
return Dependency.forInjectionPoints(new ImmutableSet.Builder<InjectionPoint>()
|
||||||
new ImmutableSet.Builder<InjectionPoint>()
|
.add(getConstructor())
|
||||||
.add(getConstructor())
|
.addAll(getInjectableMembers())
|
||||||
.addAll(getInjectableMembers())
|
.build());
|
||||||
.build());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -202,17 +196,11 @@ final class ConstructorBindingImpl<T> extends BindingImpl<T>
|
||||||
null, key, getSource(), factory, getScoping(), factory, constructorInjectionPoint);
|
null, key, getSource(), factory, getScoping(), factory, constructorInjectionPoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked") // the raw constructor member and declaring type always agree
|
@SuppressWarnings("unchecked") // the raw constructor member and declaring type always agree
|
||||||
public void applyTo(Binder binder) {
|
public void applyTo(Binder binder) {
|
||||||
InjectionPoint constructor = getConstructor();
|
InjectionPoint constructor = getConstructor();
|
||||||
getScoping()
|
getScoping().applyTo(binder.withSource(getSource()).bind(getKey()).toConstructor(
|
||||||
.applyTo(
|
(Constructor) getConstructor().getMember(), (TypeLiteral) constructor.getDeclaringType()));
|
||||||
withTrustedSource(GUICE_INTERNAL, binder, getSource())
|
|
||||||
.bind(getKey())
|
|
||||||
.toConstructor(
|
|
||||||
(Constructor) getConstructor().getMember(),
|
|
||||||
(TypeLiteral) constructor.getDeclaringType()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -252,22 +240,19 @@ final class ConstructorBindingImpl<T> extends BindingImpl<T>
|
||||||
this.key = key;
|
this.key = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public T get(InternalContext context, Dependency<?> dependency, boolean linked)
|
public T get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked)
|
||||||
throws InternalProvisionException {
|
throws ErrorsException {
|
||||||
ConstructorInjector<T> localInjector = constructorInjector;
|
checkState(constructorInjector != null, "Constructor not ready");
|
||||||
if (localInjector == null) {
|
|
||||||
throw new IllegalStateException("Constructor not ready");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!linked && failIfNotLinked) {
|
if (failIfNotLinked && !linked) {
|
||||||
throw InternalProvisionException.jitDisabled(key);
|
throw errors.jitDisabled(key).toException();
|
||||||
}
|
}
|
||||||
|
|
||||||
// This may not actually be safe because it could return a super type of T (if that's all the
|
// This may not actually be safe because it could return a super type of T (if that's all the
|
||||||
// client needs), but it should be OK in practice thanks to the wonders of erasure.
|
// client needs), but it should be OK in practice thanks to the wonders of erasure.
|
||||||
return (T) localInjector.construct(context, dependency, provisionCallback);
|
return (T) constructorInjector.construct(errors, context,
|
||||||
|
dependency.getKey().getTypeLiteral().getRawType(), provisionCallback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package com.google.inject.internal;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.inject.internal.ProvisionListenerStackCallback.ProvisionCallback;
|
import com.google.inject.internal.ProvisionListenerStackCallback.ProvisionCallback;
|
||||||
import com.google.inject.spi.Dependency;
|
|
||||||
import com.google.inject.spi.InjectionPoint;
|
import com.google.inject.spi.InjectionPoint;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
@ -41,39 +40,37 @@ final class ConstructorInjector<T> {
|
||||||
* Construct an instance. Returns {@code Object} instead of {@code T} because
|
* Construct an instance. Returns {@code Object} instead of {@code T} because
|
||||||
* it may return a proxy.
|
* it may return a proxy.
|
||||||
*/
|
*/
|
||||||
Object construct(final InternalContext context,
|
Object construct(final Errors errors, final InternalContext context,
|
||||||
Dependency<?> dependency,
|
Class<?> expectedType,
|
||||||
ProvisionListenerStackCallback<T> provisionCallback)
|
ProvisionListenerStackCallback<T> provisionCallback)
|
||||||
throws InternalProvisionException {
|
throws ErrorsException {
|
||||||
final ConstructionContext<T> constructionContext = context.getConstructionContext(this);
|
final ConstructionContext<T> constructionContext = context.getConstructionContext(this);
|
||||||
|
|
||||||
// We have a circular reference between constructors. Return a proxy.
|
// We have a circular reference between constructors. Return a proxy.
|
||||||
if (constructionContext.isConstructing()) {
|
if (constructionContext.isConstructing()) {
|
||||||
// TODO (crazybob): if we can't proxy this object, can we proxy the other object?
|
// TODO (crazybob): if we can't proxy this object, can we proxy the other object?
|
||||||
return constructionContext.createProxy(
|
return constructionContext.createProxy(
|
||||||
context.getInjectorOptions(), dependency.getKey().getTypeLiteral().getRawType());
|
errors, context.getInjectorOptions(), expectedType);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we're re-entering this factory while injecting fields or methods,
|
// If we're re-entering this factory while injecting fields or methods,
|
||||||
// return the same instance. This prevents infinite loops.
|
// return the same instance. This prevents infinite loops.
|
||||||
T t = constructionContext.getCurrentReference();
|
T t = constructionContext.getCurrentReference();
|
||||||
if (t != null) {
|
if (t != null) {
|
||||||
if (context.getInjectorOptions().disableCircularProxies) {
|
return t;
|
||||||
throw InternalProvisionException.circularDependenciesDisabled(
|
|
||||||
dependency.getKey().getTypeLiteral().getRawType());
|
|
||||||
} else {
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
constructionContext.startConstruction();
|
constructionContext.startConstruction();
|
||||||
try {
|
try {
|
||||||
// Optimization: Don't go through the callback stack if we have no listeners.
|
// Optimization: Don't go through the callback stack if we have no listeners.
|
||||||
if (provisionCallback == null) {
|
if (!provisionCallback.hasListeners()) {
|
||||||
return provision(context, constructionContext);
|
return provision(errors, context, constructionContext);
|
||||||
} else {
|
} else {
|
||||||
return provisionCallback.provision(context, () ->
|
return provisionCallback.provision(errors, context, new ProvisionCallback<T>() {
|
||||||
provision(context, constructionContext));
|
public T call() throws ErrorsException {
|
||||||
|
return provision(errors, context, constructionContext);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
constructionContext.finishConstruction();
|
constructionContext.finishConstruction();
|
||||||
|
@ -83,12 +80,12 @@ final class ConstructorInjector<T> {
|
||||||
/**
|
/**
|
||||||
* Provisions a new T.
|
* Provisions a new T.
|
||||||
*/
|
*/
|
||||||
private T provision(InternalContext context, ConstructionContext<T> constructionContext)
|
private T provision(Errors errors, InternalContext context,
|
||||||
throws InternalProvisionException {
|
ConstructionContext<T> constructionContext) throws ErrorsException {
|
||||||
try {
|
try {
|
||||||
T t;
|
T t;
|
||||||
try {
|
try {
|
||||||
Object[] parameters = SingleParameterInjector.getAll(context, parameterInjectors);
|
Object[] parameters = SingleParameterInjector.getAll(errors, context, parameterInjectors);
|
||||||
t = constructionProxy.newInstance(parameters);
|
t = constructionProxy.newInstance(parameters);
|
||||||
constructionContext.setProxyDelegates(t);
|
constructionContext.setProxyDelegates(t);
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -97,17 +94,17 @@ final class ConstructorInjector<T> {
|
||||||
|
|
||||||
// Store reference. If an injector re-enters this factory, they'll get the same reference.
|
// Store reference. If an injector re-enters this factory, they'll get the same reference.
|
||||||
constructionContext.setCurrentReference(t);
|
constructionContext.setCurrentReference(t);
|
||||||
MembersInjectorImpl<T> localMembersInjector = membersInjector;
|
|
||||||
localMembersInjector.injectMembers(t, context, false);
|
membersInjector.injectMembers(t, errors, context, false);
|
||||||
localMembersInjector.notifyListeners(t);
|
membersInjector.notifyListeners(t, errors);
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
} catch (InvocationTargetException userException) {
|
} catch (InvocationTargetException userException) {
|
||||||
Throwable cause = userException.getCause() != null
|
Throwable cause = userException.getCause() != null
|
||||||
? userException.getCause()
|
? userException.getCause()
|
||||||
: userException;
|
: userException;
|
||||||
throw InternalProvisionException.errorInjectingConstructor(cause)
|
throw errors.withSource(constructionProxy.getInjectionPoint())
|
||||||
.addSource(constructionProxy.getInjectionPoint());
|
.errorInjectingConstructor(cause).toException();
|
||||||
} finally {
|
} finally {
|
||||||
constructionContext.removeCurrentReference();
|
constructionContext.removeCurrentReference();
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,8 @@ import com.google.inject.spi.InjectionPoint;
|
||||||
final class ConstructorInjectorStore {
|
final class ConstructorInjectorStore {
|
||||||
private final InjectorImpl injector;
|
private final InjectorImpl injector;
|
||||||
|
|
||||||
private final FailableCache<InjectionPoint, ConstructorInjector<?>> cache =
|
private final FailableCache<InjectionPoint, ConstructorInjector<?>> cache
|
||||||
new FailableCache<>() {
|
= new FailableCache<InjectionPoint, ConstructorInjector<?>>() {
|
||||||
@Override
|
@Override
|
||||||
protected ConstructorInjector<?> create(InjectionPoint constructorInjector, Errors errors)
|
protected ConstructorInjector<?> create(InjectionPoint constructorInjector, Errors errors)
|
||||||
throws ErrorsException {
|
throws ErrorsException {
|
||||||
|
@ -46,12 +46,12 @@ final class ConstructorInjectorStore {
|
||||||
throws ErrorsException {
|
throws ErrorsException {
|
||||||
int numErrorsBefore = errors.size();
|
int numErrorsBefore = errors.size();
|
||||||
|
|
||||||
SingleParameterInjector<?>[] constructorParameterInjectors =
|
SingleParameterInjector<?>[] constructorParameterInjectors
|
||||||
injector.getParametersInjectors(injectionPoint.getDependencies(), errors);
|
= injector.getParametersInjectors(injectionPoint.getDependencies(), errors);
|
||||||
|
|
||||||
@SuppressWarnings("unchecked") // the injector type agrees with the injection point type
|
@SuppressWarnings("unchecked") // the injector type agrees with the injection point type
|
||||||
MembersInjectorImpl<T> membersInjector = (MembersInjectorImpl<T>)
|
MembersInjectorImpl<T> membersInjector = (MembersInjectorImpl<T>) injector.membersInjectorStore
|
||||||
injector.membersInjectorStore.get(injectionPoint.getDeclaringType(), errors);
|
.get(injectionPoint.getDeclaringType(), errors);
|
||||||
|
|
||||||
ConstructionProxyFactory<T> factory = new DefaultConstructionProxyFactory<T>(injectionPoint);
|
ConstructionProxyFactory<T> factory = new DefaultConstructionProxyFactory<T>(injectionPoint);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.google.inject.internal;
|
||||||
|
|
||||||
|
interface ContextualCallable<T> {
|
||||||
|
T call(InternalContext context) throws ErrorsException;
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package com.google.inject.internal;
|
package com.google.inject.internal;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
import com.google.common.base.Supplier;
|
||||||
import com.google.common.collect.ImmutableListMultimap;
|
import com.google.common.collect.ImmutableListMultimap;
|
||||||
import com.google.common.collect.LinkedHashMultimap;
|
import com.google.common.collect.LinkedHashMultimap;
|
||||||
import com.google.common.collect.ListMultimap;
|
import com.google.common.collect.ListMultimap;
|
||||||
|
@ -11,6 +12,7 @@ import com.google.common.collect.Multimaps;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
@ -41,7 +43,7 @@ interface CycleDetectingLock<ID> {
|
||||||
* In case no cycle is detected performance is O(threads creating singletons),
|
* In case no cycle is detected performance is O(threads creating singletons),
|
||||||
* in case cycle is detected performance is O(singleton locks).
|
* in case cycle is detected performance is O(singleton locks).
|
||||||
*/
|
*/
|
||||||
ListMultimap<Thread, ID> lockOrDetectPotentialLocksCycle();
|
ListMultimap<Long, ID> lockOrDetectPotentialLocksCycle();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unlocks previously locked lock.
|
* Unlocks previously locked lock.
|
||||||
|
@ -80,7 +82,7 @@ interface CycleDetectingLock<ID> {
|
||||||
*
|
*
|
||||||
* Guarded by {@code this}.
|
* Guarded by {@code this}.
|
||||||
*/
|
*/
|
||||||
private static final Multimap<Thread, ReentrantCycleDetectingLock<?>> locksOwnedByThread =
|
private final Multimap<Long, ReentrantCycleDetectingLock> locksOwnedByThread =
|
||||||
LinkedHashMultimap.create();
|
LinkedHashMultimap.create();
|
||||||
/**
|
/**
|
||||||
* Specifies lock that thread is currently waiting on to own it.
|
* Specifies lock that thread is currently waiting on to own it.
|
||||||
|
@ -98,22 +100,22 @@ interface CycleDetectingLock<ID> {
|
||||||
*
|
*
|
||||||
* Guarded by {@code this}.
|
* Guarded by {@code this}.
|
||||||
*/
|
*/
|
||||||
private static Map<Thread, ReentrantCycleDetectingLock<?>> lockThreadIsWaitingOn = Maps.newHashMap();
|
private Map<Long, ReentrantCycleDetectingLock> lockThreadIsWaitingOn = Maps.newHashMap();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates new lock within this factory context. We can guarantee that locks created by
|
* Creates new lock within this factory context. We can guarantee that locks created by
|
||||||
* the same factory would not deadlock.
|
* the same factory would not deadlock.
|
||||||
*
|
*
|
||||||
* @param userLockId lock id that would be used to report lock cycles if detected
|
* @param newLockId lock id that would be used to report lock cycles if detected
|
||||||
*/
|
*/
|
||||||
CycleDetectingLock<ID> create(ID userLockId) {
|
CycleDetectingLock<ID> create(ID newLockId) {
|
||||||
return new ReentrantCycleDetectingLock<>(this, userLockId, new ReentrantLock());
|
return new ReentrantCycleDetectingLock(newLockId, new ReentrantLock());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The implementation for {@link CycleDetectingLock}.
|
* The implementation for {@link CycleDetectingLock}.
|
||||||
*/
|
*/
|
||||||
static class ReentrantCycleDetectingLock<ID> implements CycleDetectingLock<ID> {
|
class ReentrantCycleDetectingLock implements CycleDetectingLock<ID> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Underlying lock used for actual waiting when no potential deadlocks are detected.
|
* Underlying lock used for actual waiting when no potential deadlocks are detected.
|
||||||
|
@ -123,56 +125,50 @@ interface CycleDetectingLock<ID> {
|
||||||
* User id for this lock.
|
* User id for this lock.
|
||||||
*/
|
*/
|
||||||
private final ID userLockId;
|
private final ID userLockId;
|
||||||
/** Factory that was used to create this lock. */
|
|
||||||
private final CycleDetectingLockFactory<ID> lockFactory;
|
|
||||||
/**
|
/**
|
||||||
* Thread id for the thread that owned this lock. Nullable.
|
* Thread id for the thread that owned this lock. Nullable.
|
||||||
* Guarded by {@code CycleDetectingLockFactory.this}.
|
* Guarded by {@code CycleDetectingLockFactory.this}.
|
||||||
*/
|
*/
|
||||||
private Thread lockOwnerThread = null;
|
private Long lockOwnerThreadId = null;
|
||||||
/**
|
/**
|
||||||
* Number of times that thread owned this lock.
|
* Number of times that thread owned this lock.
|
||||||
* Guarded by {@code CycleDetectingLockFactory.this}.
|
* Guarded by {@code CycleDetectingLockFactory.this}.
|
||||||
*/
|
*/
|
||||||
private int lockReentranceCount = 0;
|
private int lockReentranceCount = 0;
|
||||||
|
|
||||||
ReentrantCycleDetectingLock(
|
ReentrantCycleDetectingLock(ID userLockId, Lock lockImplementation) {
|
||||||
CycleDetectingLockFactory<ID> lockFactory, ID userLockId, Lock lockImplementation) {
|
|
||||||
this.lockFactory = lockFactory;
|
|
||||||
this.userLockId = Preconditions.checkNotNull(userLockId, "userLockId");
|
this.userLockId = Preconditions.checkNotNull(userLockId, "userLockId");
|
||||||
this.lockImplementation = Preconditions.checkNotNull(
|
this.lockImplementation = Preconditions.checkNotNull(
|
||||||
lockImplementation, "lockImplementation");
|
lockImplementation, "lockImplementation");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ListMultimap<Thread, ID> lockOrDetectPotentialLocksCycle() {
|
public ListMultimap<Long, ID> lockOrDetectPotentialLocksCycle() {
|
||||||
final Thread currentThread = Thread.currentThread();
|
final long currentThreadId = Thread.currentThread().getId();
|
||||||
synchronized (CycleDetectingLockFactory.class) {
|
synchronized (CycleDetectingLockFactory.this) {
|
||||||
checkState();
|
checkState();
|
||||||
// Add this lock to the waiting map to ensure it is included in any reported lock cycle.
|
ListMultimap<Long, ID> locksInCycle = detectPotentialLocksCycle();
|
||||||
lockThreadIsWaitingOn.put(currentThread, this);
|
|
||||||
ListMultimap<Thread, ID> locksInCycle = detectPotentialLocksCycle();
|
|
||||||
if (!locksInCycle.isEmpty()) {
|
if (!locksInCycle.isEmpty()) {
|
||||||
// We aren't actually going to wait for this lock, so remove it from the map.
|
|
||||||
lockThreadIsWaitingOn.remove(currentThread);
|
|
||||||
// potential deadlock is found, we don't try to take this lock
|
// potential deadlock is found, we don't try to take this lock
|
||||||
return locksInCycle;
|
return locksInCycle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lockThreadIsWaitingOn.put(currentThreadId, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// this may be blocking, but we don't expect it to cause a deadlock
|
// this may be blocking, but we don't expect it to cause a deadlock
|
||||||
lockImplementation.lock();
|
lockImplementation.lock();
|
||||||
|
|
||||||
synchronized (CycleDetectingLockFactory.class) {
|
synchronized (CycleDetectingLockFactory.this) {
|
||||||
// current thread is no longer waiting on this lock
|
// current thread is no longer waiting on this lock
|
||||||
lockThreadIsWaitingOn.remove(currentThread);
|
lockThreadIsWaitingOn.remove(currentThreadId);
|
||||||
checkState();
|
checkState();
|
||||||
|
|
||||||
// mark it as owned by us
|
// mark it as owned by us
|
||||||
lockOwnerThread = currentThread;
|
lockOwnerThreadId = currentThreadId;
|
||||||
lockReentranceCount++;
|
lockReentranceCount++;
|
||||||
// add this lock to the list of locks owned by a current thread
|
// add this lock to the list of locks owned by a current thread
|
||||||
locksOwnedByThread.put(currentThread, this);
|
locksOwnedByThread.put(currentThreadId, this);
|
||||||
}
|
}
|
||||||
// no deadlock is found, locking successful
|
// no deadlock is found, locking successful
|
||||||
return ImmutableListMultimap.of();
|
return ImmutableListMultimap.of();
|
||||||
|
@ -180,12 +176,12 @@ interface CycleDetectingLock<ID> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unlock() {
|
public void unlock() {
|
||||||
final Thread currentThread = Thread.currentThread();
|
final long currentThreadId = Thread.currentThread().getId();
|
||||||
synchronized (CycleDetectingLockFactory.class) {
|
synchronized (CycleDetectingLockFactory.this) {
|
||||||
checkState();
|
checkState();
|
||||||
Preconditions.checkState(lockOwnerThread != null,
|
Preconditions.checkState(lockOwnerThreadId != null,
|
||||||
"Thread is trying to unlock a lock that is not locked");
|
"Thread is trying to unlock a lock that is not locked");
|
||||||
Preconditions.checkState(lockOwnerThread == currentThread,
|
Preconditions.checkState(lockOwnerThreadId == currentThreadId,
|
||||||
"Thread is trying to unlock a lock owned by another thread");
|
"Thread is trying to unlock a lock owned by another thread");
|
||||||
|
|
||||||
// releasing underlying lock
|
// releasing underlying lock
|
||||||
|
@ -195,12 +191,12 @@ interface CycleDetectingLock<ID> {
|
||||||
lockReentranceCount--;
|
lockReentranceCount--;
|
||||||
if (lockReentranceCount == 0) {
|
if (lockReentranceCount == 0) {
|
||||||
// we no longer own this lock
|
// we no longer own this lock
|
||||||
lockOwnerThread = null;
|
lockOwnerThreadId = null;
|
||||||
Preconditions.checkState(locksOwnedByThread.remove(currentThread, this),
|
Preconditions.checkState(locksOwnedByThread.remove(currentThreadId, this),
|
||||||
"Internal error: Can not find this lock in locks owned by a current thread");
|
"Internal error: Can not find this lock in locks owned by a current thread");
|
||||||
if (locksOwnedByThread.get(currentThread).isEmpty()) {
|
if (locksOwnedByThread.get(currentThreadId).isEmpty()) {
|
||||||
// clearing memory
|
// clearing memory
|
||||||
locksOwnedByThread.removeAll(currentThread);
|
locksOwnedByThread.removeAll(currentThreadId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -210,14 +206,14 @@ interface CycleDetectingLock<ID> {
|
||||||
* Check consistency of an internal state.
|
* Check consistency of an internal state.
|
||||||
*/
|
*/
|
||||||
void checkState() throws IllegalStateException {
|
void checkState() throws IllegalStateException {
|
||||||
final Thread currentThread = Thread.currentThread();
|
final long currentThreadId = Thread.currentThread().getId();
|
||||||
Preconditions.checkState(!lockThreadIsWaitingOn.containsKey(currentThread),
|
Preconditions.checkState(!lockThreadIsWaitingOn.containsKey(currentThreadId),
|
||||||
"Internal error: Thread should not be in a waiting thread on a lock now");
|
"Internal error: Thread should not be in a waiting thread on a lock now");
|
||||||
if (lockOwnerThread != null) {
|
if (lockOwnerThreadId != null) {
|
||||||
// check state of a locked lock
|
// check state of a locked lock
|
||||||
Preconditions.checkState(lockReentranceCount >= 0,
|
Preconditions.checkState(lockReentranceCount >= 0,
|
||||||
"Internal error: Lock ownership and reentrance count internal states do not match");
|
"Internal error: Lock ownership and reentrance count internal states do not match");
|
||||||
Preconditions.checkState(locksOwnedByThread.get(lockOwnerThread).contains(this),
|
Preconditions.checkState(locksOwnedByThread.get(lockOwnerThreadId).contains(this),
|
||||||
"Internal error: Set of locks owned by a current thread and lock "
|
"Internal error: Set of locks owned by a current thread and lock "
|
||||||
+ "ownership status do not match");
|
+ "ownership status do not match");
|
||||||
} else {
|
} else {
|
||||||
|
@ -237,80 +233,74 @@ interface CycleDetectingLock<ID> {
|
||||||
*
|
*
|
||||||
* @see CycleDetectingLock#lockOrDetectPotentialLocksCycle()
|
* @see CycleDetectingLock#lockOrDetectPotentialLocksCycle()
|
||||||
*/
|
*/
|
||||||
private ListMultimap<Thread, ID> detectPotentialLocksCycle() {
|
private ListMultimap<Long, ID> detectPotentialLocksCycle() {
|
||||||
final Thread currentThread = Thread.currentThread();
|
final long currentThreadId = Thread.currentThread().getId();
|
||||||
if (lockOwnerThread == null || lockOwnerThread == currentThread) {
|
if (lockOwnerThreadId == null || lockOwnerThreadId == currentThreadId) {
|
||||||
// if nobody owns this lock, lock cycle is impossible
|
// if nobody owns this lock, lock cycle is impossible
|
||||||
// if a current thread owns this lock, we let Guice to handle it
|
// if a current thread owns this lock, we let Guice to handle it
|
||||||
return ImmutableListMultimap.of();
|
return ImmutableListMultimap.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
ListMultimap<Thread, ID> potentialLocksCycle = Multimaps.newListMultimap(
|
ListMultimap<Long, ID> potentialLocksCycle = Multimaps.newListMultimap(
|
||||||
new LinkedHashMap<>(), Lists::newArrayList);
|
new LinkedHashMap<Long, Collection<ID>>(),
|
||||||
|
new Supplier<List<ID>>() {
|
||||||
|
@Override
|
||||||
|
public List<ID> get() {
|
||||||
|
return Lists.newArrayList();
|
||||||
|
}
|
||||||
|
});
|
||||||
// lock that is a part of a potential locks cycle, starts with current lock
|
// lock that is a part of a potential locks cycle, starts with current lock
|
||||||
ReentrantCycleDetectingLock<?> lockOwnerWaitingOn = this;
|
ReentrantCycleDetectingLock lockOwnerWaitingOn = this;
|
||||||
// try to find a dependency path between lock's owner thread and a current thread
|
// try to find a dependency path between lock's owner thread and a current thread
|
||||||
while (lockOwnerWaitingOn != null && lockOwnerWaitingOn.lockOwnerThread != null) {
|
while (lockOwnerWaitingOn != null && lockOwnerWaitingOn.lockOwnerThreadId != null) {
|
||||||
Thread threadOwnerThreadWaits = lockOwnerWaitingOn.lockOwnerThread;
|
Long threadOwnerThreadWaits = lockOwnerWaitingOn.lockOwnerThreadId;
|
||||||
// in case locks cycle exists lock we're waiting for is part of it
|
// in case locks cycle exists lock we're waiting for is part of it
|
||||||
lockOwnerWaitingOn =
|
potentialLocksCycle.putAll(threadOwnerThreadWaits,
|
||||||
addAllLockIdsAfter(threadOwnerThreadWaits, lockOwnerWaitingOn, potentialLocksCycle);
|
getAllLockIdsAfter(threadOwnerThreadWaits, lockOwnerWaitingOn));
|
||||||
if (threadOwnerThreadWaits == currentThread) {
|
|
||||||
|
if (threadOwnerThreadWaits == currentThreadId) {
|
||||||
// owner thread depends on current thread, cycle detected
|
// owner thread depends on current thread, cycle detected
|
||||||
return potentialLocksCycle;
|
return potentialLocksCycle;
|
||||||
}
|
}
|
||||||
|
// going for the next thread we wait on indirectly
|
||||||
|
lockOwnerWaitingOn = lockThreadIsWaitingOn.get(threadOwnerThreadWaits);
|
||||||
}
|
}
|
||||||
// no dependency path from an owner thread to a current thread
|
// no dependency path from an owner thread to a current thread
|
||||||
return ImmutableListMultimap.of();
|
return ImmutableListMultimap.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds all locks held by the given thread that are after the given lock and then returns the
|
* Return locks owned by a thread after a lock specified, inclusive.
|
||||||
* lock the thread is currently waiting on, if any
|
|
||||||
*/
|
*/
|
||||||
private ReentrantCycleDetectingLock<?> addAllLockIdsAfter(
|
private List<ID> getAllLockIdsAfter(long threadId, ReentrantCycleDetectingLock lock) {
|
||||||
Thread thread,
|
List<ID> ids = Lists.newArrayList();
|
||||||
ReentrantCycleDetectingLock<?> lock,
|
|
||||||
ListMultimap<Thread, ID> potentialLocksCycle) {
|
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
Collection<ReentrantCycleDetectingLock<?>> ownedLocks = locksOwnedByThread.get(thread);
|
Collection<ReentrantCycleDetectingLock> ownedLocks = locksOwnedByThread.get(threadId);
|
||||||
Preconditions.checkNotNull(ownedLocks,
|
Preconditions.checkNotNull(ownedLocks,
|
||||||
"Internal error: No locks were found taken by a thread");
|
"Internal error: No locks were found taken by a thread");
|
||||||
for (ReentrantCycleDetectingLock<?> ownedLock : ownedLocks) {
|
for (ReentrantCycleDetectingLock ownedLock : ownedLocks) {
|
||||||
if (ownedLock == lock) {
|
if (ownedLock == lock) {
|
||||||
found = true;
|
found = true;
|
||||||
}
|
}
|
||||||
if (found && ownedLock.lockFactory == this.lockFactory) {
|
if (found) {
|
||||||
// All locks are stored in a shared map therefore there is no way to
|
ids.add(ownedLock.userLockId);
|
||||||
// enforce type safety. We know that our cast is valid as we check for a lock's
|
|
||||||
// factory. If the lock was generated by the
|
|
||||||
// same factory it has to have same type as the current lock.
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
ID userLockId = (ID) ownedLock.userLockId;
|
|
||||||
potentialLocksCycle.put(thread, userLockId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Preconditions.checkState(found,
|
Preconditions.checkState(found, "Internal error: We can not find locks that "
|
||||||
"Internal error: We can not find locks that created a cycle that we detected");
|
+ "created a cycle that we detected");
|
||||||
ReentrantCycleDetectingLock<?> unownedLock = lockThreadIsWaitingOn.get(thread);
|
return ids;
|
||||||
// If this thread is waiting for a lock add it to the cycle and return it
|
|
||||||
if (unownedLock != null && unownedLock.lockFactory == this.lockFactory) {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
ID typed = (ID) unownedLock.userLockId;
|
|
||||||
potentialLocksCycle.put(thread, typed);
|
|
||||||
}
|
|
||||||
return unownedLock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
// copy is made to prevent a data race
|
// copy is made to prevent a data race
|
||||||
// no synchronization is used, potentially stale data, should be good enough
|
// no synchronization is used, potentially stale data, should be good enough
|
||||||
Thread thread = this.lockOwnerThread;
|
Long localLockOwnerThreadId = this.lockOwnerThreadId;
|
||||||
if (thread != null) {
|
if (localLockOwnerThreadId != null) {
|
||||||
return String.format("%s[%s][locked by %s]", super.toString(), userLockId, thread);
|
return String.format("CycleDetectingLock[%s][locked by %s]",
|
||||||
|
userLockId, localLockOwnerThreadId);
|
||||||
} else {
|
} else {
|
||||||
return String.format("%s[%s][unlocked]", super.toString(), userLockId);
|
return String.format("CycleDetectingLock[%s][unlocked]", userLockId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,81 +0,0 @@
|
||||||
package com.google.inject.internal;
|
|
||||||
|
|
||||||
import com.google.common.collect.ComparisonChain;
|
|
||||||
import com.google.common.collect.Ordering;
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Utility class for retrieving declared fields or methods in a particular order, because the JVM
|
|
||||||
* doesn't guarantee ordering for listing declared methods. We don't externally guarantee an
|
|
||||||
* ordering, but having a consistent ordering allows deterministic behavior and simpler tests.
|
|
||||||
*/
|
|
||||||
public final class DeclaredMembers {
|
|
||||||
|
|
||||||
private DeclaredMembers() {}
|
|
||||||
|
|
||||||
public static Field[] getDeclaredFields(Class<?> type) {
|
|
||||||
Field[] fields = type.getDeclaredFields();
|
|
||||||
Arrays.sort(fields, FIELD_ORDERING);
|
|
||||||
return fields;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Method[] getDeclaredMethods(Class<?> type) {
|
|
||||||
Method[] methods = type.getDeclaredMethods();
|
|
||||||
Arrays.sort(methods, METHOD_ORDERING);
|
|
||||||
return methods;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An ordering suitable for comparing two classes if they are loaded by the same classloader
|
|
||||||
*
|
|
||||||
* <p>Within a single classloader there can only be one class with a given name, so we just
|
|
||||||
* compare the names.
|
|
||||||
*/
|
|
||||||
private static final Ordering<Class<?>> CLASS_ORDERING =
|
|
||||||
new Ordering<>() {
|
|
||||||
@Override
|
|
||||||
public int compare(Class<?> o1, Class<?> o2) {
|
|
||||||
return o1.getName().compareTo(o2.getName());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An ordering suitable for comparing two fields if they are owned by the same class.
|
|
||||||
*
|
|
||||||
* <p>Within a single class it is sufficent to compare the non-generic field signature which
|
|
||||||
* consists of the field name and type.
|
|
||||||
*/
|
|
||||||
private static final Ordering<Field> FIELD_ORDERING =
|
|
||||||
new Ordering<>() {
|
|
||||||
@Override
|
|
||||||
public int compare(Field left, Field right) {
|
|
||||||
return ComparisonChain.start()
|
|
||||||
.compare(left.getName(), right.getName())
|
|
||||||
.compare(left.getType(), right.getType(), CLASS_ORDERING)
|
|
||||||
.result();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An ordering suitable for comparing two methods if they are owned by the same class.
|
|
||||||
*
|
|
||||||
* <p>Within a single class it is sufficient to compare the non-generic method signature which
|
|
||||||
* consists of the name, return type and parameter types.
|
|
||||||
*/
|
|
||||||
private static final Ordering<Method> METHOD_ORDERING =
|
|
||||||
new Ordering<>() {
|
|
||||||
@Override
|
|
||||||
public int compare(Method left, Method right) {
|
|
||||||
return ComparisonChain.start()
|
|
||||||
.compare(left.getName(), right.getName())
|
|
||||||
.compare(left.getReturnType(), right.getReturnType(), CLASS_ORDERING)
|
|
||||||
.compare(
|
|
||||||
Arrays.asList(left.getParameterTypes()),
|
|
||||||
Arrays.asList(right.getParameterTypes()),
|
|
||||||
CLASS_ORDERING.lexicographical())
|
|
||||||
.result();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -24,8 +24,13 @@ final class DefaultConstructionProxyFactory<T> implements ConstructionProxyFacto
|
||||||
@SuppressWarnings("unchecked") // the injection point is for a constructor of T
|
@SuppressWarnings("unchecked") // the injection point is for a constructor of T
|
||||||
final Constructor<T> constructor = (Constructor<T>) injectionPoint.getMember();
|
final Constructor<T> constructor = (Constructor<T>) injectionPoint.getMember();
|
||||||
|
|
||||||
if (!Modifier.isPublic(constructor.getDeclaringClass().getModifiers()) ||
|
// Use FastConstructor if the constructor is public.
|
||||||
!Modifier.isPublic(constructor.getModifiers())) {
|
if (Modifier.isPublic(constructor.getModifiers())) {
|
||||||
|
Class<T> classToConstruct = constructor.getDeclaringClass();
|
||||||
|
if (!Modifier.isPublic(classToConstruct.getModifiers())) {
|
||||||
|
constructor.setAccessible(true);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
constructor.setAccessible(true);
|
constructor.setAccessible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,17 +38,17 @@ final class DefaultConstructionProxyFactory<T> implements ConstructionProxyFacto
|
||||||
public T newInstance(Object... arguments) throws InvocationTargetException {
|
public T newInstance(Object... arguments) throws InvocationTargetException {
|
||||||
try {
|
try {
|
||||||
return constructor.newInstance(arguments);
|
return constructor.newInstance(arguments);
|
||||||
} catch (InstantiationException | IllegalAccessException e) {
|
} catch (InstantiationException e) {
|
||||||
throw new AssertionError(e); // shouldn't happen, we know this is a concrete type
|
throw new AssertionError(e); // shouldn't happen, we know this is a concrete type
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new AssertionError(e); // a security manager is blocking us, we're hosed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public InjectionPoint getInjectionPoint() {
|
public InjectionPoint getInjectionPoint() {
|
||||||
return injectionPoint;
|
return injectionPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Constructor<T> getConstructor() {
|
public Constructor<T> getConstructor() {
|
||||||
return constructor;
|
return constructor;
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,13 +32,13 @@ final class DeferredLookups implements Lookups {
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> Provider<T> getProvider(Key<T> key) {
|
public <T> Provider<T> getProvider(Key<T> key) {
|
||||||
ProviderLookup<T> lookup = new ProviderLookup<>(key, key);
|
ProviderLookup<T> lookup = new ProviderLookup<T>(key, key);
|
||||||
lookups.add(lookup);
|
lookups.add(lookup);
|
||||||
return lookup.getProvider();
|
return lookup.getProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> type) {
|
public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> type) {
|
||||||
MembersInjectorLookup<T> lookup = new MembersInjectorLookup<>(type, type);
|
MembersInjectorLookup<T> lookup = new MembersInjectorLookup<T>(type, type);
|
||||||
lookups.add(lookup);
|
lookups.add(lookup);
|
||||||
return lookup.getMembersInjector();
|
return lookup.getMembersInjector();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,127 +0,0 @@
|
||||||
package com.google.inject.internal;
|
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMultimap;
|
|
||||||
import com.google.inject.Binding;
|
|
||||||
import com.google.inject.Key;
|
|
||||||
import com.google.inject.spi.Dependency;
|
|
||||||
import com.google.inject.spi.ErrorDetail;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Formatter;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Error reported by Guice when duplicate elements are found in a {@link Multibinder} that does not
|
|
||||||
* permit duplicates.
|
|
||||||
*/
|
|
||||||
final class DuplicateElementError<T> extends InternalErrorDetail<DuplicateElementError<T>> {
|
|
||||||
private final Key<Set<T>> setKey;
|
|
||||||
private final ImmutableMultimap<T, Element<T>> elements;
|
|
||||||
|
|
||||||
DuplicateElementError(
|
|
||||||
Key<Set<T>> setKey, List<Binding<T>> bindings, T[] values, List<Object> sources) {
|
|
||||||
this(setKey, indexElements(bindings, values), sources);
|
|
||||||
}
|
|
||||||
|
|
||||||
private DuplicateElementError(
|
|
||||||
Key<Set<T>> setKey, ImmutableMultimap<T, Element<T>> elements, List<Object> sources) {
|
|
||||||
super(
|
|
||||||
ErrorId.DUPLICATE_ELEMENT,
|
|
||||||
String.format("Duplicate elements found in Multibinder %s.", Messages.convert(setKey)),
|
|
||||||
sources,
|
|
||||||
null);
|
|
||||||
this.setKey = setKey;
|
|
||||||
this.elements = elements;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void formatDetail(List<ErrorDetail<?>> others, Formatter formatter) {
|
|
||||||
formatter.format("%n%s%n", Messages.bold("Duplicates:"));
|
|
||||||
int duplicateIndex = 1;
|
|
||||||
for (Map.Entry<T, Collection<Element<T>>> entry : elements.asMap().entrySet()) {
|
|
||||||
formatter.format("%-2s: ", duplicateIndex++);
|
|
||||||
if (entry.getValue().size() > 1) {
|
|
||||||
Set<String> valuesAsString =
|
|
||||||
entry.getValue().stream()
|
|
||||||
.map(element -> element.value.toString())
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
if (valuesAsString.size() == 1) {
|
|
||||||
// String representation of the duplicates elements are the same, so only print out one.
|
|
||||||
formatter.format("Element: %s%n", Messages.redBold(valuesAsString.iterator().next()));
|
|
||||||
formatter.format(" Bound at:%n");
|
|
||||||
int index = 1;
|
|
||||||
for (Element<T> element : entry.getValue()) {
|
|
||||||
formatter.format(" %-2s: ", index++);
|
|
||||||
formatElement(element, formatter);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Print out all elements as string when there are different string representations of the
|
|
||||||
// elements. To keep the logic simple, same strings are not grouped together unless all
|
|
||||||
// elements have the same string represnetation. This means some strings may be printed
|
|
||||||
// out multiple times.
|
|
||||||
// There is no indentation for the first duplicate element.
|
|
||||||
boolean indent = false;
|
|
||||||
for (Element<T> element : entry.getValue()) {
|
|
||||||
if (indent) {
|
|
||||||
formatter.format(" ");
|
|
||||||
} else {
|
|
||||||
indent = true;
|
|
||||||
}
|
|
||||||
formatter.format("Element: %s%n", Messages.redBold(element.value.toString()));
|
|
||||||
formatter.format(" Bound at: ");
|
|
||||||
formatElement(element, formatter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
formatter.format("%n%s%n", Messages.bold("Multibinder declared at:"));
|
|
||||||
// Multibinder source includes the key of the set. Filter it out since it's not useful in the
|
|
||||||
// printed error stack.
|
|
||||||
List<Object> filteredSource =
|
|
||||||
getSources().stream()
|
|
||||||
.filter(
|
|
||||||
source -> {
|
|
||||||
if (source instanceof Dependency) {
|
|
||||||
return !((Dependency<?>) source).getKey().equals(setKey);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
ErrorFormatter.formatSources(filteredSource, formatter);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void formatElement(Element<T> element, Formatter formatter) {
|
|
||||||
Object source = element.binding.getSource();
|
|
||||||
new SourceFormatter(
|
|
||||||
source,
|
|
||||||
formatter,
|
|
||||||
/** omitPreposition= */
|
|
||||||
true)
|
|
||||||
.format();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public DuplicateElementError<T> withSources(List<Object> newSources) {
|
|
||||||
return new DuplicateElementError<>(setKey, elements, newSources);
|
|
||||||
}
|
|
||||||
|
|
||||||
static <T> ImmutableMultimap<T, Element<T>> indexElements(List<Binding<T>> bindings, T[] values) {
|
|
||||||
ImmutableMultimap.Builder<T, Element<T>> map = ImmutableMultimap.builder();
|
|
||||||
for (int i = 0; i < values.length; i++) {
|
|
||||||
map.put(values[i], new Element<T>(values[i], bindings.get(i)));
|
|
||||||
}
|
|
||||||
return map.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
static class Element<T> {
|
|
||||||
T value;
|
|
||||||
Binding<T> binding;
|
|
||||||
|
|
||||||
Element(T value, Binding<T> binding) {
|
|
||||||
this.value = value;
|
|
||||||
this.binding = binding;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,70 +0,0 @@
|
||||||
package com.google.inject.internal;
|
|
||||||
|
|
||||||
import com.google.common.collect.Multimap;
|
|
||||||
import com.google.inject.Binding;
|
|
||||||
import com.google.inject.Key;
|
|
||||||
import com.google.inject.spi.ErrorDetail;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Formatter;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Error reported by Guice when a duplicate key is found in a {@link MapBinder} that does not permit
|
|
||||||
* duplicates.
|
|
||||||
*/
|
|
||||||
final class DuplicateMapKeyError<K, V> extends InternalErrorDetail<DuplicateMapKeyError<K, V>> {
|
|
||||||
private final Key<Map<K, V>> mapKey;
|
|
||||||
private final Multimap<K, Binding<V>> duplicates;
|
|
||||||
|
|
||||||
DuplicateMapKeyError(
|
|
||||||
Key<Map<K, V>> mapKey, Multimap<K, Binding<V>> duplicates, List<Object> sources) {
|
|
||||||
super(ErrorId.DUPLICATE_MAP_KEY, getDuplicateKeysMessage(mapKey, duplicates), sources, null);
|
|
||||||
this.mapKey = mapKey;
|
|
||||||
this.duplicates = duplicates;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected final void formatDetail(List<ErrorDetail<?>> others, Formatter formatter) {
|
|
||||||
formatter.format("%n%s%n", Messages.bold("Duplicates:"));
|
|
||||||
|
|
||||||
for (Map.Entry<K, Collection<Binding<V>>> entry : duplicates.asMap().entrySet()) {
|
|
||||||
formatter.format(" Key: \"%s\"%n", Messages.redBold(entry.getKey().toString()));
|
|
||||||
formatter.format(" Bound at:%n");
|
|
||||||
int index = 1;
|
|
||||||
for (Binding<V> binding : entry.getValue()) {
|
|
||||||
formatter.format(" %-2s: ", index++);
|
|
||||||
new SourceFormatter(
|
|
||||||
binding.getSource(),
|
|
||||||
formatter,
|
|
||||||
/** omitPreposition= */
|
|
||||||
true)
|
|
||||||
.format();
|
|
||||||
}
|
|
||||||
formatter.format("%n");
|
|
||||||
}
|
|
||||||
|
|
||||||
formatter.format("%s%n", Messages.bold("MapBinder declared at:"));
|
|
||||||
ErrorFormatter.formatSources(getSources(), formatter);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public DuplicateMapKeyError<K, V> withSources(List<Object> newSources) {
|
|
||||||
return new DuplicateMapKeyError<>(mapKey, duplicates, newSources);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static <K, V> String getDuplicateKeysMessage(
|
|
||||||
Key<Map<K, V>> mapKey, Multimap<K, Binding<V>> duplicates) {
|
|
||||||
Set<K> duplicateKeys = duplicates.keySet();
|
|
||||||
String mapBinderKey = Messages.convert(mapKey).toString();
|
|
||||||
String firstDuplicateKey = duplicateKeys.iterator().next().toString();
|
|
||||||
if (duplicateKeys.size() == 1) {
|
|
||||||
return String.format("Duplicate key \"%s\" found in %s.", firstDuplicateKey, mapBinderKey);
|
|
||||||
} else {
|
|
||||||
return String.format(
|
|
||||||
"\"%s\" and %s other duplicate keys found in %s.",
|
|
||||||
firstDuplicateKey, duplicateKeys.size() - 1, mapBinderKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -43,7 +43,6 @@ final class EncounterImpl<T> implements TypeEncounter<T> {
|
||||||
: ImmutableSet.copyOf(injectionListeners);
|
: ImmutableSet.copyOf(injectionListeners);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void register(MembersInjector<? super T> membersInjector) {
|
public void register(MembersInjector<? super T> membersInjector) {
|
||||||
checkState(valid, "Encounters may not be used after hear() returns.");
|
checkState(valid, "Encounters may not be used after hear() returns.");
|
||||||
|
|
||||||
|
@ -54,7 +53,6 @@ final class EncounterImpl<T> implements TypeEncounter<T> {
|
||||||
membersInjectors.add(membersInjector);
|
membersInjectors.add(membersInjector);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void register(InjectionListener<? super T> injectionListener) {
|
public void register(InjectionListener<? super T> injectionListener) {
|
||||||
checkState(valid, "Encounters may not be used after hear() returns.");
|
checkState(valid, "Encounters may not be used after hear() returns.");
|
||||||
|
|
||||||
|
@ -65,42 +63,35 @@ final class EncounterImpl<T> implements TypeEncounter<T> {
|
||||||
injectionListeners.add(injectionListener);
|
injectionListeners.add(injectionListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addError(String message, Object... arguments) {
|
public void addError(String message, Object... arguments) {
|
||||||
checkState(valid, "Encounters may not be used after hear() returns.");
|
checkState(valid, "Encounters may not be used after hear() returns.");
|
||||||
errors.addMessage(message, arguments);
|
errors.addMessage(message, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addError(Throwable t) {
|
public void addError(Throwable t) {
|
||||||
checkState(valid, "Encounters may not be used after hear() returns.");
|
checkState(valid, "Encounters may not be used after hear() returns.");
|
||||||
errors.errorInUserCode(t, "An exception was caught and reported. Message: %s", t.getMessage());
|
errors.errorInUserCode(t, "An exception was caught and reported. Message: %s", t.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addError(Message message) {
|
public void addError(Message message) {
|
||||||
checkState(valid, "Encounters may not be used after hear() returns.");
|
checkState(valid, "Encounters may not be used after hear() returns.");
|
||||||
errors.addMessage(message);
|
errors.addMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> Provider<T> getProvider(Key<T> key) {
|
public <T> Provider<T> getProvider(Key<T> key) {
|
||||||
checkState(valid, "Encounters may not be used after hear() returns.");
|
checkState(valid, "Encounters may not be used after hear() returns.");
|
||||||
return lookups.getProvider(key);
|
return lookups.getProvider(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> Provider<T> getProvider(Class<T> type) {
|
public <T> Provider<T> getProvider(Class<T> type) {
|
||||||
return getProvider(Key.get(type));
|
return getProvider(Key.get(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) {
|
public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) {
|
||||||
checkState(valid, "Encounters may not be used after hear() returns.");
|
checkState(valid, "Encounters may not be used after hear() returns.");
|
||||||
return lookups.getMembersInjector(typeLiteral);
|
return lookups.getMembersInjector(typeLiteral);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> MembersInjector<T> getMembersInjector(Class<T> type) {
|
public <T> MembersInjector<T> getMembersInjector(Class<T> type) {
|
||||||
return getMembersInjector(TypeLiteral.get(type));
|
return getMembersInjector(TypeLiteral.get(type));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
package com.google.inject.internal;
|
|
||||||
|
|
||||||
import java.util.Formatter;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/** Helper for formatting Guice errors. */
|
|
||||||
final class ErrorFormatter {
|
|
||||||
private ErrorFormatter() {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Format a list of sources to the given {@code formatter}, prefixed by the give {@code index}.
|
|
||||||
*/
|
|
||||||
static void formatSources(int index, List<Object> sources, Formatter formatter) {
|
|
||||||
for (int i = 0; i < sources.size(); i++) {
|
|
||||||
Object source = sources.get(i);
|
|
||||||
if (i == 0) {
|
|
||||||
formatter.format("%-3s: ", index);
|
|
||||||
} else {
|
|
||||||
formatter.format(SourceFormatter.INDENT);
|
|
||||||
}
|
|
||||||
new SourceFormatter(source, formatter, i == 0).format();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Format a list of sources to the given {@code formatter}. */
|
|
||||||
static void formatSources(List<Object> sources, Formatter formatter) {
|
|
||||||
for (int i = 0; i < sources.size(); i++) {
|
|
||||||
Object source = sources.get(i);
|
|
||||||
formatter.format(" ");
|
|
||||||
new SourceFormatter(source, formatter, i == 0).format();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,65 +0,0 @@
|
||||||
package com.google.inject.internal;
|
|
||||||
|
|
||||||
/** Enum used to identify a specific Guice error. */
|
|
||||||
public enum ErrorId {
|
|
||||||
AMBIGUOUS_TYPE_CONVERSION,
|
|
||||||
AOP_DISABLED,
|
|
||||||
AT_INJECT_REQUIRED,
|
|
||||||
AT_TARGET_IS_MISSING_PARAMETER,
|
|
||||||
BINDING_ALREADY_SET,
|
|
||||||
BINDING_TO_GUICE_TYPE,
|
|
||||||
BINDING_TO_PROVIDER,
|
|
||||||
CAN_NOT_PROXY_CLASS,
|
|
||||||
CHILD_BINDING_ALREADY_SET,
|
|
||||||
CIRCULAR_PROXY_DISABLED,
|
|
||||||
CONSTRUCTOR_NOT_DEFINED_BY_TYPE,
|
|
||||||
CONVERSION_TYPE_ERROR,
|
|
||||||
CONVERTER_RETURNED_NULL,
|
|
||||||
DUPLICATE_BINDING_ANNOTATIONS,
|
|
||||||
DUPLICATE_ELEMENT,
|
|
||||||
DUPLICATE_MAP_KEY,
|
|
||||||
DUPLICATE_SCOPES,
|
|
||||||
DUPLICATE_SCOPE_ANNOTATIONS,
|
|
||||||
ERROR_ENHANCING_CLASS,
|
|
||||||
ERROR_INJECTING_CONSTRUCTOR,
|
|
||||||
ERROR_INJECTING_METHOD,
|
|
||||||
ERROR_IN_CUSTOM_PROVIDER,
|
|
||||||
ERROR_IN_USER_CODE,
|
|
||||||
ERROR_IN_USER_INJECTOR,
|
|
||||||
ERROR_NOTIFYING_TYPE_LISTENER,
|
|
||||||
EXPOSED_BUT_NOT_BOUND,
|
|
||||||
INJECT_ABSTRACT_METHOD,
|
|
||||||
INJECT_FINAL_FIELD,
|
|
||||||
INJECT_INNER_CLASS,
|
|
||||||
INJECT_METHOD_WITH_TYPE_PARAMETER,
|
|
||||||
INJECT_RAW_MEMBERS_INJECTOR,
|
|
||||||
INJECT_RAW_PROVIDER,
|
|
||||||
INJECT_RAW_TYPE_LITERAL,
|
|
||||||
JIT_BINDING_ALREADY_SET,
|
|
||||||
JIT_DISABLED,
|
|
||||||
JIT_DISABLED_IN_PARENT,
|
|
||||||
KEY_NOT_FULLY_SPECIFIED,
|
|
||||||
MISPLACED_BINDING_ANNOTATION,
|
|
||||||
MISSING_CONSTANT_VALUES,
|
|
||||||
MISSING_CONSTRUCTOR,
|
|
||||||
MISSING_IMPLEMENTATION,
|
|
||||||
MISSING_RUNTIME_RETENTION,
|
|
||||||
MISSING_SCOPE_ANNOTATION,
|
|
||||||
NOT_A_SUBTYPE,
|
|
||||||
NULL_ELEMENT_IN_SET,
|
|
||||||
NULL_INJECTED_INTO_NON_NULLABLE,
|
|
||||||
NULL_VALUE_IN_MAP,
|
|
||||||
OPTIONAL_CONSTRUCTOR,
|
|
||||||
RECURSIVE_BINDING,
|
|
||||||
RECURSIVE_IMPLEMENTATION_TYPE,
|
|
||||||
RECURSIVE_PROVIDER_TYPE,
|
|
||||||
SCOPE_ANNOTATION_ON_ABSTRACT_TYPE,
|
|
||||||
SCOPE_NOT_FOUND,
|
|
||||||
STATIC_INJECTION_ON_INTERFACE,
|
|
||||||
SUBTYPE_NOT_PROVIDED,
|
|
||||||
TOO_MANY_CONSTRUCTORS,
|
|
||||||
VOID_PROVIDER_METHOD,
|
|
||||||
|
|
||||||
// All other uncommon type of errors
|
|
||||||
OTHER;
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load diff
|
@ -8,9 +8,6 @@ package com.google.inject.internal;
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class ErrorsException extends Exception {
|
public class ErrorsException extends Exception {
|
||||||
|
|
||||||
// NOTE: this is used by Gin which is abandoned. So changing this API will prevent Gin users from
|
|
||||||
// upgrading Guice version.
|
|
||||||
|
|
||||||
private final Errors errors;
|
private final Errors errors;
|
||||||
|
|
||||||
public ErrorsException(Errors errors) {
|
public ErrorsException(Errors errors) {
|
||||||
|
|
46
src/main/java/com/google/inject/internal/Exceptions.java
Normal file
46
src/main/java/com/google/inject/internal/Exceptions.java
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
package com.google.inject.internal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rethrows user-code exceptions in wrapped exceptions so that Errors can target the correct
|
||||||
|
* exception.
|
||||||
|
*/
|
||||||
|
class Exceptions {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rethrows the exception (or it's cause, if it has one) directly if possible.
|
||||||
|
* If it was a checked exception, this wraps the exception in a stack trace
|
||||||
|
* with no frames, so that the exception is shown immediately with no frames
|
||||||
|
* above it.
|
||||||
|
*/
|
||||||
|
public static RuntimeException rethrowCause(Throwable throwable) {
|
||||||
|
Throwable cause = throwable;
|
||||||
|
if (cause.getCause() != null) {
|
||||||
|
cause = cause.getCause();
|
||||||
|
}
|
||||||
|
return rethrow(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rethrows the exception.
|
||||||
|
*/
|
||||||
|
public static RuntimeException rethrow(Throwable throwable) {
|
||||||
|
if (throwable instanceof RuntimeException) {
|
||||||
|
throw (RuntimeException) throwable;
|
||||||
|
} else if (throwable instanceof Error) {
|
||||||
|
throw (Error) throwable;
|
||||||
|
} else {
|
||||||
|
throw new UnhandledCheckedUserException(throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A marker exception class that we look for in order to unwrap the exception
|
||||||
|
* into the user exception, to provide a cleaner stack trace.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("serial")
|
||||||
|
static class UnhandledCheckedUserException extends RuntimeException {
|
||||||
|
public UnhandledCheckedUserException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,17 +22,14 @@ public final class ExposedBindingImpl<T> extends BindingImpl<T> implements Expos
|
||||||
this.privateElements = privateElements;
|
this.privateElements = privateElements;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
|
public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
|
||||||
return visitor.visit(this);
|
return visitor.visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<Dependency<?>> getDependencies() {
|
public Set<Dependency<?>> getDependencies() {
|
||||||
return ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(Injector.class)));
|
return ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(Injector.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public PrivateElements getPrivateElements() {
|
public PrivateElements getPrivateElements() {
|
||||||
return privateElements;
|
return privateElements;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,10 +18,9 @@ final class ExposedKeyFactory<T> implements InternalFactory<T>, CreationListener
|
||||||
this.privateElements = privateElements;
|
this.privateElements = privateElements;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void notify(Errors errors) {
|
public void notify(Errors errors) {
|
||||||
InjectorImpl privateInjector = (InjectorImpl) privateElements.getInjector();
|
InjectorImpl privateInjector = (InjectorImpl) privateElements.getInjector();
|
||||||
BindingImpl<T> explicitBinding = privateInjector.getBindingData().getExplicitBinding(key);
|
BindingImpl<T> explicitBinding = privateInjector.state.getExplicitBinding(key);
|
||||||
|
|
||||||
// validate that the child injector has its own factory. If the getInternalFactory() returns
|
// validate that the child injector has its own factory. If the getInternalFactory() returns
|
||||||
// this, then that child injector doesn't have a factory (and getExplicitBinding has returned
|
// this, then that child injector doesn't have a factory (and getExplicitBinding has returned
|
||||||
|
@ -34,10 +33,8 @@ final class ExposedKeyFactory<T> implements InternalFactory<T>, CreationListener
|
||||||
this.delegate = explicitBinding;
|
this.delegate = explicitBinding;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public T get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked)
|
||||||
public T get(InternalContext context, Dependency<?> dependency, boolean linked)
|
throws ErrorsException {
|
||||||
throws InternalProvisionException {
|
return delegate.getInternalFactory().get(errors, context, dependency, linked);
|
||||||
// TODO(lukes): add a source to the thrown exception?
|
|
||||||
return delegate.getInternalFactory().get(context, dependency, linked);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,14 +27,12 @@ public class ExposureBuilder<T> implements AnnotatedElementBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void annotatedWith(Class<? extends Annotation> annotationType) {
|
public void annotatedWith(Class<? extends Annotation> annotationType) {
|
||||||
Preconditions.checkNotNull(annotationType, "annotationType");
|
Preconditions.checkNotNull(annotationType, "annotationType");
|
||||||
checkNotAnnotated();
|
checkNotAnnotated();
|
||||||
key = Key.get(key.getTypeLiteral(), annotationType);
|
key = Key.get(key.getTypeLiteral(), annotationType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void annotatedWith(Annotation annotation) {
|
public void annotatedWith(Annotation annotation) {
|
||||||
Preconditions.checkNotNull(annotation, "annotation");
|
Preconditions.checkNotNull(annotation, "annotation");
|
||||||
checkNotAnnotated();
|
checkNotAnnotated();
|
||||||
|
|
|
@ -25,7 +25,6 @@ final class FactoryProxy<T> implements InternalFactory<T>, CreationListener {
|
||||||
this.source = source;
|
this.source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void notify(final Errors errors) {
|
public void notify(final Errors errors) {
|
||||||
try {
|
try {
|
||||||
targetFactory = injector.getInternalFactory(targetKey, errors.withSource(source), JitLimitation.NEW_OR_EXISTING_JIT);
|
targetFactory = injector.getInternalFactory(targetKey, errors.withSource(source), JitLimitation.NEW_OR_EXISTING_JIT);
|
||||||
|
@ -34,15 +33,11 @@ final class FactoryProxy<T> implements InternalFactory<T>, CreationListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public T get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked)
|
||||||
public T get(InternalContext context, Dependency<?> dependency, boolean linked)
|
throws ErrorsException {
|
||||||
throws InternalProvisionException {
|
context.pushState(targetKey, source);
|
||||||
Key<? extends T> localTargetKey = targetKey;
|
|
||||||
context.pushState(localTargetKey, source);
|
|
||||||
try {
|
try {
|
||||||
return targetFactory.get(context, dependency, true);
|
return targetFactory.get(errors.withSource(targetKey), context, dependency, true);
|
||||||
} catch (InternalProvisionException ipe) {
|
|
||||||
throw ipe.addSource(localTargetKey);
|
|
||||||
} finally {
|
} finally {
|
||||||
context.popState();
|
context.popState();
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,6 @@ package com.google.inject.internal;
|
||||||
import com.google.common.cache.CacheBuilder;
|
import com.google.common.cache.CacheBuilder;
|
||||||
import com.google.common.cache.CacheLoader;
|
import com.google.common.cache.CacheLoader;
|
||||||
import com.google.common.cache.LoadingCache;
|
import com.google.common.cache.LoadingCache;
|
||||||
import com.google.common.collect.ImmutableMap;
|
|
||||||
import com.google.common.collect.Maps;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lazily creates (and caches) values for keys. If creating the value fails (with errors), an
|
* Lazily creates (and caches) values for keys. If creating the value fails (with errors), an
|
||||||
|
@ -13,8 +10,8 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public abstract class FailableCache<K, V> {
|
public abstract class FailableCache<K, V> {
|
||||||
|
|
||||||
private final LoadingCache<K, Object> delegate = CacheBuilder.newBuilder()
|
private final LoadingCache<K, Object> delegate = CacheBuilder.newBuilder().build(
|
||||||
.build(new CacheLoader<>() {
|
new CacheLoader<K, Object>() {
|
||||||
public Object load(K key) {
|
public Object load(K key) {
|
||||||
Errors errors = new Errors();
|
Errors errors = new Errors();
|
||||||
V result = null;
|
V result = null;
|
||||||
|
@ -36,7 +33,7 @@ public abstract class FailableCache<K, V> {
|
||||||
throw errors.toException();
|
throw errors.toException();
|
||||||
} else {
|
} else {
|
||||||
@SuppressWarnings("unchecked") // create returned a non-error result, so this is safe
|
@SuppressWarnings("unchecked") // create returned a non-error result, so this is safe
|
||||||
V result = (V) resultOrError;
|
V result = (V) resultOrError;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,14 +41,4 @@ public abstract class FailableCache<K, V> {
|
||||||
boolean remove(K key) {
|
boolean remove(K key) {
|
||||||
return delegate.asMap().remove(key) != null;
|
return delegate.asMap().remove(key) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<K, V> asMap() {
|
|
||||||
return Maps.transformValues(Maps.filterValues(ImmutableMap.copyOf(delegate.asMap()),
|
|
||||||
resultOrError -> !(resultOrError instanceof Errors)),
|
|
||||||
resultOrError -> {
|
|
||||||
@SuppressWarnings("unchecked") // create returned a non-error result, so this is safe
|
|
||||||
V result = (V) resultOrError;
|
|
||||||
return result;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
package com.google.inject.internal;
|
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.google.inject.spi.ErrorDetail;
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.Formatter;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/** Generic error message representing a Guice internal error. */
|
|
||||||
public final class GenericErrorDetail extends InternalErrorDetail<GenericErrorDetail>
|
|
||||||
implements Serializable {
|
|
||||||
public GenericErrorDetail(
|
|
||||||
ErrorId errorId, String message, List<Object> sources, Throwable cause) {
|
|
||||||
super(errorId, checkNotNull(message, "message"), sources, cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void formatDetail(List<ErrorDetail<?>> mergeableErrors, Formatter formatter) {
|
|
||||||
Preconditions.checkArgument(mergeableErrors.isEmpty(), "Unexpected mergeable errors");
|
|
||||||
List<Object> dependencies = getSources();
|
|
||||||
for (Object source : Lists.reverse(dependencies)) {
|
|
||||||
formatter.format(" ");
|
|
||||||
new SourceFormatter(source, formatter, /* omitPreposition= */ false).format();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public GenericErrorDetail withSources(List<Object> newSources) {
|
|
||||||
return new GenericErrorDetail(errorId, getMessage(), newSources, getCause());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
package com.google.inject.internal;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class used for restricting APIs in other packages to only be used by this package.
|
|
||||||
*
|
|
||||||
* <p>Other packages can reference this class but only this package can reference an instance of it,
|
|
||||||
* so adding this class as a method param ensures that only this package can call it (provided null
|
|
||||||
* is disallowed).
|
|
||||||
*/
|
|
||||||
public final class GuiceInternal {
|
|
||||||
static final GuiceInternal GUICE_INTERNAL = new GuiceInternal();
|
|
||||||
|
|
||||||
private GuiceInternal() {}
|
|
||||||
}
|
|
160
src/main/java/com/google/inject/internal/InheritingState.java
Normal file
160
src/main/java/com/google/inject/internal/InheritingState.java
Normal file
|
@ -0,0 +1,160 @@
|
||||||
|
package com.google.inject.internal;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
import com.google.inject.Binding;
|
||||||
|
import com.google.inject.Key;
|
||||||
|
import com.google.inject.Scope;
|
||||||
|
import com.google.inject.TypeLiteral;
|
||||||
|
import com.google.inject.spi.ModuleAnnotatedMethodScannerBinding;
|
||||||
|
import com.google.inject.spi.ProvisionListenerBinding;
|
||||||
|
import com.google.inject.spi.ScopeBinding;
|
||||||
|
import com.google.inject.spi.TypeConverterBinding;
|
||||||
|
import com.google.inject.spi.TypeListenerBinding;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
|
final class InheritingState implements State {
|
||||||
|
|
||||||
|
private final State parent;
|
||||||
|
|
||||||
|
// Must be a linked hashmap in order to preserve order of bindings in Modules.
|
||||||
|
private final Map<Key<?>, Binding<?>> explicitBindingsMutable = Maps.newLinkedHashMap();
|
||||||
|
private final Map<Key<?>, Binding<?>> explicitBindings
|
||||||
|
= Collections.unmodifiableMap(explicitBindingsMutable);
|
||||||
|
private final Map<Class<? extends Annotation>, ScopeBinding> scopes = Maps.newHashMap();
|
||||||
|
private final List<TypeConverterBinding> converters = Lists.newArrayList();
|
||||||
|
private final List<TypeListenerBinding> typeListenerBindings = Lists.newArrayList();
|
||||||
|
private final List<ProvisionListenerBinding> provisionListenerBindings = Lists.newArrayList();
|
||||||
|
private final List<ModuleAnnotatedMethodScannerBinding> scannerBindings = Lists.newArrayList();
|
||||||
|
private final WeakKeySet blacklistedKeys;
|
||||||
|
private final Object lock;
|
||||||
|
|
||||||
|
InheritingState(State parent) {
|
||||||
|
this.parent = checkNotNull(parent, "parent");
|
||||||
|
this.lock = (parent == State.NONE) ? this : parent.lock();
|
||||||
|
this.blacklistedKeys = new WeakKeySet(lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
public State parent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked") // we only put in BindingImpls that match their key types
|
||||||
|
public <T> BindingImpl<T> getExplicitBinding(Key<T> key) {
|
||||||
|
Binding<?> binding = explicitBindings.get(key);
|
||||||
|
return binding != null ? (BindingImpl<T>) binding : parent.getExplicitBinding(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Key<?>, Binding<?>> getExplicitBindingsThisLevel() {
|
||||||
|
return explicitBindings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putBinding(Key<?> key, BindingImpl<?> binding) {
|
||||||
|
explicitBindingsMutable.put(key, binding);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScopeBinding getScopeBinding(Class<? extends Annotation> annotationType) {
|
||||||
|
ScopeBinding scopeBinding = scopes.get(annotationType);
|
||||||
|
return scopeBinding != null ? scopeBinding : parent.getScopeBinding(annotationType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putScopeBinding(Class<? extends Annotation> annotationType, ScopeBinding scope) {
|
||||||
|
scopes.put(annotationType, scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterable<TypeConverterBinding> getConvertersThisLevel() {
|
||||||
|
return converters;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addConverter(TypeConverterBinding typeConverterBinding) {
|
||||||
|
converters.add(typeConverterBinding);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypeConverterBinding getConverter(
|
||||||
|
String stringValue, TypeLiteral<?> type, Errors errors, Object source) {
|
||||||
|
TypeConverterBinding matchingConverter = null;
|
||||||
|
for (State s = this; s != State.NONE; s = s.parent()) {
|
||||||
|
for (TypeConverterBinding converter : s.getConvertersThisLevel()) {
|
||||||
|
if (converter.getTypeMatcher().matches(type)) {
|
||||||
|
if (matchingConverter != null) {
|
||||||
|
errors.ambiguousTypeConversion(stringValue, source, type, matchingConverter, converter);
|
||||||
|
}
|
||||||
|
matchingConverter = converter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matchingConverter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addTypeListener(TypeListenerBinding listenerBinding) {
|
||||||
|
typeListenerBindings.add(listenerBinding);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<TypeListenerBinding> getTypeListenerBindings() {
|
||||||
|
List<TypeListenerBinding> parentBindings = parent.getTypeListenerBindings();
|
||||||
|
List<TypeListenerBinding> result =
|
||||||
|
Lists.newArrayListWithCapacity(parentBindings.size() + typeListenerBindings.size());
|
||||||
|
result.addAll(parentBindings);
|
||||||
|
result.addAll(typeListenerBindings);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addProvisionListener(ProvisionListenerBinding listenerBinding) {
|
||||||
|
provisionListenerBindings.add(listenerBinding);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ProvisionListenerBinding> getProvisionListenerBindings() {
|
||||||
|
List<ProvisionListenerBinding> parentBindings = parent.getProvisionListenerBindings();
|
||||||
|
List<ProvisionListenerBinding> result =
|
||||||
|
Lists.newArrayListWithCapacity(parentBindings.size() + provisionListenerBindings.size());
|
||||||
|
result.addAll(parentBindings);
|
||||||
|
result.addAll(provisionListenerBindings);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addScanner(ModuleAnnotatedMethodScannerBinding scanner) {
|
||||||
|
scannerBindings.add(scanner);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ModuleAnnotatedMethodScannerBinding> getScannerBindings() {
|
||||||
|
List<ModuleAnnotatedMethodScannerBinding> parentBindings = parent.getScannerBindings();
|
||||||
|
List<ModuleAnnotatedMethodScannerBinding> result =
|
||||||
|
Lists.newArrayListWithCapacity(parentBindings.size() + scannerBindings.size());
|
||||||
|
result.addAll(parentBindings);
|
||||||
|
result.addAll(scannerBindings);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void blacklist(Key<?> key, State state, Object source) {
|
||||||
|
parent.blacklist(key, state, source);
|
||||||
|
blacklistedKeys.add(key, state, source);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBlacklisted(Key<?> key) {
|
||||||
|
return blacklistedKeys.contains(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Object> getSourcesForBlacklistedKey(Key<?> key) {
|
||||||
|
return blacklistedKeys.getSources(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object lock() {
|
||||||
|
return lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Class<? extends Annotation>, Scope> getScopes() {
|
||||||
|
ImmutableMap.Builder<Class<? extends Annotation>, Scope> builder = ImmutableMap.builder();
|
||||||
|
for (Map.Entry<Class<? extends Annotation>, ScopeBinding> entry : scopes.entrySet()) {
|
||||||
|
builder.put(entry.getKey(), entry.getValue().getScope());
|
||||||
|
}
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,5 +8,5 @@ interface Initializable<T> {
|
||||||
/**
|
/**
|
||||||
* Ensures the reference is initialized, then returns it.
|
* Ensures the reference is initialized, then returns it.
|
||||||
*/
|
*/
|
||||||
T get() throws InternalProvisionException;
|
T get(Errors errors) throws ErrorsException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,8 @@ final class Initializables {
|
||||||
* Returns an initializable for an instance that requires no initialization.
|
* Returns an initializable for an instance that requires no initialization.
|
||||||
*/
|
*/
|
||||||
static <T> Initializable<T> of(final T instance) {
|
static <T> Initializable<T> of(final T instance) {
|
||||||
return new Initializable<>() {
|
return new Initializable<T>() {
|
||||||
public T get() {
|
public T get(Errors errors) throws ErrorsException {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,16 +3,15 @@ package com.google.inject.internal;
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import com.google.common.collect.Multimap;
|
|
||||||
import com.google.inject.Binding;
|
import com.google.inject.Binding;
|
||||||
import com.google.inject.Key;
|
import com.google.inject.Key;
|
||||||
import com.google.inject.Stage;
|
import com.google.inject.Stage;
|
||||||
import com.google.inject.TypeLiteral;
|
import com.google.inject.TypeLiteral;
|
||||||
import com.google.inject.spi.InjectionPoint;
|
import com.google.inject.spi.InjectionPoint;
|
||||||
|
|
||||||
import java.util.IdentityHashMap;
|
import java.util.Map;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
|
@ -24,30 +23,27 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
*/
|
*/
|
||||||
final class Initializer {
|
final class Initializer {
|
||||||
|
|
||||||
/** Is set to true once {@link #validateOustandingInjections} is called. */
|
/**
|
||||||
private volatile boolean validationStarted = false;
|
* the only thread that we'll use to inject members.
|
||||||
|
*/
|
||||||
|
private final Thread creatingThread = Thread.currentThread();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows us to detect circular dependencies. It's only used during injectable reference
|
* zero means everything is injected.
|
||||||
* initialization. After initialization direct access through volatile field is used.
|
|
||||||
*/
|
*/
|
||||||
private final CycleDetectingLock.CycleDetectingLockFactory<Class<?>> cycleDetectingLockFactory =
|
private final CountDownLatch ready = new CountDownLatch(1);
|
||||||
new CycleDetectingLock.CycleDetectingLockFactory<>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instances that need injection during injector creation to a source that registered them. New
|
* Maps from instances that need injection to the MembersInjector that will inject them.
|
||||||
* references added before {@link #validateOustandingInjections}. Cleared up in {@link
|
|
||||||
* #injectAll}.
|
|
||||||
*/
|
*/
|
||||||
private final List<InjectableReference<?>> pendingInjections = Lists.newArrayList();
|
private final Map<Object, MembersInjectorImpl<?>> pendingMembersInjectors =
|
||||||
|
|
||||||
/**
|
|
||||||
* Map that guarantees that no instance would get two references. New references added before
|
|
||||||
* {@link #validateOustandingInjections}. Cleared up in {@link #validateOustandingInjections}.
|
|
||||||
*/
|
|
||||||
private final IdentityHashMap<Object, InjectableReference<?>> initializablesCache =
|
|
||||||
Maps.newIdentityHashMap();
|
Maps.newIdentityHashMap();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps instances that need injection to a source that registered them
|
||||||
|
*/
|
||||||
|
private final Map<Object, InjectableReference<?>> pendingInjection = Maps.newIdentityHashMap();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers an instance for member injection when that step is performed.
|
* Registers an instance for member injection when that step is performed.
|
||||||
*
|
*
|
||||||
|
@ -59,35 +55,21 @@ final class Initializer {
|
||||||
<T> Initializable<T> requestInjection(InjectorImpl injector, T instance, Binding<T> binding,
|
<T> Initializable<T> requestInjection(InjectorImpl injector, T instance, Binding<T> binding,
|
||||||
Object source, Set<InjectionPoint> injectionPoints) {
|
Object source, Set<InjectionPoint> injectionPoints) {
|
||||||
checkNotNull(source);
|
checkNotNull(source);
|
||||||
Preconditions.checkState(
|
|
||||||
!validationStarted, "Member injection could not be requested after validation is started");
|
|
||||||
ProvisionListenerStackCallback<T> provisionCallback =
|
ProvisionListenerStackCallback<T> provisionCallback =
|
||||||
binding == null ? null : injector.provisionListenerStore.get(binding);
|
binding == null ? null : injector.provisionListenerStore.get(binding);
|
||||||
|
|
||||||
// short circuit if the object has no injections or listeners.
|
// short circuit if the object has no injections or listeners.
|
||||||
if (instance == null || (injectionPoints.isEmpty()
|
if (instance == null || (injectionPoints.isEmpty()
|
||||||
&& !injector.membersInjectorStore.hasTypeListeners()
|
&& !injector.membersInjectorStore.hasTypeListeners()
|
||||||
&& (provisionCallback == null))) {
|
&& (provisionCallback == null || !provisionCallback.hasListeners()))) {
|
||||||
return Initializables.of(instance);
|
return Initializables.of(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (initializablesCache.containsKey(instance)) {
|
InjectableReference<T> initializable = new InjectableReference<T>(
|
||||||
@SuppressWarnings("unchecked") // Map from T to InjectableReference<T>
|
injector, instance, binding == null ? null : binding.getKey(), provisionCallback, source);
|
||||||
Initializable<T> cached = (Initializable<T>) initializablesCache.get(instance);
|
pendingInjection.put(instance, initializable);
|
||||||
return cached;
|
return initializable;
|
||||||
}
|
|
||||||
|
|
||||||
InjectableReference<T> injectableReference =
|
|
||||||
new InjectableReference<T>(
|
|
||||||
injector,
|
|
||||||
instance,
|
|
||||||
binding == null ? null : binding.getKey(),
|
|
||||||
provisionCallback,
|
|
||||||
source,
|
|
||||||
cycleDetectingLockFactory.create(instance.getClass()));
|
|
||||||
initializablesCache.put(instance, injectableReference);
|
|
||||||
pendingInjections.add(injectableReference);
|
|
||||||
return injectableReference;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -95,11 +77,9 @@ final class Initializer {
|
||||||
* on the injected instances.
|
* on the injected instances.
|
||||||
*/
|
*/
|
||||||
void validateOustandingInjections(Errors errors) {
|
void validateOustandingInjections(Errors errors) {
|
||||||
validationStarted = true;
|
for (InjectableReference<?> reference : pendingInjection.values()) {
|
||||||
initializablesCache.clear();
|
|
||||||
for (InjectableReference<?> reference : pendingInjections) {
|
|
||||||
try {
|
try {
|
||||||
reference.validate(errors);
|
pendingMembersInjectors.put(reference.instance, reference.validate(errors));
|
||||||
} catch (ErrorsException e) {
|
} catch (ErrorsException e) {
|
||||||
errors.merge(e.getErrors());
|
errors.merge(e.getErrors());
|
||||||
}
|
}
|
||||||
|
@ -112,122 +92,85 @@ final class Initializer {
|
||||||
* instances are codependent (directly or transitively), ordering of injection is arbitrary.
|
* instances are codependent (directly or transitively), ordering of injection is arbitrary.
|
||||||
*/
|
*/
|
||||||
void injectAll(final Errors errors) {
|
void injectAll(final Errors errors) {
|
||||||
Preconditions.checkState(validationStarted, "Validation should be done before injection");
|
// loop over a defensive copy since ensureInjected() mutates the set. Unfortunately, that copy
|
||||||
for (InjectableReference<?> reference : pendingInjections) {
|
// is made complicated by a bug in IBM's JDK, wherein entrySet().toArray(Object[]) doesn't work
|
||||||
|
for (InjectableReference<?> reference : Lists.newArrayList(pendingInjection.values())) {
|
||||||
try {
|
try {
|
||||||
reference.get();
|
reference.get(errors);
|
||||||
} catch (InternalProvisionException ipe) {
|
} catch (ErrorsException e) {
|
||||||
errors.merge(ipe);
|
errors.merge(e.getErrors());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pendingInjections.clear();
|
|
||||||
|
if (!pendingInjection.isEmpty()) {
|
||||||
|
throw new AssertionError("Failed to satisfy " + pendingInjection);
|
||||||
|
}
|
||||||
|
|
||||||
|
ready.countDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum InjectableReferenceState {
|
private class InjectableReference<T> implements Initializable<T> {
|
||||||
NEW,
|
|
||||||
VALIDATED,
|
|
||||||
INJECTING,
|
|
||||||
READY
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class InjectableReference<T> implements Initializable<T> {
|
|
||||||
private volatile InjectableReferenceState state = InjectableReferenceState.NEW;
|
|
||||||
private volatile MembersInjectorImpl<T> membersInjector = null;
|
|
||||||
|
|
||||||
private final InjectorImpl injector;
|
private final InjectorImpl injector;
|
||||||
private final T instance;
|
private final T instance;
|
||||||
private final Object source;
|
private final Object source;
|
||||||
private final Key<T> key;
|
private final Key<T> key;
|
||||||
private final ProvisionListenerStackCallback<T> provisionCallback;
|
private final ProvisionListenerStackCallback<T> provisionCallback;
|
||||||
private final CycleDetectingLock<?> lock;
|
|
||||||
|
|
||||||
public InjectableReference(InjectorImpl injector, T instance, Key<T> key,
|
public InjectableReference(InjectorImpl injector, T instance, Key<T> key,
|
||||||
ProvisionListenerStackCallback<T> provisionCallback,
|
ProvisionListenerStackCallback<T> provisionCallback, Object source) {
|
||||||
Object source,
|
|
||||||
CycleDetectingLock<?> lock) {
|
|
||||||
this.injector = injector;
|
this.injector = injector;
|
||||||
this.key = key; // possibly null!
|
this.key = key; // possibly null!
|
||||||
this.provisionCallback = provisionCallback; // possibly null!
|
this.provisionCallback = provisionCallback; // possibly null!
|
||||||
this.instance = checkNotNull(instance, "instance");
|
this.instance = checkNotNull(instance, "instance");
|
||||||
this.source = checkNotNull(source, "source");
|
this.source = checkNotNull(source, "source");
|
||||||
this.lock = checkNotNull(lock, "lock");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void validate(Errors errors) throws ErrorsException {
|
public MembersInjectorImpl<T> validate(Errors errors) throws ErrorsException {
|
||||||
@SuppressWarnings("unchecked") // the type of 'T' is a TypeLiteral<T>
|
@SuppressWarnings("unchecked") // the type of 'T' is a TypeLiteral<T>
|
||||||
TypeLiteral<T> type = TypeLiteral.get((Class<T>) instance.getClass());
|
TypeLiteral<T> type = TypeLiteral.get((Class<T>) instance.getClass());
|
||||||
membersInjector = injector.membersInjectorStore.get(type, errors.withSource(source));
|
return injector.membersInjectorStore.get(type, errors.withSource(source));
|
||||||
Preconditions.checkNotNull(
|
|
||||||
membersInjector,
|
|
||||||
"No membersInjector available for instance: %s, from key: %s",
|
|
||||||
instance,
|
|
||||||
key);
|
|
||||||
state = InjectableReferenceState.VALIDATED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reentrant. If {@code instance} was registered for injection at injector-creation time, this
|
* Reentrant. If {@code instance} was registered for injection at injector-creation time, this
|
||||||
* method will ensure that all its members have been injected before returning.
|
* method will ensure that all its members have been injected before returning.
|
||||||
*/
|
*/
|
||||||
@Override
|
public T get(Errors errors) throws ErrorsException {
|
||||||
public T get() throws InternalProvisionException {
|
if (ready.getCount() == 0) {
|
||||||
// skipping acquiring lock if initialization is already finished
|
|
||||||
if (state == InjectableReferenceState.READY) {
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
// acquire lock for current binding to initialize an instance
|
|
||||||
Multimap<?, ?> lockCycle = lock.lockOrDetectPotentialLocksCycle();
|
|
||||||
if (!lockCycle.isEmpty()) {
|
|
||||||
// Potential deadlock detected and creation lock is not taken.
|
|
||||||
// According to injectAll()'s contract return non-initialized instance.
|
|
||||||
|
|
||||||
// This condition should not be possible under the current Guice implementation.
|
// just wait for everything to be injected by another thread
|
||||||
// This clause exists for defensive programming purposes.
|
if (Thread.currentThread() != creatingThread) {
|
||||||
|
try {
|
||||||
// Reasoning:
|
ready.await();
|
||||||
// get() is called either directly from injectAll(), holds no locks and can not create
|
return instance;
|
||||||
// a cycle, or it is called through a singleton scope, which resolves deadlocks by itself.
|
} catch (InterruptedException e) {
|
||||||
// Before calling get() object has to be requested for injection.
|
// Give up, since we don't know if our injection is ready
|
||||||
// Initializer.requestInjection() is called either for constant object bindings, which wrap
|
throw new RuntimeException(e);
|
||||||
// creation into a Singleton scope, or from Binder.requestInjection(), which
|
|
||||||
// has to use Singleton scope to reuse the same InjectableReference to potentially
|
|
||||||
// create a lock cycle.
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
// lock acquired, current thread owns this instance initialization
|
|
||||||
switch (state) {
|
|
||||||
case READY:
|
|
||||||
return instance;
|
|
||||||
// When instance depends on itself in the same thread potential dead lock
|
|
||||||
// is not detected. We have to prevent a stack overflow and we use
|
|
||||||
// an "injecting" stage to short-circuit a call.
|
|
||||||
case INJECTING:
|
|
||||||
return instance;
|
|
||||||
case VALIDATED:
|
|
||||||
state = InjectableReferenceState.INJECTING;
|
|
||||||
break;
|
|
||||||
case NEW:
|
|
||||||
throw new IllegalStateException("InjectableReference is not validated yet");
|
|
||||||
default:
|
|
||||||
throw new IllegalStateException("Unknown state: " + state);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// toInject needs injection, do it right away. we only do this once, even if it fails
|
||||||
|
if (pendingInjection.remove(instance) != null) {
|
||||||
|
// safe because we only insert a members injector for the appropriate instance
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
MembersInjectorImpl<T> membersInjector =
|
||||||
|
(MembersInjectorImpl<T>) pendingMembersInjectors.remove(instance);
|
||||||
|
Preconditions.checkState(membersInjector != null,
|
||||||
|
"No membersInjector available for instance: %s, from key: %s", instance, key);
|
||||||
|
|
||||||
// if in Stage.TOOL, we only want to inject & notify toolable injection points.
|
// if in Stage.TOOL, we only want to inject & notify toolable injection points.
|
||||||
// (otherwise we'll inject all of them)
|
// (otherwise we'll inject all of them)
|
||||||
try {
|
membersInjector.injectAndNotify(instance,
|
||||||
membersInjector.injectAndNotify(
|
errors.withSource(source),
|
||||||
instance, key, provisionCallback, source, injector.options.stage == Stage.TOOL);
|
key,
|
||||||
} catch (InternalProvisionException ipe) {
|
provisionCallback,
|
||||||
throw ipe.addSource(source);
|
source,
|
||||||
}
|
injector.options.stage == Stage.TOOL);
|
||||||
// mark instance as ready to skip a lock on subsequent calls
|
|
||||||
state = InjectableReferenceState.READY;
|
|
||||||
return instance;
|
|
||||||
} finally {
|
|
||||||
// always release our creation lock, even on failures
|
|
||||||
lock.unlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -98,19 +98,21 @@ final class InjectionRequestProcessor extends AbstractProcessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
void injectMembers() {
|
void injectMembers() {
|
||||||
try (InternalContext context = injector.enterContext()) {
|
try {
|
||||||
boolean isStageTool = injector.options.stage == Stage.TOOL;
|
injector.callInContext(new ContextualCallable<Void>() {
|
||||||
for (SingleMemberInjector memberInjector : memberInjectors) {
|
public Void call(InternalContext context) {
|
||||||
// Run injections if we're not in tool stage (ie, PRODUCTION or DEV),
|
for (SingleMemberInjector memberInjector : memberInjectors) {
|
||||||
// or if we are in tool stage and the injection point is toolable.
|
// Run injections if we're not in tool stage (ie, PRODUCTION or DEV),
|
||||||
if (!isStageTool || memberInjector.getInjectionPoint().isToolable()) {
|
// or if we are in tool stage and the injection point is toolable.
|
||||||
try {
|
if (injector.options.stage != Stage.TOOL || memberInjector.getInjectionPoint().isToolable()) {
|
||||||
memberInjector.inject(context, null);
|
memberInjector.inject(errors, context, null);
|
||||||
} catch (InternalProvisionException e) {
|
}
|
||||||
errors.merge(e);
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
} catch (ErrorsException e) {
|
||||||
|
throw new AssertionError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,231 +0,0 @@
|
||||||
package com.google.inject.internal;
|
|
||||||
|
|
||||||
import com.google.common.collect.ArrayListMultimap;
|
|
||||||
import com.google.common.collect.ImmutableList;
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
|
||||||
import com.google.common.collect.ListMultimap;
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.google.common.collect.Maps;
|
|
||||||
import com.google.common.collect.Sets;
|
|
||||||
import com.google.inject.Binding;
|
|
||||||
import com.google.inject.Key;
|
|
||||||
import com.google.inject.Scope;
|
|
||||||
import com.google.inject.TypeLiteral;
|
|
||||||
import com.google.inject.spi.InjectionRequest;
|
|
||||||
import com.google.inject.spi.MembersInjectorLookup;
|
|
||||||
import com.google.inject.spi.ModuleAnnotatedMethodScannerBinding;
|
|
||||||
import com.google.inject.spi.ProviderLookup;
|
|
||||||
import com.google.inject.spi.ProvisionListenerBinding;
|
|
||||||
import com.google.inject.spi.ScopeBinding;
|
|
||||||
import com.google.inject.spi.StaticInjectionRequest;
|
|
||||||
import com.google.inject.spi.TypeConverterBinding;
|
|
||||||
import com.google.inject.spi.TypeListenerBinding;
|
|
||||||
import java.lang.annotation.Annotation;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A container that stores an injector's binding data. This excludes JIT binding data, which is
|
|
||||||
* stored in {@link InjectorJitBindingData}.
|
|
||||||
*/
|
|
||||||
class InjectorBindingData {
|
|
||||||
|
|
||||||
// The parent injector's InjectorBindingData, if the parent injector exists.
|
|
||||||
private final Optional<InjectorBindingData> parent;
|
|
||||||
|
|
||||||
// Must be a linked hashmap in order to preserve order of bindings in Modules.
|
|
||||||
private final Map<Key<?>, Binding<?>> explicitBindingsMutable = Maps.newLinkedHashMap();
|
|
||||||
private final Map<Key<?>, Binding<?>> explicitBindings =
|
|
||||||
Collections.unmodifiableMap(explicitBindingsMutable);
|
|
||||||
private final Map<Class<? extends Annotation>, ScopeBinding> scopes = Maps.newHashMap();
|
|
||||||
private final Set<ProviderLookup<?>> providerLookups = Sets.newLinkedHashSet();
|
|
||||||
private final Set<StaticInjectionRequest> staticInjectionRequests = Sets.newLinkedHashSet();
|
|
||||||
private final Set<MembersInjectorLookup<?>> membersInjectorLookups = Sets.newLinkedHashSet();
|
|
||||||
private final Set<InjectionRequest<?>> injectionRequests = Sets.newLinkedHashSet();
|
|
||||||
private final List<TypeConverterBinding> converters = Lists.newArrayList();
|
|
||||||
private final List<TypeListenerBinding> typeListenerBindings = Lists.newArrayList();
|
|
||||||
private final List<ProvisionListenerBinding> provisionListenerBindings = Lists.newArrayList();
|
|
||||||
private final List<ModuleAnnotatedMethodScannerBinding> scannerBindings = Lists.newArrayList();
|
|
||||||
// The injector's explicit bindings, indexed by the binding's type.
|
|
||||||
private final ListMultimap<TypeLiteral<?>, Binding<?>> indexedExplicitBindings =
|
|
||||||
ArrayListMultimap.create();
|
|
||||||
|
|
||||||
InjectorBindingData(Optional<InjectorBindingData> parent) {
|
|
||||||
this.parent = parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<InjectorBindingData> parent() {
|
|
||||||
return parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked") // we only put in BindingImpls that match their key types
|
|
||||||
public <T> BindingImpl<T> getExplicitBinding(Key<T> key) {
|
|
||||||
Binding<?> binding = explicitBindings.get(key);
|
|
||||||
if (binding == null && parent.isPresent()) {
|
|
||||||
return parent.get().getExplicitBinding(key);
|
|
||||||
}
|
|
||||||
return (BindingImpl<T>) binding;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<Key<?>, Binding<?>> getExplicitBindingsThisLevel() {
|
|
||||||
return explicitBindings;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void putBinding(Key<?> key, BindingImpl<?> binding) {
|
|
||||||
explicitBindingsMutable.put(key, binding);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void putProviderLookup(ProviderLookup<?> lookup) {
|
|
||||||
providerLookups.add(lookup);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<ProviderLookup<?>> getProviderLookupsThisLevel() {
|
|
||||||
return providerLookups;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void putStaticInjectionRequest(StaticInjectionRequest staticInjectionRequest) {
|
|
||||||
staticInjectionRequests.add(staticInjectionRequest);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<StaticInjectionRequest> getStaticInjectionRequestsThisLevel() {
|
|
||||||
return staticInjectionRequests;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void putInjectionRequest(InjectionRequest<?> injectionRequest) {
|
|
||||||
injectionRequests.add(injectionRequest);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<InjectionRequest<?>> getInjectionRequestsThisLevel() {
|
|
||||||
return injectionRequests;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void putMembersInjectorLookup(MembersInjectorLookup<?> membersInjectorLookup) {
|
|
||||||
membersInjectorLookups.add(membersInjectorLookup);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<MembersInjectorLookup<?>> getMembersInjectorLookupsThisLevel() {
|
|
||||||
return membersInjectorLookups;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ScopeBinding getScopeBinding(Class<? extends Annotation> annotationType) {
|
|
||||||
ScopeBinding scopeBinding = scopes.get(annotationType);
|
|
||||||
if (scopeBinding == null && parent.isPresent()) {
|
|
||||||
return parent.get().getScopeBinding(annotationType);
|
|
||||||
}
|
|
||||||
return scopeBinding;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void putScopeBinding(Class<? extends Annotation> annotationType, ScopeBinding scope) {
|
|
||||||
scopes.put(annotationType, scope);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Collection<ScopeBinding> getScopeBindingsThisLevel() {
|
|
||||||
return scopes.values();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Iterable<TypeConverterBinding> getConvertersThisLevel() {
|
|
||||||
return converters;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addConverter(TypeConverterBinding typeConverterBinding) {
|
|
||||||
converters.add(typeConverterBinding);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TypeConverterBinding getConverter(
|
|
||||||
String stringValue, TypeLiteral<?> type, Errors errors, Object source) {
|
|
||||||
TypeConverterBinding matchingConverter = null;
|
|
||||||
InjectorBindingData b = this;
|
|
||||||
while (b != null) {
|
|
||||||
for (TypeConverterBinding converter : b.getConvertersThisLevel()) {
|
|
||||||
if (converter.getTypeMatcher().matches(type)) {
|
|
||||||
if (matchingConverter != null) {
|
|
||||||
errors.ambiguousTypeConversion(stringValue, source, type, matchingConverter, converter);
|
|
||||||
}
|
|
||||||
matchingConverter = converter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b = b.parent().orElse(null);
|
|
||||||
}
|
|
||||||
return matchingConverter;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addTypeListener(TypeListenerBinding listenerBinding) {
|
|
||||||
typeListenerBindings.add(listenerBinding);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ImmutableList<TypeListenerBinding> getTypeListenerBindings() {
|
|
||||||
if (parent.isPresent()) {
|
|
||||||
return new ImmutableList.Builder<TypeListenerBinding>()
|
|
||||||
.addAll(parent.get().getTypeListenerBindings())
|
|
||||||
.addAll(typeListenerBindings)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
return ImmutableList.copyOf(typeListenerBindings);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ImmutableList<TypeListenerBinding> getTypeListenerBindingsThisLevel() {
|
|
||||||
return ImmutableList.copyOf(typeListenerBindings);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addProvisionListener(ProvisionListenerBinding listenerBinding) {
|
|
||||||
provisionListenerBindings.add(listenerBinding);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ImmutableList<ProvisionListenerBinding> getProvisionListenerBindings() {
|
|
||||||
if (parent.isPresent()) {
|
|
||||||
return new ImmutableList.Builder<ProvisionListenerBinding>()
|
|
||||||
.addAll(parent.get().getProvisionListenerBindings())
|
|
||||||
.addAll(provisionListenerBindings)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
return ImmutableList.copyOf(provisionListenerBindings);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ImmutableList<ProvisionListenerBinding> getProvisionListenerBindingsThisLevel() {
|
|
||||||
return ImmutableList.copyOf(provisionListenerBindings);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addScanner(ModuleAnnotatedMethodScannerBinding scanner) {
|
|
||||||
scannerBindings.add(scanner);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ImmutableList<ModuleAnnotatedMethodScannerBinding> getScannerBindings() {
|
|
||||||
if (parent.isPresent()) {
|
|
||||||
return new ImmutableList.Builder<ModuleAnnotatedMethodScannerBinding>()
|
|
||||||
.addAll(parent.get().getScannerBindings())
|
|
||||||
.addAll(scannerBindings)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
return ImmutableList.copyOf(scannerBindings);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ImmutableList<ModuleAnnotatedMethodScannerBinding> getScannerBindingsThisLevel() {
|
|
||||||
return ImmutableList.copyOf(scannerBindings);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<Class<? extends Annotation>, Scope> getScopes() {
|
|
||||||
ImmutableMap.Builder<Class<? extends Annotation>, Scope> builder = ImmutableMap.builder();
|
|
||||||
for (Map.Entry<Class<? extends Annotation>, ScopeBinding> entry : scopes.entrySet()) {
|
|
||||||
builder.put(entry.getKey(), entry.getValue().getScope());
|
|
||||||
}
|
|
||||||
return builder.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Once the injector's explicit bindings are finalized, this method is called to index all
|
|
||||||
* explicit bindings by their return type.
|
|
||||||
*/
|
|
||||||
void indexBindingsByType() {
|
|
||||||
for (Binding<?> binding : getExplicitBindingsThisLevel().values()) {
|
|
||||||
indexedExplicitBindings.put(binding.getKey().getTypeLiteral(), binding);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ListMultimap<TypeLiteral<?>, Binding<?>> getIndexedExplicitBindings() {
|
|
||||||
return indexedExplicitBindings;
|
|
||||||
}
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,106 +0,0 @@
|
||||||
package com.google.inject.internal;
|
|
||||||
|
|
||||||
import com.google.common.collect.Maps;
|
|
||||||
import com.google.common.collect.Sets;
|
|
||||||
import com.google.inject.Key;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A container for most just-in-time (JIT) binding data corresponding to an Injector. It
|
|
||||||
* includes @Inject constructor bindings. It does not include {@link MembersInjectorStore} or {@link
|
|
||||||
* ProvisionListenerCallbackStore}, which are still stored in {@link InjectorImpl}.
|
|
||||||
*/
|
|
||||||
final class InjectorJitBindingData {
|
|
||||||
/** Just-in-time binding cache. Guarded by {@link #lock}. */
|
|
||||||
private final Map<Key<?>, BindingImpl<?>> jitBindings = Maps.newHashMap();
|
|
||||||
/**
|
|
||||||
* Cache of Keys that we were unable to create JIT bindings for, so we don't keep trying. Guarded
|
|
||||||
* by {@link #lock}.
|
|
||||||
*/
|
|
||||||
private final Set<Key<?>> failedJitBindings = Sets.newHashSet();
|
|
||||||
|
|
||||||
// The set of JIT binding keys that are banned for this particular injector, because a binding
|
|
||||||
// already exists in a child injector. Guarded by {@link #lock}.
|
|
||||||
private final WeakKeySet bannedKeys;
|
|
||||||
|
|
||||||
// The InjectorJitBindingData corresponding to the Injector's parent, if it exists.
|
|
||||||
private final Optional<InjectorJitBindingData> parent;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This lock is needed for threadsafe InjectorJitBindingData accesses. It corresponds to this
|
|
||||||
* InjectorJitBindingData's highest ancestor.
|
|
||||||
*/
|
|
||||||
private final Object lock;
|
|
||||||
|
|
||||||
InjectorJitBindingData(Optional<InjectorJitBindingData> parent) {
|
|
||||||
this.parent = parent;
|
|
||||||
this.lock = parent.isPresent() ? parent.get().lock() : this;
|
|
||||||
this.bannedKeys = new WeakKeySet(lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<Key<?>, BindingImpl<?>> getJitBindings() {
|
|
||||||
return Collections.unmodifiableMap(jitBindings);
|
|
||||||
}
|
|
||||||
|
|
||||||
BindingImpl<?> getJitBinding(Key<?> key) {
|
|
||||||
return jitBindings.get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
void putJitBinding(Key<?> key, BindingImpl<?> binding) {
|
|
||||||
jitBindings.put(key, binding);
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeJitBinding(Key<?> key) {
|
|
||||||
jitBindings.remove(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean isFailedJitBinding(Key<?> key) {
|
|
||||||
return failedJitBindings.contains(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
void addFailedJitBinding(Key<?> key) {
|
|
||||||
failedJitBindings.add(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Forbids the corresponding injector and its ancestors from creating a binding to {@code key}.
|
|
||||||
* Child injectors ban their bound keys on their parent injectors to prevent just-in-time bindings
|
|
||||||
* on the parent injector that would conflict, and pass along their InjectorBindingData to control
|
|
||||||
* the banned key's lifetime.
|
|
||||||
*/
|
|
||||||
void banKey(Key<?> key, InjectorBindingData injectorBindingData, Object source) {
|
|
||||||
banKeyInParent(key, injectorBindingData, source);
|
|
||||||
bannedKeys.add(key, injectorBindingData, source);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Similar to {@link #banKey(Key, InjectorBindingData, Object)} but we only begin banning the
|
|
||||||
* binding at the parent level. This is used to prevent JIT bindings in the parent injector from
|
|
||||||
* overriding explicit bindings declared in a child injector.
|
|
||||||
*/
|
|
||||||
void banKeyInParent(Key<?> key, InjectorBindingData injectorBindingData, Object source) {
|
|
||||||
if (parent.isPresent()) {
|
|
||||||
parent.get().banKey(key, injectorBindingData, source);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if {@code key} is forbidden from being bound in the injector corresponding to this
|
|
||||||
* data object. This indicates that one of the injector's children has bound the key.
|
|
||||||
*/
|
|
||||||
boolean isBannedKey(Key<?> key) {
|
|
||||||
return bannedKeys.contains(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns the source of a banned key. */
|
|
||||||
Set<Object> getSourcesForBannedKey(Key<?> key) {
|
|
||||||
return bannedKeys.getSources(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
Object lock() {
|
|
||||||
return lock;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +1,5 @@
|
||||||
package com.google.inject.internal;
|
package com.google.inject.internal;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkState;
|
|
||||||
import static com.google.inject.Scopes.SINGLETON;
|
|
||||||
import static com.google.inject.internal.GuiceInternal.GUICE_INTERNAL;
|
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.inject.Binder;
|
import com.google.inject.Binder;
|
||||||
|
@ -14,9 +10,8 @@ import com.google.inject.Provider;
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
import com.google.inject.Stage;
|
import com.google.inject.Stage;
|
||||||
import com.google.inject.internal.InjectorImpl.InjectorOptions;
|
import com.google.inject.internal.InjectorImpl.InjectorOptions;
|
||||||
import com.google.inject.internal.util.ContinuousStopwatch;
|
|
||||||
import com.google.inject.internal.util.SourceProvider;
|
import com.google.inject.internal.util.SourceProvider;
|
||||||
import com.google.inject.spi.BindingSourceRestriction;
|
import com.google.inject.internal.util.Stopwatch;
|
||||||
import com.google.inject.spi.Dependency;
|
import com.google.inject.spi.Dependency;
|
||||||
import com.google.inject.spi.Element;
|
import com.google.inject.spi.Element;
|
||||||
import com.google.inject.spi.Elements;
|
import com.google.inject.spi.Elements;
|
||||||
|
@ -25,34 +20,64 @@ import com.google.inject.spi.ModuleAnnotatedMethodScannerBinding;
|
||||||
import com.google.inject.spi.PrivateElements;
|
import com.google.inject.spi.PrivateElements;
|
||||||
import com.google.inject.spi.ProvisionListenerBinding;
|
import com.google.inject.spi.ProvisionListenerBinding;
|
||||||
import com.google.inject.spi.TypeListenerBinding;
|
import com.google.inject.spi.TypeListenerBinding;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.logging.Logger;
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
|
import static com.google.inject.Scopes.SINGLETON;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* InjectorShell is used by {@link InternalInjectorCreator} to recursively create a tree of
|
* A partially-initialized injector. See {@link InternalInjectorCreator}, which
|
||||||
* uninitialized {@link Injector}s. Each InjectorShell corresponds to either the top-level root
|
* uses this to build a tree of injectors in batch.
|
||||||
* injector, or a private child injector.
|
|
||||||
*
|
|
||||||
* <p>The root InjectorShell extracts elements from its list of modules and processes these elements
|
|
||||||
* to aggregate data that is used to populate its injector's fields. Child injectors are constructed
|
|
||||||
* similarly, but using {@link PrivateElements} instead of modules.
|
|
||||||
*
|
|
||||||
* <p>It is necessary to create the root and child injectors in a single batch because there can be
|
|
||||||
* bidirectional parent <-> child injector dependencies that require the entire tree of injectors to
|
|
||||||
* be initialized together in the {@link InternalInjectorCreator}.
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
final class InjectorShell {
|
final class InjectorShell {
|
||||||
|
|
||||||
private final List<Element> elements;
|
private final List<Element> elements;
|
||||||
private final InjectorImpl injector;
|
private final InjectorImpl injector;
|
||||||
|
|
||||||
private InjectorShell(List<Element> elements, InjectorImpl injector) {
|
private InjectorShell(Builder builder, List<Element> elements, InjectorImpl injector) {
|
||||||
this.elements = elements;
|
this.elements = elements;
|
||||||
this.injector = injector;
|
this.injector = injector;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Injector is a special case because we allow both parent and child injectors to both have
|
||||||
|
* a binding for that key.
|
||||||
|
*/
|
||||||
|
private static void bindInjector(InjectorImpl injector) {
|
||||||
|
Key<Injector> key = Key.get(Injector.class);
|
||||||
|
InjectorFactory injectorFactory = new InjectorFactory(injector);
|
||||||
|
injector.state.putBinding(key,
|
||||||
|
new ProviderInstanceBindingImpl<Injector>(injector, key, SourceProvider.UNKNOWN_SOURCE,
|
||||||
|
injectorFactory, Scoping.UNSCOPED, injectorFactory,
|
||||||
|
ImmutableSet.<InjectionPoint>of()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Logger is a special case because it knows the injection point of the injected member. It's
|
||||||
|
* the only binding that does this.
|
||||||
|
*/
|
||||||
|
/*private static void bindLogger(InjectorImpl injector) {
|
||||||
|
Key<Logger> key = Key.get(Logger.class);
|
||||||
|
LoggerFactory loggerFactory = new LoggerFactory();
|
||||||
|
injector.state.putBinding(key,
|
||||||
|
new ProviderInstanceBindingImpl<Logger>(injector, key,
|
||||||
|
SourceProvider.UNKNOWN_SOURCE, loggerFactory, Scoping.UNSCOPED,
|
||||||
|
loggerFactory, ImmutableSet.<InjectionPoint>of()));
|
||||||
|
}*/
|
||||||
|
|
||||||
|
private static void bindStage(InjectorImpl injector, Stage stage) {
|
||||||
|
Key<Stage> key = Key.get(Stage.class);
|
||||||
|
InstanceBindingImpl<Stage> stageBinding = new InstanceBindingImpl<Stage>(
|
||||||
|
injector,
|
||||||
|
key,
|
||||||
|
SourceProvider.UNKNOWN_SOURCE,
|
||||||
|
new ConstantFactory<Stage>(Initializables.of(stage)),
|
||||||
|
ImmutableSet.<InjectionPoint>of(),
|
||||||
|
stage);
|
||||||
|
injector.state.putBinding(key, stageBinding);
|
||||||
|
}
|
||||||
|
|
||||||
InjectorImpl getInjector() {
|
InjectorImpl getInjector() {
|
||||||
return injector;
|
return injector;
|
||||||
}
|
}
|
||||||
|
@ -65,15 +90,18 @@ final class InjectorShell {
|
||||||
private final List<Element> elements = Lists.newArrayList();
|
private final List<Element> elements = Lists.newArrayList();
|
||||||
private final List<Module> modules = Lists.newArrayList();
|
private final List<Module> modules = Lists.newArrayList();
|
||||||
|
|
||||||
// lazily constructed fields
|
/**
|
||||||
private InjectorBindingData bindingData;
|
* lazily constructed
|
||||||
private InjectorJitBindingData jitBindingData;
|
*/
|
||||||
|
private State state;
|
||||||
|
|
||||||
private InjectorImpl parent;
|
private InjectorImpl parent;
|
||||||
private InjectorOptions options;
|
private InjectorOptions options;
|
||||||
private Stage stage;
|
private Stage stage;
|
||||||
|
|
||||||
/** null unless this exists in a {@link Binder#newPrivateBinder private environment} */
|
/**
|
||||||
|
* null unless this exists in a {@link Binder#newPrivateBinder private environment}
|
||||||
|
*/
|
||||||
private PrivateElementsImpl privateElements;
|
private PrivateElementsImpl privateElements;
|
||||||
|
|
||||||
Builder stage(Stage stage) {
|
Builder stage(Stage stage) {
|
||||||
|
@ -83,8 +111,7 @@ final class InjectorShell {
|
||||||
|
|
||||||
Builder parent(InjectorImpl parent) {
|
Builder parent(InjectorImpl parent) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.jitBindingData = new InjectorJitBindingData(Optional.of(parent.getJitBindingData()));
|
this.state = new InheritingState(parent.state);
|
||||||
this.bindingData = new InjectorBindingData(Optional.of(parent.getBindingData()));
|
|
||||||
this.options = parent.options;
|
this.options = parent.options;
|
||||||
this.stage = options.stage;
|
this.stage = options.stage;
|
||||||
return this;
|
return this;
|
||||||
|
@ -97,8 +124,10 @@ final class InjectorShell {
|
||||||
}
|
}
|
||||||
|
|
||||||
void addModules(Iterable<? extends Module> modules) {
|
void addModules(Iterable<? extends Module> modules) {
|
||||||
for (Module module : modules) {
|
if (modules != null) {
|
||||||
this.modules.add(module);
|
for (Module module : modules) {
|
||||||
|
this.modules.add(module);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,15 +135,11 @@ final class InjectorShell {
|
||||||
return options.stage;
|
return options.stage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Synchronize on this before calling {@link #build}. */
|
/**
|
||||||
|
* Synchronize on this before calling {@link #build}.
|
||||||
|
*/
|
||||||
Object lock() {
|
Object lock() {
|
||||||
// Lazily initializes bindingData and jitBindingData, if they were not already
|
return getState().lock();
|
||||||
// initialized with a parent injector by {@link #parent(InjectorImpl)}.
|
|
||||||
if (bindingData == null) {
|
|
||||||
jitBindingData = new InjectorJitBindingData(Optional.empty());
|
|
||||||
bindingData = new InjectorBindingData(Optional.empty());
|
|
||||||
}
|
|
||||||
return jitBindingData.lock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -124,38 +149,27 @@ final class InjectorShell {
|
||||||
*/
|
*/
|
||||||
List<InjectorShell> build(
|
List<InjectorShell> build(
|
||||||
Initializer initializer,
|
Initializer initializer,
|
||||||
ProcessedBindingData processedBindingData,
|
ProcessedBindingData bindingData,
|
||||||
ContinuousStopwatch stopwatch,
|
Stopwatch stopwatch,
|
||||||
Errors errors) {
|
Errors errors) {
|
||||||
checkState(stage != null, "Stage not initialized");
|
checkState(stage != null, "Stage not initialized");
|
||||||
checkState(privateElements == null || parent != null, "PrivateElements with no parent");
|
checkState(privateElements == null || parent != null, "PrivateElements with no parent");
|
||||||
checkState(bindingData != null, "no binding data. Did you remember to lock() ?");
|
checkState(state != null, "no state. Did you remember to lock() ?");
|
||||||
checkState(
|
|
||||||
(privateElements == null && elements.isEmpty()) || modules.isEmpty(),
|
|
||||||
"The shell is either built from modules (root) or from PrivateElements (children).");
|
|
||||||
|
|
||||||
// bind Singleton if this is a top-level injector
|
// bind Singleton if this is a top-level injector
|
||||||
if (parent == null) {
|
if (parent == null) {
|
||||||
modules.add(0, new RootModule());
|
modules.add(0, new RootModule());
|
||||||
} else {
|
} else {
|
||||||
modules.add(0, new InheritedScannersModule(parent.getBindingData()));
|
modules.add(0, new InheritedScannersModule(parent.state));
|
||||||
}
|
}
|
||||||
elements.addAll(Elements.getElements(stage, modules));
|
elements.addAll(Elements.getElements(stage, modules));
|
||||||
|
|
||||||
// Check binding source restrictions only for the root shell (note that the root shell
|
|
||||||
// can have a parent Injector, when Injector.createChildInjector is called). It isn't
|
|
||||||
// necessary to call this check on child PrivateElements shells because it walks the entire
|
|
||||||
// tree of elements, recurring on PrivateElements.
|
|
||||||
if (privateElements == null) {
|
|
||||||
elements.addAll(BindingSourceRestriction.check(GUICE_INTERNAL, elements));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Look for injector-changing options
|
// Look for injector-changing options
|
||||||
InjectorOptionsProcessor optionsProcessor = new InjectorOptionsProcessor(errors);
|
InjectorOptionsProcessor optionsProcessor = new InjectorOptionsProcessor(errors);
|
||||||
optionsProcessor.process(null, elements);
|
optionsProcessor.process(null, elements);
|
||||||
options = optionsProcessor.getOptions(stage, options);
|
options = optionsProcessor.getOptions(stage, options);
|
||||||
|
|
||||||
InjectorImpl injector = new InjectorImpl(parent, bindingData, jitBindingData, options);
|
InjectorImpl injector = new InjectorImpl(parent, state, options);
|
||||||
if (privateElements != null) {
|
if (privateElements != null) {
|
||||||
privateElements.initInjector(injector);
|
privateElements.initInjector(injector);
|
||||||
}
|
}
|
||||||
|
@ -170,11 +184,10 @@ final class InjectorShell {
|
||||||
new MessageProcessor(errors).process(injector, elements);
|
new MessageProcessor(errors).process(injector, elements);
|
||||||
|
|
||||||
new ListenerBindingProcessor(errors).process(injector, elements);
|
new ListenerBindingProcessor(errors).process(injector, elements);
|
||||||
List<TypeListenerBinding> typeListenerBindings =
|
List<TypeListenerBinding> typeListenerBindings = injector.state.getTypeListenerBindings();
|
||||||
injector.getBindingData().getTypeListenerBindings();
|
|
||||||
injector.membersInjectorStore = new MembersInjectorStore(injector, typeListenerBindings);
|
injector.membersInjectorStore = new MembersInjectorStore(injector, typeListenerBindings);
|
||||||
List<ProvisionListenerBinding> provisionListenerBindings =
|
List<ProvisionListenerBinding> provisionListenerBindings =
|
||||||
injector.getBindingData().getProvisionListenerBindings();
|
injector.state.getProvisionListenerBindings();
|
||||||
injector.provisionListenerStore =
|
injector.provisionListenerStore =
|
||||||
new ProvisionListenerCallbackStore(provisionListenerBindings);
|
new ProvisionListenerCallbackStore(provisionListenerBindings);
|
||||||
stopwatch.resetAndLog("TypeListeners & ProvisionListener creation");
|
stopwatch.resetAndLog("TypeListeners & ProvisionListener creation");
|
||||||
|
@ -187,53 +200,38 @@ final class InjectorShell {
|
||||||
|
|
||||||
bindStage(injector, stage);
|
bindStage(injector, stage);
|
||||||
bindInjector(injector);
|
bindInjector(injector);
|
||||||
bindLogger(injector);
|
//bindLogger(injector);
|
||||||
|
|
||||||
// Process all normal bindings, then UntargettedBindings.
|
// Process all normal bindings, then UntargettedBindings.
|
||||||
// This is necessary because UntargettedBindings can create JIT bindings
|
// This is necessary because UntargettedBindings can create JIT bindings
|
||||||
// and need all their other dependencies set up ahead of time.
|
// and need all their other dependencies set up ahead of time.
|
||||||
new BindingProcessor(errors, initializer, processedBindingData).process(injector, elements);
|
new BindingProcessor(errors, initializer, bindingData).process(injector, elements);
|
||||||
new UntargettedBindingProcessor(errors, processedBindingData).process(injector, elements);
|
new UntargettedBindingProcessor(errors, bindingData).process(injector, elements);
|
||||||
stopwatch.resetAndLog("Binding creation");
|
stopwatch.resetAndLog("Binding creation");
|
||||||
|
|
||||||
new ModuleAnnotatedMethodScannerProcessor(errors).process(injector, elements);
|
new ModuleAnnotatedMethodScannerProcessor(errors).process(injector, elements);
|
||||||
stopwatch.resetAndLog("Module annotated method scanners creation");
|
stopwatch.resetAndLog("Module annotated method scanners creation");
|
||||||
|
|
||||||
List<InjectorShell> injectorShells = Lists.newArrayList();
|
List<InjectorShell> injectorShells = Lists.newArrayList();
|
||||||
injectorShells.add(new InjectorShell(elements, injector));
|
injectorShells.add(new InjectorShell(this, elements, injector));
|
||||||
|
|
||||||
// recursively build child shells
|
// recursively build child shells
|
||||||
PrivateElementProcessor processor = new PrivateElementProcessor(errors);
|
PrivateElementProcessor processor = new PrivateElementProcessor(errors);
|
||||||
processor.process(injector, elements);
|
processor.process(injector, elements);
|
||||||
for (Builder builder : processor.getInjectorShellBuilders()) {
|
for (Builder builder : processor.getInjectorShellBuilders()) {
|
||||||
injectorShells.addAll(builder.build(initializer, processedBindingData, stopwatch, errors));
|
injectorShells.addAll(builder.build(initializer, bindingData, stopwatch, errors));
|
||||||
}
|
}
|
||||||
stopwatch.resetAndLog("Private environment creation");
|
stopwatch.resetAndLog("Private environment creation");
|
||||||
|
|
||||||
return injectorShells;
|
return injectorShells;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
private State getState() {
|
||||||
|
if (state == null) {
|
||||||
/**
|
state = new InheritingState(State.NONE);
|
||||||
* The Injector is a special case because we allow both parent and child injectors to both have a
|
}
|
||||||
* binding for that key.
|
return state;
|
||||||
*/
|
}
|
||||||
private static void bindInjector(InjectorImpl injector) {
|
|
||||||
Key<Injector> key = Key.get(Injector.class);
|
|
||||||
InjectorFactory injectorFactory = new InjectorFactory(injector);
|
|
||||||
injector
|
|
||||||
.getBindingData()
|
|
||||||
.putBinding(
|
|
||||||
key,
|
|
||||||
new ProviderInstanceBindingImpl<Injector>(
|
|
||||||
injector,
|
|
||||||
key,
|
|
||||||
SourceProvider.UNKNOWN_SOURCE,
|
|
||||||
injectorFactory,
|
|
||||||
Scoping.UNSCOPED,
|
|
||||||
injectorFactory,
|
|
||||||
ImmutableSet.<InjectionPoint>of()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class InjectorFactory implements InternalFactory<Injector>, Provider<Injector> {
|
private static class InjectorFactory implements InternalFactory<Injector>, Provider<Injector> {
|
||||||
|
@ -243,78 +241,38 @@ final class InjectorShell {
|
||||||
this.injector = injector;
|
this.injector = injector;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public Injector get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked)
|
||||||
public Injector get(InternalContext context, Dependency<?> dependency, boolean linked) {
|
throws ErrorsException {
|
||||||
return injector;
|
return injector;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Injector get() {
|
public Injector get() {
|
||||||
return injector;
|
return injector;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Provider<Injector>";
|
return "Provider<Injector>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*private static class LoggerFactory implements InternalFactory<Logger>, Provider<Logger> {
|
||||||
* The Logger is a special case because it knows the injection point of the injected member. It's
|
public Logger get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked) {
|
||||||
* the only binding that does this.
|
|
||||||
*/
|
|
||||||
private static void bindLogger(InjectorImpl injector) {
|
|
||||||
Key<Logger> key = Key.get(Logger.class);
|
|
||||||
LoggerFactory loggerFactory = new LoggerFactory();
|
|
||||||
injector
|
|
||||||
.getBindingData()
|
|
||||||
.putBinding(
|
|
||||||
key,
|
|
||||||
new ProviderInstanceBindingImpl<Logger>(
|
|
||||||
injector,
|
|
||||||
key,
|
|
||||||
SourceProvider.UNKNOWN_SOURCE,
|
|
||||||
loggerFactory,
|
|
||||||
Scoping.UNSCOPED,
|
|
||||||
loggerFactory,
|
|
||||||
ImmutableSet.<InjectionPoint>of()));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class LoggerFactory implements InternalFactory<Logger>, Provider<Logger> {
|
|
||||||
@Override
|
|
||||||
public Logger get(InternalContext context, Dependency<?> dependency, boolean linked) {
|
|
||||||
InjectionPoint injectionPoint = dependency.getInjectionPoint();
|
InjectionPoint injectionPoint = dependency.getInjectionPoint();
|
||||||
return injectionPoint == null
|
return injectionPoint == null
|
||||||
? Logger.getAnonymousLogger()
|
? Logger.getAnonymousLogger()
|
||||||
: Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
|
: Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Logger get() {
|
public Logger get() {
|
||||||
return Logger.getAnonymousLogger();
|
return Logger.getAnonymousLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Provider<Logger>";
|
return "Provider<Logger>";
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
private static void bindStage(InjectorImpl injector, Stage stage) {
|
|
||||||
Key<Stage> key = Key.get(Stage.class);
|
|
||||||
InstanceBindingImpl<Stage> stageBinding =
|
|
||||||
new InstanceBindingImpl<Stage>(
|
|
||||||
injector,
|
|
||||||
key,
|
|
||||||
SourceProvider.UNKNOWN_SOURCE,
|
|
||||||
new ConstantFactory<Stage>(Initializables.of(stage)),
|
|
||||||
ImmutableSet.<InjectionPoint>of(),
|
|
||||||
stage);
|
|
||||||
injector.getBindingData().putBinding(key, stageBinding);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class RootModule implements Module {
|
private static class RootModule implements Module {
|
||||||
@Override
|
|
||||||
public void configure(Binder binder) {
|
public void configure(Binder binder) {
|
||||||
binder = binder.withSource(SourceProvider.UNKNOWN_SOURCE);
|
binder = binder.withSource(SourceProvider.UNKNOWN_SOURCE);
|
||||||
binder.bindScope(Singleton.class, SINGLETON);
|
binder.bindScope(Singleton.class, SINGLETON);
|
||||||
|
@ -323,15 +281,14 @@ final class InjectorShell {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class InheritedScannersModule implements Module {
|
private static class InheritedScannersModule implements Module {
|
||||||
private final InjectorBindingData bindingData;
|
private final State state;
|
||||||
|
|
||||||
InheritedScannersModule(InjectorBindingData bindingData) {
|
InheritedScannersModule(State state) {
|
||||||
this.bindingData = bindingData;
|
this.state = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void configure(Binder binder) {
|
public void configure(Binder binder) {
|
||||||
for (ModuleAnnotatedMethodScannerBinding binding : bindingData.getScannerBindings()) {
|
for (ModuleAnnotatedMethodScannerBinding binding : state.getScannerBindings()) {
|
||||||
binding.applyTo(binder);
|
binding.applyTo(binder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,17 +5,20 @@ import com.google.common.base.Objects;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.inject.Binder;
|
import com.google.inject.Binder;
|
||||||
import com.google.inject.Key;
|
import com.google.inject.Key;
|
||||||
|
import com.google.inject.Provider;
|
||||||
import com.google.inject.spi.BindingTargetVisitor;
|
import com.google.inject.spi.BindingTargetVisitor;
|
||||||
import com.google.inject.spi.Dependency;
|
import com.google.inject.spi.Dependency;
|
||||||
import com.google.inject.spi.HasDependencies;
|
import com.google.inject.spi.HasDependencies;
|
||||||
import com.google.inject.spi.InjectionPoint;
|
import com.google.inject.spi.InjectionPoint;
|
||||||
import com.google.inject.spi.InstanceBinding;
|
import com.google.inject.spi.InstanceBinding;
|
||||||
|
import com.google.inject.util.Providers;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
final class InstanceBindingImpl<T> extends BindingImpl<T> implements InstanceBinding<T> {
|
final class InstanceBindingImpl<T> extends BindingImpl<T> implements InstanceBinding<T> {
|
||||||
|
|
||||||
final T instance;
|
final T instance;
|
||||||
|
final Provider<T> provider;
|
||||||
final ImmutableSet<InjectionPoint> injectionPoints;
|
final ImmutableSet<InjectionPoint> injectionPoints;
|
||||||
|
|
||||||
public InstanceBindingImpl(InjectorImpl injector, Key<T> key, Object source,
|
public InstanceBindingImpl(InjectorImpl injector, Key<T> key, Object source,
|
||||||
|
@ -24,6 +27,7 @@ final class InstanceBindingImpl<T> extends BindingImpl<T> implements InstanceBin
|
||||||
super(injector, key, source, internalFactory, Scoping.EAGER_SINGLETON);
|
super(injector, key, source, internalFactory, Scoping.EAGER_SINGLETON);
|
||||||
this.injectionPoints = ImmutableSet.copyOf(injectionPoints);
|
this.injectionPoints = ImmutableSet.copyOf(injectionPoints);
|
||||||
this.instance = instance;
|
this.instance = instance;
|
||||||
|
this.provider = Providers.of(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
public InstanceBindingImpl(Object source, Key<T> key, Scoping scoping,
|
public InstanceBindingImpl(Object source, Key<T> key, Scoping scoping,
|
||||||
|
@ -31,41 +35,40 @@ final class InstanceBindingImpl<T> extends BindingImpl<T> implements InstanceBin
|
||||||
super(source, key, scoping);
|
super(source, key, scoping);
|
||||||
this.injectionPoints = ImmutableSet.copyOf(injectionPoints);
|
this.injectionPoints = ImmutableSet.copyOf(injectionPoints);
|
||||||
this.instance = instance;
|
this.instance = instance;
|
||||||
|
this.provider = Providers.of(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
public Provider<T> getProvider() {
|
||||||
|
return this.provider;
|
||||||
|
}
|
||||||
|
|
||||||
public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
|
public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) {
|
||||||
return visitor.visit(this);
|
return visitor.visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public T getInstance() {
|
public T getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<InjectionPoint> getInjectionPoints() {
|
public Set<InjectionPoint> getInjectionPoints() {
|
||||||
return injectionPoints;
|
return injectionPoints;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<Dependency<?>> getDependencies() {
|
public Set<Dependency<?>> getDependencies() {
|
||||||
return instance instanceof HasDependencies
|
return instance instanceof HasDependencies
|
||||||
? ImmutableSet.copyOf(((HasDependencies) instance).getDependencies())
|
? ImmutableSet.copyOf(((HasDependencies) instance).getDependencies())
|
||||||
: Dependency.forInjectionPoints(injectionPoints);
|
: Dependency.forInjectionPoints(injectionPoints);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingImpl<T> withScoping(Scoping scoping) {
|
public BindingImpl<T> withScoping(Scoping scoping) {
|
||||||
return new InstanceBindingImpl<T>(getSource(), getKey(), scoping, injectionPoints, instance);
|
return new InstanceBindingImpl<T>(getSource(), getKey(), scoping, injectionPoints, instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public BindingImpl<T> withKey(Key<T> key) {
|
public BindingImpl<T> withKey(Key<T> key) {
|
||||||
return new InstanceBindingImpl<T>(getSource(), key, getScoping(), injectionPoints, instance);
|
return new InstanceBindingImpl<T>(getSource(), key, getScoping(), injectionPoints, instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void applyTo(Binder binder) {
|
public void applyTo(Binder binder) {
|
||||||
// instance bindings aren't scoped
|
// instance bindings aren't scoped
|
||||||
binder.withSource(getSource()).bind(getKey()).toInstance(instance);
|
binder.withSource(getSource()).bind(getKey()).toInstance(instance);
|
||||||
|
|
|
@ -1,76 +1,37 @@
|
||||||
package com.google.inject.internal;
|
package com.google.inject.internal;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
import com.google.inject.Key;
|
import com.google.inject.Key;
|
||||||
import com.google.inject.internal.InjectorImpl.InjectorOptions;
|
import com.google.inject.internal.InjectorImpl.InjectorOptions;
|
||||||
import com.google.inject.spi.Dependency;
|
import com.google.inject.spi.Dependency;
|
||||||
|
import com.google.inject.spi.DependencyAndSource;
|
||||||
|
|
||||||
import java.util.IdentityHashMap;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal context. Used to coordinate injections and support circular
|
* Internal context. Used to coordinate injections and support circular
|
||||||
* dependencies.
|
* dependencies.
|
||||||
*/
|
*/
|
||||||
final class InternalContext implements AutoCloseable {
|
final class InternalContext {
|
||||||
|
|
||||||
private final InjectorOptions options;
|
private final InjectorOptions options;
|
||||||
|
/**
|
||||||
private final Map<Object, ConstructionContext<?>> constructionContexts =
|
* Keeps track of the hierarchy of types needed during injection.
|
||||||
new IdentityHashMap<>();
|
*/
|
||||||
|
private final DependencyStack state = new DependencyStack();
|
||||||
|
private Map<Object, ConstructionContext<?>> constructionContexts = Maps.newHashMap();
|
||||||
/**
|
/**
|
||||||
* Keeps track of the type that is currently being requested for injection.
|
* Keeps track of the type that is currently being requested for injection.
|
||||||
*/
|
*/
|
||||||
private Dependency<?> dependency;
|
private Dependency<?> dependency;
|
||||||
|
|
||||||
/**
|
InternalContext(InjectorOptions options) {
|
||||||
* Keeps track of the hierarchy of types needed during injection.
|
|
||||||
*
|
|
||||||
* <p>This is a pairwise combination of dependencies and sources, with dependencies or keys on
|
|
||||||
* even indices, and sources on odd indices. This structure is to avoid the memory overhead of
|
|
||||||
* DependencyAndSource objects, which can add to several tens of megabytes in large applications.
|
|
||||||
*/
|
|
||||||
private Object[] dependencyStack = new Object[16];
|
|
||||||
|
|
||||||
private int dependencyStackSize = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The number of times {@link #enter()} has been called + 1 for initial construction.
|
|
||||||
*/
|
|
||||||
private int enterCount;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A single element array to clear when the {@link #enterCount} hits {@code 0}.
|
|
||||||
*
|
|
||||||
* <p>This is the value stored in the {@code InjectorImpl.localContext} thread local.
|
|
||||||
*/
|
|
||||||
private final Object[] toClear;
|
|
||||||
|
|
||||||
InternalContext(InjectorOptions options, Object[] toClear) {
|
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.toClear = toClear;
|
|
||||||
this.enterCount = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Should only be called by InjectorImpl.enterContext(). */
|
|
||||||
void enter() {
|
|
||||||
enterCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Should be called any any method that received an instance via InjectorImpl.enterContext(). */
|
|
||||||
@Override
|
|
||||||
public void close() {
|
|
||||||
int newCount = --enterCount;
|
|
||||||
if (newCount < 0) {
|
|
||||||
throw new IllegalStateException("Called close() too many times");
|
|
||||||
}
|
|
||||||
if (newCount == 0) {
|
|
||||||
toClear[0] = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public InjectorOptions getInjectorOptions() {
|
public InjectorOptions getInjectorOptions() {
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
@ -96,7 +57,7 @@ final class InternalContext implements AutoCloseable {
|
||||||
public Dependency<?> pushDependency(Dependency<?> dependency, Object source) {
|
public Dependency<?> pushDependency(Dependency<?> dependency, Object source) {
|
||||||
Dependency<?> previous = this.dependency;
|
Dependency<?> previous = this.dependency;
|
||||||
this.dependency = dependency;
|
this.dependency = dependency;
|
||||||
doPushState(dependency, source);
|
state.add(dependency, source);
|
||||||
return previous;
|
return previous;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +65,7 @@ final class InternalContext implements AutoCloseable {
|
||||||
* Pops the current state & sets the new dependency.
|
* Pops the current state & sets the new dependency.
|
||||||
*/
|
*/
|
||||||
public void popStateAndSetDependency(Dependency<?> newDependency) {
|
public void popStateAndSetDependency(Dependency<?> newDependency) {
|
||||||
popState();
|
state.pop();
|
||||||
this.dependency = newDependency;
|
this.dependency = newDependency;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,29 +73,64 @@ final class InternalContext implements AutoCloseable {
|
||||||
* Adds to the state without setting the dependency.
|
* Adds to the state without setting the dependency.
|
||||||
*/
|
*/
|
||||||
public void pushState(Key<?> key, Object source) {
|
public void pushState(Key<?> key, Object source) {
|
||||||
doPushState(key, source);
|
state.add(key, source);
|
||||||
}
|
|
||||||
|
|
||||||
private void doPushState(Object dependencyOrKey, Object source) {
|
|
||||||
int localSize = dependencyStackSize;
|
|
||||||
Object[] localStack = dependencyStack;
|
|
||||||
if (localStack.length < localSize + 2) {
|
|
||||||
localStack = dependencyStack =
|
|
||||||
java.util.Arrays.copyOf(localStack, (localStack.length * 3) / 2 + 2);
|
|
||||||
}
|
|
||||||
localStack[localSize++] = dependencyOrKey;
|
|
||||||
localStack[localSize++] = source;
|
|
||||||
dependencyStackSize = localSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pops from the state without setting a dependency.
|
* Pops from the state without setting a dependency.
|
||||||
*/
|
*/
|
||||||
public void popState() {
|
public void popState() {
|
||||||
// N.B. we don't null out the array entries. It isn't necessary since all the objects in the
|
state.pop();
|
||||||
// array (Key, Dependency, or Binding source objects) are all tied to the lifetime of the
|
}
|
||||||
// injector, which is greater than the lifetime of this object. So removing them from the array
|
|
||||||
// doesn't matter.
|
/**
|
||||||
dependencyStackSize -= 2;
|
* Returns the current dependency chain (all the state).
|
||||||
|
*/
|
||||||
|
public List<DependencyAndSource> getDependencyChain() {
|
||||||
|
ImmutableList.Builder<DependencyAndSource> builder = ImmutableList.builder();
|
||||||
|
for (int i = 0; i < state.size(); i += 2) {
|
||||||
|
Object evenEntry = state.get(i);
|
||||||
|
Dependency<?> dependency;
|
||||||
|
if (evenEntry instanceof Key) {
|
||||||
|
dependency = Dependency.get((Key<?>) evenEntry);
|
||||||
|
} else {
|
||||||
|
dependency = (Dependency<?>) evenEntry;
|
||||||
|
}
|
||||||
|
builder.add(new DependencyAndSource(dependency, state.get(i + 1)));
|
||||||
|
}
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keeps track of the hierarchy of types needed during injection.
|
||||||
|
*
|
||||||
|
* <p>This is a pairwise combination of dependencies and sources, with dependencies or keys on
|
||||||
|
* even indices, and sources on odd indices. This structure is to avoid the memory overhead of
|
||||||
|
* DependencyAndSource objects, which can add to several tens of megabytes in large applications.
|
||||||
|
*/
|
||||||
|
private static final class DependencyStack {
|
||||||
|
private Object[] elements = new Object[16];
|
||||||
|
private int size = 0;
|
||||||
|
|
||||||
|
public void add(Object dependencyOrKey, Object source) {
|
||||||
|
if (elements.length < size + 2) {
|
||||||
|
elements = Arrays.copyOf(elements, (elements.length * 3) / 2 + 2);
|
||||||
|
}
|
||||||
|
elements[size++] = dependencyOrKey;
|
||||||
|
elements[size++] = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pop() {
|
||||||
|
elements[--size] = null;
|
||||||
|
elements[--size] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(int i) {
|
||||||
|
return elements[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,61 +0,0 @@
|
||||||
package com.google.inject.internal;
|
|
||||||
|
|
||||||
import com.google.common.base.CaseFormat;
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
|
||||||
import com.google.inject.spi.ErrorDetail;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents an error created by Guice as opposed to custom error added to the binder from
|
|
||||||
* application code.
|
|
||||||
*/
|
|
||||||
abstract class InternalErrorDetail<T extends ErrorDetail<T>> extends ErrorDetail<T> {
|
|
||||||
// A list of errors that have help documentation.
|
|
||||||
private static final ImmutableSet<ErrorId> DOCUMENTED_ERRORS =
|
|
||||||
ImmutableSet.<ErrorId>builder()
|
|
||||||
.add(ErrorId.BINDING_ALREADY_SET)
|
|
||||||
.add(ErrorId.CAN_NOT_PROXY_CLASS)
|
|
||||||
.add(ErrorId.CIRCULAR_PROXY_DISABLED)
|
|
||||||
.add(ErrorId.DUPLICATE_BINDING_ANNOTATIONS)
|
|
||||||
.add(ErrorId.DUPLICATE_ELEMENT)
|
|
||||||
.add(ErrorId.DUPLICATE_SCOPES)
|
|
||||||
.add(ErrorId.ERROR_INJECTING_CONSTRUCTOR)
|
|
||||||
.add(ErrorId.ERROR_INJECTING_METHOD)
|
|
||||||
.add(ErrorId.ERROR_IN_CUSTOM_PROVIDER)
|
|
||||||
.add(ErrorId.INJECT_INNER_CLASS)
|
|
||||||
.add(ErrorId.MISSING_CONSTRUCTOR)
|
|
||||||
.add(ErrorId.MISSING_IMPLEMENTATION)
|
|
||||||
.add(ErrorId.NULL_INJECTED_INTO_NON_NULLABLE)
|
|
||||||
.add(ErrorId.NULL_VALUE_IN_MAP)
|
|
||||||
.add(ErrorId.SCOPE_NOT_FOUND)
|
|
||||||
.add(ErrorId.TOO_MANY_CONSTRUCTORS)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
private static final String DOC_BASE_URL = "https://github.com/google/guice/wiki/";
|
|
||||||
|
|
||||||
protected final ErrorId errorId;
|
|
||||||
|
|
||||||
protected InternalErrorDetail(
|
|
||||||
ErrorId errorId, String message, List<Object> sources, Throwable cause) {
|
|
||||||
super(message, sources, cause);
|
|
||||||
this.errorId = errorId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected final Optional<String> getLearnMoreLink() {
|
|
||||||
if (DOCUMENTED_ERRORS.contains(errorId)) {
|
|
||||||
return Optional.of(DOC_BASE_URL + errorId.name());
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected final Optional<String> getErrorIdentifier() {
|
|
||||||
if (errorId == ErrorId.OTHER) {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
String id = "Guice/" + CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, errorId.name());
|
|
||||||
return Optional.of(id);
|
|
||||||
}
|
|
||||||
}
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue