├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── checkstyle.xml ├── doc ├── binding.md ├── container.md ├── getting_start.md ├── index.md ├── integration.md ├── performance.md ├── sample.md ├── scope.md └── value.md ├── mkdocs.yml ├── pom.xml └── src ├── etc └── javadoc.css ├── main ├── java │ └── org │ │ └── osgl │ │ └── inject │ │ ├── BeanSpec.java │ │ ├── BeanTransformer.java │ │ ├── CDIAdaptor.java │ │ ├── CircularReferenceException.java │ │ ├── ElementFilter.java │ │ ├── ElementLoader.java │ │ ├── ElementLoaderProvider.java │ │ ├── ElementType.java │ │ ├── GenericTypedBeanLoader.java │ │ ├── Genie.java │ │ ├── GeniePlugin.java │ │ ├── GuiceAdaptor.java │ │ ├── InjectException.java │ │ ├── InjectListener.java │ │ ├── Injector.java │ │ ├── KeyExtractor.java │ │ ├── Module.java │ │ ├── NamedProvider.java │ │ ├── PostConstructProcessor.java │ │ ├── PostConstructProcessorInvoker.java │ │ ├── PostConstructorInvoker.java │ │ ├── ScopeCache.java │ │ ├── ScopedProvider.java │ │ ├── ValueLoader.java │ │ ├── ValueLoaderFactory.java │ │ ├── annotation │ │ ├── AnnotatedWith.java │ │ ├── Configuration.java │ │ ├── Filter.java │ │ ├── InjectTag.java │ │ ├── LoadCollection.java │ │ ├── LoadValue.java │ │ ├── MapKey.java │ │ ├── Nonbinding.java │ │ ├── PostConstructProcess.java │ │ ├── Provided.java │ │ ├── Provides.java │ │ ├── RequestScoped.java │ │ ├── SessionScoped.java │ │ ├── StopInheritedScope.java │ │ ├── Transform.java │ │ ├── TypeOf.java │ │ └── package-info.java │ │ ├── loader │ │ ├── AnnotatedElementLoader.java │ │ ├── ConfigurationValueLoader.java │ │ ├── ElementLoaderBase.java │ │ ├── LoaderUtil.java │ │ ├── TypedElementLoader.java │ │ └── package-info.java │ │ ├── package-info.java │ │ ├── provider │ │ ├── ArrayListProvider.java │ │ ├── ArrayProvider.java │ │ ├── ConcurrentMapProvider.java │ │ ├── DequeProvider.java │ │ ├── DirectProvider.java │ │ ├── LazyProvider.java │ │ ├── LinkedHashMapProvider.java │ │ ├── LinkedListProvider.java │ │ ├── NullValueProvider.java │ │ ├── OsglListProvider.java │ │ ├── OsglMapProvider.java │ │ ├── OsglSetProvider.java │ │ ├── PrimitiveArrayProvider.java │ │ ├── SortedMapProvider.java │ │ ├── SortedSetProvider.java │ │ └── package-info.java │ │ └── util │ │ ├── AnnotationUtil.java │ │ ├── ArrayLoader.java │ │ ├── ParameterizedTypeImpl.java │ │ ├── PostConstructProcessorBase.java │ │ ├── SimpleAnnoInvocationHandler.java │ │ ├── SimpleSingletonScope.java │ │ └── package-info.java └── resources │ └── org │ └── osgl │ └── inject │ └── .version └── test └── java ├── issue ├── GH10.java ├── GH21.java ├── GH22.java ├── GH25.java ├── GH26.java ├── GH33.java ├── GH34.java ├── GH38.java ├── GH42.java ├── GH43.java ├── GH45.java ├── GH46.java ├── GH47.java ├── GH50.java ├── GH51.java ├── GH54.java ├── GH55.java ├── GH56.java ├── GH57.java ├── Gh18.java ├── Gh19.java ├── Gh20.java ├── Gh58.java ├── Gh59.java └── gh42 │ ├── Account.java │ ├── BsbfDao.java │ ├── BsbfRecord.java │ ├── Dao.java │ ├── DaoBase.java │ ├── MorphiaDao.java │ └── Order.java └── org └── osgl └── inject ├── AnnotatedClasses.java ├── BaseWithPostConstructor.java ├── BaseWithPostConstructorHolder.java ├── BeanSpecTest.java ├── CDIScopedFactory.java ├── Circular.java ├── ConfigurationLoadTestBed.java ├── Context.java ├── Dao.java ├── DaoInjectListener.java ├── DerivedFromBaseWithPostConstructor.java ├── ErrorDispatcher.java ├── ErrorHandler.java ├── EvenFibonacciSeriesHolder.java ├── EvenNumber.java ├── EvenNumberFilter.java ├── FibonacciSeries.java ├── FibonacciSeriesHolder.java ├── FibonacciSeriesHolder2.java ├── FibonacciSeriesHolder3.java ├── FibonacciSeriesLoader.java ├── GenieTest.java ├── GreetingService.java ├── InheritedStateless.java ├── InternalErrorHandler.java ├── Leather.java ├── LeatherSmoother.java ├── Lowercase.java ├── ModuleWithBindings.java ├── ModuleWithConstructorBinding.java ├── ModuleWithFactories.java ├── ModuleWithNamedBindings.java ├── ModuleWithStaticFactories.java ├── MultipleConstructors.java ├── NotFoundHandler.java ├── OddFibonacciSeriesHolder.java ├── Order.java ├── OrderService.java ├── OverwriteBaseWithPostConstructor.java ├── Person.java ├── RandomList.java ├── RandomListHolder.java ├── RandomListLoader.java ├── ScopedFactory.java ├── ScopedObjects.java ├── SessionPostConstruct.java ├── SimpleConstructorInjection.java ├── SimpleConstructorInjectionByProvider.java ├── SimpleEmptyConstructor.java ├── SimpleFieldInjection.java ├── SimpleMethodInjection.java ├── SimpleMethodInjectionByProvider.java ├── SimpleTypeElementLoader.java ├── SingletonPostConstruct.java ├── Stateful.java ├── TomAndJen.java ├── Transformers.java ├── TypedClasses.java ├── Uppercase.java ├── User.java ├── UserService.java ├── ValueLoaderAndQualifiers.java ├── VersionTest.java └── util └── AnnotationUtilTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | deploy 3 | .classpath 4 | .project 5 | .settings 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | lib 11 | target 12 | .idea 13 | classes 14 | *.iml 15 | .DS_Store 16 | **/*-delete 17 | **/*.delete 18 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: false # faster builds 3 | 4 | script: "mvn cobertura:cobertura" 5 | 6 | after_success: 7 | - bash <(curl -s https://codecov.io/bash) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Genie 2 | 3 | [![APL v2](https://img.shields.io/badge/license-Apache%202-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0.html) 4 | [![Maven Central](https://img.shields.io/maven-central/v/org.osgl/genie.svg)](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22genie%22) 5 | [![Build Status](https://travis-ci.org/osglworks/java-di.svg?branch=master)](https://travis-ci.org/osglworks/java-di) 6 | [![codecov](https://codecov.io/gh/osglworks/java-di/branch/master/graph/badge.svg)](https://codecov.io/gh/osglworks/java-di) 7 | [![Documentation Status](https://readthedocs.org/projects/genie/badge/?version=latest)](http://genie.readthedocs.io/en/latest/?badge=latest) 8 | [![Javadocs](http://www.javadoc.io/badge/org.osgl/genie.svg?color=blue)](http://www.javadoc.io/doc/org.osgl/genie) 9 | 10 | Genie is a JSR330 Dependency Injection solution inspired by [Feather](https://github.com/zsoltherpai/feather). 11 | Genie is designed to provide richer feature set than Feather while remaining much lightweight 12 | in comparing to [Google Guice](https://github.com/google/guice). 13 | 14 | Genie is a core component of [ActFramework](https://github.com/actframework/actframework). 15 | 16 | ## Maven Dependency 17 | 18 | ```xml 19 | 20 | org.osgl 21 | genie 22 | ${genie.version} 23 | 24 | ``` 25 | 26 | ## Features: 27 | 28 | * JSR330 Dependency injection support on Constructor/Field/Method 29 | * Module and `@Provides` factory methods 30 | * Fully support on `@Qualifier` tags 31 | * Support `Singleton`, `SessionScoped`, `RequestScoped` annotation from built-in and CDI-api-1.2, plus Guice version 32 | * Support Collection/Map loader 33 | * Support inject array type bean 34 | * Support generic value loader and more specific configuration value loader 35 | * Support `@javax.annotations.PostConstruct` semantic 36 | 37 | ## Benchmark 38 | 39 | Genie has very high runtime performance when comparing to Guice and Spring. See [this benchmark](https://github.com/greenlaw110/di-benchmark) 40 | 41 | ## Documents 42 | 43 | * [Getting started](doc/getting_start.md) 44 | * [Type binding](doc/binding.md) 45 | * [Inject Collection and Map](doc/container.md) 46 | * [Load bean from other sources](doc/value.md) 47 | * [Scoping](doc/scope.md) 48 | 49 | ## Demos 50 | 51 | * [Implement Spring 4.3 style InjectionPoint style binding](https://github.com/greenlaw110/hello-genie-injectionPoint) 52 | * [Implement Spring 4.3 style customized annotation](https://github.com/greenlaw110/genie-custom-annotation-demo) 53 | -------------------------------------------------------------------------------- /doc/index.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | 4 | Genie is a JSR330 Dependency Injection solution inspired by [Feather](https://github.com/zsoltherpai/feather). Genie is designed to provide richer feature set than Feather while remaining much lightweight in comparing to [Google Guice](https://github.com/google/guice). 5 | 6 | Genie is a core component of [ActFramework](https://github.com/actframework/actframework). However Genie can be used in any Java application independently. 7 | 8 | ## Install 9 | 10 | Genie is provided through [![Maven Central](https://img.shields.io/maven-central/v/org.osgl/genie.svg)](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22genie%22) 11 | 12 | ```xml 13 | 14 | org.osgl 15 | genie 16 | ${genie.version} 17 | 18 | ``` 19 | 20 | ## Where to start 21 | 22 | If you are new to [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection), I recommend reading the [Getting Started](getting_start.md) guide, which walks through the basics. 23 | 24 | If you are a veteran [Guice](https://github.com/google/guice) user, go straight to [Binding](binding.md) and walk through into Genie specific features including 25 | 26 | * [Inject Collection and Map](doc/container.md) 27 | * [Load bean from other sources](doc/value.md) 28 | 29 | If you are curious about Genie's performance, please checkout [performance benchmark](performance.md) 30 | -------------------------------------------------------------------------------- /doc/integration.md: -------------------------------------------------------------------------------- 1 | # Integrate Genie with IoC container 2 | 3 | **The content of this secion is WIP** 4 | 5 | Genie could be integrated into IOC container. At the moment Genie is used as 6 | 7 | -------------------------------------------------------------------------------- /doc/sample.md: -------------------------------------------------------------------------------- 1 | # Genie Sample Projects 2 | 3 | * [Implement Spring 4.3 style InjectionPoint style binding](https://github.com/greenlaw110/hello-genie-injectionPoint) 4 | * [Implement Spring 4.3 style customized annotation](https://github.com/greenlaw110/genie-custom-annotation-demo) 5 | * [Using dependency injection in ActFramework](https://github.com/actframework/act-demo-apps/tree/master/injection) 6 | -------------------------------------------------------------------------------- /doc/scope.md: -------------------------------------------------------------------------------- 1 | # Scoping 2 | 3 | Genie support the following scopes: 4 | 5 | * `javax.inject.Singleton` 6 | * `org.osgl.inject.annotation.RequestScoped` 7 | * `org.osgl.inject.annotation.SessionScoped` 8 | 9 | The first scope has built-in support. The `RequestSceoped` and `SessionScoped` is provided when Genie is used 10 | in a framework (e.g. [ActFramework](https://github.com/actframework/actframework)). In which case the framework 11 | will provide the implementation of the following services: 12 | 13 | * `org.osgl.inject.ScopeCache.SessionScope` 14 | * `org.osgl.inject.ScopeCache.RequestScope` 15 | 16 | 17 | -------------------------------------------------------------------------------- /doc/value.md: -------------------------------------------------------------------------------- 1 | # Inject customized value loader 2 | 3 | Genie allows application developer to plugin customised value loading logic in an elegant and fast way 4 | 5 | Suppose you have a Greeting service: 6 | 7 | ```java 8 | public class GreeterService { 9 | 10 | @LocalizedMessage("greeterservice.greeting") 11 | private Message message; 12 | 13 | public String sayHello(String caller) { 14 | return message.format($.notNull(caller)); 15 | } 16 | 17 | } 18 | ``` 19 | 20 | You need the framework to inject the greeting message into the service based on the `@LocalizedMessage` 21 | annotation. There are two approaches to get this done. 22 | 23 | 1. Create a module and provides factory method that takes `BeanSpec` as parameter. 24 | 25 | ```java 26 | // Declare LocalizedMessage with Qualifier annotation 27 | @InjectTag 28 | @Qualifier 29 | @Retention(RetentionPolicy.RUNTIME) 30 | public @interface LocalizedMessage { 31 | 32 | String value() default ""; 33 | 34 | } 35 | 36 | // Create the module to provides factory method 37 | public class MyModule { 38 | @Provides 39 | @LocalizedMessage // make sure LocalizedMessage has been annotated with Qualifier 40 | public Message createMessage(BeanSpec spec, Injector injector) { 41 | LocalizedMessage localizedMessage = spec.getAnnotation(LocalizedMessage.class); 42 | if (null == localizedMessage) { 43 | return null; 44 | } 45 | MessageSource messageSource = injector.get(MessageSource.class); 46 | return new Message(localizedMessage.value(), messageSource); 47 | } 48 | } 49 | ``` 50 | 51 | 2. Use Genie's value loading mechanism 52 | 53 | ```java 54 | // Declare LocalizedMessage with @LoadValue annotation 55 | @InjectTag 56 | @LoadValue(MessageLoader.class) 57 | @Retention(RetentionPolicy.RUNTIME) 58 | public @interface LocalizedMessage { 59 | 60 | String value() default ""; 61 | 62 | } 63 | 64 | // Implement MessageLoader class 65 | public class MessageLoader extends ValueLoader.Base { 66 | 67 | @Inject 68 | private MessageSource messageSource; 69 | 70 | @Override 71 | public Message get() { 72 | return new Message(value(), messageSource); 73 | } 74 | 75 | } 76 | ``` 77 | 78 | The first approach is very like configure with `InjectionPoint` in Spring 4.3. However it is not encouraged 79 | when the second approach is available, because: 80 | 81 | 1. It involves annotation look up for each injection request. While value loader approach has much faster runtime performance 82 | 2. Value loader approach code is more cleaner 83 | 84 | 85 | -------------------------------------------------------------------------------- /mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name : OSGL Genie Documentation 2 | site_description: A JSR 330 dependency injection library 3 | 4 | repo_url: https://github.com/osglworks/java-di/ 5 | 6 | docs_dir: doc/ 7 | 8 | theme: readthedocs 9 | 10 | pages: 11 | - 'Home' : 'index.md' 12 | - 'Getting Started' : 'getting_start.md' 13 | - 'Binding' : 'binding.md' 14 | - 'Scoping' : 'scope.md' 15 | - 'Genie Bonus' : 16 | - 'Inject collection and map' : 'container.md' 17 | - 'Load bean from other sources' : 'value.md' 18 | - 'Sample projects' : 'sample.md' 19 | - 'Performance Benchmark' : 'performance.md' -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/BeanTransformer.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.$; 24 | 25 | import java.util.Map; 26 | 27 | /** 28 | * Implementation shall provide the logic that can 29 | * transform a bean instance (no type change). 30 | * 31 | * @param the generic type of element 32 | */ 33 | public interface BeanTransformer { 34 | 35 | /** 36 | * Create a filter function with the hint and options specified. This could 37 | * be used to produce composite Bean loader based on other bean loaders. 38 | * 39 | * @param options 40 | * the optional parameters specified to refine the filtering logic 41 | * @param bean 42 | * the bean spec of the bean to be injected 43 | * @return 44 | * a function that transform the bean instance 45 | * (e.g. convert a string to uppercase etc) 46 | */ 47 | $.Function filter(Map options, BeanSpec bean); 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/CDIAdaptor.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.RequestScoped; 24 | import org.osgl.inject.annotation.SessionScoped; 25 | 26 | import javax.enterprise.context.ApplicationScoped; 27 | import javax.inject.Singleton; 28 | 29 | public class CDIAdaptor implements GeniePlugin { 30 | @Override 31 | public void register(Genie genie) { 32 | genie.registerScopeAlias(Singleton.class, ApplicationScoped.class); 33 | genie.registerScopeAlias(SessionScoped.class, javax.enterprise.context.SessionScoped.class); 34 | genie.registerScopeAlias(RequestScoped.class, javax.enterprise.context.RequestScoped.class); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/CircularReferenceException.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | public class CircularReferenceException extends InjectException { 24 | public CircularReferenceException(String message, Object... args) { 25 | super(message, args); 26 | } 27 | 28 | public CircularReferenceException(Throwable cause) { 29 | super(cause); 30 | } 31 | 32 | public CircularReferenceException(Throwable cause, String message, Object... args) { 33 | super(cause, message, args); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/ElementFilter.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.$; 24 | 25 | import java.util.Map; 26 | 27 | /** 28 | * Implementation shall provide the logic that can 29 | * filter a bean instance. 30 | * 31 | * @param the generic type of element 32 | */ 33 | public interface ElementFilter { 34 | 35 | /** 36 | * Create a filter function with the hint and options specified. 37 | * This could be used to produce composite Bean loader based on 38 | * other bean loaders. 39 | * 40 | * @param options 41 | * the optional parameters specified to refine the filtering logic 42 | * @param container 43 | * the bean spec of the container into which the element will 44 | * be loaded 45 | * @return 46 | * a filter to check if a certain bean instance matches this 47 | * bean loader specification 48 | */ 49 | $.Function filter(Map options, BeanSpec container); 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/ElementLoader.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import java.util.Collection; 24 | import java.util.Map; 25 | 26 | /** 27 | * An `ElementLoader` is responsible for providing initial data 28 | * to be loaded into a {@link Collection} type. 29 | * 30 | * @param the generic element type 31 | */ 32 | public interface ElementLoader extends ElementFilter { 33 | 34 | /** 35 | * Provide element data to be loaded. 36 | * 37 | * @param options 38 | * options that could be used to regulate the data loading logic 39 | * @param container 40 | * the bean spec about the container into which the element will be loaded 41 | * @param genie 42 | * the dependency injector that could be used to recursively load dependencies 43 | * @return 44 | * an {@link Iterable} of elements 45 | */ 46 | Iterable load(Map options, BeanSpec container, Genie genie); 47 | 48 | /** 49 | * When multiple `ElementLoader` are used together to load initial data, the 50 | * `priority()` method can used to determine which loader is called first. 51 | * All following loaders will be treated as {@link ElementFilter} to filter 52 | * the data series generated by the first loader. 53 | *

54 | * A good practice is if a loader loads fewer number of instance, 55 | * then the priority value shall be lower than that of the loader 56 | * which loads more instances. 57 | * 58 | * @return 59 | * priority of the loader 60 | */ 61 | int priority(); 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/GenericTypedBeanLoader.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | /** 24 | * A `GenericTypedBeanLoader` can be used to load instance of certain type 25 | * with generic type parameters. 26 | * 27 | * A typical usage scenario is to load a `Dao` implementation. E.g. 28 | * 29 | * ``` 30 | * public class FooService { 31 | * {@literal @}Inject 32 | * private Dao fooDao; 33 | * ... 34 | * } 35 | * ``` 36 | * 37 | * One must register the `GenericTypedBeanLoader` via calling 38 | * the {@link Genie#registerGenericTypedBeanLoader(Class, GenericTypedBeanLoader)} 39 | * method 40 | */ 41 | public interface GenericTypedBeanLoader { 42 | /** 43 | * Returns an instance matches the spec. 44 | * 45 | * @param spec 46 | * the bean spec 47 | * @return 48 | * the bean instance 49 | */ 50 | T load(BeanSpec spec); 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/GeniePlugin.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | /** 24 | * A `GeniePlugin` can register it self to a genie instance 25 | */ 26 | public interface GeniePlugin { 27 | /** 28 | * Implementation shall register to a Genie instance 29 | * @param genie the Genie instance 30 | */ 31 | void register(Genie genie); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/GuiceAdaptor.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.RequestScoped; 24 | import org.osgl.inject.annotation.SessionScoped; 25 | 26 | import javax.inject.Singleton; 27 | 28 | public class GuiceAdaptor implements GeniePlugin { 29 | @Override 30 | public void register(Genie genie) { 31 | genie.registerScopeAlias(Singleton.class, com.google.inject.Singleton.class); 32 | genie.registerScopeAlias(SessionScoped.class, com.google.inject.servlet.SessionScoped.class); 33 | genie.registerScopeAlias(RequestScoped.class, com.google.inject.servlet.RequestScoped.class); 34 | // TODO support Guice Module 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/InjectException.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.exception.UnexpectedException; 24 | 25 | /** 26 | * `InjectException` is thrown out when error occurred within 27 | * dependency injection process 28 | */ 29 | public class InjectException extends UnexpectedException { 30 | 31 | public InjectException(String message, Object... args) { 32 | super(message, args); 33 | } 34 | 35 | public InjectException(Throwable cause) { 36 | super(cause); 37 | } 38 | 39 | public InjectException(Throwable cause, String message, Object... args) { 40 | super(cause, message, args); 41 | } 42 | 43 | public static InjectException circularDependency(CharSequence dependencyChain) { 44 | return new CircularReferenceException("Circular dependency found: %s", dependencyChain); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/InjectListener.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | public interface InjectListener { 24 | void providerRegistered(Class targetType); 25 | void injected(Object bean, BeanSpec beanSpec); 26 | 27 | class Adaptor implements InjectListener { 28 | @Override 29 | public void providerRegistered(Class targetType) { 30 | } 31 | 32 | @Override 33 | public void injected(Object bean, BeanSpec beanSpec) { 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/NamedProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | /** 24 | * A `NamedProvider` can be used to get an instance with a given name. 25 | */ 26 | public interface NamedProvider { 27 | /** 28 | * Return an instance with a given name 29 | * @param name the name 30 | * @return an instance corresponding to the name 31 | */ 32 | T get(String name); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/PostConstructProcessor.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import java.lang.annotation.Annotation; 24 | 25 | /** 26 | * Define the logic that needs to be invoked on the bean before return back. 27 | */ 28 | public interface PostConstructProcessor { 29 | /** 30 | * Process a `T` typed bean with the relevant annotation instance. 31 | * 32 | * @param bean 33 | * the bean to be processed 34 | * @param annotation 35 | * the relevant annotation tagged on the parameter or field 36 | */ 37 | void process(T bean, Annotation annotation); 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/PostConstructProcessorInvoker.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.$; 24 | import org.osgl.util.C; 25 | 26 | import java.lang.annotation.Annotation; 27 | import java.util.List; 28 | import java.util.Set; 29 | import javax.inject.Provider; 30 | 31 | class PostConstructProcessorInvoker implements Provider { 32 | 33 | private Provider realProvider; 34 | private List<$.T2>> processors; 35 | 36 | private PostConstructProcessorInvoker( 37 | Provider realProvider, 38 | List<$.T2>> processors 39 | ) { 40 | this.realProvider = realProvider; 41 | this.processors = processors; 42 | } 43 | 44 | @Override 45 | public T get() { 46 | T t = realProvider.get(); 47 | for ($.T2> pair : processors) { 48 | pair._2.process(t, pair._1); 49 | } 50 | return t; 51 | } 52 | 53 | static Provider decorate(BeanSpec spec, Provider realProvider, Genie genie) { 54 | if (realProvider instanceof PostConstructorInvoker) { 55 | return realProvider; 56 | } 57 | Set postProcessors = spec.postProcessors(); 58 | if (postProcessors.isEmpty()) { 59 | return realProvider; 60 | } 61 | C.List<$.T2>> processors = C.newSizedList(postProcessors.size()); 62 | for (Annotation annotation : postProcessors) { 63 | PostConstructProcessor pcp = genie.postConstructProcessor(annotation); 64 | processors.add($.T2(annotation, pcp)); 65 | } 66 | return new PostConstructProcessorInvoker<>(realProvider, processors); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/ScopeCache.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | /** 24 | * Provide function to retrieve bean from scoped cache. 25 | */ 26 | public interface ScopeCache { 27 | /** 28 | * Get a bean from the cache defined in the scope. 29 | * 30 | * @param target 31 | * the key to retrieve the bean 32 | * @param 33 | * generic type of the bean 34 | * @return 35 | * the bean instance 36 | */ 37 | T get(BeanSpec target); 38 | 39 | /** 40 | * Put a bean instance into the cache associated with the class key 41 | * specified. 42 | * 43 | * @param target 44 | * the key to store the bean instance 45 | * @param bean 46 | * the bean instance to be stored 47 | * @param 48 | * generic type of the bean 49 | */ 50 | void put(BeanSpec target, T bean); 51 | 52 | /** 53 | * Implementation of `ScopeCache.SingletonScope` provide access 54 | * to bean instances stored in a singleton registry. 55 | */ 56 | interface SingletonScope extends ScopeCache { 57 | } 58 | 59 | /** 60 | * Implementation of `ScopeCache.RequestScope` shall provide access 61 | * to bean instance stored in a request scope 62 | */ 63 | interface RequestScope extends ScopeCache { 64 | } 65 | 66 | /** 67 | * Implementation of `ScopeCache.RequestScope` shall provide access 68 | * to bean instance stored in a session scope 69 | */ 70 | interface SessionScope extends ScopeCache { 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/ValueLoaderFactory.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.$; 24 | import org.osgl.inject.annotation.LoadValue; 25 | import org.osgl.util.E; 26 | 27 | import javax.inject.Provider; 28 | import java.lang.annotation.Annotation; 29 | import java.util.Map; 30 | 31 | /** 32 | * `ValueLoaderFactory` load the bean instance directly from 33 | * {@link ValueLoader} based on the option data specified in 34 | * {@link org.osgl.inject.annotation.LoadValue} annotation 35 | */ 36 | class ValueLoaderFactory { 37 | 38 | static Provider create(BeanSpec spec, Genie genie) { 39 | Annotation anno = spec.valueLoader(); 40 | E.illegalArgumentIf(null == anno); 41 | Map options = $.evaluate(anno); 42 | Class annoType = anno.annotationType(); 43 | LoadValue loadValue; 44 | if (LoadValue.class == annoType) { 45 | loadValue = (LoadValue) anno; 46 | } else { 47 | loadValue = annoType.getAnnotation(LoadValue.class); 48 | } 49 | ValueLoader valueLoader = genie.get(loadValue.value()); 50 | valueLoader.init(options, spec); 51 | return valueLoader; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/AnnotatedWith.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | 24 | import org.osgl.inject.loader.AnnotatedElementLoader; 25 | 26 | import java.lang.annotation.ElementType; 27 | import java.lang.annotation.Retention; 28 | import java.lang.annotation.RetentionPolicy; 29 | import java.lang.annotation.Target; 30 | 31 | /** 32 | * Mark the type of elements of a field or method parameter should 33 | * be annotated with specified annotation. 34 | */ 35 | @InjectTag 36 | @Retention(RetentionPolicy.RUNTIME) 37 | @Target({ElementType.FIELD, ElementType.PARAMETER}) 38 | @LoadCollection(AnnotatedElementLoader.class) 39 | public @interface AnnotatedWith { 40 | /** 41 | * Specify the annotation class. 42 | * 43 | * @return the annotation class 44 | */ 45 | Class value(); 46 | 47 | /** 48 | * Specify the type of element the loader should return. 49 | * 50 | * @return the element type 51 | */ 52 | org.osgl.inject.ElementType elementType() default org.osgl.inject.ElementType.BEAN; 53 | 54 | /** 55 | * Should the loader load non-public class or not. 56 | * 57 | * @return `true` or `false` as described above 58 | */ 59 | boolean loadNonPublic() default false; 60 | 61 | /** 62 | * Should the loader load abstract class or not. 63 | * 64 | * **Note** the value of `loadAbstract` will be ignored if 65 | * {@link #elementType()} is set to {@link org.osgl.inject.ElementType#BEAN} 66 | * 67 | * @return `true` or `false` as described above. 68 | */ 69 | boolean loadAbstract() default false; 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/Configuration.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.loader.ConfigurationValueLoader; 24 | 25 | import java.lang.annotation.*; 26 | 27 | /** 28 | * Used to specify a field or parameter shall be load by 29 | * {@link org.osgl.inject.loader.ConfigurationValueLoader}. 30 | */ 31 | @Documented 32 | @InjectTag 33 | @LoadValue(ConfigurationValueLoader.class) 34 | @Retention(RetentionPolicy.RUNTIME) 35 | @Target({ElementType.FIELD, ElementType.PARAMETER}) 36 | public @interface Configuration { 37 | 38 | /** 39 | * The name of {@link #defaultValue()} property. 40 | */ 41 | String DEFAULT_VALUE_PROP = "defaultValue"; 42 | 43 | /** 44 | * Specify the configuration key. 45 | * 46 | * @return the configuration key 47 | */ 48 | String value(); 49 | 50 | /** 51 | * Specify the default value of the configuration 52 | * @return the default value 53 | */ 54 | String defaultValue() default ""; 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/Filter.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.ElementFilter; 24 | 25 | import java.lang.annotation.*; 26 | 27 | /** 28 | * Used to tag an annotation with {@link ElementFilter bean filter} 29 | * specification. 30 | * 31 | * This annotation can be used in conjunction with {@link LoadCollection} 32 | * to filter the element to be loaded into a collection typed bean 33 | */ 34 | @Documented 35 | @Retention(RetentionPolicy.RUNTIME) 36 | @Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.PARAMETER}) 37 | public @interface Filter { 38 | /** 39 | * Specify the {@link ElementFilter} implementation used to 40 | * filter bean(s) loaded by {@link org.osgl.inject.ElementLoader bean loaders} 41 | * 42 | * @return the `ElementFilter` class 43 | */ 44 | Class value(); 45 | 46 | /** 47 | * Specify it shall reverse the filter function when applying the filter 48 | */ 49 | boolean reverse() default false; 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/InjectTag.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import java.lang.annotation.*; 24 | 25 | /** 26 | * Used to specify a certain annotation is an `Inject tag`. 27 | * 28 | * Once an annotation is annotated with `InjectTag` it means 29 | * the field or method is subjected to dependency injection 30 | * without the need for {@link javax.inject.Inject} annotation. 31 | * 32 | * For example, let's say a developer want to create an annotation 33 | * to inject fibonacci series: 34 | * 35 | * ``` 36 | * {@literal @}Retention(RetentionPolicy.RUNTIME) 37 | * {@literal @}InjectTag 38 | * {@literal @}LoadCollection(FibonacciSeriesLoader.class) 39 | * public @interface FibonacciSeries { 40 | * int max() default 100; 41 | * } 42 | * ``` 43 | * 44 | * Because the `FibonacciSeries` is tagged with `InjectTag`, thus 45 | * user can directly use it to mark a field is subject to 46 | * dependency injection: 47 | * 48 | * ```java 49 | * public class EvenFibonacciSeriesHolder { 50 | * {@literal @}FibonacciSeries List series; 51 | * } 52 | * ``` 53 | * 54 | * If the `FibonacciSeries` annotation is not tagged with 55 | * `InjectTag`, then it must add `@Inject` in order to 56 | * mark the field needs dependency injection: 57 | * 58 | * ```java 59 | * public class EvenFibonacciSeriesHolder { 60 | * {@literal @}Inject @FibonacciSeries List series; 61 | * } 62 | * ``` 63 | * 64 | * @see org.osgl.inject.Genie#registerInjectTag(Class[]) 65 | */ 66 | @Documented 67 | @Retention(RetentionPolicy.RUNTIME) 68 | @Target(ElementType.ANNOTATION_TYPE) 69 | public @interface InjectTag { 70 | } 71 | 72 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/LoadCollection.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.BeanSpec; 24 | import org.osgl.inject.ElementLoader; 25 | 26 | import java.lang.annotation.*; 27 | import java.util.Map; 28 | 29 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 30 | import static java.lang.annotation.ElementType.FIELD; 31 | import static java.lang.annotation.ElementType.PARAMETER; 32 | 33 | /** 34 | * Used to tag an annotation with {@link ElementLoader collection element loader} 35 | * specification. Annotations tagged with `LoadCollection` is used to mark 36 | * a {@link java.util.Collection} or {@link java.util.Map} type 37 | * inject target needs additional logic to load element data 38 | */ 39 | @Documented 40 | @InjectTag 41 | @Retention(RetentionPolicy.RUNTIME) 42 | @Target({ANNOTATION_TYPE, FIELD, PARAMETER}) 43 | public @interface LoadCollection { 44 | /** 45 | * Specify the {@link ElementLoader} implementation used to 46 | * load bean(s) 47 | * 48 | * @return the `ElementLoader` implementation 49 | */ 50 | Class value(); 51 | 52 | /** 53 | * Indicate it shall reverse the {@link org.osgl.inject.ElementFilter#filter(Map, BeanSpec) filter function} 54 | * when loading element from collection 55 | */ 56 | boolean reverseFilter() default false; 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/LoadValue.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.ValueLoader; 24 | 25 | import java.lang.annotation.*; 26 | 27 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 28 | import static java.lang.annotation.ElementType.FIELD; 29 | import static java.lang.annotation.ElementType.PARAMETER; 30 | 31 | /** 32 | * Used to tag an annotation with {@link ValueLoader value loader} 33 | * specification. Annotations tagged with `LoadValue` is used to mark 34 | * a bean instance shall be loaded by value loader instead of being 35 | * constructed by Genie 36 | */ 37 | @Documented 38 | @InjectTag 39 | @Retention(RetentionPolicy.RUNTIME) 40 | @Target({ANNOTATION_TYPE, FIELD, PARAMETER}) 41 | public @interface LoadValue { 42 | /** 43 | * Specifies a {@link ValueLoader} implementation class 44 | * @return the value loader class 45 | */ 46 | Class value(); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/MapKey.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import com.google.inject.internal.cglib.core.$DefaultGeneratorStrategy; 24 | import org.osgl.inject.KeyExtractor; 25 | import org.osgl.inject.util.AnnotationUtil; 26 | import org.osgl.util.C; 27 | import org.osgl.util.E; 28 | import org.osgl.util.S; 29 | 30 | import java.lang.annotation.*; 31 | import java.util.Map; 32 | 33 | /** 34 | * Used to specify how to extract {@link java.util.Map} key 35 | * from a value. 36 | */ 37 | @Documented 38 | @Retention(RetentionPolicy.RUNTIME) 39 | @Target({ElementType.FIELD, ElementType.PARAMETER}) 40 | public @interface MapKey { 41 | 42 | /** 43 | * Specify the `hint` to be passed into {@link KeyExtractor#keyOf(String, Object)} 44 | * function call. 45 | * 46 | * @return the `hint` used to extract the key. Default value is "" 47 | */ 48 | String value() default ""; 49 | 50 | /** 51 | * Specify a {@link KeyExtractor key extractor}. Default value is 52 | * {@link org.osgl.inject.KeyExtractor.PropertyExtractor}. 53 | */ 54 | Class extractor() default KeyExtractor.PropertyExtractor.class; 55 | 56 | class Factory { 57 | public static MapKey create(String value) { 58 | E.illegalArgumentIf(S.isBlank(value), "Value required"); 59 | Map memberValues = C.newMap("value", value); 60 | return AnnotationUtil.createAnnotation(MapKey.class, memberValues); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/Nonbinding.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import static java.lang.annotation.ElementType.METHOD; 24 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 25 | 26 | import java.lang.annotation.Retention; 27 | import java.lang.annotation.Target; 28 | 29 | /** 30 | * Note this annotation class is borrowed from cdi-api. 31 | * 32 | *

33 | * Excludes a member of an annotation type (such as a {@link javax.inject.Qualifier qualifier type} or 34 | * {@link javax.interceptor interceptor binding type}) from consideration when the container compares two annotation 35 | * instances. 36 | *

37 | * 38 | *
39 |  * @Qualifier
40 |  * @Retention(RUNTIME)
41 |  * @Target({ METHOD, FIELD, PARAMETER, TYPE })
42 |  * public @interface PayBy {
43 |  *     PaymentMethod value();
44 |  *
45 |  *     @Nonbinding
46 |  *     String comment();
47 |  * }
48 |  * 
49 | * 50 | * @author Gavin King 51 | * 52 | * @see javax.inject.Qualifier @Qualifier 53 | * @see javax.interceptor.InterceptorBinding @InterceptorBinding 54 | * 55 | */ 56 | @Retention(RUNTIME) 57 | @Target(METHOD) 58 | public @interface Nonbinding { 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/PostConstructProcess.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.PostConstructProcessor; 24 | 25 | import java.lang.annotation.*; 26 | 27 | /** 28 | * When a field or parameter is annotated with a `PostConstructProcess` 29 | * tagged annotation, it tells Genie to load specified {@link org.osgl.inject.PostConstructProcessor} to apply on the bean 30 | * after bean is constructed and, if the bean has {@link javax.annotation.PostConstruct} 31 | * method, after that method is called. 32 | */ 33 | @Retention(RetentionPolicy.RUNTIME) 34 | @Target(ElementType.ANNOTATION_TYPE) 35 | @Documented 36 | public @interface PostConstructProcess { 37 | 38 | /** 39 | * Specify the {@link PostConstructProcessor} implementation to 40 | * be called after bean has been constructed 41 | * 42 | * @return a {@link PostConstructProcessor} type 43 | */ 44 | Class value(); 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/Provided.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import java.lang.annotation.*; 24 | 25 | /** 26 | * Mark a method parameter value should be provided by 27 | * {@link org.osgl.inject.Injector}. 28 | * 29 | * This annotation is created because {@link javax.inject.Inject} 30 | * annotation cannot be applied on method parameters 31 | * 32 | * **Note** Genie does not know how to inject method parameters 33 | * and this annotation is provided to support framework author 34 | * on implementing method parameter injection. For example 35 | * [ActFramework](http://www.actframework.org) favor this annotation 36 | * on controller action handler parameter injection implementation 37 | */ 38 | @Documented 39 | @Retention(RetentionPolicy.RUNTIME) 40 | @Target(ElementType.PARAMETER) 41 | public @interface Provided { 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/Provides.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import java.lang.annotation.ElementType; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.RetentionPolicy; 26 | import java.lang.annotation.Target; 27 | 28 | /** 29 | * Mark a factory method of a module (any class) that can be used to 30 | * create a bean instance injection. 31 | * 32 | * The factory method could be annotated with 33 | * {@link javax.inject.Qualifier} annotations like {@link javax.inject.Named} to provide 34 | * some differentiation on injection 35 | */ 36 | @Retention(RetentionPolicy.RUNTIME) 37 | @Target(ElementType.METHOD) 38 | public @interface Provides { 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/RequestScoped.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Scope; 24 | import java.lang.annotation.*; 25 | 26 | /** 27 | * Mark a class whose instance, when get injected into program, should be 28 | * instantiated only once per user request 29 | * 30 | * Note we make it apply to {@link ElementType#PARAMETER} by intention so 31 | * in a controller method we can specify a class (e.g. a collection) as 32 | * session scoped and framework can keep build up the bean across multiple 33 | * requests in the same session. Instead of replacing the bean instance 34 | * everytime with each new request 35 | * 36 | * @see Scope 37 | */ 38 | @Scope 39 | @Documented 40 | @Retention(RetentionPolicy.RUNTIME) 41 | @Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER}) 42 | public @interface RequestScoped { 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/SessionScoped.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Scope; 24 | import java.lang.annotation.*; 25 | 26 | /** 27 | * Mark a class whose instance, when get injected into program, should be 28 | * instantiated only once per user session 29 | * 30 | * Note we make it apply to {@link ElementType#PARAMETER} by intention so 31 | * in a controller method we can specify a class (e.g. a collection) to indicate 32 | * the bean shall persist within a single request scope. E.g. if the interceptor 33 | * and the action handler has the same signature, the bean will NOT been 34 | * constructed for multiple times 35 | * 36 | * @see Scope 37 | */ 38 | @Scope 39 | @Documented 40 | @Retention(RetentionPolicy.RUNTIME) 41 | @Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD}) 42 | public @interface SessionScoped { 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/StopInheritedScope.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import java.lang.annotation.*; 24 | import javax.inject.Scope; 25 | 26 | /** 27 | * Stop a scope specification inherited from super class. 28 | * 29 | * @see Scope 30 | */ 31 | @Scope 32 | @Documented 33 | @Retention(RetentionPolicy.RUNTIME) 34 | @Target(ElementType.TYPE) 35 | public @interface StopInheritedScope { 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/Transform.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.annotation; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.BeanTransformer; 24 | 25 | import java.lang.annotation.*; 26 | 27 | /** 28 | * Used to tag an annotation with {@link org.osgl.Osgl.Function} 29 | * specification. 30 | */ 31 | @Documented 32 | @Retention(RetentionPolicy.RUNTIME) 33 | @Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.PARAMETER}) 34 | public @interface Transform { 35 | /** 36 | * Specify the {@link BeanTransformer bean transformer} implementation 37 | */ 38 | Class value(); 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/annotation/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Defines annotation classes. 3 | * 4 | * Genie annotations are created as a complement to JSR 330 annotations 5 | * to provide extended dependency injection mechanism including and 6 | * not limited to the following: 7 | * 8 | * * {@link org.osgl.inject.annotation.LoadCollection inject collection} 9 | * with item pre-populated. 10 | * * {@link org.osgl.inject.annotation.LoadValue inject value} 11 | * * {@link org.osgl.inject.annotation.Provides factory method marker} 12 | * * {@link org.osgl.inject.annotation.RequestScoped request scope marker} 13 | * * {@link org.osgl.inject.annotation.SessionScoped session scope marker} 14 | */ 15 | package org.osgl.inject.annotation; 16 | 17 | /*- 18 | * #%L 19 | * OSGL Genie 20 | * %% 21 | * Copyright (C) 2017 OSGL (Open Source General Library) 22 | * %% 23 | * Licensed under the Apache License, Version 2.0 (the "License"); 24 | * you may not use this file except in compliance with the License. 25 | * You may obtain a copy of the License at 26 | * 27 | * http://www.apache.org/licenses/LICENSE-2.0 28 | * 29 | * Unless required by applicable law or agreed to in writing, software 30 | * distributed under the License is distributed on an "AS IS" BASIS, 31 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 | * See the License for the specific language governing permissions and 33 | * limitations under the License. 34 | * #L% 35 | */ 36 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/loader/ElementLoaderBase.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.loader; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.ElementLoader; 24 | 25 | /** 26 | * Base class for {@link ElementLoader} implementations 27 | */ 28 | public abstract class ElementLoaderBase implements ElementLoader { 29 | 30 | /** 31 | * The default loader priority is set to `5` 32 | * 33 | * @return the default priority value: `5` 34 | */ 35 | @Override 36 | public int priority() { 37 | return 5; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/loader/LoaderUtil.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.loader; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.$; 24 | import org.osgl.inject.BeanSpec; 25 | import org.osgl.inject.ElementType; 26 | 27 | import java.lang.reflect.ParameterizedType; 28 | import java.lang.reflect.Type; 29 | import java.util.List; 30 | 31 | class LoaderUtil { 32 | static Class targetClass($.Var typeVar, BeanSpec container) { 33 | List types = container.typeParams(); 34 | Class targetClass = null; 35 | if (!types.isEmpty()) { 36 | // the effective type is always the last one 37 | // this is for both Collection and Map 38 | Type type = types.get(types.size() - 1); 39 | if (type instanceof Class) { 40 | targetClass = $.cast(type); 41 | if (targetClass == Class.class) { 42 | typeVar.set(ElementType.CLASS); 43 | } 44 | } else if (type instanceof ParameterizedType) { 45 | ParameterizedType ptype = $.cast(type); 46 | if (ptype.getRawType() instanceof Class) { 47 | type = ptype.getActualTypeArguments()[0]; 48 | if (type instanceof Class) { 49 | targetClass = $.cast(type); 50 | typeVar.set(ElementType.CLASS); 51 | } 52 | } 53 | } 54 | } 55 | return targetClass; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/loader/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Defines loader classes. 3 | * 4 | * Working with {@link org.osgl.inject.annotation genie annotations}, 5 | * loader provides extra facility for application to define how to 6 | * inject values including collections, maps and scala type values 7 | */ 8 | package org.osgl.inject.loader; 9 | 10 | /*- 11 | * #%L 12 | * OSGL Genie 13 | * %% 14 | * Copyright (C) 2017 OSGL (Open Source General Library) 15 | * %% 16 | * Licensed under the Apache License, Version 2.0 (the "License"); 17 | * you may not use this file except in compliance with the License. 18 | * You may obtain a copy of the License at 19 | * 20 | * http://www.apache.org/licenses/LICENSE-2.0 21 | * 22 | * Unless required by applicable law or agreed to in writing, software 23 | * distributed under the License is distributed on an "AS IS" BASIS, 24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 25 | * See the License for the specific language governing permissions and 26 | * limitations under the License. 27 | * #L% 28 | */ 29 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Act `genie` is a dependency injection solution inspired by 3 | * [Feather](https://github.com/zsoltherpai/feather). 4 | * 5 | * `genie` support all features provided by `Feather` and more: 6 | * 7 | * * JSR330 dependency injection on Constructors/Fields/Methods 8 | * * {@link javax.inject.Qualifier} annotations 9 | * * {@link javax.inject.Scope} annotations 10 | * * {@link org.osgl.inject.annotation.LoadCollection} annotations 11 | * * {@link org.osgl.inject.annotation.LoadValue} annotations 12 | * * {@link javax.annotation.PostConstruct} semantic 13 | * * Scope defined by [cdi-api-1.2](http://docs.jboss.org/cdi/api/1.2/) 14 | * * Scope defined by [Google Guice](https://github.com/google/guice) 15 | * * [Feather](https://github.com/zsoltherpai/feather) style module/provider factory methods 16 | * * [Google Guice](https://github.com/google/guice) style module 17 | * * {@link org.osgl.inject.annotation.LoadCollection collection inject mechanism} 18 | * * {@link org.osgl.inject.annotation.LoadValue value inject mechanism} 19 | * * {@link org.osgl.inject.annotation.Configuration configuration inject mechanism} 20 | */ 21 | package org.osgl.inject; 22 | 23 | /*- 24 | * #%L 25 | * OSGL Genie 26 | * %% 27 | * Copyright (C) 2017 OSGL (Open Source General Library) 28 | * %% 29 | * Licensed under the Apache License, Version 2.0 (the "License"); 30 | * you may not use this file except in compliance with the License. 31 | * You may obtain a copy of the License at 32 | * 33 | * http://www.apache.org/licenses/LICENSE-2.0 34 | * 35 | * Unless required by applicable law or agreed to in writing, software 36 | * distributed under the License is distributed on an "AS IS" BASIS, 37 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 38 | * See the License for the specific language governing permissions and 39 | * limitations under the License. 40 | * #L% 41 | */ 42 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/ArrayListProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.util.C; 24 | 25 | import javax.inject.Provider; 26 | import java.util.ArrayList; 27 | import java.util.List; 28 | 29 | /** 30 | * Inject {@link List} and {@link C.List} using 31 | * {@link org.osgl.util.DelegatingList} implementation. 32 | */ 33 | public class ArrayListProvider implements Provider> { 34 | 35 | public static final ArrayListProvider INSTANCE = new ArrayListProvider(); 36 | 37 | @Override 38 | public ArrayList get() { 39 | return new ArrayList(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/ArrayProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.BeanSpec; 24 | import org.osgl.inject.Genie; 25 | import org.osgl.inject.util.ArrayLoader; 26 | 27 | import javax.inject.Provider; 28 | import java.util.ArrayList; 29 | import java.util.List; 30 | 31 | public class ArrayProvider implements Provider { 32 | protected final Class elementType; 33 | protected final BeanSpec listSpec; 34 | protected final Genie genie; 35 | 36 | 37 | private ArrayProvider(Class elementType, Genie genie) { 38 | this.elementType = elementType; 39 | this.listSpec = BeanSpec.of(ArrayList.class, null, genie); 40 | this.genie = genie; 41 | } 42 | 43 | private ArrayProvider(Class elementType, BeanSpec listSpec, Genie genie) { 44 | this.elementType = elementType; 45 | this.listSpec = listSpec; 46 | this.genie = genie; 47 | } 48 | 49 | @Override 50 | public Object get() { 51 | ArrayList list = genie.get(listSpec); 52 | return listToArray(list); 53 | } 54 | 55 | private Object listToArray(List list) { 56 | return ArrayLoader.listToArray(list, elementType); 57 | } 58 | 59 | public static ArrayProvider of(Class arrayClass, Genie genie) { 60 | if (!arrayClass.isArray()) { 61 | throw new IllegalArgumentException("Array class expected"); 62 | } 63 | return new ArrayProvider(arrayClass.getComponentType(), genie); 64 | } 65 | 66 | public static ArrayProvider of(BeanSpec beanSpec, Genie genie) { 67 | if (!beanSpec.isArray()) { 68 | throw new IllegalArgumentException("Array bean spec required"); 69 | } 70 | Class arrayClass = beanSpec.rawType(); 71 | return new ArrayProvider(arrayClass.getComponentType(), beanSpec.toList(), genie); 72 | } 73 | 74 | 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/ConcurrentMapProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Provider; 24 | import java.util.concurrent.ConcurrentHashMap; 25 | import java.util.concurrent.ConcurrentMap; 26 | 27 | /** 28 | * Inject {@link java.util.concurrent.ConcurrentMap} with {@link ConcurrentHashMap} 29 | * implementation 30 | */ 31 | public class ConcurrentMapProvider implements Provider> { 32 | 33 | public static final ConcurrentMapProvider INSTANCE = new ConcurrentMapProvider(); 34 | 35 | @Override 36 | public ConcurrentMap get() { 37 | return new ConcurrentHashMap(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/DequeProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Provider; 24 | import java.util.ArrayDeque; 25 | import java.util.Deque; 26 | 27 | /** 28 | * Inject {@link Deque} type bean using {@link ArrayDeque} 29 | */ 30 | public class DequeProvider implements Provider> { 31 | 32 | public static final DequeProvider INSTANCE = new DequeProvider(); 33 | 34 | @Override 35 | public Deque get() { 36 | return new ArrayDeque(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/DirectProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.$; 24 | 25 | import javax.inject.Provider; 26 | 27 | /** 28 | * A Simple implementation of {@link javax.inject.Provider} that provide the bean 29 | * prepared before hand 30 | */ 31 | public class DirectProvider implements Provider { 32 | 33 | public T bean; 34 | 35 | public DirectProvider(T bean) { 36 | this.bean = $.requireNotNull(bean); 37 | } 38 | 39 | @Override 40 | public T get() { 41 | return bean; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/LazyProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.$; 24 | import org.osgl.inject.Injector; 25 | 26 | import javax.inject.Provider; 27 | 28 | /** 29 | * A lazy provider is prepared with the bean class and initialize 30 | * the bean upon demand 31 | */ 32 | public class LazyProvider implements Provider { 33 | 34 | private Class clazz; 35 | private Injector injector; 36 | 37 | public LazyProvider(Class clazz, Injector injector) { 38 | this.clazz = $.requireNotNull(clazz); 39 | this.injector = $.requireNotNull(injector); 40 | } 41 | 42 | 43 | @Override 44 | public T get() { 45 | return injector.get(clazz); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/LinkedHashMapProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import java.util.LinkedHashMap; 24 | import javax.inject.Provider; 25 | 26 | /** 27 | * Inject {@link java.util.Map} using {@link LinkedHashMap} 28 | */ 29 | public class LinkedHashMapProvider implements Provider> { 30 | 31 | public static final LinkedHashMapProvider INSTANCE = new LinkedHashMapProvider(); 32 | 33 | @Override 34 | public LinkedHashMap get() { 35 | return new LinkedHashMap<>(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/LinkedListProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Provider; 24 | import java.util.LinkedList; 25 | 26 | /** 27 | * Inject {@link LinkedList} and {@link LinkedList} using 28 | * {@link org.osgl.util.DelegatingList} implementation. 29 | */ 30 | public class LinkedListProvider implements Provider> { 31 | 32 | public static final LinkedListProvider INSTANCE = new LinkedListProvider(); 33 | 34 | @Override 35 | public LinkedList get() { 36 | return new LinkedList(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/NullValueProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Provider; 24 | 25 | /** 26 | * A provider that always returns `null` 27 | */ 28 | public class NullValueProvider implements Provider { 29 | 30 | public static final Provider INSTANCE = new NullValueProvider(); 31 | 32 | private NullValueProvider() {} 33 | 34 | @Override 35 | public Object get() { 36 | return null; 37 | } 38 | 39 | public static Provider instance() { 40 | return INSTANCE; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/OsglListProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.util.C; 24 | 25 | import javax.inject.Provider; 26 | import java.util.List; 27 | 28 | /** 29 | * Inject {@link List} and {@link org.osgl.util.C.List} using 30 | * {@link org.osgl.util.DelegatingList} implementation. 31 | */ 32 | public class OsglListProvider implements Provider> { 33 | 34 | public static final OsglListProvider INSTANCE = new OsglListProvider(); 35 | 36 | @Override 37 | public C.List get() { 38 | return C.newList(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/OsglMapProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.util.C; 24 | 25 | import javax.inject.Provider; 26 | 27 | /** 28 | * Inject {@link java.util.Map} using {@link org.osgl.util.C.Map} 29 | */ 30 | public class OsglMapProvider implements Provider> { 31 | 32 | public static final OsglMapProvider INSTANCE = new OsglMapProvider(); 33 | 34 | @Override 35 | public C.Map get() { 36 | return C.newMap(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/OsglSetProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.util.C; 24 | 25 | import javax.inject.Provider; 26 | 27 | /** 28 | * Inject {@link java.util.Set} using {@link org.osgl.util.DelegatingSet} 29 | */ 30 | public class OsglSetProvider implements Provider> { 31 | 32 | public static final OsglSetProvider INSTANCE = new OsglSetProvider(); 33 | 34 | @Override 35 | public C.Set get() { 36 | return C.newSet(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/PrimitiveArrayProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Provider; 24 | 25 | public class PrimitiveArrayProvider implements Provider { 26 | 27 | 28 | 29 | @Override 30 | public Object get() { 31 | return null; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/SortedMapProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Provider; 24 | import java.util.SortedMap; 25 | import java.util.TreeMap; 26 | 27 | /** 28 | * Inject {@link SortedMap} using {@link TreeMap} 29 | */ 30 | public class SortedMapProvider implements Provider> { 31 | 32 | public static final SortedMapProvider INSTANCE = new SortedMapProvider(); 33 | 34 | @Override 35 | public SortedMap get() { 36 | return new TreeMap(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/SortedSetProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.provider; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Provider; 24 | import java.util.SortedSet; 25 | import java.util.TreeSet; 26 | 27 | /** 28 | * Inject {@link SortedSet} using {@link TreeSet} 29 | */ 30 | public class SortedSetProvider implements Provider> { 31 | 32 | public static final SortedSetProvider INSTANCE = new SortedSetProvider(); 33 | 34 | @Override 35 | public SortedSet get() { 36 | return new TreeSet(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/provider/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Define Genie built-in {@link javax.inject.Provider providers} 3 | * for commonly used interfaces including and not limited 4 | * to the following: 5 | * 6 | * * {@link java.util.List} 7 | * * {@link java.util.Set} 8 | * * {@link java.util.Map} 9 | */ 10 | package org.osgl.inject.provider; 11 | 12 | /*- 13 | * #%L 14 | * OSGL Genie 15 | * %% 16 | * Copyright (C) 2017 OSGL (Open Source General Library) 17 | * %% 18 | * Licensed under the Apache License, Version 2.0 (the "License"); 19 | * you may not use this file except in compliance with the License. 20 | * You may obtain a copy of the License at 21 | * 22 | * http://www.apache.org/licenses/LICENSE-2.0 23 | * 24 | * Unless required by applicable law or agreed to in writing, software 25 | * distributed under the License is distributed on an "AS IS" BASIS, 26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 27 | * See the License for the specific language governing permissions and 28 | * limitations under the License. 29 | * #L% 30 | */ 31 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/util/ParameterizedTypeImpl.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.util; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.$; 24 | 25 | import java.lang.reflect.ParameterizedType; 26 | import java.lang.reflect.Type; 27 | import java.util.Arrays; 28 | 29 | public class ParameterizedTypeImpl implements ParameterizedType { 30 | 31 | private final Type[] actualTypeArguments; 32 | private final Type ownerType; 33 | private final Type rawType; 34 | 35 | public ParameterizedTypeImpl(Type[] actualTypeArguments, Type ownerType, Type rawType){ 36 | this.actualTypeArguments = actualTypeArguments; 37 | this.ownerType = ownerType; 38 | this.rawType = rawType; 39 | } 40 | 41 | public Type[] getActualTypeArguments() { 42 | return actualTypeArguments; 43 | } 44 | 45 | public Type getOwnerType() { 46 | return ownerType; 47 | } 48 | 49 | public Type getRawType() { 50 | return rawType; 51 | } 52 | 53 | 54 | @Override 55 | public boolean equals(Object o) { 56 | if (this == o) return true; 57 | if (o == null || getClass() != o.getClass()) return false; 58 | 59 | ParameterizedTypeImpl that = (ParameterizedTypeImpl) o; 60 | 61 | // Probably incorrect - comparing Object[] arrays with Arrays.equals 62 | if (!Arrays.equals(actualTypeArguments, that.actualTypeArguments)) return false; 63 | if (ownerType != null ? !ownerType.equals(that.ownerType) : that.ownerType != null) return false; 64 | return rawType != null ? rawType.equals(that.rawType) : that.rawType == null; 65 | 66 | } 67 | 68 | @Override 69 | public int hashCode() { 70 | return $.hc(actualTypeArguments, ownerType, rawType); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/util/PostConstructProcessorBase.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.util; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.$; 24 | import org.osgl.inject.PostConstructProcessor; 25 | 26 | /** 27 | * Base class for implementing {@link org.osgl.inject.PostConstructProcessor} 28 | */ 29 | public abstract class PostConstructProcessorBase extends $.Visitor implements PostConstructProcessor { 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/util/SimpleSingletonScope.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject.util; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.BeanSpec; 24 | import org.osgl.inject.ScopeCache; 25 | 26 | import java.util.concurrent.ConcurrentHashMap; 27 | 28 | public class SimpleSingletonScope implements ScopeCache.SingletonScope { 29 | 30 | private ConcurrentHashMap lookup = new ConcurrentHashMap<>(); 31 | 32 | @Override 33 | public T get(BeanSpec target) { 34 | return (T)lookup.get(target); 35 | } 36 | 37 | @Override 38 | public void put(BeanSpec target, T bean) { 39 | lookup.put(target, bean); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/osgl/inject/util/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Define utility classes used by {@link org.osgl.inject.Genie} 3 | */ 4 | package org.osgl.inject.util; 5 | 6 | /*- 7 | * #%L 8 | * OSGL Genie 9 | * %% 10 | * Copyright (C) 2017 OSGL (Open Source General Library) 11 | * %% 12 | * Licensed under the Apache License, Version 2.0 (the "License"); 13 | * you may not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * http://www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an "AS IS" BASIS, 20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | * #L% 24 | */ 25 | -------------------------------------------------------------------------------- /src/main/resources/org/osgl/inject/.version: -------------------------------------------------------------------------------- 1 | artifact=${project.artifactId} 2 | version=${project.version} 3 | build=${buildNumber} -------------------------------------------------------------------------------- /src/test/java/issue/GH10.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.Genie; 25 | import org.osgl.inject.Module; 26 | import osgl.ut.TestBase; 27 | 28 | /** 29 | * [Genie shall use default constructor when possible] 30 | * (https://github.com/osglworks/java-di/issues/10) 31 | */ 32 | public class GH10 extends TestBase { 33 | 34 | public interface A { 35 | String a(); 36 | } 37 | 38 | public static class Foo implements A { 39 | 40 | private String a; 41 | 42 | public Foo() { 43 | a = "foo"; 44 | } 45 | 46 | public Foo(String a) { 47 | this.a = a; 48 | } 49 | 50 | @Override 51 | public String a() { 52 | return a; 53 | } 54 | } 55 | 56 | public static class Binder extends Module { 57 | @Override 58 | protected void configure() { 59 | bind(A.class).to(Foo.class); 60 | } 61 | } 62 | 63 | @Test 64 | public void test() { 65 | Genie genie = Genie.create(Binder.class); 66 | A a = genie.get(A.class); 67 | eq("foo", a.a()); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/issue/GH21.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Ignore; 24 | import org.junit.Test; 25 | import org.osgl.$; 26 | import org.osgl.inject.BeanSpec; 27 | import org.osgl.inject.Genie; 28 | import org.osgl.inject.annotation.Configuration; 29 | import osgl.ut.TestBase; 30 | 31 | import java.lang.reflect.Field; 32 | import javax.enterprise.context.SessionScoped; 33 | 34 | // Scope annotation is a decorator for inject library 35 | // to decide the inject logic. It is not a spec of 36 | // the inject element 37 | @Ignore 38 | public class GH21 extends TestBase { 39 | private static class X { 40 | @Configuration("foo") 41 | public String foo; 42 | } 43 | 44 | private static class Y { 45 | @Configuration("foo") 46 | @SessionScoped 47 | public String foo; 48 | } 49 | 50 | @Test 51 | public void testEquality() { 52 | Genie genie = Genie.create(); 53 | Field xfoo = $.fieldOf(X.class, "foo"); 54 | Field yfoo = $.fieldOf(Y.class, "foo"); 55 | assertNotEquals(BeanSpec.of(xfoo, genie), BeanSpec.of(yfoo, genie)); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/issue/GH25.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.Genie; 25 | import org.osgl.inject.NamedProvider; 26 | import osgl.ut.TestBase; 27 | 28 | import javax.inject.Inject; 29 | import javax.inject.Named; 30 | 31 | public class GH25 extends TestBase { 32 | 33 | public static class Foo { 34 | private String name; 35 | public Foo(String name) { 36 | this.name = name; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return name; 42 | } 43 | } 44 | 45 | public static class FooProvider implements NamedProvider { 46 | @Override 47 | public Foo get(String name) { 48 | return new Foo(name); 49 | } 50 | } 51 | 52 | public static class FooHolder { 53 | 54 | @Named("bar") 55 | @Inject 56 | private Foo foo; 57 | 58 | } 59 | 60 | @Test 61 | public void test() { 62 | Genie genie = Genie.create(); 63 | genie.registerNamedProvider(Foo.class, new FooProvider()); 64 | FooHolder fooHolder = genie.get(FooHolder.class); 65 | eq("bar", fooHolder.foo.name); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/test/java/issue/GH26.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.BeanSpec; 25 | import org.osgl.inject.Genie; 26 | import osgl.ut.TestBase; 27 | 28 | public class GH26 extends TestBase { 29 | 30 | static class Foo { 31 | T data; 32 | } 33 | 34 | static class FooHolder { 35 | Foo foo; 36 | } 37 | 38 | @Test 39 | public void test() { 40 | Genie genie = Genie.create(); 41 | BeanSpec fooHolderSpec = BeanSpec.of(FooHolder.class, genie); 42 | BeanSpec fooSpec = fooHolderSpec.field("foo"); 43 | BeanSpec dataSpec = fooSpec.field("data"); 44 | eq(String.class, dataSpec.rawType()); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/issue/GH33.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.BeanSpec; 25 | import org.osgl.inject.Genie; 26 | import osgl.ut.TestBase; 27 | 28 | import java.lang.reflect.Type; 29 | import java.util.List; 30 | 31 | public class GH33 extends TestBase { 32 | 33 | private static class PackItem { 34 | public String name; 35 | } 36 | 37 | public static class BaseMapper { 38 | } 39 | 40 | public static class ItemMapper extends BaseMapper { 41 | } 42 | 43 | @Test 44 | public void test() { 45 | Genie genie = Genie.create(); 46 | BeanSpec spec = BeanSpec.of(ItemMapper.class, genie); 47 | BeanSpec parent = spec.parent(); 48 | List typeParams = parent.typeParams(); 49 | eq(1, typeParams.size()); 50 | eq(PackItem.class, typeParams.get(0)); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/issue/GH34.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.BeanSpec; 25 | import org.osgl.inject.GenericTypedBeanLoader; 26 | import org.osgl.inject.Genie; 27 | import osgl.ut.TestBase; 28 | 29 | import java.lang.reflect.Type; 30 | import java.util.List; 31 | import javax.inject.Inject; 32 | 33 | public class GH34 extends TestBase { 34 | private static class PackItem { 35 | public String name; 36 | } 37 | 38 | public interface BaseMapper {} 39 | 40 | public static class ItemMapper implements BaseMapper {} 41 | 42 | public static class BaseService { 43 | @Inject 44 | BaseMapper mapper; 45 | } 46 | 47 | public static class ItemService extends BaseService {} 48 | 49 | @Test 50 | public void test() { 51 | Genie genie = Genie.create(); 52 | genie.registerGenericTypedBeanLoader(BaseMapper.class, new GenericTypedBeanLoader() { 53 | @Override 54 | public BaseMapper load(BeanSpec spec) { 55 | List typeParams = spec.typeParams(); 56 | if (typeParams.size() > 0) { 57 | Type type = typeParams.get(0); 58 | if (type.equals(PackItem.class)) { 59 | return new ItemMapper(); 60 | } 61 | } 62 | return null; 63 | } 64 | }); 65 | ItemService itemService = genie.get(ItemService.class); 66 | notNull(itemService); 67 | notNull(itemService.mapper); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/issue/GH38.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.Genie; 25 | import osgl.ut.TestBase; 26 | 27 | import java.util.LinkedHashMap; 28 | 29 | public class GH38 extends TestBase { 30 | @Test 31 | public void test() { 32 | Genie genie = Genie.create(); 33 | LinkedHashMap map = genie.get(LinkedHashMap.class); 34 | notNull(map); 35 | yes(map.isEmpty()); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/issue/GH42.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import issue.gh42.Order; 24 | import org.junit.Ignore; 25 | import org.junit.Test; 26 | import org.osgl.inject.Genie; 27 | import osgl.ut.TestBase; 28 | 29 | @Ignore 30 | // no way to get this fixed in Genie 31 | public class GH42 extends TestBase { 32 | @Test 33 | public void test() { 34 | Genie genie = Genie.create(); 35 | Order.Dao orderDao = genie.get(Order.Dao.class); 36 | notNull(orderDao.accDao); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/issue/GH43.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.$; 25 | import org.osgl.inject.BeanSpec; 26 | import org.osgl.inject.Genie; 27 | import org.osgl.util.Generics; 28 | import osgl.ut.TestBase; 29 | 30 | import java.lang.annotation.Annotation; 31 | import java.lang.reflect.Method; 32 | import java.lang.reflect.Type; 33 | import java.util.List; 34 | import java.util.Map; 35 | import javax.inject.Named; 36 | 37 | public class GH43 extends TestBase { 38 | 39 | public static abstract class Foo { 40 | public void doIt(@Named("foo") List id) {} 41 | } 42 | 43 | public static class IntFoo extends Foo { 44 | } 45 | 46 | @Test 47 | public void test() { 48 | Genie genie = Genie.create(); 49 | Method method = $.getMethod(Foo.class, "doIt", List.class); 50 | Map typeVarLookup = Generics.buildTypeParamImplLookup(IntFoo.class); 51 | Type type = method.getGenericParameterTypes()[0]; 52 | Annotation[] anno = method.getParameterAnnotations()[0]; 53 | BeanSpec spec = BeanSpec.of(type, anno, genie, typeVarLookup); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/issue/GH45.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.$; 25 | import org.osgl.inject.BeanSpec; 26 | import org.osgl.inject.Genie; 27 | import org.osgl.util.C; 28 | import osgl.ut.TestBase; 29 | 30 | import java.lang.reflect.Field; 31 | import java.lang.reflect.Type; 32 | import java.util.List; 33 | import java.util.Map; 34 | 35 | public class GH45 extends TestBase { 36 | 37 | class Foo { 38 | List list; 39 | } 40 | 41 | @Test 42 | public void test() { 43 | Genie genie = Genie.create(); 44 | Field field = $.fieldOf(Foo.class, "list"); 45 | Type type = field.getGenericType(); 46 | Map typeLookup = C.Map("ID", Long.class); 47 | BeanSpec spec = BeanSpec.of(type, genie, typeLookup); 48 | List typeParams = spec.typeParams(); 49 | eq(Long.class, typeParams.get(0)); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/issue/GH46.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.BeanSpec; 25 | import org.osgl.inject.Genie; 26 | import osgl.ut.TestBase; 27 | 28 | public class GH46 extends TestBase { 29 | 30 | @Test 31 | public void test() { 32 | Genie genie = Genie.create(); 33 | BeanSpec spec = BeanSpec.of(char.class, genie); 34 | isNull(spec.parent()); 35 | isEmpty(spec.fields()); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/issue/GH47.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.BeanSpec; 25 | import org.osgl.inject.Genie; 26 | import org.osgl.util.KVStore; 27 | import osgl.ut.TestBase; 28 | 29 | import java.util.List; 30 | 31 | public class GH47 extends TestBase { 32 | 33 | public static class Foo { 34 | public KVStore kv; 35 | } 36 | 37 | @Test 38 | public void test() { 39 | Genie genie = Genie.create(); 40 | BeanSpec spec = BeanSpec.of(Foo.class, genie); 41 | List fields = spec.nonStaticFields(); 42 | spec = fields.get(0); 43 | fields = spec.nonStaticFields(); 44 | eq(9, fields.size()); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/issue/GH50.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2019 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.Genie; 25 | import osgl.ut.TestBase; 26 | 27 | import javax.inject.Inject; 28 | 29 | public class GH50 extends TestBase { 30 | 31 | public static class Foo {} 32 | 33 | public static class GrandParent { 34 | @Inject 35 | T data; 36 | } 37 | 38 | public static class Parent extends GrandParent {} 39 | 40 | public static class Me extends Parent {} 41 | 42 | @Test 43 | public void test() { 44 | Genie genie = Genie.create(); 45 | genie.get(Me.class); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/issue/GH51.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2019 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.BeanSpec; 25 | import org.osgl.inject.Genie; 26 | import osgl.ut.TestBase; 27 | 28 | public class GH51 extends TestBase { 29 | 30 | public static class GrandParent { 31 | T t; 32 | } 33 | 34 | public static class Req { 35 | ID id; 36 | V v; 37 | } 38 | 39 | public static class Parent> extends GrandParent { 40 | } 41 | 42 | public static class Me extends Parent> {} 43 | 44 | @Test 45 | public void test() { 46 | Genie genie = Genie.create(); 47 | BeanSpec spec = BeanSpec.of(Me.class, genie); 48 | BeanSpec t = spec.field("t"); 49 | BeanSpec id = t.field("id"); 50 | eq(Integer.class, id.rawType()); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/issue/GH54.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2019 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.Genie; 25 | import org.osgl.inject.Module; 26 | import org.osgl.inject.annotation.MapKey; 27 | import org.osgl.inject.annotation.TypeOf; 28 | import org.osgl.inject.loader.TypedElementLoader; 29 | import org.osgl.util.Keyword; 30 | import osgl.ut.TestBase; 31 | 32 | import java.util.ArrayList; 33 | import java.util.List; 34 | import java.util.Map; 35 | 36 | public class GH54 extends TestBase { 37 | public enum Color { 38 | LightGray, DarkBlue 39 | } 40 | 41 | @TypeOf 42 | @MapKey("name") 43 | private Map colors; 44 | 45 | @Test 46 | public void test() { 47 | Genie genie = Genie.create(new Module() { 48 | @Override 49 | protected void configure() { 50 | bind(TypedElementLoader.class).to(new TypedElementLoader() { 51 | @Override 52 | protected List load(Class type, boolean loadNonPublic, boolean loadAbstract, boolean loadRoot) { 53 | List list = new ArrayList<>(); 54 | list.add(Color.class); 55 | return list; 56 | } 57 | }); 58 | } 59 | }); 60 | GH54 gh54 = genie.get(GH54.class); 61 | eq(Color.DarkBlue, gh54.colors.get(Keyword.of("dark-blue"))); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/issue/GH55.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2019 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.Genie; 25 | import org.osgl.inject.Module; 26 | import org.osgl.inject.annotation.TypeOf; 27 | import org.osgl.inject.loader.TypedElementLoader; 28 | import osgl.ut.TestBase; 29 | 30 | import java.util.ArrayList; 31 | import java.util.List; 32 | 33 | public class GH55 extends TestBase { 34 | 35 | public enum Size { 36 | SMALL, MEDIUM, LARGE; 37 | } 38 | 39 | @TypeOf 40 | private List sizes; 41 | 42 | @Test 43 | public void test() { 44 | Genie genie = Genie.create(new Module() { 45 | @Override 46 | protected void configure() { 47 | bind(TypedElementLoader.class).to(new TypedElementLoader() { 48 | @Override 49 | protected List load(Class type, boolean loadNonPublic, boolean loadAbstract, boolean loadRoot) { 50 | List list = new ArrayList<>(); 51 | list.add(Size.class); 52 | return list; 53 | } 54 | }); 55 | } 56 | }); 57 | GH55 gh55 = genie.get(GH55.class); 58 | eq(3, gh55.sizes.size()); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/test/java/issue/GH56.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2019 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.Genie; 25 | import org.osgl.inject.Module; 26 | import org.osgl.inject.loader.TypedElementLoader; 27 | import osgl.ut.TestBase; 28 | 29 | import javax.inject.Inject; 30 | import java.util.ArrayList; 31 | import java.util.List; 32 | import java.util.Map; 33 | 34 | public class GH56 extends TestBase { 35 | 36 | public enum Color {R, G, B} 37 | 38 | @Inject 39 | public Map colorLookup; 40 | 41 | @Inject 42 | public List colorList; 43 | 44 | @Test 45 | public void test() { 46 | Genie genie = Genie.create(new Module() { 47 | @Override 48 | protected void configure() { 49 | bind(TypedElementLoader.class).to(new TypedElementLoader() { 50 | @Override 51 | protected List load(Class type, boolean loadNonPublic, boolean loadAbstract, boolean loadRoot) { 52 | List list = new ArrayList<>(); 53 | list.add(Color.class); 54 | return list; 55 | } 56 | }); 57 | } 58 | }); 59 | GH56 gh56 = genie.get(GH56.class); 60 | eq(3, gh56.colorList.size()); 61 | eq(Color.B, gh56.colorLookup.get("B")); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/issue/GH57.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2019 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.BeanSpec; 25 | import org.osgl.inject.Genie; 26 | import org.osgl.inject.Module; 27 | import org.osgl.inject.loader.TypedElementLoader; 28 | import osgl.ut.TestBase; 29 | 30 | import javax.inject.Inject; 31 | import java.util.ArrayList; 32 | import java.util.List; 33 | import java.util.Map; 34 | 35 | public class GH57 extends TestBase { 36 | 37 | public static class TestResp { 38 | public Integer code; 39 | public T result; 40 | 41 | public TestResp(Integer code, T result) { 42 | this.code = code; 43 | this.result = result; 44 | } 45 | } 46 | 47 | @Test 48 | public void test() { 49 | Genie genie = Genie.create(); 50 | BeanSpec spec = BeanSpec.of(TestResp.class, genie); 51 | Map fieldSpecs = spec.fields(); 52 | eq(Object.class, fieldSpecs.get("result").rawType()); 53 | eq(Integer.class, fieldSpecs.get("code").rawType()); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/issue/Gh18.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Ignore; 24 | import org.junit.Test; 25 | import org.osgl.inject.BeanSpec; 26 | import org.osgl.inject.Genie; 27 | import osgl.ut.TestBase; 28 | 29 | public class Gh18 extends TestBase { 30 | 31 | @Test 32 | @Ignore 33 | public void test() { 34 | Genie genie = Genie.create(); 35 | BeanSpec spec = BeanSpec.of(int[].class, genie); 36 | eq(int.class, spec.typeParams().get(0)); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/issue/Gh19.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.BeanSpec; 25 | import org.osgl.inject.Genie; 26 | import osgl.ut.TestBase; 27 | 28 | public class Gh19 extends TestBase { 29 | 30 | public interface IntA {} 31 | 32 | @Test 33 | public void test() { 34 | Genie genie = Genie.create(); 35 | BeanSpec spec = BeanSpec.of(IntA.class, genie); 36 | yes(spec.isInterface()); 37 | spec = BeanSpec.of(Gh19.class, genie); 38 | no(spec.isInterface()); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/issue/Gh20.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.BeanSpec; 25 | import org.osgl.inject.Genie; 26 | import osgl.ut.TestBase; 27 | 28 | public class Gh20 extends TestBase { 29 | 30 | @Test 31 | public void test() { 32 | Genie genie = Genie.create(); 33 | BeanSpec spec = BeanSpec.of(int[].class, genie); 34 | eq(int.class, spec.componentSpec().rawType()); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/issue/Gh58.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2020 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.inject.BeanSpec; 25 | import org.osgl.inject.Genie; 26 | import osgl.ut.TestBase; 27 | 28 | public class Gh58 extends TestBase { 29 | 30 | public static class Foo { 31 | public int id; 32 | } 33 | 34 | @Test 35 | public void test() { 36 | Genie genie = Genie.create(); 37 | Integer i = genie.get(Integer.class); 38 | BeanSpec fooSpec = BeanSpec.of(Foo.class, genie); 39 | no(genie.subjectToInject(fooSpec)); 40 | genie.get(Foo.class); 41 | no(genie.subjectToInject(fooSpec)); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/issue/Gh59.java: -------------------------------------------------------------------------------- 1 | package issue; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2020 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import org.osgl.$; 25 | import org.osgl.Lang; 26 | import org.osgl.inject.Genie; 27 | import osgl.ut.TestBase; 28 | 29 | import java.lang.reflect.Field; 30 | 31 | public class Gh59 extends TestBase { 32 | @Test 33 | public void test() { 34 | Genie genie = Genie.create(); 35 | eq(0, genie.get(Integer.class)); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/issue/gh42/Account.java: -------------------------------------------------------------------------------- 1 | package issue.gh42; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | public class Account extends BsbfRecord { 24 | 25 | public static class Dao extends BsbfDao { 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/issue/gh42/BsbfDao.java: -------------------------------------------------------------------------------- 1 | package issue.gh42; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | public abstract class BsbfDao extends MorphiaDao { 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/issue/gh42/BsbfRecord.java: -------------------------------------------------------------------------------- 1 | package issue.gh42; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | public class BsbfRecord { 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/issue/gh42/Dao.java: -------------------------------------------------------------------------------- 1 | package issue.gh42; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | public interface Dao { 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/issue/gh42/DaoBase.java: -------------------------------------------------------------------------------- 1 | package issue.gh42; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.util.Generics; 24 | 25 | import java.lang.reflect.Type; 26 | import java.util.List; 27 | 28 | public class DaoBase implements Dao { 29 | 30 | public Type modelType; 31 | public Class modelClass; 32 | public Type idType; 33 | public Class idClass; 34 | 35 | public DaoBase() { 36 | exploreTypes(); 37 | } 38 | 39 | private void exploreTypes() { 40 | List types = Generics.typeParamImplementations(getClass(), DaoBase.class); 41 | int sz = types.size(); 42 | if (sz < 1) { 43 | return; 44 | } 45 | if (sz > 1) { 46 | modelType = types.get(1); 47 | modelClass = Generics.classOf(modelType); 48 | } 49 | idType = types.get(0); 50 | idClass = Generics.classOf(idType); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/issue/gh42/MorphiaDao.java: -------------------------------------------------------------------------------- 1 | package issue.gh42; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | public class MorphiaDao extends DaoBase { 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/issue/gh42/Order.java: -------------------------------------------------------------------------------- 1 | package issue.gh42; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2018 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | 25 | public class Order extends BsbfRecord { 26 | public static class Dao extends BsbfDao { 27 | @Inject 28 | public MorphiaDao accDao; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/BaseWithPostConstructor.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.annotation.PostConstruct; 24 | import javax.inject.Inject; 25 | 26 | class BaseWithPostConstructor { 27 | 28 | static ThreadLocal current = new ThreadLocal(); 29 | 30 | @PostConstruct 31 | protected void init() { 32 | current.set(this); 33 | } 34 | 35 | static class Holder { 36 | @Inject BaseWithPostConstructor bean; 37 | } 38 | 39 | static void reset() { 40 | current.remove(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/BaseWithPostConstructorHolder.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | 25 | class BaseWithPostConstructorHolder { 26 | 27 | @Inject 28 | BaseWithPostConstructor bean; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/CDIScopedFactory.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.ScopedObjects.SessionProduct; 24 | import org.osgl.inject.annotation.Provides; 25 | 26 | import javax.enterprise.context.SessionScoped; 27 | import javax.inject.Inject; 28 | import javax.inject.Provider; 29 | import javax.inject.Singleton; 30 | 31 | class CDIScopedFactory extends Module { 32 | 33 | @Override 34 | protected void configure() { 35 | bind(ScopeCache.SessionScope.class).to(ScopedFactory.SESSION_SCOPE_PROVIDER); 36 | } 37 | 38 | @Provides 39 | @SessionScoped 40 | static SessionProduct createSession(ScopedObjects.JEESessionObject bean) { 41 | return bean; 42 | } 43 | 44 | static class ProductHolder { 45 | @SessionScoped 46 | @Inject 47 | SessionProduct product; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/Circular.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | import java.util.concurrent.atomic.AtomicInteger; 25 | 26 | class Circular { 27 | 28 | private static final AtomicInteger num = new AtomicInteger(0); 29 | 30 | protected int n; 31 | 32 | Circular() { 33 | n = num.incrementAndGet(); 34 | } 35 | 36 | Circular(Circular circular) { 37 | this.n = circular.n; 38 | } 39 | 40 | static class A extends Circular { 41 | @Inject 42 | A(C c) { 43 | super(c); 44 | } 45 | } 46 | 47 | static class B extends Circular { 48 | @Inject 49 | B(A a) { 50 | super(a); 51 | } 52 | } 53 | 54 | static class C extends Circular { 55 | @Inject 56 | C(B b) { 57 | super(b); 58 | } 59 | } 60 | 61 | static class Self { 62 | @Inject 63 | Self self; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/ConfigurationLoadTestBed.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2016 - 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.Configuration; 24 | 25 | public class ConfigurationLoadTestBed { 26 | 27 | @Configuration("foo.bar") 28 | private int n; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/Context.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.util.C; 24 | 25 | import java.util.Map; 26 | import java.util.UUID; 27 | 28 | /** 29 | * Emulate computational context 30 | */ 31 | class Context { 32 | 33 | private String id; 34 | 35 | private Map data = C.newMap(); 36 | 37 | Context() { 38 | id = UUID.randomUUID().toString(); 39 | } 40 | 41 | public String id() { 42 | return id; 43 | } 44 | 45 | T get(String key) { 46 | return (T) data.get(key); 47 | } 48 | 49 | void put(String key, T object) { 50 | data.put(key, object); 51 | } 52 | 53 | private static final ThreadLocal current = new ThreadLocal(); 54 | 55 | static Context get() { 56 | return current.get(); 57 | } 58 | static void set(Context context) { 59 | current.set(context); 60 | } 61 | static void reset() { 62 | current.remove(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/Dao.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.$; 24 | 25 | public class Dao { 26 | 27 | private Class modelType; 28 | 29 | Class modelType() { 30 | return this.modelType; 31 | } 32 | void setModelType(Class modelType) { 33 | this.modelType = $.requireNotNull(modelType); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/DaoInjectListener.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.$; 24 | 25 | import java.lang.reflect.Type; 26 | import java.util.List; 27 | 28 | public class DaoInjectListener extends InjectListener.Adaptor { 29 | @Override 30 | public void injected(Object bean, BeanSpec beanSpec) { 31 | if (bean instanceof Dao) { 32 | Dao dao = $.cast(bean); 33 | List typeParams = beanSpec.typeParams(); 34 | dao.setModelType((Class)typeParams.get(0)); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/DerivedFromBaseWithPostConstructor.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | 25 | class DerivedFromBaseWithPostConstructor extends BaseWithPostConstructor { 26 | static class Holder { 27 | @Inject 28 | BaseWithPostConstructor bean; 29 | } 30 | 31 | static class Module extends org.osgl.inject.Module { 32 | @Override 33 | protected void configure() { 34 | bind(BaseWithPostConstructor.class).to(DerivedFromBaseWithPostConstructor.class); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/ErrorDispatcher.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.MapKey; 24 | import org.osgl.inject.annotation.TypeOf; 25 | import org.osgl.util.Keyword; 26 | 27 | import javax.inject.Inject; 28 | import java.util.List; 29 | import java.util.Map; 30 | 31 | /** 32 | * Dispatch error to proper handlers 33 | */ 34 | class ErrorDispatcher { 35 | 36 | @TypeOf 37 | List handlerList; 38 | 39 | @TypeOf 40 | @MapKey("errorCode") 41 | Map registry; 42 | 43 | @Inject 44 | @MapKey("errorCode") 45 | Map registry2; 46 | 47 | @Inject 48 | Map registry3; 49 | 50 | String handle(int error) { 51 | ErrorHandler handler = registry.get(error); 52 | return null == handler ? "unknown" : handler.toString(); 53 | } 54 | 55 | String handle2(int error) { 56 | ErrorHandler handler = registry2.get(error); 57 | return null == handler ? "unknown" : handler.toString(); 58 | } 59 | 60 | String handle3(String errorType) { 61 | ErrorHandler handler = registry3.get(Keyword.of(errorType)); 62 | return null == handler ? "unknown" : handler.toString(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/ErrorHandler.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | /** 24 | * A test class: define a abstract class for error handlers 25 | */ 26 | public abstract class ErrorHandler { 27 | /** 28 | * Returns the error code the implementation is looking for 29 | * @return the interested error code 30 | */ 31 | public abstract int errorCode(); 32 | 33 | @Override 34 | public String toString() { 35 | return getClass().getSimpleName(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/EvenFibonacciSeriesHolder.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | 24 | import org.osgl.util.S; 25 | 26 | import javax.inject.Inject; 27 | import java.util.List; 28 | 29 | public class EvenFibonacciSeriesHolder { 30 | 31 | @FibonacciSeries 32 | @EvenNumber 33 | List series; 34 | 35 | @Override 36 | public String toString() { 37 | return S.join(",", series); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/EvenNumber.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.Filter; 24 | 25 | import java.lang.annotation.Documented; 26 | import java.lang.annotation.Retention; 27 | import java.lang.annotation.RetentionPolicy; 28 | 29 | @Documented 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Filter(EvenNumberFilter.class) 32 | public @interface EvenNumber { 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/EvenNumberFilter.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.$; 24 | import org.osgl.util.N; 25 | 26 | import java.util.Map; 27 | 28 | class EvenNumberFilter implements ElementFilter { 29 | @Override 30 | public $.Function filter(Map options, BeanSpec container) { 31 | return N.F.IS_EVEN; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/FibonacciSeries.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.InjectTag; 24 | import org.osgl.inject.annotation.LoadCollection; 25 | 26 | import java.lang.annotation.Documented; 27 | import java.lang.annotation.Retention; 28 | import java.lang.annotation.RetentionPolicy; 29 | 30 | @Documented 31 | @Retention(RetentionPolicy.RUNTIME) 32 | @InjectTag 33 | @LoadCollection(FibonacciSeriesLoader.class) 34 | public @interface FibonacciSeries { 35 | /** 36 | * The max value in the series 37 | * @return the max value 38 | */ 39 | int max() default 100; 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/FibonacciSeriesHolder.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | 24 | import org.osgl.util.S; 25 | 26 | import javax.inject.Inject; 27 | import java.util.List; 28 | 29 | public class FibonacciSeriesHolder { 30 | 31 | @FibonacciSeries(max = 20) 32 | private List series; 33 | 34 | @Override 35 | public String toString() { 36 | return S.join(",", series); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/FibonacciSeriesHolder2.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | 24 | import org.osgl.util.C; 25 | import org.osgl.util.S; 26 | 27 | import javax.inject.Inject; 28 | import java.util.List; 29 | 30 | public class FibonacciSeriesHolder2 { 31 | 32 | @FibonacciSeries(max = 20) 33 | private int[] series; 34 | 35 | @Override 36 | public String toString() { 37 | return S.join(",", C.listOf(series)); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/FibonacciSeriesHolder3.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | 24 | import org.osgl.inject.annotation.LoadCollection; 25 | import org.osgl.util.S; 26 | 27 | import javax.inject.Inject; 28 | import java.util.List; 29 | 30 | public class FibonacciSeriesHolder3 { 31 | 32 | @LoadCollection(FibonacciSeriesLoader.class) 33 | private List series; 34 | 35 | @Override 36 | public String toString() { 37 | return S.join(",", series); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/GreetingService.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | public class GreetingService { 24 | 25 | @Uppercase("hElLo WoRlD") 26 | private String upper; 27 | 28 | @Lowercase("hElLo WoRlD") 29 | private String lower; 30 | 31 | public String sayHello(String caller) { 32 | return String.format("%s (%s), %s", upper, lower, caller); 33 | } 34 | 35 | public static void main(String[] args) { 36 | Genie genie = Genie.create(); 37 | GreetingService service = genie.get(GreetingService.class); 38 | System.out.println(service.sayHello("Genie")); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/InheritedStateless.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import java.lang.annotation.ElementType; 24 | import java.lang.annotation.*; 25 | 26 | /** 27 | * Mark a Class is stateless in the context of ActFramework. 28 | * 29 | * Note this annotation **is** inherited 30 | * 31 | * See https://github.com/actframework/actframework/issues/161 32 | */ 33 | @Retention(RetentionPolicy.RUNTIME) 34 | @Inherited 35 | @Target({ElementType.TYPE, ElementType.FIELD}) 36 | public @interface InheritedStateless { 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/InternalErrorHandler.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Named; 24 | import javax.inject.Singleton; 25 | 26 | /** 27 | * Handle 500 28 | */ 29 | @Singleton 30 | @Named("internal-error") 31 | public class InternalErrorHandler extends ErrorHandler { 32 | @Override 33 | public int errorCode() { 34 | return 500; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/Leather.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 24 | 25 | @java.lang.annotation.Documented 26 | @java.lang.annotation.Retention(RUNTIME) 27 | @javax.inject.Qualifier 28 | public @interface Leather { 29 | Color color() default Color.TAN; 30 | enum Color { 31 | RED () { 32 | @Override 33 | public LeatherSmoother smoother() { 34 | return new LeatherSmoother.RedLeatherSmoother(); 35 | } 36 | }, BLACK() { 37 | @Override 38 | public LeatherSmoother smoother() { 39 | return new LeatherSmoother.BlackLeatherSmoother(); 40 | } 41 | }, TAN() { 42 | @Override 43 | public LeatherSmoother smoother() { 44 | return new LeatherSmoother.TanLeatherSmoother(); 45 | } 46 | }; 47 | 48 | public abstract LeatherSmoother smoother(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/LeatherSmoother.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.Provides; 24 | 25 | import javax.inject.Inject; 26 | 27 | public interface LeatherSmoother { 28 | 29 | class RedLeatherSmoother implements LeatherSmoother {} 30 | 31 | class BlackLeatherSmoother implements LeatherSmoother {} 32 | 33 | class TanLeatherSmoother implements LeatherSmoother {} 34 | 35 | class Module { 36 | @Leather(color = Leather.Color.RED) 37 | @Provides 38 | public static LeatherSmoother red(RedLeatherSmoother smoother) { 39 | return smoother; 40 | } 41 | @Leather(color = Leather.Color.BLACK) 42 | @Provides 43 | public static LeatherSmoother black(BlackLeatherSmoother smoother) { 44 | return smoother; 45 | } 46 | @Leather(color = Leather.Color.TAN) 47 | @Provides 48 | public static LeatherSmoother redOne(TanLeatherSmoother smoother) { 49 | return smoother; 50 | } 51 | } 52 | 53 | class DynamicModule { 54 | @Provides 55 | public static LeatherSmoother find(BeanSpec spec) { 56 | Leather leather = spec.getAnnotation(Leather.class); 57 | if (null != leather) { 58 | return leather.color().smoother(); 59 | } 60 | return null; 61 | } 62 | } 63 | 64 | class Host { 65 | LeatherSmoother smoother; 66 | 67 | @Inject 68 | public Host(@Leather(color = Leather.Color.RED) LeatherSmoother smoother) { 69 | this.smoother = smoother; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/Lowercase.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.InjectTag; 24 | import org.osgl.inject.annotation.LoadValue; 25 | 26 | import javax.inject.Qualifier; 27 | import java.lang.annotation.Retention; 28 | import java.lang.annotation.RetentionPolicy; 29 | 30 | @Qualifier 31 | @InjectTag 32 | @LoadValue(Transformers.ToLowerCase.class) 33 | @Retention(RetentionPolicy.RUNTIME) 34 | public @interface Lowercase { 35 | String value() default ""; 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/ModuleWithBindings.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | class ModuleWithBindings extends Module { 24 | 25 | @Override 26 | protected void configure() { 27 | bind(Person.class).to(Person.Man.class); 28 | bind(Person.class).qualifiedWith(Person.Female.class).to(Person.Woman.class); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/ModuleWithConstructorBinding.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.Provides; 24 | 25 | import javax.inject.Provider; 26 | 27 | public class ModuleWithConstructorBinding extends Module { 28 | 29 | @Override 30 | protected void configure() { 31 | bind(Person.class).toConstructor(Person.Flexible.class, Provider.class); 32 | } 33 | 34 | @Provides 35 | public Person.Gender gender() { 36 | return Person.Gender.F; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/ModuleWithFactories.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.Provides; 24 | 25 | class ModuleWithFactories { 26 | 27 | @Provides 28 | public Person male(Person.Man man) { 29 | return man; 30 | } 31 | 32 | @Provides 33 | @Person.Female 34 | public static Person female(Person.Woman woman) { 35 | return woman; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/ModuleWithNamedBindings.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.Provides; 24 | 25 | import javax.inject.Named; 26 | 27 | class ModuleWithNamedBindings extends Module { 28 | 29 | @Override 30 | protected void configure() { 31 | bind(Person.class).named("male").to(Person.Man.class); 32 | } 33 | 34 | @Provides 35 | @Named("female") 36 | public Person female(Person.Woman woman) { 37 | return woman; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/ModuleWithStaticFactories.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.Provides; 24 | 25 | abstract class ModuleWithStaticFactories { 26 | 27 | @Provides 28 | public static Person male(Person.Man man) { 29 | return man; 30 | } 31 | 32 | @Provides 33 | @Person.Female 34 | public static Person female(Person.Woman woman) { 35 | return woman; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/MultipleConstructors.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | 25 | /** 26 | * Test inject class with multiple constructors of which only one has Inject annotation 27 | */ 28 | public class MultipleConstructors { 29 | 30 | private String id; 31 | private Order order; 32 | 33 | public MultipleConstructors() { 34 | 35 | } 36 | 37 | public MultipleConstructors(String id) { 38 | this.id = id; 39 | } 40 | 41 | @Inject 42 | public MultipleConstructors(Order order) { 43 | this.order = order; 44 | } 45 | 46 | public boolean hasId() { 47 | return null != id; 48 | } 49 | 50 | public boolean hasOrder() { 51 | return null != order; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/NotFoundHandler.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Named; 24 | import javax.inject.Singleton; 25 | 26 | /** 27 | * Handle 404 28 | */ 29 | @Singleton 30 | @Named("not-found") 31 | public class NotFoundHandler extends ErrorHandler { 32 | @Override 33 | public int errorCode() { 34 | return 404; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/OddFibonacciSeriesHolder.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | 24 | import org.osgl.inject.annotation.Filter; 25 | import org.osgl.util.C; 26 | import org.osgl.util.S; 27 | 28 | import javax.inject.Inject; 29 | import java.util.List; 30 | 31 | public class OddFibonacciSeriesHolder { 32 | 33 | @FibonacciSeries 34 | @Filter(value = EvenNumberFilter.class, reverse = true) 35 | Integer[] series; 36 | 37 | @Override 38 | public String toString() { 39 | return S.join(",", C.listOf(series)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/Order.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | class Order { 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/OrderService.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | 25 | public class OrderService { 26 | @Inject Dao dao; 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/OverwriteBaseWithPostConstructor.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | 25 | class OverwriteBaseWithPostConstructor extends BaseWithPostConstructor { 26 | 27 | @Override 28 | protected void init() { 29 | // Do nothing here 30 | } 31 | 32 | static class Holder { 33 | @Inject 34 | BaseWithPostConstructor bean; 35 | } 36 | 37 | static class Module extends org.osgl.inject.Module { 38 | @Override 39 | protected void configure() { 40 | bind(BaseWithPostConstructor.class).to(OverwriteBaseWithPostConstructor.class); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/Person.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | import javax.inject.Provider; 25 | import javax.inject.Qualifier; 26 | import java.lang.annotation.Retention; 27 | import java.lang.annotation.RetentionPolicy; 28 | 29 | import static org.osgl.inject.Person.Gender.F; 30 | import static org.osgl.inject.Person.Gender.M; 31 | 32 | public interface Person { 33 | 34 | @Qualifier 35 | @Retention(RetentionPolicy.RUNTIME) 36 | @interface Female { 37 | } 38 | 39 | enum Gender { 40 | M, F; 41 | public boolean isFemale() { 42 | return this == F; 43 | } 44 | } 45 | 46 | Gender gender(); 47 | 48 | class Man implements Person { 49 | @Override 50 | public Gender gender() { 51 | return M; 52 | } 53 | } 54 | 55 | class Woman implements Person { 56 | @Override 57 | public Gender gender() { 58 | return F; 59 | } 60 | } 61 | 62 | class Flexible implements Person { 63 | 64 | private Gender gender; 65 | 66 | public Flexible(Provider gender) { 67 | this.gender = gender.get(); 68 | } 69 | 70 | @Override 71 | public Gender gender() { 72 | return gender; 73 | } 74 | } 75 | 76 | class Family { 77 | 78 | @Inject 79 | Person dad; 80 | 81 | @Inject 82 | @Female 83 | Person mom; 84 | 85 | @Female 86 | Person daughter; 87 | 88 | Person son; 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/RandomList.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.LoadValue; 24 | 25 | import java.lang.annotation.ElementType; 26 | import java.lang.annotation.Retention; 27 | import java.lang.annotation.RetentionPolicy; 28 | import java.lang.annotation.Target; 29 | 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Target({ElementType.FIELD, ElementType.PARAMETER}) 32 | @LoadValue(RandomListLoader.class) 33 | public @interface RandomList { 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/RandomListHolder.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | import java.util.List; 25 | 26 | public class RandomListHolder { 27 | 28 | @Inject 29 | @RandomList 30 | private List list; 31 | 32 | public List list() { 33 | return list; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/RandomListLoader.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.util.C; 24 | import org.osgl.util.N; 25 | 26 | import java.util.List; 27 | import java.util.Map; 28 | 29 | public class RandomListLoader extends ValueLoader.Base> { 30 | 31 | @Override 32 | public List get() { 33 | List list = C.newList(); 34 | for (int i = 0; i < 10; ++i) { 35 | list.add(N.randInt(100)); 36 | } 37 | return list; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/SessionPostConstruct.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.SessionScoped; 24 | 25 | import javax.annotation.PostConstruct; 26 | import javax.inject.Singleton; 27 | import java.util.concurrent.atomic.AtomicInteger; 28 | 29 | @SessionScoped 30 | class SessionPostConstruct { 31 | 32 | static final AtomicInteger instances = new AtomicInteger(0); 33 | 34 | @PostConstruct 35 | void init() { 36 | instances.incrementAndGet(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/SimpleConstructorInjection.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | 25 | class SimpleConstructorInjection { 26 | 27 | SimpleEmptyConstructor foo; 28 | 29 | @Inject 30 | SimpleConstructorInjection(SimpleEmptyConstructor foo) { 31 | this.foo = foo; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return getClass().getSimpleName(); 37 | } 38 | 39 | public SimpleEmptyConstructor foo() { 40 | return foo; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/SimpleConstructorInjectionByProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | import javax.inject.Provider; 25 | 26 | class SimpleConstructorInjectionByProvider { 27 | 28 | private SimpleEmptyConstructor foo; 29 | 30 | @Inject 31 | public void initFoo(Provider foo) { 32 | this.foo = foo.get(); 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return getClass().getSimpleName(); 38 | } 39 | 40 | public SimpleEmptyConstructor foo() { 41 | return foo; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/SimpleEmptyConstructor.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | class SimpleEmptyConstructor { 24 | SimpleEmptyConstructor() { 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return getClass().getSimpleName(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/SimpleFieldInjection.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | 25 | class SimpleFieldInjection { 26 | 27 | @Inject 28 | SimpleEmptyConstructor foo; 29 | 30 | @Override 31 | public String toString() { 32 | return getClass().getSimpleName(); 33 | } 34 | 35 | public SimpleEmptyConstructor foo() { 36 | return foo; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/SimpleMethodInjection.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | 25 | class SimpleMethodInjection { 26 | 27 | private SimpleEmptyConstructor foo; 28 | 29 | @Inject 30 | public void initFoo(SimpleEmptyConstructor foo) { 31 | this.foo = foo; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return getClass().getSimpleName(); 37 | } 38 | 39 | public SimpleEmptyConstructor foo() { 40 | return foo; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/SimpleMethodInjectionByProvider.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | import javax.inject.Provider; 25 | 26 | class SimpleMethodInjectionByProvider { 27 | 28 | private SimpleEmptyConstructor foo; 29 | 30 | @Inject 31 | public void initFoo(Provider foo) { 32 | this.foo = foo.get(); 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return getClass().getSimpleName(); 38 | } 39 | 40 | public SimpleEmptyConstructor foo() { 41 | return foo; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/SimpleTypeElementLoader.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.loader.TypedElementLoader; 24 | import org.osgl.util.C; 25 | 26 | import java.util.List; 27 | 28 | /** 29 | * Emulate Type discovery mechanism in IoC container 30 | */ 31 | public class SimpleTypeElementLoader extends TypedElementLoader { 32 | @Override 33 | protected List load(Class type, boolean loadNonPublic, boolean loadAbstract, boolean loadRoot) { 34 | if (type == ErrorHandler.class) { 35 | return (List)C.list(NotFoundHandler.class, InternalErrorHandler.class); 36 | } 37 | return C.list(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/SingletonPostConstruct.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.annotation.PostConstruct; 24 | import javax.inject.Singleton; 25 | import java.util.concurrent.atomic.AtomicInteger; 26 | 27 | @Singleton 28 | class SingletonPostConstruct { 29 | 30 | static final AtomicInteger instances = new AtomicInteger(0); 31 | 32 | @PostConstruct 33 | void init() { 34 | instances.incrementAndGet(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/Stateful.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import java.lang.annotation.ElementType; 24 | import java.lang.annotation.Retention; 25 | import java.lang.annotation.RetentionPolicy; 26 | import java.lang.annotation.Target; 27 | 28 | /** 29 | * Mark a Class is stateful in the context of ActFramework. 30 | * 31 | * This annotation is used to terminate the stateless declaration introduced by 32 | * parent class's {@link InheritedStateless} annotation 33 | * 34 | * Note this annotation is **NOT** inherited 35 | * 36 | * See https://github.com/actframework/actframework/issues/223 37 | */ 38 | @Retention(RetentionPolicy.RUNTIME) 39 | @Target({ElementType.TYPE, ElementType.FIELD}) 40 | public @interface Stateful { 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/TomAndJen.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | 24 | import javax.inject.Inject; 25 | import javax.inject.Named; 26 | 27 | class TomAndJen { 28 | 29 | @Inject 30 | @Named("male") 31 | Person tom; 32 | 33 | @Inject 34 | @Named("female") 35 | Person jen; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/Transformers.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.util.S; 24 | 25 | public class Transformers { 26 | public static class ToUpperCase extends ValueLoader.Base { 27 | @Override 28 | public String get() { 29 | return S.string(this.value()).toUpperCase(); 30 | } 31 | } 32 | public static class ToLowerCase extends ValueLoader.Base { 33 | @Override 34 | public String get() { 35 | return S.string(this.value()).toLowerCase(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/Uppercase.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.osgl.inject.annotation.InjectTag; 24 | import org.osgl.inject.annotation.LoadValue; 25 | 26 | import javax.inject.Qualifier; 27 | import java.lang.annotation.Retention; 28 | import java.lang.annotation.RetentionPolicy; 29 | 30 | @Qualifier 31 | @InjectTag 32 | @LoadValue(Transformers.ToUpperCase.class) 33 | @Retention(RetentionPolicy.RUNTIME) 34 | public @interface Uppercase { 35 | String value() default ""; 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/User.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | public class User { 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/UserService.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | 25 | public class UserService { 26 | @Inject Dao dao; 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/ValueLoaderAndQualifiers.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * OSGL Genie 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import javax.inject.Inject; 24 | import java.util.List; 25 | 26 | public class ValueLoaderAndQualifiers { 27 | 28 | 29 | @Inject 30 | @FibonacciSeries 31 | @RandomList 32 | private List list; 33 | 34 | public List list() { 35 | return list; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/osgl/inject/VersionTest.java: -------------------------------------------------------------------------------- 1 | package org.osgl.inject; 2 | 3 | /*- 4 | * #%L 5 | * Java Unit Test Tool 6 | * %% 7 | * Copyright (C) 2017 OSGL (Open Source General Library) 8 | * %% 9 | * Licensed under the Apache License, Version 2.0 (the "License"); 10 | * you may not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * #L% 21 | */ 22 | 23 | import org.junit.Test; 24 | import osgl.ut.TestBase; 25 | 26 | public class VersionTest extends TestBase { 27 | 28 | @Test 29 | public void itShallReportGenieVersion() { 30 | yes(Genie.VERSION.toString().contains("genie")); 31 | } 32 | 33 | } 34 | --------------------------------------------------------------------------------