diff -Nru libsejda-injector-java-1.0.2/debian/changelog libsejda-injector-java-2.0.0/debian/changelog --- libsejda-injector-java-1.0.2/debian/changelog 2017-07-03 20:55:11.000000000 +0000 +++ libsejda-injector-java-2.0.0/debian/changelog 2020-10-24 20:35:58.000000000 +0000 @@ -1,3 +1,11 @@ +libsejda-injector-java (2.0.0-1) unstable; urgency=medium + + * New upstream version 2.0.0. + * Switch to debhelper-compat = 13. + * Declare compliance with Debian Policy 4.5.0. + + -- Markus Koschany Sat, 24 Oct 2020 22:35:58 +0200 + libsejda-injector-java (1.0.2-1) unstable; urgency=medium * Initial release. (Closes: #866995) diff -Nru libsejda-injector-java-1.0.2/debian/compat libsejda-injector-java-2.0.0/debian/compat --- libsejda-injector-java-1.0.2/debian/compat 2017-07-03 20:55:11.000000000 +0000 +++ libsejda-injector-java-2.0.0/debian/compat 1970-01-01 00:00:00.000000000 +0000 @@ -1 +0,0 @@ -10 diff -Nru libsejda-injector-java-1.0.2/debian/control libsejda-injector-java-2.0.0/debian/control --- libsejda-injector-java-1.0.2/debian/control 2017-07-03 20:55:11.000000000 +0000 +++ libsejda-injector-java-2.0.0/debian/control 2020-10-24 20:35:58.000000000 +0000 @@ -5,14 +5,14 @@ Uploaders: Markus Koschany Build-Depends: - debhelper (>= 10), + debhelper-compat (= 13), default-jdk, libatinject-jsr330-api-java, libslf4j-java (>= 1.7.21), maven-debian-helper (>= 2.1) -Standards-Version: 4.0.0 -Vcs-Git: https://anonscm.debian.org/git/pkg-java/libsejda-injector-java.git -Vcs-Browser: https://anonscm.debian.org/git/pkg-java/libsejda-injector-java.git +Standards-Version: 4.5.0 +Vcs-Git: https://salsa.debian.org/java-team/libsejda-injector-java.git +Vcs-Browser: https://salsa.debian.org/java-team/libsejda-injector-java Homepage: http://www.sejda.org Package: libsejda-injector-java diff -Nru libsejda-injector-java-1.0.2/debian/copyright libsejda-injector-java-2.0.0/debian/copyright --- libsejda-injector-java-1.0.2/debian/copyright 2017-07-03 20:55:11.000000000 +0000 +++ libsejda-injector-java-2.0.0/debian/copyright 2020-10-24 20:35:58.000000000 +0000 @@ -3,11 +3,11 @@ Source: https://github.com/torakiki/sejda-injector Files: * -Copyright: 2017, Andrea Vacondio +Copyright: 2017-2020, Andrea Vacondio License: Apache-2.0 Files: debian/* -Copyright: 2017, Markus Koschany +Copyright: 2017-2020, Markus Koschany License: Apache-2.0 License: Apache-2.0 diff -Nru libsejda-injector-java-1.0.2/debian/rules libsejda-injector-java-2.0.0/debian/rules --- libsejda-injector-java-1.0.2/debian/rules 2017-07-03 20:55:11.000000000 +0000 +++ libsejda-injector-java-2.0.0/debian/rules 2020-10-24 20:35:58.000000000 +0000 @@ -3,5 +3,3 @@ %: dh $@ -get-orig-source: - uscan --download-current-version --force-download diff -Nru libsejda-injector-java-1.0.2/development/eclipse/codetemplates.xml libsejda-injector-java-2.0.0/development/eclipse/codetemplates.xml --- libsejda-injector-java-1.0.2/development/eclipse/codetemplates.xml 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/development/eclipse/codetemplates.xml 2020-02-03 15:07:25.000000000 +0000 @@ -1,13 +1,8 @@ - \ No newline at end of file diff -Nru libsejda-injector-java-1.0.2/pom.xml libsejda-injector-java-2.0.0/pom.xml --- libsejda-injector-java-1.0.2/pom.xml 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/pom.xml 2020-02-03 15:07:25.000000000 +0000 @@ -1,23 +1,23 @@ 4.0.0 - org.sejda - sejda-injector + org.pdfsam + pdfsam-injector jar - sejda-injector - 1.0.2 + pdfsam-injector + 2.0.0 A simple dependency injection engine based on Feather - http://www.sejda.org + https://pdfsam.org GitHub - https://github.com/torakiki/sejda-injector/ + https://github.com/torakiki/pdfsam-injector/ - sejda - http://www.sejda.org + Sober Lemur S.a.s. di Vacondio Andrea + https://pdfsam.org @@ -34,10 +34,10 @@ - scm:git:git@github.com:torakiki/sejda-injector.git - scm:git:git@github.com:torakiki/sejda-injector.git - scm:git:git@github.com:torakiki/sejda-injector.git - v1.0.2 + scm:git:git@github.com:torakiki/pdfsam-injector.git + scm:git:git@github.com:torakiki/pdfsam-injector.git + scm:git:git@github.com:torakiki/pdfsam-injector.git + v2.0.0 @@ -54,8 +54,8 @@ https://oss.sonatype.org/content/repositories/snapshots - bintray - https://api.bintray.com/maven/sejdapdf/maven/sejda + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ @@ -67,7 +67,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.10.3 + 3.0.1 attach-javadocs @@ -80,7 +80,7 @@ org.apache.maven.plugins maven-release-plugin - 2.5.2 + 3.0.0-M1 v@{project.version} true @@ -101,6 +101,17 @@ + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.8 + true + + ossrh + https://oss.sonatype.org/ + true + + @@ -111,23 +122,21 @@ org.apache.maven.plugins maven-compiler-plugin - 3.3 + 3.8.1 1.8 1.8 - true - lines,vars,source org.apache.maven.plugins maven-jar-plugin - 2.6 + 3.1.2 org.apache.maven.plugins maven-source-plugin - 2.4 + 3.1.0 attach-sources @@ -137,24 +146,29 @@ + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M4 + org.slf4j slf4j-api - 1.7.21 + 1.7.29 ch.qos.logback logback-classic - 1.1.7 + 1.2.3 test org.mockito mockito-core - 1.10.19 + 3.0.0 test diff -Nru libsejda-injector-java-1.0.2/README.md libsejda-injector-java-2.0.0/README.md --- libsejda-injector-java-1.0.2/README.md 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/README.md 2020-02-03 15:07:25.000000000 +0000 @@ -1,7 +1,7 @@ -Sejda-injector (http://www.sejda.org) +pdfsam-injector (https://pdfsam.org) ===== [![Build Status](https://travis-ci.org/torakiki/pdfsam-injector.png)](https://travis-ci.org/torakiki/pdfsam-injector) [![License](http://img.shields.io/badge/license-APLv2-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0.html) -A simple dependency injection engine based on [Feather](https://github.com/zsoltherpai/feather) and used in [PDFsam](http://pdfsam.org). +A simple dependency injection engine based on [Feather](https://github.com/zsoltherpai/feather) and used in [PDFsam](https://pdfsam.org). diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/Auto.java libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/Auto.java --- libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/Auto.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/Auto.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,38 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.inject.Singleton; + +/** + * Configure the injector to automatically create the singleton instance + * + * @author Andrea Vacondio + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.METHOD, ElementType.TYPE }) +@Singleton +public @interface Auto { + +} diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/Components.java libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/Components.java --- libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/Components.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/Components.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,39 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Condiguration instances can be annotated with classes to tell the injector it should scan those classes. + * + * @author Andrea Vacondio + * + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface Components { + /** + * Classes to scan and add to the injector + */ + Class[] value() default {}; +} diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/InjectionException.java libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/InjectionException.java --- libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/InjectionException.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/InjectionException.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,27 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +public class InjectionException extends RuntimeException { + InjectionException(String message) { + super(message); + } + + InjectionException(String message, Throwable cause) { + super(message, cause); + } +} diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/Injector.java libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/Injector.java --- libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/Injector.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/Injector.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,375 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static java.util.Collections.singleton; +import static java.util.Objects.isNull; +import static java.util.Objects.nonNull; +import static java.util.Optional.ofNullable; +import static java.util.stream.Collectors.joining; +import static java.util.stream.Stream.concat; +import static java.util.stream.Stream.of; + +import java.io.Closeable; +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; + +import javax.inject.Inject; +import javax.inject.Provider; +import javax.inject.Qualifier; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Injector implements Closeable { + + private static final Logger LOG = LoggerFactory.getLogger(Injector.class); + + private static final Queue CONFIGURATIONS = new ConcurrentLinkedQueue<>(); + private static final Map, Class> COMPONENTS = new ConcurrentHashMap<>(); + + private final Map, Provider> providers = new ConcurrentHashMap<>(); + private final Map, Object> singletons = new ConcurrentHashMap<>(); + private final Set> autos = new HashSet<>(); + + public static void addConfig(Object... configurations) { + Arrays.stream(configurations).forEach(CONFIGURATIONS::add); + } + + public static void addConfig(Iterable configurations) { + for (Object config : configurations) { + CONFIGURATIONS.add(config); + } + } + + /** + * Adds the given components classes to the injectable ones + * + * @param components + */ + public static void add(Class... components) { + Arrays.stream(components).forEach(c -> COMPONENTS.put(c, c)); + } + + /** + * @return the context started with the given additional configurations + */ + public static Injector start(Object... configurations) { + Injector.addConfig(configurations); + return Injector.start(); + } + + /** + * @return the context started with the given additional configurations + */ + public static Injector start(Iterable configurations) { + Injector.addConfig(configurations); + return Injector.start(); + } + + /** + * @return the context started with the previously provided configuration + */ + public static Injector start() { + LOG.debug("Starting injector with {} configuration modules", CONFIGURATIONS.size()); + return new Injector(); + } + + @Override + public void close() { + LOG.debug("Closing injector"); + this.providers.clear(); + this.singletons.clear(); + } + + private Injector() { + providers.put(Key.of(Injector.class), () -> this); + try { + for (final Object config : CONFIGURATIONS) { + if (config instanceof Class) { + throw new InjectionException(String.format("%s provided as class instead of an instance.", + ((Class) config).getName())); + } + for (Method providerMethod : providers(config.getClass())) { + providerMethod(config, providerMethod); + } + ofNullable(config.getClass().getAnnotation(Components.class)).map(c -> c.value()) + .filter(Objects::nonNull).ifPresent(Injector::add); + } + LOG.debug("Parsing {} components", COMPONENTS.size()); + COMPONENTS.keySet().forEach(this::provider); + LOG.trace("Parsed components classes"); + } finally { + CONFIGURATIONS.clear(); + COMPONENTS.clear(); + } + LOG.debug("Autocreating {} singletons", autos.size()); + autos.forEach(k -> providers.get(k).get()); + autos.clear(); + } + + /** + * @return an instance of type + */ + public T instance(Class type) { + return provider(Key.of(type), null).get(); + } + + /** + * @return instance specified by key (type and qualifier) + */ + public T instance(Key key) { + return provider(key, null).get(); + } + + /** + * @return an instance of type + */ + @SuppressWarnings("unchecked") + public List instancesOfType(Class type) { + return (List) listProvider(Key.of(type)).get(); + } + + /** + * @return provider of type + */ + public Provider provider(Class type) { + return provider(Key.of(type), null); + } + + /** + * @return provider of key (type, qualifier) + */ + public Provider provider(Key key) { + return provider(key, null); + } + + @SuppressWarnings("unchecked") + private Provider provider(final Key key, Set> chain) { + if (!providers.containsKey(key)) { + if (nonNull(key.qualifier)) { + throw new InjectionException("Unable to find provider for " + key); + } + final Constructor constructor = constructor(key); + final Provider[] paramProviders = paramProviders(key, constructor.getParameterTypes(), + constructor.getGenericParameterTypes(), constructor.getParameterAnnotations(), chain); + providers.put(key, singletonProvider(key, + !key.type.isAnnotationPresent(Prototype.class) || key.type.isAnnotationPresent(Auto.class), () -> { + try { + return constructor.newInstance(params(paramProviders)); + } catch (Exception e) { + throw new InjectionException(String.format("Can't instantiate %s", key.toString()), e); + } + })); + if (key.type.isAnnotationPresent(Auto.class)) { + LOG.trace("To be autocreated {}", key); + autos.add(key); + } + } + return (Provider) providers.get(key); + } + + private void providerMethod(final Object module, final Method m) { + final Key key = Key.of(m.getReturnType(), qualifier(m.getAnnotations())); + if (providers.containsKey(key)) { + throw new InjectionException( + String.format("%s has multiple providers, configuration %s", key.toString(), module.getClass())); + } + boolean singleton = !(m.isAnnotationPresent(Prototype.class) + || m.getReturnType().isAnnotationPresent(Prototype.class)) + || (m.isAnnotationPresent(Auto.class) || m.getReturnType().isAnnotationPresent(Auto.class)); + final Provider[] paramProviders = paramProviders(key, m.getParameterTypes(), m.getGenericParameterTypes(), + m.getParameterAnnotations(), Collections.singleton(key)); + providers.put(key, singletonProvider(key, singleton, () -> { + try { + return m.invoke(module, params(paramProviders)); + } catch (Exception e) { + throw new InjectionException(String.format("Can't instantiate %s with provider", key.toString()), e); + } + })); + if (m.isAnnotationPresent(Auto.class) || m.getReturnType().isAnnotationPresent(Auto.class)) { + LOG.trace("To be autocreated {}", key); + autos.add(key); + } + } + + @SuppressWarnings("unchecked") + private Provider singletonProvider(final Key key, boolean singleton, final Provider provider) { + if (singleton) { + return () -> { + if (!singletons.containsKey(key)) { + synchronized (singletons) { + if (!singletons.containsKey(key)) { + singletons.put(key, provider.get()); + } + } + } + return (T) singletons.get(key); + }; + } + return provider; + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private Provider> listProvider(final Key key) { + return () -> { + List items = new ArrayList<>(); + providers.keySet().stream().filter(k -> key.type.isAssignableFrom(k.type)).map(providers::get) + .map(p -> p.get()).forEach(i -> items.add(i)); + return items; + }; + + } + + private Provider[] paramProviders(final Key key, Class[] parameterClasses, Type[] parameterTypes, + Annotation[][] annotations, final Set> chain) { + Provider[] providers = new Provider[parameterTypes.length]; + for (int i = 0; i < parameterTypes.length; ++i) { + Class parameterClass = parameterClasses[i]; + Annotation qualifier = qualifier(annotations[i]); + Optional> parametrizedType = Optional.empty(); + if (Provider.class.equals(parameterClass) || List.class.equals(parameterClass)) { + Type type = ((ParameterizedType) parameterTypes[i]).getActualTypeArguments()[0]; + if (!(type instanceof Class)) { + throw new InjectionException("Unable to inject parameterized type \"" + type.toString() + "\""); + } + parametrizedType = ofNullable((Class) type); + } + + // we handle special cases of a Provider and unqualified List + if (parametrizedType.isPresent() && (Provider.class.equals(parameterClass) + || (List.class.equals(parameterClass) && isNull(qualifier)))) { + final Key newKey = Key.of(parametrizedType.get(), qualifier); + if (Provider.class.equals(parameterClass)) { + providers[i] = () -> { + return provider(newKey, null); + }; + } + if (List.class.equals(parameterClass)) { + providers[i] = listProvider(newKey); + } + } else { + final Key newKey = Key.of(parameterClass, qualifier); + final Set> newChain = append(chain, key); + if (newChain.contains(newKey)) { + throw new InjectionException(String.format("Circular dependency: %s", chain(newChain, newKey))); + } + providers[i] = () -> { + return provider(newKey, newChain).get(); + }; + } + + } + return providers; + } + + private static Object[] params(Provider[] paramProviders) { + Object[] params = new Object[paramProviders.length]; + for (int i = 0; i < paramProviders.length; ++i) { + params[i] = paramProviders[i].get(); + } + return params; + } + + private static Set> append(Set> set, Key newKey) { + if (set != null && !set.isEmpty()) { + Set> appended = new LinkedHashSet<>(set); + appended.add(newKey); + return appended; + } + return singleton(newKey); + } + + private static String chain(Set> chain, Key lastKey) { + return concat(chain.stream().map(Key::toString), of(lastKey.toString())).collect(joining(" -> ")); + } + + private static Constructor constructor(Key key) { + Constructor inject = null; + Constructor noarg = null; + for (Constructor c : key.type.getDeclaredConstructors()) { + if (c.isAnnotationPresent(Inject.class)) { + if (inject == null) { + inject = c; + } else { + throw new InjectionException(String.format("%s has multiple @Inject constructors", key.type)); + } + } else if (c.getParameterTypes().length == 0) { + noarg = c; + } + } + Constructor constructor = inject != null ? inject : noarg; + if (constructor != null) { + constructor.setAccessible(true); + return constructor; + } + throw new InjectionException(String.format( + "%s doesn't have an @Inject or no-arg constructor, or a configured provider", key.type.getName())); + + } + + private static Set providers(Class type) { + Class current = type; + Set providers = new HashSet<>(); + while (!current.equals(Object.class)) { + for (Method method : current.getDeclaredMethods()) { + if (method.isAnnotationPresent(Provides.class) + && (type.equals(current) || !providerInSubClass(method, providers))) { + method.setAccessible(true); + providers.add(method); + } + } + current = current.getSuperclass(); + } + return providers; + } + + private static Annotation qualifier(Annotation[] annotations) { + for (Annotation annotation : annotations) { + if (annotation.annotationType().isAnnotationPresent(Qualifier.class)) { + return annotation; + } + } + return null; + } + + private static boolean providerInSubClass(Method method, Set discoveredMethods) { + for (Method discovered : discoveredMethods) { + if (discovered.getName().equals(method.getName()) + && Arrays.equals(method.getParameterTypes(), discovered.getParameterTypes())) { + return true; + } + } + return false; + } +} diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/Key.java libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/Key.java --- libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/Key.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/Key.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,95 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import java.lang.annotation.Annotation; + +import javax.inject.Named; + +public class Key { + final Class type; + final Class qualifier; + final String name; + + private Key(Class type, Class qualifier, String name) { + this.type = type; + this.qualifier = qualifier; + this.name = name; + } + + /** + * @return Key for a given type + */ + public static Key of(Class type) { + return new Key<>(type, null, null); + } + + /** + * @return Key for a given type and qualifier annotation type + */ + public static Key of(Class type, Class qualifier) { + return new Key<>(type, qualifier, null); + } + + /** + * @return Key for a given type and name (@Named value) + */ + public static Key of(Class type, String name) { + return new Key<>(type, Named.class, name); + } + + static Key of(Class type, Annotation qualifier) { + if (qualifier == null) { + return Key.of(type); + } + return qualifier.annotationType().equals(Named.class) ? Key.of(type, ((Named) qualifier).value()) + : Key.of(type, qualifier.annotationType()); + + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + + Key key = (Key) o; + + if (!type.equals(key.type)) + return false; + if (qualifier != null ? !qualifier.equals(key.qualifier) : key.qualifier != null) + return false; + return !(name != null ? !name.equals(key.name) : key.name != null); + + } + + @Override + public int hashCode() { + int result = type.hashCode(); + result = 31 * result + (qualifier != null ? qualifier.hashCode() : 0); + result = 31 * result + (name != null ? name.hashCode() : 0); + return result; + } + + @Override + public String toString() { + String suffix = name != null ? "@\"" + name + "\"" : qualifier != null ? "@" + qualifier.getSimpleName() : ""; + return type.getName() + suffix; + } + +} \ No newline at end of file diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/Prototype.java libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/Prototype.java --- libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/Prototype.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/Prototype.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,36 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; + +import javax.inject.Scope; + +/** + * Identifies a type that the injector instantiates every time the type is requested. + * + * @see javax.inject.Scope @Scope + */ +@Scope +@Documented +@Retention(RUNTIME) +public @interface Prototype { + +} diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/Provides.java libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/Provides.java --- libsejda-injector-java-1.0.2/src/main/java/org/pdfsam/injector/Provides.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/pdfsam/injector/Provides.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,24 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface Provides { +} diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/Auto.java libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/Auto.java --- libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/Auto.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/Auto.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,35 +0,0 @@ -/* - * 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 org.sejda.injector; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import javax.inject.Singleton; - -/** - * Configure the injector to automatically create the singleton instance - * - * @author Andrea Vacondio - */ -@Documented -@Retention(RetentionPolicy.RUNTIME) -@Target({ ElementType.METHOD, ElementType.TYPE }) -@Singleton -public @interface Auto { - -} diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/Components.java libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/Components.java --- libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/Components.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/Components.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,36 +0,0 @@ -/* - * 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 org.sejda.injector; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Condiguration instances can be annotated with classes to tell the injector it should scan those classes. - * - * @author Andrea Vacondio - * - */ -@Documented -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE) -public @interface Components { - /** - * Classes to scan and add to the injector - */ - Class[] value() default {}; -} diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/InjectionException.java libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/InjectionException.java --- libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/InjectionException.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/InjectionException.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,24 +0,0 @@ -/* - * 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 org.sejda.injector; - -public class InjectionException extends RuntimeException { - InjectionException(String message) { - super(message); - } - - InjectionException(String message, Throwable cause) { - super(message, cause); - } -} diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/Injector.java libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/Injector.java --- libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/Injector.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/Injector.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,373 +0,0 @@ -/* - * 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 org.sejda.injector; - -import static java.util.Collections.singleton; -import static java.util.Objects.isNull; -import static java.util.Objects.nonNull; -import static java.util.Optional.ofNullable; -import static java.util.stream.Collectors.joining; -import static java.util.stream.Stream.concat; -import static java.util.stream.Stream.of; - -import java.io.Closeable; -import java.lang.annotation.Annotation; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Queue; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; - -import javax.inject.Inject; -import javax.inject.Provider; -import javax.inject.Qualifier; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Injector implements Closeable { - - private static final Logger LOG = LoggerFactory.getLogger(Injector.class); - - private static final Queue CONFIGURATIONS = new ConcurrentLinkedQueue<>(); - private static final Map, Class> COMPONENTS = new ConcurrentHashMap<>(); - - private final Map, Provider> providers = new ConcurrentHashMap<>(); - private final Map, Object> singletons = new ConcurrentHashMap<>(); - private final Set> autos = new HashSet<>(); - - public static void addConfig(Object... configurations) { - Arrays.stream(configurations).forEach(CONFIGURATIONS::add); - } - - public static void addConfig(Iterable configurations) { - for (Object config : configurations) { - CONFIGURATIONS.add(config); - } - } - - /** - * Adds the given components classes to the injectable ones - * - * @param components - */ - public static void add(Class... components) { - Arrays.stream(components).forEach(c -> COMPONENTS.put(c, c)); - } - - /** - * @return the context started with the given additional configurations - */ - public static Injector start(Object... configurations) { - Injector.addConfig(configurations); - return Injector.start(); - } - - /** - * @return the context started with the given additional configurations - */ - public static Injector start(Iterable configurations) { - Injector.addConfig(configurations); - return Injector.start(); - } - - /** - * @return the context started with the previously provided configuration - */ - public static Injector start() { - LOG.debug("Starting injector with {} configuration modules", CONFIGURATIONS.size()); - return new Injector(); - } - - @Override - public void close() { - LOG.debug("Closing injector"); - this.providers.clear(); - this.singletons.clear(); - } - - private Injector() { - providers.put(Key.of(Injector.class), () -> this); - try { - for (final Object config : CONFIGURATIONS) { - if (config instanceof Class) { - throw new InjectionException(String.format("%s provided as class instead of an instance.", - ((Class) config).getName())); - } - for (Method providerMethod : providers(config.getClass())) { - providerMethod(config, providerMethod); - } - ofNullable(config.getClass().getAnnotation(Components.class)).map(c -> c.value()) - .filter(Objects::nonNull).ifPresent(Injector::add); - } - LOG.debug("Parsing {} components", COMPONENTS.size()); - COMPONENTS.keySet().forEach(this::provider); - LOG.trace("Parsed components classes"); - } finally { - CONFIGURATIONS.clear(); - COMPONENTS.clear(); - } - LOG.debug("Autocreating {} singletons", autos.size()); - autos.forEach(k -> providers.get(k).get()); - autos.clear(); - } - - /** - * @return an instance of type - */ - public T instance(Class type) { - return provider(Key.of(type), null).get(); - } - - /** - * @return instance specified by key (type and qualifier) - */ - public T instance(Key key) { - return provider(key, null).get(); - } - - /** - * @return an instance of type - */ - @SuppressWarnings("unchecked") - public List instancesOfType(Class type) { - return (List) listProvider(Key.of(type)).get(); - } - - /** - * @return provider of type - */ - public Provider provider(Class type) { - return provider(Key.of(type), null); - } - - /** - * @return provider of key (type, qualifier) - */ - public Provider provider(Key key) { - return provider(key, null); - } - - @SuppressWarnings("unchecked") - private Provider provider(final Key key, Set> chain) { - if (!providers.containsKey(key)) { - if (nonNull(key.qualifier)) { - throw new InjectionException("Unable to find provider for " + key); - } - final Constructor constructor = constructor(key); - final Provider[] paramProviders = paramProviders(key, constructor.getParameterTypes(), - constructor.getGenericParameterTypes(), constructor.getParameterAnnotations(), chain); - providers.put(key, singletonProvider(key, - !key.type.isAnnotationPresent(Prototype.class) || key.type.isAnnotationPresent(Auto.class), () -> { - try { - return constructor.newInstance(params(paramProviders)); - } catch (Exception e) { - throw new InjectionException(String.format("Can't instantiate %s", key.toString()), e); - } - })); - if (key.type.isAnnotationPresent(Auto.class)) { - LOG.trace("To be autocreated {}", key); - autos.add(key); - } - } - return (Provider) providers.get(key); - } - - private void providerMethod(final Object module, final Method m) { - final Key key = Key.of(m.getReturnType(), qualifier(m.getAnnotations())); - if (providers.containsKey(key)) { - throw new InjectionException( - String.format("%s has multiple providers, configuration %s", key.toString(), module.getClass())); - } - boolean singleton = !(m.isAnnotationPresent(Prototype.class) - || m.getReturnType().isAnnotationPresent(Prototype.class)) - || (m.isAnnotationPresent(Auto.class) || m.getReturnType().isAnnotationPresent(Auto.class)); - final Provider[] paramProviders = paramProviders(key, m.getParameterTypes(), m.getGenericParameterTypes(), - m.getParameterAnnotations(), Collections.singleton(key)); - providers.put(key, singletonProvider(key, singleton, () -> { - try { - return m.invoke(module, params(paramProviders)); - } catch (Exception e) { - throw new InjectionException(String.format("Can't instantiate %s with provider", key.toString()), e); - } - })); - if (m.isAnnotationPresent(Auto.class) || m.getReturnType().isAnnotationPresent(Auto.class)) { - LOG.trace("To be autocreated {}", key); - autos.add(key); - } - } - - @SuppressWarnings("unchecked") - private Provider singletonProvider(final Key key, boolean singleton, final Provider provider) { - if (singleton) { - return () -> { - if (!singletons.containsKey(key)) { - synchronized (singletons) { - if (!singletons.containsKey(key)) { - singletons.put(key, provider.get()); - } - } - } - return (T) singletons.get(key); - }; - } - return provider; - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - private Provider> listProvider(final Key key) { - return () -> { - List items = new ArrayList<>(); - providers.keySet().stream().filter(k -> key.type.isAssignableFrom(k.type)).map(providers::get) - .map(p -> p.get()).forEach(i -> items.add(i)); - return items; - }; - - } - - private Provider[] paramProviders(final Key key, Class[] parameterClasses, Type[] parameterTypes, - Annotation[][] annotations, final Set> chain) { - Provider[] providers = new Provider[parameterTypes.length]; - for (int i = 0; i < parameterTypes.length; ++i) { - Class parameterClass = parameterClasses[i]; - Annotation qualifier = qualifier(annotations[i]); - Optional> parametrizedType = Optional.empty(); - if (Provider.class.equals(parameterClass) || List.class.equals(parameterClass)) { - Type type = ((ParameterizedType) parameterTypes[i]).getActualTypeArguments()[0]; - if (!(type instanceof Class)) { - throw new InjectionException("Unable to inject parameterized type \"" + type.toString() + "\""); - } - parametrizedType = ofNullable((Class) type); - } - - // we handle special cases of a Provider and unqualified List - if (parametrizedType.isPresent() && (Provider.class.equals(parameterClass) - || (List.class.equals(parameterClass) && isNull(qualifier)))) { - final Key newKey = Key.of(parametrizedType.get(), qualifier); - if (Provider.class.equals(parameterClass)) { - providers[i] = () -> { - return provider(newKey, null); - }; - } - if (List.class.equals(parameterClass)) { - providers[i] = listProvider(newKey); - } - } else { - final Key newKey = Key.of(parameterClass, qualifier); - final Set> newChain = append(chain, key); - if (newChain.contains(newKey)) { - throw new InjectionException(String.format("Circular dependency: %s", chain(newChain, newKey))); - } - providers[i] = () -> { - return provider(newKey, newChain).get(); - }; - } - - } - return providers; - } - - private static Object[] params(Provider[] paramProviders) { - Object[] params = new Object[paramProviders.length]; - for (int i = 0; i < paramProviders.length; ++i) { - params[i] = paramProviders[i].get(); - } - return params; - } - - private static Set> append(Set> set, Key newKey) { - if (set != null && !set.isEmpty()) { - Set> appended = new LinkedHashSet<>(set); - appended.add(newKey); - return appended; - } else { - return singleton(newKey); - } - } - - private static String chain(Set> chain, Key lastKey) { - return concat(chain.stream().map(Key::toString), of(lastKey.toString())).collect(joining(" -> ")); - } - - private static Constructor constructor(Key key) { - Constructor inject = null; - Constructor noarg = null; - for (Constructor c : key.type.getDeclaredConstructors()) { - if (c.isAnnotationPresent(Inject.class)) { - if (inject == null) { - inject = c; - } else { - throw new InjectionException(String.format("%s has multiple @Inject constructors", key.type)); - } - } else if (c.getParameterTypes().length == 0) { - noarg = c; - } - } - Constructor constructor = inject != null ? inject : noarg; - if (constructor != null) { - constructor.setAccessible(true); - return constructor; - } else { - throw new InjectionException(String.format( - "%s doesn't have an @Inject or no-arg constructor, or a configured provider", key.type.getName())); - } - } - - private static Set providers(Class type) { - Class current = type; - Set providers = new HashSet<>(); - while (!current.equals(Object.class)) { - for (Method method : current.getDeclaredMethods()) { - if (method.isAnnotationPresent(Provides.class) - && (type.equals(current) || !providerInSubClass(method, providers))) { - method.setAccessible(true); - providers.add(method); - } - } - current = current.getSuperclass(); - } - return providers; - } - - private static Annotation qualifier(Annotation[] annotations) { - for (Annotation annotation : annotations) { - if (annotation.annotationType().isAnnotationPresent(Qualifier.class)) { - return annotation; - } - } - return null; - } - - private static boolean providerInSubClass(Method method, Set discoveredMethods) { - for (Method discovered : discoveredMethods) { - if (discovered.getName().equals(method.getName()) - && Arrays.equals(method.getParameterTypes(), discovered.getParameterTypes())) { - return true; - } - } - return false; - } -} diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/Key.java libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/Key.java --- libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/Key.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/Key.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,92 +0,0 @@ -/* - * 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 org.sejda.injector; - -import java.lang.annotation.Annotation; - -import javax.inject.Named; - -public class Key { - final Class type; - final Class qualifier; - final String name; - - private Key(Class type, Class qualifier, String name) { - this.type = type; - this.qualifier = qualifier; - this.name = name; - } - - /** - * @return Key for a given type - */ - public static Key of(Class type) { - return new Key<>(type, null, null); - } - - /** - * @return Key for a given type and qualifier annotation type - */ - public static Key of(Class type, Class qualifier) { - return new Key<>(type, qualifier, null); - } - - /** - * @return Key for a given type and name (@Named value) - */ - public static Key of(Class type, String name) { - return new Key<>(type, Named.class, name); - } - - static Key of(Class type, Annotation qualifier) { - if (qualifier == null) { - return Key.of(type); - } else { - return qualifier.annotationType().equals(Named.class) ? Key.of(type, ((Named) qualifier).value()) - : Key.of(type, qualifier.annotationType()); - } - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - - Key key = (Key) o; - - if (!type.equals(key.type)) - return false; - if (qualifier != null ? !qualifier.equals(key.qualifier) : key.qualifier != null) - return false; - return !(name != null ? !name.equals(key.name) : key.name != null); - - } - - @Override - public int hashCode() { - int result = type.hashCode(); - result = 31 * result + (qualifier != null ? qualifier.hashCode() : 0); - result = 31 * result + (name != null ? name.hashCode() : 0); - return result; - } - - @Override - public String toString() { - String suffix = name != null ? "@\"" + name + "\"" : qualifier != null ? "@" + qualifier.getSimpleName() : ""; - return type.getName() + suffix; - } - -} \ No newline at end of file diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/Prototype.java libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/Prototype.java --- libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/Prototype.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/Prototype.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,33 +0,0 @@ -/* - * 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 org.sejda.injector; - -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; - -import javax.inject.Scope; - -/** - * Identifies a type that the injector instantiates every time the type is requested. - * - * @see javax.inject.Scope @Scope - */ -@Scope -@Documented -@Retention(RUNTIME) -public @interface Prototype { - -} diff -Nru libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/Provides.java libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/Provides.java --- libsejda-injector-java-1.0.2/src/main/java/org/sejda/injector/Provides.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/main/java/org/sejda/injector/Provides.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,21 +0,0 @@ -/* - * 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 org.sejda.injector; - -import java.lang.annotation.*; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface Provides { -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/AmbiguousModuleTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/AmbiguousModuleTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/AmbiguousModuleTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/AmbiguousModuleTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,41 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import org.junit.Test; +import org.pdfsam.injector.InjectionException; +import org.pdfsam.injector.Injector; +import org.pdfsam.injector.Provides; + +public class AmbiguousModuleTest { + @Test(expected = InjectionException.class) + public void ambiguousModule() { + Injector.start(new Module()); + } + + public static class Module { + @Provides + String foo() { + return "foo"; + } + + @Provides + String bar() { + return "bar"; + } + } +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/AutoProviderTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/AutoProviderTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/AutoProviderTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/AutoProviderTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,70 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.mockito.Mockito.verify; + +import java.util.function.Consumer; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.pdfsam.injector.Auto; +import org.pdfsam.injector.Components; +import org.pdfsam.injector.Injector; +import org.pdfsam.injector.Provides; + +public class AutoProviderTest { + private static Consumer HIT; + + @Before + public void setUp() { + HIT = Mockito.mock(Consumer.class); + } + + @Test + public void autoCreatedAnnotatedClass() { + Injector injector = Injector.start(new Config()); + verify(HIT).accept("hit"); + } + + @Test + public void autoCreatedAnnotatedConfig() { + Injector injector = Injector.start(new ConfigComponent()); + verify(HIT).accept("hit"); + } + + public class Config { + + @Provides + public AnnotatedClass sing() { + return new AnnotatedClass(); + } + } + + @Components({ AnnotatedClass.class }) + public class ConfigComponent { + + } + + @Auto + public static class AnnotatedClass { + AnnotatedClass() { + HIT.accept("hit"); + } + } +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/CircularDependencyTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/CircularDependencyTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/CircularDependencyTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/CircularDependencyTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,77 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.junit.Assert.assertNotNull; + +import javax.inject.Inject; +import javax.inject.Provider; + +import org.junit.Test; +import org.pdfsam.injector.InjectionException; +import org.pdfsam.injector.Injector; + +public class CircularDependencyTest { + @Test(expected = InjectionException.class) + public void circularDependencyCaught() { + Injector injector = Injector.start(); + injector.instance(Circle1.class); + } + + @Test + public void circularDependencyWithProviderAllowed() { + Injector injector = Injector.start(); + CircleWithProvider1 circle1 = injector.instance(CircleWithProvider1.class); + assertNotNull(circle1.circleWithProvider2.circleWithProvider1.get()); + } + + public static class Circle1 { + private final Circle2 circle2; + + @Inject + public Circle1(Circle2 circle2) { + this.circle2 = circle2; + } + } + + public static class Circle2 { + private final Circle1 circle1; + + @Inject + public Circle2(Circle1 circle1) { + this.circle1 = circle1; + } + } + + public static class CircleWithProvider1 { + private final CircleWithProvider2 circleWithProvider2; + + @Inject + public CircleWithProvider1(CircleWithProvider2 circleWithProvider2) { + this.circleWithProvider2 = circleWithProvider2; + } + } + + public static class CircleWithProvider2 { + private final Provider circleWithProvider1; + + @Inject + public CircleWithProvider2(Provider circleWithProvider1) { + this.circleWithProvider1 = circleWithProvider1; + } + } +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/DependencyTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/DependencyTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/DependencyTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/DependencyTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,58 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.junit.Assert.assertNotNull; + +import javax.inject.Provider; + +import org.junit.Test; +import org.pdfsam.injector.InjectionException; +import org.pdfsam.injector.Injector; + +public class DependencyTest { + @Test + public void dependencyInstance() { + Injector injector = Injector.start(); + assertNotNull(injector.instance(Plain.class)); + } + + @Test + public void provider() { + Injector injector = Injector.start(); + Provider plainProvider = injector.provider(Plain.class); + assertNotNull(plainProvider.get()); + } + + @Test(expected = InjectionException.class) + public void unknown() { + Injector injector = Injector.start(); + injector.instance(Unknown.class); + } + + public static class Plain { + + } + + public static class Unknown { + public Unknown(String noSuitableConstructor) { + + } + } +} + + diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/InjectorTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/InjectorTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/InjectorTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/InjectorTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,56 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.pdfsam.injector.InjectionException; +import org.pdfsam.injector.Injector; +import org.pdfsam.injector.Provides; + +public class InjectorTest { + @Test(expected = InjectionException.class) + public void classAsConfig() { + Injector.addConfig(String.class); + Injector.start(); + } + + @Test(expected = InjectionException.class) + public void closed() { + Injector.addConfig(new Config()); + Injector injector = Injector.start(); + assertEquals("ChuckNorris", injector.instance(Obj.class).val); + injector.close(); + injector.instance(Obj.class); + } + + public class Config { + @Provides + public Obj obj() { + return new Obj("ChuckNorris"); + } + } + + private static class Obj { + final String val; + + Obj(String val) { + this.val = val; + } + } +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/ListInjectionTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/ListInjectionTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/ListInjectionTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/ListInjectionTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,106 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.List; + +import javax.inject.Inject; +import javax.inject.Named; + +import org.junit.Test; +import org.pdfsam.injector.InjectionException; +import org.pdfsam.injector.Injector; +import org.pdfsam.injector.Key; +import org.pdfsam.injector.Provides; + +public class ListInjectionTest { + @Test + public void listOfInjected() { + Injector injector = Injector.start(new Config()); + assertEquals(2, injector.instance(A.class).list.size()); + } + + @Test + public void instancesOfType() { + Injector injector = Injector.start(new Config()); + assertEquals(2, injector.instancesOfType(Base.class).size()); + } + + @Test(expected = InjectionException.class) + public void invalidType() { + Injector injector = Injector.start(new Config()); + injector.instance(B.class).list.size(); + } + + @Test + public void qualifiedListOfInjected() { + Injector injector = Injector.start(new Config()); + assertEquals(4, injector.instance(Key.of(List.class, "aList")).size()); + } + + public static class A { + private final List list; + + @Inject + public A(List list) { + this.list = list; + } + } + + public static class B { + private final List list; + + @Inject + public B(List list) { + this.list = list; + } + } + + public static class Config { + + @Provides + Sub1 sub1() { + return new Sub1(); + } + + @Provides + Sub2 sub2() { + return new Sub2(); + } + + @Provides + @Named("aList") + List qualifiedList() { + return Arrays.asList("a", "b", "c", "d"); + } + } + + public static class Base { + + } + + public static class Sub1 extends Base { + + } + + public static class Sub2 extends Base { + + } +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/ModuleOverrideTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/ModuleOverrideTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/ModuleOverrideTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/ModuleOverrideTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,72 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.pdfsam.injector.Injector; +import org.pdfsam.injector.Provides; + +public class ModuleOverrideTest { + @Test + public void dependencyOverridenByModule() { + Injector injector = Injector.start(new PlainStubOverrideModule()); + assertEquals(PlainStub.class, injector.instance(Plain.class).getClass()); + } + + + @Test + public void moduleOverwrittenBySubClass() { + assertEquals("foo", Injector.start(new FooModule()).instance(String.class)); + assertEquals("bar", Injector.start(new FooOverrideModule()).instance(String.class)); + } + + public static class Plain { + } + + public static class PlainStub extends Plain { + + } + + public static class PlainStubOverrideModule { + @Provides + public Plain plain(PlainStub plainStub) { + return plainStub; + } + + } + + public static class FooModule { + @Provides + String foo() { + return "foo"; + } + } + + public static class FooOverrideModule extends FooModule { + @Provides + @Override + String foo() { + return "bar"; + } + } + + + + +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/NamedDependencyTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/NamedDependencyTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/NamedDependencyTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/NamedDependencyTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,68 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.junit.Assert.assertEquals; + +import javax.inject.Inject; +import javax.inject.Named; + +import org.junit.Test; +import org.pdfsam.injector.InjectionException; +import org.pdfsam.injector.Injector; +import org.pdfsam.injector.Key; +import org.pdfsam.injector.Provides; + +public class NamedDependencyTest { + @Test + public void namedInstanceWithModule() { + Injector injector = Injector.start(new HelloWorldModule()); + assertEquals("Hello!", injector.instance(Key.of(String.class, "hello"))); + assertEquals("Hi!", injector.instance(Key.of(String.class, "hi"))); + assertEquals("Hello!", injector.instance(Bean.class).s); + } + + @Test(expected = InjectionException.class) + public void failingName() { + Injector injector = Injector.start(new HelloWorldModule()); + injector.instance(Key.of(String.class, "ChuckNorris")); + } + + public static class HelloWorldModule { + @Provides + @Named("hello") + String hello() { + return "Hello!"; + } + + @Provides + @Named("hi") + String hi() { + return "Hi!"; + } + } + + public static class Bean { + private final String s; + + @Inject + public Bean(@Named("hello") String s) { + this.s = s; + } + } + +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/PojoProvidedThroughModuleTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/PojoProvidedThroughModuleTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/PojoProvidedThroughModuleTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/PojoProvidedThroughModuleTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,65 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.pdfsam.injector.InjectionException; +import org.pdfsam.injector.Injector; +import org.pdfsam.injector.Provides; + +public class PojoProvidedThroughModuleTest { + @Test(expected = InjectionException.class) + public void pojoNotProvided() { + Injector injector = Injector.start(); + injector.instance(Pojo.class); + } + + @Test + public void pojoProvided() { + Injector injector = Injector.start(new Module()); + assertNotNull(injector.instance(Pojo.class)); + } + + @Test + public void dependecyInjected() { + Injector injector = Injector.start(new Module()); + assertEquals("foo", injector.instance(String.class)); + } + + public static class Module { + @Provides + Pojo pojo() { + return new Pojo("foo"); + } + + @Provides + String myString(Pojo pojo) { + return pojo.foo; + } + } + + public static class Pojo { + private final String foo; + + public Pojo(String foo) { + this.foo = foo; + } + } +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/PolymorphicDependencyTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/PolymorphicDependencyTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/PolymorphicDependencyTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/PolymorphicDependencyTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,65 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.junit.Assert.assertEquals; + +import javax.inject.Inject; +import javax.inject.Named; + +import org.junit.Test; +import org.pdfsam.injector.Injector; +import org.pdfsam.injector.Key; +import org.pdfsam.injector.Provides; + +public class PolymorphicDependencyTest { + @Test + public void multipleImplementations() { + Injector injector = Injector.start(new Module()); + assertEquals(FooA.class, injector.instance(Key.of(Foo.class, "A")).getClass()); + assertEquals(FooB.class, injector.instance(Key.of(Foo.class, "B")).getClass()); + } + + public static class Module { + @Provides @Named("A") + Foo a(FooA fooA) { + return fooA; + } + + @Provides @Named("B") + Foo a(FooB fooB) { + return fooB; + } + } + + interface Foo { + + } + + public static class FooA implements Foo { + @Inject + public FooA() { + } + } + + public static class FooB implements Foo { + @Inject + public FooB() { + } + + } +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/ProviderInjectionTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/ProviderInjectionTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/ProviderInjectionTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/ProviderInjectionTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,46 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.junit.Assert.assertNotNull; + +import javax.inject.Inject; +import javax.inject.Provider; + +import org.junit.Test; +import org.pdfsam.injector.Injector; + +public class ProviderInjectionTest { + @Test + public void providerInjected() { + Injector injector = Injector.start(); + assertNotNull(injector.instance(A.class).plainProvider.get()); + } + + public static class A { + private final Provider plainProvider; + + @Inject + public A(Provider plainProvider) { + this.plainProvider = plainProvider; + } + } + + public static class B { + + } +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/QualifiedDependencyTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/QualifiedDependencyTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/QualifiedDependencyTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/QualifiedDependencyTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,94 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.junit.Assert.assertEquals; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import javax.inject.Inject; +import javax.inject.Qualifier; + +import org.junit.Test; +import org.pdfsam.injector.Injector; +import org.pdfsam.injector.Key; +import org.pdfsam.injector.Provides; + +public class QualifiedDependencyTest { + @Test + public void qualifiedInstances() { + Injector injector = Injector.start(new Module()); + assertEquals(FooA.class, injector.instance(Key.of(Foo.class, A.class)).getClass()); + assertEquals(FooB.class, injector.instance(Key.of(Foo.class, B.class)).getClass()); + } + + @Test + public void injectedQualified() { + Injector injector = Injector.start(new Module()); + Dummy dummy = injector.instance(Dummy.class); + assertEquals(FooB.class, dummy.foo.getClass()); + } + + interface Foo { + + } + + public static class FooA implements Foo { + + } + + public static class FooB implements Foo { + + } + + @Qualifier + @Retention(RetentionPolicy.RUNTIME) + @interface A { + + } + + @Qualifier + @Retention(RetentionPolicy.RUNTIME) + @interface B { + + } + + public static class Module { + @Provides + @A + Foo a(FooA fooA) { + return fooA; + } + + @Provides + @B + Foo b(FooB fooB) { + return fooB; + } + } + + public static class Dummy { + private final Foo foo; + + @Inject + public Dummy(@B Foo foo) { + this.foo = foo; + } + } + +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/SingletonTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/SingletonTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/SingletonTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/SingletonTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,72 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import javax.inject.Provider; + +import org.junit.Test; +import org.pdfsam.injector.Injector; +import org.pdfsam.injector.Prototype; +import org.pdfsam.injector.Provides; + +public class SingletonTest { + @Test + public void nonSingleton() { + Injector injector = Injector.start(); + assertNotEquals(injector.instance(ProtoPlain.class), injector.instance(ProtoPlain.class)); + } + + @Test + public void nonSingletonConfig() { + Injector injector = Injector.start(new Config()); + assertNotEquals(injector.instance(Plain.class), injector.instance(Plain.class)); + } + + @Test + public void singletonByDefault() { + Injector injector = Injector.start(); + assertEquals(injector.instance(Plain.class), injector.instance(Plain.class)); + } + + @Test + public void singletonThroughProvider() { + Injector injector = Injector.start(); + Provider provider = injector.provider(Plain.class); + assertEquals(provider.get(), provider.get()); + } + + @Prototype + public static class ProtoPlain { + + } + + public static class Plain { + + } + + + public class Config { + @Provides + @Prototype + Plain plain() { + return new Plain(); + } + } +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/TransitiveDependencyTest.java libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/TransitiveDependencyTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/pdfsam/injector/TransitiveDependencyTest.java 1970-01-01 00:00:00.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/pdfsam/injector/TransitiveDependencyTest.java 2020-02-03 15:07:25.000000000 +0000 @@ -0,0 +1,57 @@ +/* + * This file is part of the PDF Split And Merge source code + * Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org). + * + * 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 org.pdfsam.injector; + +import static org.junit.Assert.assertNotNull; + +import javax.inject.Inject; + +import org.junit.Test; +import org.pdfsam.injector.Injector; + +public class TransitiveDependencyTest { + @Test + public void transitive() { + Injector injector = Injector.start(); + A a = injector.instance(A.class); + assertNotNull(a.b.c); + } + + public static class A { + private final B b; + + @Inject + public A(B b) { + this.b = b; + } + } + + public static class B { + private final C c; + + @Inject + public B(C c) { + this.c = c; + } + } + + public static class C { + + } + + +} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/AmbiguousModuleTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/AmbiguousModuleTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/AmbiguousModuleTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/AmbiguousModuleTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,25 +0,0 @@ -package org.sejda.injector; - -import org.junit.Test; -import org.sejda.injector.InjectionException; -import org.sejda.injector.Injector; -import org.sejda.injector.Provides; - -public class AmbiguousModuleTest { - @Test(expected = InjectionException.class) - public void ambiguousModule() { - Injector.start(new Module()); - } - - public static class Module { - @Provides - String foo() { - return "foo"; - } - - @Provides - String bar() { - return "bar"; - } - } -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/AutoProviderTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/AutoProviderTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/AutoProviderTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/AutoProviderTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,54 +0,0 @@ -package org.sejda.injector; - -import static org.mockito.Mockito.verify; - -import java.util.function.Consumer; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; -import org.sejda.injector.Auto; -import org.sejda.injector.Components; -import org.sejda.injector.Injector; -import org.sejda.injector.Provides; - -public class AutoProviderTest { - private static Consumer HIT; - - @Before - public void setUp() { - HIT = Mockito.mock(Consumer.class); - } - - @Test - public void autoCreatedAnnotatedClass() { - Injector injector = Injector.start(new Config()); - verify(HIT).accept("hit"); - } - - @Test - public void autoCreatedAnnotatedConfig() { - Injector injector = Injector.start(new ConfigComponent()); - verify(HIT).accept("hit"); - } - - public class Config { - - @Provides - public AnnotatedClass sing() { - return new AnnotatedClass(); - } - } - - @Components({ AnnotatedClass.class }) - public class ConfigComponent { - - } - - @Auto - public static class AnnotatedClass { - AnnotatedClass() { - HIT.accept("hit"); - } - } -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/CircularDependencyTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/CircularDependencyTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/CircularDependencyTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/CircularDependencyTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,61 +0,0 @@ -package org.sejda.injector; - -import static org.junit.Assert.assertNotNull; - -import javax.inject.Inject; -import javax.inject.Provider; - -import org.junit.Test; -import org.sejda.injector.InjectionException; -import org.sejda.injector.Injector; - -public class CircularDependencyTest { - @Test(expected = InjectionException.class) - public void circularDependencyCaught() { - Injector injector = Injector.start(); - injector.instance(Circle1.class); - } - - @Test - public void circularDependencyWithProviderAllowed() { - Injector injector = Injector.start(); - CircleWithProvider1 circle1 = injector.instance(CircleWithProvider1.class); - assertNotNull(circle1.circleWithProvider2.circleWithProvider1.get()); - } - - public static class Circle1 { - private final Circle2 circle2; - - @Inject - public Circle1(Circle2 circle2) { - this.circle2 = circle2; - } - } - - public static class Circle2 { - private final Circle1 circle1; - - @Inject - public Circle2(Circle1 circle1) { - this.circle1 = circle1; - } - } - - public static class CircleWithProvider1 { - private final CircleWithProvider2 circleWithProvider2; - - @Inject - public CircleWithProvider1(CircleWithProvider2 circleWithProvider2) { - this.circleWithProvider2 = circleWithProvider2; - } - } - - public static class CircleWithProvider2 { - private final Provider circleWithProvider1; - - @Inject - public CircleWithProvider2(Provider circleWithProvider1) { - this.circleWithProvider1 = circleWithProvider1; - } - } -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/DependencyTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/DependencyTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/DependencyTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/DependencyTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,42 +0,0 @@ -package org.sejda.injector; - -import static org.junit.Assert.assertNotNull; - -import javax.inject.Provider; - -import org.junit.Test; -import org.sejda.injector.InjectionException; -import org.sejda.injector.Injector; - -public class DependencyTest { - @Test - public void dependencyInstance() { - Injector injector = Injector.start(); - assertNotNull(injector.instance(Plain.class)); - } - - @Test - public void provider() { - Injector injector = Injector.start(); - Provider plainProvider = injector.provider(Plain.class); - assertNotNull(plainProvider.get()); - } - - @Test(expected = InjectionException.class) - public void unknown() { - Injector injector = Injector.start(); - injector.instance(Unknown.class); - } - - public static class Plain { - - } - - public static class Unknown { - public Unknown(String noSuitableConstructor) { - - } - } -} - - diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/InjectorTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/InjectorTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/InjectorTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/InjectorTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,40 +0,0 @@ -package org.sejda.injector; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.sejda.injector.InjectionException; -import org.sejda.injector.Injector; -import org.sejda.injector.Provides; - -public class InjectorTest { - @Test(expected = InjectionException.class) - public void classAsConfig() { - Injector.addConfig(String.class); - Injector.start(); - } - - @Test(expected = InjectionException.class) - public void closed() { - Injector.addConfig(new Config()); - Injector injector = Injector.start(); - assertEquals("ChuckNorris", injector.instance(Obj.class).val); - injector.close(); - injector.instance(Obj.class); - } - - public class Config { - @Provides - public Obj obj() { - return new Obj("ChuckNorris"); - } - } - - private static class Obj { - final String val; - - Obj(String val) { - this.val = val; - } - } -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/ListInjectionTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/ListInjectionTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/ListInjectionTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/ListInjectionTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,90 +0,0 @@ -package org.sejda.injector; - -import static org.junit.Assert.assertEquals; - -import java.util.Arrays; -import java.util.List; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.junit.Test; -import org.sejda.injector.InjectionException; -import org.sejda.injector.Injector; -import org.sejda.injector.Key; -import org.sejda.injector.Provides; - -public class ListInjectionTest { - @Test - public void listOfInjected() { - Injector injector = Injector.start(new Config()); - assertEquals(2, injector.instance(A.class).list.size()); - } - - @Test - public void instancesOfType() { - Injector injector = Injector.start(new Config()); - assertEquals(2, injector.instancesOfType(Base.class).size()); - } - - @Test(expected = InjectionException.class) - public void invalidType() { - Injector injector = Injector.start(new Config()); - injector.instance(B.class).list.size(); - } - - @Test - public void qualifiedListOfInjected() { - Injector injector = Injector.start(new Config()); - assertEquals(4, injector.instance(Key.of(List.class, "aList")).size()); - } - - public static class A { - private final List list; - - @Inject - public A(List list) { - this.list = list; - } - } - - public static class B { - private final List list; - - @Inject - public B(List list) { - this.list = list; - } - } - - public static class Config { - - @Provides - Sub1 sub1() { - return new Sub1(); - } - - @Provides - Sub2 sub2() { - return new Sub2(); - } - - @Provides - @Named("aList") - List qualifiedList() { - return Arrays.asList("a", "b", "c", "d"); - } - } - - public static class Base { - - } - - public static class Sub1 extends Base { - - } - - public static class Sub2 extends Base { - - } -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/ModuleOverrideTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/ModuleOverrideTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/ModuleOverrideTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/ModuleOverrideTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,56 +0,0 @@ -package org.sejda.injector; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.sejda.injector.Injector; -import org.sejda.injector.Provides; - -public class ModuleOverrideTest { - @Test - public void dependencyOverridenByModule() { - Injector injector = Injector.start(new PlainStubOverrideModule()); - assertEquals(PlainStub.class, injector.instance(Plain.class).getClass()); - } - - - @Test - public void moduleOverwrittenBySubClass() { - assertEquals("foo", Injector.start(new FooModule()).instance(String.class)); - assertEquals("bar", Injector.start(new FooOverrideModule()).instance(String.class)); - } - - public static class Plain { - } - - public static class PlainStub extends Plain { - - } - - public static class PlainStubOverrideModule { - @Provides - public Plain plain(PlainStub plainStub) { - return plainStub; - } - - } - - public static class FooModule { - @Provides - String foo() { - return "foo"; - } - } - - public static class FooOverrideModule extends FooModule { - @Provides - @Override - String foo() { - return "bar"; - } - } - - - - -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/NamedDependencyTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/NamedDependencyTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/NamedDependencyTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/NamedDependencyTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,52 +0,0 @@ -package org.sejda.injector; - -import static org.junit.Assert.assertEquals; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.junit.Test; -import org.sejda.injector.InjectionException; -import org.sejda.injector.Injector; -import org.sejda.injector.Key; -import org.sejda.injector.Provides; - -public class NamedDependencyTest { - @Test - public void namedInstanceWithModule() { - Injector injector = Injector.start(new HelloWorldModule()); - assertEquals("Hello!", injector.instance(Key.of(String.class, "hello"))); - assertEquals("Hi!", injector.instance(Key.of(String.class, "hi"))); - assertEquals("Hello!", injector.instance(Bean.class).s); - } - - @Test(expected = InjectionException.class) - public void failingName() { - Injector injector = Injector.start(new HelloWorldModule()); - injector.instance(Key.of(String.class, "ChuckNorris")); - } - - public static class HelloWorldModule { - @Provides - @Named("hello") - String hello() { - return "Hello!"; - } - - @Provides - @Named("hi") - String hi() { - return "Hi!"; - } - } - - public static class Bean { - private final String s; - - @Inject - public Bean(@Named("hello") String s) { - this.s = s; - } - } - -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/PojoProvidedThroughModuleTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/PojoProvidedThroughModuleTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/PojoProvidedThroughModuleTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/PojoProvidedThroughModuleTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,49 +0,0 @@ -package org.sejda.injector; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import org.junit.Test; -import org.sejda.injector.InjectionException; -import org.sejda.injector.Injector; -import org.sejda.injector.Provides; - -public class PojoProvidedThroughModuleTest { - @Test(expected = InjectionException.class) - public void pojoNotProvided() { - Injector injector = Injector.start(); - injector.instance(Pojo.class); - } - - @Test - public void pojoProvided() { - Injector injector = Injector.start(new Module()); - assertNotNull(injector.instance(Pojo.class)); - } - - @Test - public void dependecyInjected() { - Injector injector = Injector.start(new Module()); - assertEquals("foo", injector.instance(String.class)); - } - - public static class Module { - @Provides - Pojo pojo() { - return new Pojo("foo"); - } - - @Provides - String myString(Pojo pojo) { - return pojo.foo; - } - } - - public static class Pojo { - private final String foo; - - public Pojo(String foo) { - this.foo = foo; - } - } -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/PolymorphicDependencyTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/PolymorphicDependencyTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/PolymorphicDependencyTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/PolymorphicDependencyTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,49 +0,0 @@ -package org.sejda.injector; - -import static org.junit.Assert.assertEquals; - -import javax.inject.Inject; -import javax.inject.Named; - -import org.junit.Test; -import org.sejda.injector.Injector; -import org.sejda.injector.Key; -import org.sejda.injector.Provides; - -public class PolymorphicDependencyTest { - @Test - public void multipleImplementations() { - Injector injector = Injector.start(new Module()); - assertEquals(FooA.class, injector.instance(Key.of(Foo.class, "A")).getClass()); - assertEquals(FooB.class, injector.instance(Key.of(Foo.class, "B")).getClass()); - } - - public static class Module { - @Provides @Named("A") - Foo a(FooA fooA) { - return fooA; - } - - @Provides @Named("B") - Foo a(FooB fooB) { - return fooB; - } - } - - interface Foo { - - } - - public static class FooA implements Foo { - @Inject - public FooA() { - } - } - - public static class FooB implements Foo { - @Inject - public FooB() { - } - - } -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/ProviderInjectionTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/ProviderInjectionTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/ProviderInjectionTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/ProviderInjectionTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,30 +0,0 @@ -package org.sejda.injector; - -import static org.junit.Assert.assertNotNull; - -import javax.inject.Inject; -import javax.inject.Provider; - -import org.junit.Test; -import org.sejda.injector.Injector; - -public class ProviderInjectionTest { - @Test - public void providerInjected() { - Injector injector = Injector.start(); - assertNotNull(injector.instance(A.class).plainProvider.get()); - } - - public static class A { - private final Provider plainProvider; - - @Inject - public A(Provider plainProvider) { - this.plainProvider = plainProvider; - } - } - - public static class B { - - } -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/QualifiedDependencyTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/QualifiedDependencyTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/QualifiedDependencyTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/QualifiedDependencyTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,78 +0,0 @@ -package org.sejda.injector; - -import static org.junit.Assert.assertEquals; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - -import javax.inject.Inject; -import javax.inject.Qualifier; - -import org.junit.Test; -import org.sejda.injector.Injector; -import org.sejda.injector.Key; -import org.sejda.injector.Provides; - -public class QualifiedDependencyTest { - @Test - public void qualifiedInstances() { - Injector injector = Injector.start(new Module()); - assertEquals(FooA.class, injector.instance(Key.of(Foo.class, A.class)).getClass()); - assertEquals(FooB.class, injector.instance(Key.of(Foo.class, B.class)).getClass()); - } - - @Test - public void injectedQualified() { - Injector injector = Injector.start(new Module()); - Dummy dummy = injector.instance(Dummy.class); - assertEquals(FooB.class, dummy.foo.getClass()); - } - - interface Foo { - - } - - public static class FooA implements Foo { - - } - - public static class FooB implements Foo { - - } - - @Qualifier - @Retention(RetentionPolicy.RUNTIME) - @interface A { - - } - - @Qualifier - @Retention(RetentionPolicy.RUNTIME) - @interface B { - - } - - public static class Module { - @Provides - @A - Foo a(FooA fooA) { - return fooA; - } - - @Provides - @B - Foo b(FooB fooB) { - return fooB; - } - } - - public static class Dummy { - private final Foo foo; - - @Inject - public Dummy(@B Foo foo) { - this.foo = foo; - } - } - -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/SingletonTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/SingletonTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/SingletonTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/SingletonTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,56 +0,0 @@ -package org.sejda.injector; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; - -import javax.inject.Provider; - -import org.junit.Test; -import org.sejda.injector.Injector; -import org.sejda.injector.Prototype; -import org.sejda.injector.Provides; - -public class SingletonTest { - @Test - public void nonSingleton() { - Injector injector = Injector.start(); - assertNotEquals(injector.instance(ProtoPlain.class), injector.instance(ProtoPlain.class)); - } - - @Test - public void nonSingletonConfig() { - Injector injector = Injector.start(new Config()); - assertNotEquals(injector.instance(Plain.class), injector.instance(Plain.class)); - } - - @Test - public void singletonByDefault() { - Injector injector = Injector.start(); - assertEquals(injector.instance(Plain.class), injector.instance(Plain.class)); - } - - @Test - public void singletonThroughProvider() { - Injector injector = Injector.start(); - Provider provider = injector.provider(Plain.class); - assertEquals(provider.get(), provider.get()); - } - - @Prototype - public static class ProtoPlain { - - } - - public static class Plain { - - } - - - public class Config { - @Provides - @Prototype - Plain plain() { - return new Plain(); - } - } -} diff -Nru libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/TransitiveDependencyTest.java libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/TransitiveDependencyTest.java --- libsejda-injector-java-1.0.2/src/test/java/org/sejda/injector/TransitiveDependencyTest.java 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/src/test/java/org/sejda/injector/TransitiveDependencyTest.java 1970-01-01 00:00:00.000000000 +0000 @@ -1,41 +0,0 @@ -package org.sejda.injector; - -import static org.junit.Assert.assertNotNull; - -import javax.inject.Inject; - -import org.junit.Test; -import org.sejda.injector.Injector; - -public class TransitiveDependencyTest { - @Test - public void transitive() { - Injector injector = Injector.start(); - A a = injector.instance(A.class); - assertNotNull(a.b.c); - } - - public static class A { - private final B b; - - @Inject - public A(B b) { - this.b = b; - } - } - - public static class B { - private final C c; - - @Inject - public B(C c) { - this.c = c; - } - } - - public static class C { - - } - - -} diff -Nru libsejda-injector-java-1.0.2/.travis.yml libsejda-injector-java-2.0.0/.travis.yml --- libsejda-injector-java-1.0.2/.travis.yml 2016-11-09 17:55:08.000000000 +0000 +++ libsejda-injector-java-2.0.0/.travis.yml 2020-02-03 15:07:25.000000000 +0000 @@ -1,4 +1,5 @@ language: java sudo: false +dist: xenial jdk: - - oraclejdk8 + - openjdk8