Dynamic Context Dependency Injection

Overview

Welcome to the Project Dynamic CDI

Maven Central Codacy Badge

JDK Informations

Actual version is running from JDK8 up to JDK13. The version 1.0.x is based on JDK8.

The implementation is covered with over 90% mutation test coverage. The last report you can find under _docu/

_docu/20180319/pit_overview.png

How DDI will resolve the corresponding Implementation?

if you write @Inject Interface

1 ClassResolver means: 1 ClassResolver responsible for the given Interface. You can have n ClassResolver inside your classpath. But for every Interface only one that is responsible for it.

  • Interface , no Impl. -> Exception

  • Interface , no Impl., 1 Producer for the Interface -> Producer for the Interface will be used

  • Interface , 1 Impl. -> will use the Impl.

  • Interface , 1 Impl., 1 Producer for Impl. -> Producer for the Impl will be used

  • Interface , 1 Impl., n Producer for Impl. -> Exception

  • Interface , 1 Impl., 1 Producer for Interface -> Producer for the Interface will be used

  • Interface , 1 Impl., n Producer for Interface -> Exception

  • Interface , 1 Impl., 1 Producer for Interface , 1 Producer for Impl. -> Exception

  • Interface , n Impl. -> Exception

  • Interface , n Impl., 1 responsible ClassResolver -> result of the ClassResolver will be used

  • Interface , n Impl., 1 Producer for Interface -> Producer for the Interface will be used

  • Interface , n Impl., 1-n Producer for Impl. -> Exception

  • Interface , n Impl., 1 responsible ClassResolver, 1 Producer for Interface -> Producer for the Interface will be used

  • Interface , n Impl., 1 responsible ClassResolver, 1-n Producer for Impl. -> will use the resolved Class or corresponding Producer if available

  • Interface , n Impl., n responsible ClassResolver -> Exception

  • Interface, 1 Impl., n Producer for Impl, 1 ProducerResolver for Impl -> selected Producer from ProducerResolver

  • Interface, n Impl., 1 responsible ClassResolver, 0-n Producer for every Impl, 1 ProducerResolver for every Impl -> selected Producer from ProducerResolver, for selected Impl from ClassResolver

if you write @Inject Impl.

  • Impl. -> will use the Impl.
  • Impl., 1 Producer -> will use the Producer for the Impl.

How to write Mocks for Interfaces or classes ?

You can use this framework for the mocking too. The concept is based on the Designpattern ObjectAdapter. You can have too versions of an ObjectAdapter , based on the implementations the ProxyBuilder will give you. Version one is a DynamicObjectAdapter. This you could only use if you have an interface available. The DynamicObjectAdapterBuilder will be created during the compile phase if you annotate the corresponding interface with the Annotation @DynamicObjectAdapterBuilder. Please check the documentation about the DynamicObjectAdapter for more details.

Version two is a StaticObjectAdapter. This you could use for Interfaces and Classes. To create a StaticObjectAdapter for your target you have to annotate the the target class or interface with the Annotation @StaticObjectAdapter. After the compile phase you could find the generated classes at target/generated-sources (if you are using maven).

Mocking

If you want to use a Mock for an jUnitTest, you have to use a ClassResolver or ProducerResolver to make the decision for the Mock explicit for your test. The easiest way is the usage of an Producer that will create the Mock. With this you could mock something that is deep inside your system, without creating the full hierarchy with frameworks like Mockito. Mostly this will lead to constructions that are much more like the production system.

example with one interface and one production class

Inside the production source path you will have an interface and one implementation.

  public interface Service {
    String doWork(String txt);
  }

  public static class ServiceA implements Service {
    public String doWork(String txt) {
      return txt + "A";
    }
  }

During the runtime the solution is very simple, because there is only on implementation available. But now we would like to activate a Mock during the jUnit test. For this we need a second implementation called ServiceB

 public static class ServiceB implements Service {
    public String doWork(String txt) {
      return txt + "B";
    }
  }

To activate this mock only for the jUnit Tests we have to config the DI Container in a way that the ServiceB will be available. This means, during the startup phase @Before we are clearing the existing Reflection Model to make sure there is no other config from any other test anymore. After this we re activating only production classes and the package from the test itself.

  @Before
  public void setUp() throws Exception {
    DI.clearReflectionModel();
    DI.activatePackages("org.rapidpm");
    DI.activatePackages(this.getClass());
  }

Now we have the mock inside the reflection model, too. This means we have to give the container a possibility to resolve this. For this we have to create a ClassResolver. This will be instantiated and asked for the solution.

  @ResponsibleFor(Service.class)
  public static class ServiceClassResolver implements ClassResolver<Service> {
    @Override
    public ClassService> resolve(final Class<Service> interf) {
      return ServiceB.class;
    }
  }

Now the container is able to ask the ClassResolver, what will be the right implementation to use. In our case, the mock.

Here you can see, one of the jUnit Tests that we are using to test the ClassResolver. ClassResolverTest014.java

{ @Override public Class resolve(final Class interf) { toggle = !toggle; System.out.println("toggle = " + toggle); return (toggle) ? ServiceA.class : ServiceB.class; } }">
  @Before
  public void setUp() throws Exception {
    DI.clearReflectionModel();
    DI.activatePackages("org.rapidpm");
    DI.activatePackages(this.getClass());
  }

  public static Boolean toggle = true;

  @Test
  public void test001() throws Exception {
    Assert.assertEquals(ServiceB.class, DI.activateDI(Service.class).getClass());
    Assert.assertEquals(ServiceA.class, DI.activateDI(Service.class).getClass());
    Assert.assertEquals(ServiceB.class, DI.activateDI(Service.class).getClass());
    DI.clearReflectionModel();
  }

  public interface Service {
    String doWork(String txt);
  }

  public static class ServiceA implements Service {
    public String doWork(String txt) {
      return txt + "A";
    }
  }

  public static class ServiceB implements Service {
    public String doWork(String txt) {
      return txt + "B";
    }
  }

  @ResponsibleFor(Service.class)
  public static class ServiceClassResolver implements ClassResolver<Service> {
    @Override
    public ClassService> resolve(final Class<Service> interf) {
      toggle = !toggle;
      System.out.println("toggle = " + toggle);
      return (toggle) ? ServiceA.class : ServiceB.class;
    }
  }
You might also like...

DataFX - is a JavaFX frameworks that provides additional features to create MVC based applications in JavaFX by providing routing and a context for CDI.

What you’ve stumbled upon here is a project that intends to make retrieving, massaging, populating, viewing, and editing data in JavaFX UI controls ea

Dec 29, 2022

Distributed Tracing, Metrics and Context Propagation for application running on the JVM

Kamon Kamon is a set of tools for instrumenting applications running on the JVM. The best way to get started is to go to our official Get Started Page

Dec 25, 2022

A free injection hacked client for Minecraft using Java-agents

Swift Swift is a free and open-source injection hacked client base for Minecraft using Java-agents. Issues If you notice any bugs, you can let us know

Oct 8, 2022

A free mixin-based injection hacked-client for Minecraft using Minecraft Forge.

Custom LiquidBounce 1.8.9 build that aims to improve original visuals and bypasses, along with built-in ViaVersion to help you change from 1.8 to 1.17.1 without creating any other version branch.

Jan 2, 2023

A free mixin-based injection hacked client for Minecraft 1.8.9

LiquidCat is a free and open source mixin-based injection hacked client using Forge for Minecraft 1.8.9

Nov 18, 2022

A injection client for Minecraft 1.8.9forge,forked and optimize on VapuLite

A injection client for Minecraft 1.8.9forge,forked and optimize on VapuLite

May 8, 2022

A 1.12.2 forge injection based hacked client for anarchy servers

nebula another failed abortion of inferno and the aftermath of gavin at coding also is an anarchy utility client, i think thats important to point out

Dec 24, 2022

A lightweight, mixin like injection lib using ASM

ClassTransform A lightweight, mixin like injection lib using ASM. The usage is like Mixins. You can almost copy-paste mixins code and it works. Why? I

Dec 22, 2022

A fast dependency injector for Android and Java.

Dagger A fast dependency injector for Java and Android. Dagger is a compile-time framework for dependency injection. It uses no reflection or runtime

Dec 30, 2022

This open source project allows you to easily integrate Camunda's External Task Clients into Micronaut projects: simply add a dependency in your Micronaut project

micronaut-camunda-external-client This open source project allows you to easily integrate Camunda 's External Task Clients into Micronaut projects. Mi

Dec 18, 2022

Sauron, the all seeing eye! It is a service to generate automated reports and track migrations, changes and dependency versions for backend services also report on known CVE and security issues.

Sauron, the all seeing eye! It is a service to generate automated reports and track migrations, changes and dependency versions for backend services also report on known CVE and security issues.

SAURON - VERSION AND DEPLOYMENT TRACKER DESCRIPTION Sauron, the all seeing eye! It is a service to generate automated reports and track migrations, ch

Oct 31, 2022

Zero-dependency Reactive Streams publishers library

⚡️ Mutiny Zero: a zero-dependency Reactive Streams publishers library for Java Mutiny Zero is a minimal API for creating reactive-streams compliant pu

Dec 14, 2022

PotPvP 1.8 with CarbonSpigot as its dependency

PotPvP Reprised A fork of PotPvP, porting it to 1.8 and changing mSpigot's dependency to CarbonSpigot. Information This fork has also changed all APIs

Nov 30, 2022

Zero-Dependency RFC 8252 OAuth 2.0 Authorization Flow

Tiny OAuth2 Client This is a minimal zero-dependency implementation of the RFC 8252 OAuth 2.0 for Native Apps, relying on Loopback Interface Redirecti

Jun 17, 2022

cglib - Byte Code Generation Library is high level API to generate and transform Java byte code. It is used by AOP, testing, data access frameworks to generate dynamic proxy objects and intercept field access.

cglib Byte Code Generation Library is high level API to generate and transform JAVA byte code. It is used by AOP, testing, data access frameworks to g

Jan 8, 2023

Vibur DBCP - concurrent and dynamic JDBC connection pool

Vibur DBCP is concurrent, fast, and fully-featured JDBC connection pool, which provides advanced performance monitoring capabilities, including slow S

Apr 20, 2022

Dynamic Code Evolution VM for Java 7/8

NEWS: Dcevm-11 on Trava OpenJDK There is a new distribution channel for DCEVM-11 binaries on - TravaOpenjdk! DCEVM This project is a fork of original

Dec 28, 2022

Zuul is a gateway service that provides dynamic routing, monitoring, resiliency, security, and more.

Zuul is a gateway service that provides dynamic routing, monitoring, resiliency, security, and more.

Zuul Zuul is an L7 application gateway that provides capabilities for dynamic routing, monitoring, resiliency, security, and more. Please view the wik

Jan 3, 2023
Comments
  • switching resolving strategy

    switching resolving strategy

    switching from

    • Interface , 1 Impl., 1 Producer for Interface , 1 Producer for Impl. -> Exception

    to

    • Interface , 1 Impl., 1 Producer for Interface , 1 Producer for Impl. -> Producerl for Interface will be used
    enhancement 
    opened by svenruppert 1
  • Add a Gitter chat badge to README.md

    Add a Gitter chat badge to README.md

    RapidPM/dynamic-cdi now has a Chat Room on Gitter

    @svenruppert has just created a chat room. You can visit it here: https://gitter.im/RapidPM/dynamic-cdi.

    This pull-request adds this badge to your README.md:

    Gitter

    If my aim is a little off, please let me know.

    Happy chatting.

    PS: Click here if you would prefer not to receive automatic pull-requests from Gitter in future.

    opened by gitter-badger 0
  • Configure WhiteSource Bolt for GitHub

    Configure WhiteSource Bolt for GitHub

    Welcome to WhiteSource Bolt for GitHub! This is an onboarding PR to help you understand and configure settings before WhiteSource starts scanning your repository for security vulnerabilities.

    :vertical_traffic_light: WhiteSource Bolt for GitHub will start scanning your repository only once you merge this Pull Request. To disable WhiteSource Bolt for GitHub, simply close this Pull Request.


    What to Expect

    This PR contains a '.whitesource' configuration file which can be customized to your needs. If no changes were applied to this file, WhiteSource Bolt for GitHub will use the default configuration.

    Before merging this PR, Make sure the Issues tab is enabled. Once you merge this PR, WhiteSource Bolt for GitHub will scan your repository and create a GitHub Issue for every vulnerability detected in your repository.

    If you do not want a GitHub Issue to be created for each detected vulnerability, you can edit the '.whitesource' file and set the 'minSeverityLevel' parameter to 'NONE'.


    :question: Got questions? Check out WhiteSource Bolt for GitHub docs. If you need any further assistance then you can also request help here.

    opened by mend-bolt-for-github[bot] 0
Owner
Sven Ruppert
@java since 1996, Oracle Developer Champion (@Dev_Champions), Developer Advocate @jfrog and Speaker, Helping developers world-wide to grow their business.
Sven Ruppert
Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 6 and above, brought to you by Google.

Guice Latest release: 5.0.1 Documentation: User Guide, 5.0.1 javadocs, Latest javadocs Continuous Integration: Mailing Lists: User Mailing List Licens

Google 11.7k Dec 29, 2022
JayWire Dependency Injection

JayWire Dependency Injection A very small and lightweight dependency injection framework for Java 8 without magic. Main features are: 100% pure Java c

VanillaSource 52 Jul 13, 2022
A fast dependency injector for Android and Java.

Dagger A fast dependency injector for Java and Android. Dagger is a compile-time framework for dependency injection. It uses no reflection or runtime

Google 16.9k Dec 30, 2022
A light-weight and dynamic dependency injection framework

⚠️ This project is now part of the EE4J initiative. This repository has been archived as all activities are now happening in the corresponding Eclipse

Java EE 105 Dec 23, 2022
Lightweight dependency injection for Java and Android (JSR-330)

About Feather Feather is an ultra-lightweight dependency injection (JSR-330) library for Java and Android. Dependency injection frameworks are often p

Zsolt Herpai 341 Nov 29, 2022
Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 6 and above, brought to you by Google.

Guice Latest release: 5.0.1 Documentation: User Guide, 5.0.1 javadocs, Latest javadocs Continuous Integration: Mailing Lists: User Mailing List Licens

Google 11.7k Dec 29, 2022
JayWire Dependency Injection

JayWire Dependency Injection A very small and lightweight dependency injection framework for Java 8 without magic. Main features are: 100% pure Java c

VanillaSource 52 Jul 13, 2022
Spring Kurulumundan Başlayarak, Spring IOC ve Dependency Injection, Hibernate, Maven ve Spring Boot Konularına Giriş Yapıyoruz.

Spring Tutorial for Beginners File Directory Apache Tomcat Apache Tomcat - Eclipse Bağlantısı Spring Paketlerinin İndirilmesi ve Projeye Entegrasyonu

İbrahim Can Erdoğan 11 Apr 11, 2022
JavaFX micro-framework that follows MVVM Pattern with Google Guice dependency Injection

ReactiveDeskFX (JavaFX and Google Guice MVVM Pattern micro-framework) JavaFX micro-framework to develop very fast JavaFX components with minimal code

TangoraBox 3 Jan 9, 2022
Yet another Java annotation-based command parsing library, with Dependency Injection inspired by Guice

Commander A universal java command parsing library Building This project uses Gradle. Clone this repository: git clone https://github.com/OctoPvP/Comm

OctoDev 4 Oct 2, 2022