├── .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 | [](http://www.apache.org/licenses/LICENSE-2.0.html)
4 | [](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22genie%22)
5 | [](https://travis-ci.org/osglworks/java-di)
6 | [](https://codecov.io/gh/osglworks/java-di)
7 | [](http://genie.readthedocs.io/en/latest/?badge=latest)
8 | [](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 [](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 extends Annotation> 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 extends ElementFilter> 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 extends ElementLoader> 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 extends ValueLoader> 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 extends KeyExtractor> 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 | *
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 extends PostConstructProcessor> 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 extends BeanTransformer> 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