commit be7265f43e155dd2c5e204a1ab1780082554e6d5 Author: Jörg Prante Date: Thu May 25 21:20:42 2023 +0200 initial import diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b5b797d --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +data +work +out +logs +/.idea +/target +/.settings +/.classpath +/.project +/.gradle +/plugins +/sessions +.DS_Store +*.iml +*~ +.secret +build +**/alkmene.json +**/alkmene*.options +**/herakles.json +**/herakles*.options +**/prod.json +**/prod*.options +**/*.crt +**/*.pkcs8 +**/*.gz diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..039c02a --- /dev/null +++ b/build.gradle @@ -0,0 +1,31 @@ +plugins { + id 'maven-publish' + id 'signing' +} + +wrapper { + gradleVersion = libs.versions.gradle.get() + distributionType = Wrapper.DistributionType.ALL +} + +ext { + user = 'joerg' + name = 'javax-inject' + description = 'javax inject (JSR 330)' + inceptionYear = '2016' + 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 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/publish/maven.gradle') +apply from: rootProject.file('gradle/publish/forgejo.gradle') diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..fb204b5 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,5 @@ +group = org.xbib +name = javax-inject +version = 1 + +org.gradle.warning.mode = ALL diff --git a/gradle/compile/java.gradle b/gradle/compile/java.gradle new file mode 100644 index 0000000..a37f057 --- /dev/null +++ b/gradle/compile/java.gradle @@ -0,0 +1,32 @@ + +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.compilerArgs << '-Xlint:all,-fallthrough' +} + +javadoc { + options.addStringOption('Xdoclint:none', '-quiet') +} diff --git a/gradle/documentation/asciidoc.gradle b/gradle/documentation/asciidoc.gradle new file mode 100644 index 0000000..da6dd7e --- /dev/null +++ b/gradle/documentation/asciidoc.gradle @@ -0,0 +1,19 @@ +apply plugin: 'org.xbib.gradle.plugin.asciidoctor' + +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" +} diff --git a/gradle/ide/idea.gradle b/gradle/ide/idea.gradle new file mode 100644 index 0000000..5bd2095 --- /dev/null +++ b/gradle/ide/idea.gradle @@ -0,0 +1,8 @@ +apply plugin: 'idea' + +idea { + module { + outputDir file('build/classes/java/main') + testOutputDir file('build/classes/java/test') + } +} diff --git a/gradle/publish/forgejo.gradle b/gradle/publish/forgejo.gradle new file mode 100644 index 0000000..b99b2fb --- /dev/null +++ b/gradle/publish/forgejo.gradle @@ -0,0 +1,16 @@ +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) + } + } + } + } +} diff --git a/gradle/publish/ivy.gradle b/gradle/publish/ivy.gradle new file mode 100644 index 0000000..71aa155 --- /dev/null +++ b/gradle/publish/ivy.gradle @@ -0,0 +1,27 @@ +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 + } + } + } + } +} \ No newline at end of file diff --git a/gradle/publish/maven.gradle b/gradle/publish/maven.gradle new file mode 100644 index 0000000..867e23a --- /dev/null +++ b/gradle/publish/maven.gradle @@ -0,0 +1,51 @@ + +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}" + } +} diff --git a/gradle/publish/sonatype.gradle b/gradle/publish/sonatype.gradle new file mode 100644 index 0000000..5d739de --- /dev/null +++ b/gradle/publish/sonatype.gradle @@ -0,0 +1,11 @@ +if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')) { + nexusPublishing { + repositories { + sonatype { + username = project.property('ossrhUsername') + password = project.property('ossrhPassword') + packageGroup = "org.xbib" + } + } + } +} diff --git a/gradle/repositories/maven.gradle b/gradle/repositories/maven.gradle new file mode 100644 index 0000000..ec58acb --- /dev/null +++ b/gradle/repositories/maven.gradle @@ -0,0 +1,4 @@ +repositories { + mavenLocal() + mavenCentral() +} diff --git a/gradle/test/junit5.gradle b/gradle/test/junit5.gradle new file mode 100644 index 0000000..09ce233 --- /dev/null +++ b/gradle/test/junit5.gradle @@ -0,0 +1,35 @@ +dependencies { + testImplementation libs.junit.jupiter.api + testImplementation libs.junit.jupiter.params + testImplementation libs.hamcrest + testRuntimeOnly libs.junit.jupiter.engine +} + +test { + useJUnitPlatform() + failFast = true + 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" + } + } + // for Guava using reflection on JDK classes which JDK17 does not like + jvmArgs '--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED', + '--add-exports=java.base/jdk.internal.ref=ALL-UNNAMED', + '--add-exports=java.base/sun.nio.ch=ALL-UNNAMED', + '--add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED', + '--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED', + '--add-opens=jdk.compiler/com.sun.tools.javac=ALL-UNNAMED', + '--add-opens=java.base/java.lang=ALL-UNNAMED', + '--add-opens=java.base/java.lang.reflect=ALL-UNNAMED', + '--add-opens=java.base/java.io=ALL-UNNAMED', + '--add-opens=java.base/java.util=ALL-UNNAMED' + +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..c1962a7 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..8707e8b --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +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 diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..aeb74cb --- /dev/null +++ b/gradlew @@ -0,0 +1,245 @@ +#!/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" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..6689b85 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,92 @@ +@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 diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..04aab49 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,14 @@ +dependencyResolutionManagement { + versionCatalogs { + libs { + version('gradle', '8.1.1') + version('groovy', '3.0.15') + version('junit', '5.9.3') + 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') + } + } +} diff --git a/src/main/java/javax/inject/Inject.java b/src/main/java/javax/inject/Inject.java new file mode 100644 index 0000000..b77996d --- /dev/null +++ b/src/main/java/javax/inject/Inject.java @@ -0,0 +1,185 @@ +/* + * Copyright (C) 2009 The JSR-330 Expert Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package javax.inject; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import java.lang.annotation.Documented; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.FIELD; + +/** + * Identifies injectable constructors, methods, and fields. May apply to static + * as well as instance members. An injectable member may have any access + * modifier (private, package-private, protected, public). Constructors are + * injected first, followed by fields, and then methods. Fields and methods + * in superclasses are injected before those in subclasses. Ordering of + * injection among fields and among methods in the same class is not specified. + * + *

Injectable constructors are annotated with {@code @Inject} and accept + * zero or more dependencies as arguments. {@code @Inject} can apply to at most + * one constructor per class. + * + *

@Inject + * ConstructorModifiersopt + * SimpleTypeName(FormalParameterListopt) + * Throwsopt + * ConstructorBody
+ * + *

{@code @Inject} is optional for public, no-argument constructors when no + * other constructors are present. This enables injectors to invoke default + * constructors. + * + *

+ * {@literal @}Injectopt + * Annotationsopt + * public + * SimpleTypeName() + * Throwsopt + * ConstructorBody
+ * + *

Injectable fields: + *

+ * + *

@Inject + * FieldModifiersopt + * Type + * VariableDeclarators;
+ * + *

Injectable methods: + *

+ * + *

@Inject + * MethodModifiersopt + * ResultType + * Identifier(FormalParameterListopt) + * Throwsopt + * MethodBody
+ * + *

The injector ignores the result of an injected method, but + * non-{@code void} return types are allowed to support use of the method in + * other contexts (builder-style method chaining, for example). + * + *

Examples: + * + *

+ *   public class Car {
+ *     // Injectable constructor
+ *     @Inject public Car(Engine engine) { ... }
+ *
+ *     // Injectable field
+ *     @Inject private Provider<Seat> seatProvider;
+ *
+ *     // Injectable package-private method
+ *     @Inject void install(Windshield windshield, Trunk trunk) { ... }
+ *   }
+ * + *

A method annotated with {@code @Inject} that overrides another method + * annotated with {@code @Inject} will only be injected once per injection + * request per instance. A method with no {@code @Inject} annotation + * that overrides a method annotated with {@code @Inject} will not be + * injected. + * + *

Injection of members annotated with {@code @Inject} is required. While an + * injectable member may use any accessibility modifier (including + * private), platform or injector limitations (like security + * restrictions or lack of reflection support) might preclude injection + * of non-public members. + * + *

Qualifiers

+ * + *

A {@linkplain Qualifier qualifier} may annotate an injectable field + * or parameter and, combined with the type, identify the implementation to + * inject. Qualifiers are optional, and when used with {@code @Inject} in + * injector-independent classes, no more than one qualifier should annotate a + * single field or parameter. The qualifiers are bold in the following example: + * + *

+ *   public class Car {
+ *     @Inject private @Leather Provider<Seat> seatProvider;
+ *
+ *     @Inject void install(@Tinted Windshield windshield,
+ *         @Big Trunk trunk) { ... }
+ *   }
+ * + *

If one injectable method overrides another, the overriding method's + * parameters do not automatically inherit qualifiers from the overridden + * method's parameters. + * + *

Injectable Values

+ * + *

For a given type T and optional qualifier, an injector must be able to + * inject a user-specified class that: + * + *

    + *
  1. is assignment compatible with T and
  2. + *
  3. has an injectable constructor.
  4. + *
+ * + *

For example, the user might use external configuration to pick an + * implementation of T. Beyond that, which values are injected depend upon the + * injector implementation and its configuration. + * + *

Circular Dependencies

+ * + *

Detecting and resolving circular dependencies is left as an exercise for + * the injector implementation. Circular dependencies between two constructors + * is an obvious problem, but you can also have a circular dependency between + * injectable fields or methods: + * + *

+ *   class A {
+ *     @Inject B b;
+ *   }
+ *   class B {
+ *     @Inject A a;
+ *   }
+ * + *

When constructing an instance of {@code A}, a naive injector + * implementation might go into an infinite loop constructing an instance of + * {@code B} to set on {@code A}, a second instance of {@code A} to set on + * {@code B}, a second instance of {@code B} to set on the second instance of + * {@code A}, and so on. + * + *

A conservative injector might detect the circular dependency at build + * time and generate an error, at which point the programmer could break the + * circular dependency by injecting {@link Provider Provider<A>} or {@code + * Provider} instead of {@code A} or {@code B} respectively. Calling {@link + * Provider#get() get()} on the provider directly from the constructor or + * method it was injected into defeats the provider's ability to break up + * circular dependencies. In the case of method or field injection, scoping + * one of the dependencies (using {@linkplain Singleton singleton scope}, for + * example) may also enable a valid circular relationship. + * + * @see javax.inject.Qualifier @Qualifier + * @see javax.inject.Provider + */ +@Target({ METHOD, CONSTRUCTOR, FIELD }) +@Retention(RUNTIME) +@Documented +public @interface Inject {} diff --git a/src/main/java/javax/inject/Named.java b/src/main/java/javax/inject/Named.java new file mode 100644 index 0000000..2fb4cab --- /dev/null +++ b/src/main/java/javax/inject/Named.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2009 The JSR-330 Expert Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package javax.inject; + +import java.lang.annotation.Retention; +import java.lang.annotation.Documented; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * String-based {@linkplain Qualifier qualifier}. + * + *

Example usage: + * + *

+ *   public class Car {
+ *     @Inject @Named("driver") Seat driverSeat;
+ *     @Inject @Named("passenger") Seat passengerSeat;
+ *     ...
+ *   }
+ */ +@Qualifier +@Documented +@Retention(RUNTIME) +public @interface Named { + + /** The name. */ + String value() default ""; +} diff --git a/src/main/java/javax/inject/Provider.java b/src/main/java/javax/inject/Provider.java new file mode 100644 index 0000000..dd77f59 --- /dev/null +++ b/src/main/java/javax/inject/Provider.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2009 The JSR-330 Expert Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package javax.inject; + +/** + * Provides instances of {@code T}. Typically implemented by an injector. For + * any type {@code T} that can be injected, you can also inject + * {@code Provider}. Compared to injecting {@code T} directly, injecting + * {@code Provider} enables: + * + *
    + *
  • retrieving multiple instances.
  • + *
  • lazy or optional retrieval of an instance.
  • + *
  • breaking circular dependencies.
  • + *
  • abstracting scope so you can look up an instance in a smaller scope + * from an instance in a containing scope.
  • + *
+ * + *

For example: + * + *

+ *   class Car {
+ *     @Inject Car(Provider<Seat> seatProvider) {
+ *       Seat driver = seatProvider.get();
+ *       Seat passenger = seatProvider.get();
+ *       ...
+ *     }
+ *   }
+ */ +public interface Provider { + + /** + * Provides a fully-constructed and injected instance of {@code T}. + * + * @throws RuntimeException if the injector encounters an error while + * providing an instance. For example, if an injectable member on + * {@code T} throws an exception, the injector may wrap the exception + * and throw it to the caller of {@code get()}. Callers should not try + * to handle such exceptions as the behavior may vary across injector + * implementations and even different configurations of the same injector. + */ + T get(); +} diff --git a/src/main/java/javax/inject/Qualifier.java b/src/main/java/javax/inject/Qualifier.java new file mode 100644 index 0000000..6b4c42d --- /dev/null +++ b/src/main/java/javax/inject/Qualifier.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2009 The JSR-330 Expert Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package javax.inject; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import java.lang.annotation.Documented; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; + +/** + * Identifies qualifier annotations. Anyone can define a new qualifier. A + * qualifier annotation: + * + *
    + *
  • is annotated with {@code @Qualifier}, {@code @Retention(RUNTIME)}, + * and typically {@code @Documented}.
  • + *
  • can have attributes.
  • + *
  • may be part of the public API, much like the dependency type, but + * unlike implementation types which needn't be part of the public + * API.
  • + *
  • may have restricted usage if annotated with {@code @Target}. While + * this specification covers applying qualifiers to fields and + * parameters only, some injector configurations might use qualifier + * annotations in other places (on methods or classes for example).
  • + *
+ * + *

For example: + * + *

+ *   @java.lang.annotation.Documented
+ *   @java.lang.annotation.Retention(RUNTIME)
+ *   @javax.inject.Qualifier
+ *   public @interface Leather {
+ *     Color color() default Color.TAN;
+ *     public enum Color { RED, BLACK, TAN }
+ *   }
+ * + * @see javax.inject.Named @Named + */ +@Target(ANNOTATION_TYPE) +@Retention(RUNTIME) +@Documented +public @interface Qualifier {} diff --git a/src/main/java/javax/inject/Scope.java b/src/main/java/javax/inject/Scope.java new file mode 100644 index 0000000..27133f1 --- /dev/null +++ b/src/main/java/javax/inject/Scope.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2009 The JSR-330 Expert Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package javax.inject; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import java.lang.annotation.Documented; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; + +/** + * Identifies scope annotations. A scope annotation applies to a class + * containing an injectable constructor and governs how the injector reuses + * instances of the type. By default, if no scope annotation is present, the + * injector creates an instance (by injecting the type's constructor), uses + * the instance for one injection, and then forgets it. If a scope annotation + * is present, the injector may retain the instance for possible reuse in a + * later injection. If multiple threads can access a scoped instance, its + * implementation should be thread safe. The implementation of the scope + * itself is left up to the injector. + * + *

In the following example, the scope annotation {@code @Singleton} ensures + * that we only have one Log instance: + * + *

+ *   @Singleton
+ *   class Log {
+ *     void log(String message) { ... }
+ *   }
+ * + *

The injector generates an error if it encounters more than one scope + * annotation on the same class or a scope annotation it doesn't support. + * + *

A scope annotation: + *

    + *
  • is annotated with {@code @Scope}, {@code @Retention(RUNTIME)}, + * and typically {@code @Documented}.
  • + *
  • should not have attributes.
  • + *
  • is typically not {@code @Inherited}, so scoping is orthogonal to + * implementation inheritance.
  • + *
  • may have restricted usage if annotated with {@code @Target}. While + * this specification covers applying scopes to classes only, some + * injector configurations might use scope annotations + * in other places (on factory method results for example).
  • + *
+ * + *

For example: + * + *

+ *   @java.lang.annotation.Documented
+ *   @java.lang.annotation.Retention(RUNTIME)
+ *   @javax.inject.Scope
+ *   public @interface RequestScoped {}
+ * + *

Annotating scope annotations with {@code @Scope} helps the injector + * detect the case where a programmer used the scope annotation on a class but + * forgot to configure the scope in the injector. A conservative injector + * would generate an error rather than not apply a scope. + * + * @see javax.inject.Singleton @Singleton + */ +@Target(ANNOTATION_TYPE) +@Retention(RUNTIME) +@Documented +public @interface Scope {} diff --git a/src/main/java/javax/inject/Singleton.java b/src/main/java/javax/inject/Singleton.java new file mode 100644 index 0000000..a2af7b9 --- /dev/null +++ b/src/main/java/javax/inject/Singleton.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2009 The JSR-330 Expert Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package javax.inject; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Identifies a type that the injector only instantiates once. Not inherited. + * + * @see javax.inject.Scope @Scope + */ +@Scope +@Documented +@Retention(RUNTIME) +public @interface Singleton {} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java new file mode 100644 index 0000000..241ed2c --- /dev/null +++ b/src/main/java/module-info.java @@ -0,0 +1,3 @@ +module org.xbib.javax.inject { + exports javax.inject; +}