Skip to content

Commit

Permalink
GH-392 Add example for the DI Wiki post
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Mar 23, 2020
1 parent abc6f3a commit 430f62c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
import java.util.Arrays;
import java.util.List;



public final class JavassistAdapter implements MetadataAdapter<ClassFile, FieldInfo, MethodInfo> {

public static final boolean includeInvisibleTag = true;

@Override
public boolean acceptsInput(String file) {
return file.endsWith(".class");
return file.endsWith(".class") && !file.contains("module-info.class");
}

private List<String> splitDescriptorToTypeNames(String descriptors) {
Expand Down Expand Up @@ -171,21 +173,16 @@ public String getFieldName(FieldInfo field) {
}

@Override
public @Nullable ClassFile getOfCreateClassObject(AnnotationsScanner scanner, AnnotationsScannerFile file) {
public ClassFile getOfCreateClassObject(AnnotationsScanner scanner, AnnotationsScannerFile file) throws Exception {
InputStream inputStream = null;

try {
inputStream = file.openInputStream();
DataInputStream dis = new DataInputStream(new BufferedInputStream(inputStream));
return new ClassFile(dis);
} catch (Exception e) {
scanner.getLogger().exception(e);
scanner.getLogger().error("Could not create class file from " + file.getInternalPath());
} finally {
IOUtils.close(inputStream);
}

return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public interface MetadataAdapter<C, F, M> {

String getFieldName(F field);

C getOfCreateClassObject(AnnotationsScanner scanner, AnnotationsScannerFile file);
C getOfCreateClassObject(AnnotationsScanner scanner, AnnotationsScannerFile file) throws Exception;

String getMethodModifier(M method);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
import java.util.HashMap;
import java.util.Map;

/**
* Annotation data wrapper.
* Stores data about the annotation in {@link org.panda_lang.utilities.inject.InjectorAnnotation.Metadata} container.
*
* @param <T> annotation type
*/
public final class InjectorAnnotation<T extends Annotation> {

private final T annotation;
Expand Down Expand Up @@ -53,6 +59,11 @@ public T getAnnotation() {
return annotation;
}

/**
* Stores in key-value format data about the associated annotation
*
* @param <T>
*/
public static final class Metadata<T extends Annotation> {

private final Class<? extends Annotation> annotationType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ public interface InjectorResources {
*
* @param annotation the annotation to bind
* @param <T> type of annotation
* @return the bind based on associate annotation
* @return the bind based on associated annotation
*/
<T extends Annotation> InjectorResourceBind<T, T> annotatedWith(Class<T> annotation);

/**
* Create bind for parameters annotated with the specified annotation with no
* Create bind for parameters annotated with the specified annotation converted into to {@link org.panda_lang.utilities.inject.InjectorAnnotation}
*
* @param annotation the annotation to bind
* @param <T> type of annotation
* @return the bind based on associate annotation
* @return the bind based on associated annotation
*/
<T extends Annotation> InjectorResourceBind<T, InjectorAnnotation<T>> annotatedWithMetadata(Class<T> annotation);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2015-2020 Dzikoysk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.panda_lang.utilities.inject;

import org.junit.jupiter.api.Test;
import org.panda_lang.utilities.inject.annotations.Inject;
import org.panda_lang.utilities.inject.annotations.Injectable;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.UUID;

final class DependencyInjectionWikiTest {

@Test
void testWikiExample() throws Exception {
Injector injector = DependencyInjection.createInjector(resources -> {
resources.annotatedWith(AwesomeRandom.class).assignHandler((expectedType, annotation) -> {
if (expectedType.equals(String.class)) {
return UUID.randomUUID().toString();
}

if (expectedType.equals(UUID.class)) {
return UUID.randomUUID();
}

throw new IllegalArgumentException("Unsupported type " + expectedType);
});
});

Entity entityA = injector.newInstance(Entity.class);
Entity entityB = injector.newInstance(Entity.class);

System.out.println(entityA.getId());
System.out.println(entityB.getId());
}

@Injectable // mark annotation as DI ready annotation
@Retention(RetentionPolicy.RUNTIME) // make sure that the annotation is visible at runtime
@interface AwesomeRandom { }

private static final class Entity {
private final UUID id;

@Inject //it's not required, but it might be useful to disable "unused method" warnings/scanning for annotations
private Entity(@AwesomeRandom UUID random) {
this.id = random;
}

public UUID getId() {
return id;
}
}

}

0 comments on commit 430f62c

Please sign in to comment.