Skip to content

Commit

Permalink
Support @typed, align @nAmed
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jan 25, 2024
1 parent 256570d commit c046895
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.maven.api.di;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

Expand All @@ -26,4 +27,5 @@

@Target({FIELD, PARAMETER, METHOD})
@Retention(RUNTIME)
@Documented
public @interface Eager {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
*/
package org.apache.maven.api.di;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({FIELD, CONSTRUCTOR, METHOD, TYPE})
@Target({FIELD, CONSTRUCTOR, METHOD})
@Retention(RUNTIME)
@Documented
public @interface Inject {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
*/
package org.apache.maven.api.di;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Qualifier
@Target({FIELD, PARAMETER, METHOD})
@Retention(RUNTIME)
@Documented
public @interface Named {
String value() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.maven.api.di;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

Expand All @@ -26,4 +27,5 @@

@Target(METHOD)
@Retention(RUNTIME)
@Documented
public @interface Provides {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.maven.api.di;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

Expand All @@ -26,4 +27,5 @@

@Target(ANNOTATION_TYPE)
@Retention(RUNTIME)
@Documented
public @interface Qualifier {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
*/
package org.apache.maven.api.di;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Retention(RUNTIME)
@Target(ANNOTATION_TYPE)
@Retention(RUNTIME)
@Documented
public @interface Scope {
boolean threadsafe() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.maven.api.di;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

Expand All @@ -26,4 +27,5 @@

@Target(METHOD)
@Retention(RUNTIME)
@Documented
public @interface Transient {}
33 changes: 33 additions & 0 deletions api/maven-api-di/src/main/java/org/apache/maven/api/di/Typed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.di;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({FIELD, METHOD, TYPE})
@Retention(RUNTIME)
@Documented
public @interface Typed {
Class<?>[] value() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import java.util.Map;
import java.util.stream.Collectors;

import com.google.inject.Provides;
import org.apache.maven.internal.impl.DefaultLookup;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.testing.PlexusExtension;
import org.codehaus.plexus.testing.PlexusTest;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import java.util.stream.Stream;

import org.apache.maven.api.annotations.Nullable;
import org.apache.maven.api.di.Typed;
import org.apache.maven.di.*;
import org.apache.maven.di.binding.*;
import org.apache.maven.di.module.UniqueQualifierImpl;
import org.apache.maven.di.util.ReflectionUtils;
import org.apache.maven.di.util.Trie;
import org.apache.maven.di.util.Types;
import org.apache.maven.di.util.Utils;

import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -185,8 +187,49 @@ private static void reduce(
//noinspection rawtypes
Binding<?> transformed = ((BindingTransformer) transformer).transform(recursiveLocator, scope, key, binding);

// For non-explicit bindings, also bind all their base classes and interfaces according to the @Type
Set<Key<?>> done = new HashSet<>();
if (transformed.getType() == BindingType.SYNTHETIC) {
Deque<Key<?>> todo = new ArrayDeque<>();
todo.add(key);

Set<Class<?>> types;
Typed typed = key.getRawType().getAnnotation(Typed.class);
if (typed != null) {
Class<?>[] typesArray = typed.value();
if (typesArray == null || typesArray.length == 0) {
types = new HashSet<>(Arrays.asList(key.getRawType().getInterfaces()));
types.add(Object.class);
} else {
types = new HashSet<>(Arrays.asList(typesArray));
}
} else {
types = null;
}

while (!todo.isEmpty()) {
Key<?> type = todo.remove();
if (done.add(type)) {
Class<?> cls = Types.getRawType(type.getType());
if (types != null && !types.contains(cls)) {
continue;
}
Type[] interfaces = cls.getGenericInterfaces();
Arrays.stream(interfaces)
.map(t -> Key.ofType(t, key.getQualifier()))
.forEach(todo::add);
Type supercls = cls.getGenericSuperclass();
if (supercls != null) {
todo.add(Key.ofType(supercls, key.getQualifier()));
}
}
}
} else {
done.add(key);
}

// and store it as resolved (also mark as visited)
resolvedBindings.put(key, transformed);
done.forEach(k -> resolvedBindings.put(k, transformed));

// and then recursively walk over its dependencies (so this is a recursive dfs after all)
for (Key<?> d : transformed.getDependencies()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.maven.di.module;

import org.apache.maven.api.di.Eager;
import org.apache.maven.api.di.Transient;
import org.apache.maven.di.binding.BindingType;

public interface ModuleBuilder1<T> extends ModuleBuilder0<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public static <T extends AnnotatedElement & Member> List<T> getAnnotatedElements
public static <T> @Nullable Binding<T> generateConstructorBinding(Key<T> key) {
Class<?> cls = key.getRawType();

Inject classInjectAnnotation = cls.getAnnotation(Inject.class);
Named classInjectAnnotation = cls.getAnnotation(Named.class);
List<Constructor<?>> constructors = Arrays.asList(cls.getDeclaredConstructors());
List<Constructor<?>> injectConstructors = constructors.stream()
.filter(c -> c.isAnnotationPresent(Inject.class))
Expand Down
52 changes: 39 additions & 13 deletions maven-di/src/test/java/org/apache/maven/di/TestDI.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class ClassWithCustomDeps {
String string;
}

@Inject
@Named
class Inherited extends ClassWithCustomDeps {}

Injector injector = Injector.of(ModuleBuilder.create()
Expand All @@ -354,14 +354,14 @@ class Inherited extends ClassWithCustomDeps {}
assertEquals("str: 42", instance.string);
}

@Inject
@Named
static class RecursiveA {

@Inject
RecursiveB dependency;
}

@Inject
@Named
static class RecursiveB {

@Inject
Expand Down Expand Up @@ -411,7 +411,7 @@ public void cyclicInjects2() {
@Test
public void optionalInjects() {

@Inject
@Named
class ClassWithCustomDeps {

@Inject
Expand Down Expand Up @@ -498,8 +498,7 @@ public void injectFactoryMethod() {
assertEquals(43, service.value);
}

@SuppressWarnings("unused")
@Inject
@Named
static class Container<Z, T, U> {

@Inject
Expand Down Expand Up @@ -665,7 +664,7 @@ String string(Integer integer) {
@Test
public void abstractModuleGenerics() {

@Inject
@Named
class AndAContainerToo<T> {

@Inject
Expand Down Expand Up @@ -753,7 +752,7 @@ class Injectable {

@Test
public void transitiveImplicitBinding() {
@Inject
@Named
class Container {
@Inject
InstanceProvider<InstanceProvider<String>> provider;
Expand Down Expand Up @@ -951,7 +950,7 @@ public void methodLocalClass() {

String captured = "captured";

@Inject
@Named
class MethodLocal {

MethodLocal() {}
Expand Down Expand Up @@ -1645,6 +1644,33 @@ public void automaticInterfaceGenerationNotAmbiguous() {
assertEquals("test4", instance.getObj());
}

@Named
static class ClassImpl implements TestInterface<String> {
@Override
public String getObj() {
return null;
}
}

@Named
@Typed
static class TypedClassImpl implements TestInterface<String> {
@Override
public String getObj() {
return null;
}
}

@Test
public void methodBindInterfaces() {
Module module = ModuleBuilder.create()
.bind(ClassImpl.class)
.bind(TypedClassImpl.class)
.build();
Injector injector = Injector.of(module);
TestInterface<String> instance = injector.getInstance(new Key<TestInterface<String>>() {});
}

public static final class SingleInjectConstructor {
private final String text;

Expand Down Expand Up @@ -1676,7 +1702,7 @@ public String getText() {
}
}

@Inject
@Named
public static final class SingleConstructorInject {
private final String text;

Expand All @@ -1689,7 +1715,7 @@ public String getText() {
}
}

@Inject
@Named
public static final class MultipleConstructorsInject {
private final String text;

Expand All @@ -1706,10 +1732,10 @@ public String getText() {
}
}

@Inject
@Named
public interface InjectInterface {}

@Inject
@Named
public static final class ConstructorAndFactoryInject {
private final String text;

Expand Down

0 comments on commit c046895

Please sign in to comment.