Skip to content

Commit

Permalink
Minor cleanup to make code easier to read and more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
P3ridot committed Jul 18, 2023
1 parent 40496cd commit 6eb52d4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

public final class GeneratedMethodInjector implements MethodInjector {

private static final Object[] EMPTY = new Object[0];

private static final AtomicInteger ID = new AtomicInteger();

private final InjectorProcessor processor;
Expand All @@ -50,7 +52,7 @@ public final class GeneratedMethodInjector implements MethodInjector {
@Override
public <T> T invoke(Object instance, Object... injectorArgs) throws Exception {
return (T) function.apply(instance, empty
? InjectorProcessor.EMPTY
? EMPTY
: processor.fetchValues(cache, injectorArgs));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
package org.panda_lang.utilities.inject;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.jetbrains.annotations.Nullable;
import org.panda_lang.utilities.inject.annotations.PostConstruct;
import panda.std.Lazy;
Expand Down Expand Up @@ -125,7 +122,7 @@ public MethodInjector forGeneratedMethod(Method method) throws Exception {

@Override
public <T> @Nullable T invokeParameter(Parameter parameter, Object... injectorArgs) throws Exception {
return ObjectUtils.cast(processor.tryFetchValue(processor, new PropertyParameter(parameter), injectorArgs));
return ObjectUtils.cast(processor.fetchValue(new PropertyParameter(parameter), injectorArgs));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package org.panda_lang.utilities.inject;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.panda_lang.utilities.inject.annotations.AutoConstruct;
import org.panda_lang.utilities.inject.annotations.Inject;

Expand All @@ -19,24 +15,27 @@ public final class FieldsInjector<T> {
}

public T newInstance(Object... injectorArgs) throws Exception {
T instance = constructorInjector.newInstance(injectorArgs);

T instance = this.constructorInjector.newInstance(injectorArgs);
for (Field field : getAllFields(instance.getClass())) {
if (!field.isAnnotationPresent(Inject.class) && !field.isAnnotationPresent(AutoConstruct.class)) {
continue;
}

field.setAccessible(true);
field.set(instance, processor.tryFetchValue(processor, new PropertyField(field), injectorArgs));
field.set(instance, this.processor.fetchValue(new PropertyField(field), injectorArgs));
}

return instance;
}

private static List<Field> getAllFields(Class<?> type) {
List<Field> fields = new ArrayList<>(Arrays.asList(type.getDeclaredFields()));
if (type.getSuperclass() != null) {
fields.addAll(getAllFields(type.getSuperclass()));
private static Field[] getAllFields(Class<?> type) {
Field[] fields = type.getDeclaredFields();
Class<?> superType = type.getSuperclass();
if (superType != null) {
Field[] superFields = getAllFields(superType);
Field[] allFields = new Field[fields.length + superFields.length];
System.arraycopy(fields, 0, allFields, 0, fields.length);
System.arraycopy(superFields, 0, allFields, fields.length, superFields.length);
return allFields;
}
return fields;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static InjectorCache of(InjectorProcessor processor, Executable executabl
}

public static InjectorCache of(InjectorProcessor processor, Property property) {
Annotation annotation = ArrayUtils.findIn(property.getAnnotations(), a -> a.annotationType().isAnnotationPresent(Injectable.class)).getOrNull();
Annotation annotation = ArrayUtils.findIn(property.getAnnotations(), a -> a.annotationType().isAnnotationPresent(Injectable.class)).orNull();

return new InjectorCache(
ArrayUtils.of(property),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

final class InjectorProcessor {

protected static final Object[] EMPTY = { };

private final Injector injector;
private final Map<Executable, Annotation[]> injectableCache = new HashMap<>();

Expand All @@ -49,16 +47,14 @@ final class InjectorProcessor {
protected Object[] fetchValues(InjectorCache cache, Object... injectorArgs) throws Exception {
Property[] properties = cache.getProperties();
Object[] values = new Object[cache.getInjectable().length];

for (int index = 0; index < values.length; index++) {
values[index] = fetchValue(cache, properties[index], index, injectorArgs);
}

return values;
}

protected Object tryFetchValue(InjectorProcessor processor, Property property, Object... injectorArgs) throws Exception {
InjectorCache cache = InjectorCache.of(processor, property);
protected @Nullable Object fetchValue(Property property, Object... injectorArgs) throws Exception {
InjectorCache cache = InjectorCache.of(this, property);
return fetchValue(cache, property, 0, injectorArgs);
}

Expand All @@ -67,11 +63,9 @@ protected Object tryFetchValue(InjectorProcessor processor, Property property, O

for (BindHandler<Annotation, Object, ?> handler : cache.getHandlers()[index]) {
Annotation annotation = null;

if (handler.getAnnotation().isPresent()) {
annotation = cache.getAnnotations()[index].get(handler.getAnnotation().get());
}

value = handler.process(property, annotation, ObjectUtils.cast(value), injectorArgs);
}

Expand All @@ -80,17 +74,14 @@ protected Object tryFetchValue(InjectorProcessor processor, Property property, O

protected Property[] fetchInjectorProperties(Parameter[] parameters) {
Property[] properties = new Property[parameters.length];

for (int index = 0; index < parameters.length; index++) {
properties[index] = new PropertyParameter(parameters[index]);
}

return properties;
}

protected Annotation[] fetchAnnotations(Executable executable) {
Annotation[] injectorAnnotations = injectableCache.get(executable);

if (injectorAnnotations != null) {
return injectorAnnotations;
}
Expand All @@ -116,11 +107,9 @@ protected Map<Class<? extends Annotation>, Annotation>[] fetchAnnotationsMap(Exe

for (int index = 0; index < annotations.length; index++) {
Map<Class<? extends Annotation>, Annotation> annotationMap = new HashMap<>();

for (Annotation annotation : annotations[index]) {
annotationMap.put(annotation.annotationType(), annotation);
}

mappedAnnotations[index] = annotationMap;
}

Expand All @@ -136,8 +125,14 @@ protected Bind<Annotation>[] fetchBinds(Annotation[] annotations, Executable exe
Annotation annotation = annotations[index];
Parameter parameter = parameters[index];

Class<?> requiredType = annotation != null ? annotation.annotationType() : parameter.getType();
Bind<Annotation> bind = resources.getBind(requiredType).orNull();
Bind<Annotation> bind = annotation != null
? this.injector.getResources().getBind(annotation.annotationType()).orNull()
: null;

if (bind == null) {
bind = this.injector.getResources().getBind(parameter.getType()).orNull();
}

if (bind == null && parameter.getAnnotation(AutoConstruct.class) != null) {
bind = this.autoConstructBind;
}
Expand Down

0 comments on commit 6eb52d4

Please sign in to comment.