Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgrefer committed Sep 8, 2017
1 parent 0291a96 commit 1669ac9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public Context get() {

applicationInjector.inject(this);
injectAttributesAndResources();

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.freefair.android.injection.injector;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.lang.annotation.Annotation;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -58,13 +58,13 @@ protected Injector getInjector(Object instance) {
*
* @param instance The object to inject into
*/
public final void inject(@NotNull Object instance) {
public final void inject(@NonNull Object instance) {
inject(instance, instance.getClass());
}

private Deque<Object> instancesStack = new LinkedList<>();

public final void inject(@NotNull Object instance, @NotNull Class<?> clazz) {
public final void inject(@NonNull Object instance, @NonNull Class<?> clazz) {
responsibleInjectors.put(instance, this);
if (!alreadyInjectedInstances.containsKey(instance)) {
long start = System.currentTimeMillis();
Expand All @@ -84,7 +84,7 @@ public final void inject(@NotNull Object instance, @NotNull Class<?> clazz) {

private static WeakHashMap<Class<?>, List<Field>> fieldCache = new WeakHashMap<>();

private List<Field> getFields(@NotNull Class<?> clazz) {
private List<Field> getFields(@NonNull Class<?> clazz) {
if (!fieldCache.containsKey(clazz)) {
fieldCache.put(clazz, Reflection.getAllFields(clazz, getUpToExcluding(clazz)));
}
Expand All @@ -94,7 +94,7 @@ private List<Field> getFields(@NotNull Class<?> clazz) {
@Getter(PROTECTED)
private Set<Class<?>> topClasses;

@NotNull
@NonNull
@SuppressWarnings("unchecked")
private <X> Class<X> getUpToExcluding(Class<? extends X> clazz) {
for (Class<?> topClazz : topClasses) {
Expand All @@ -111,7 +111,7 @@ private <X> Class<X> getUpToExcluding(Class<? extends X> clazz) {
* @param instance the instance to inject into
* @param field the field to inject
*/
protected void visitField(@NotNull Object instance, @NotNull FieldWrapper field) {
protected void visitField(@NonNull Object instance, @NonNull FieldWrapper field) {
if (field.isAnnotationPresent(Inject.class)) {
Inject injectAnnotation = field.getAnnotation(Inject.class);

Expand Down Expand Up @@ -139,8 +139,8 @@ protected void visitField(@NotNull Object instance, @NotNull FieldWrapper field)
* @return The object to use for the given type
*/
@SuppressWarnings("unchecked")
@NotNull
public <T> Optional<? extends T> resolveBean(@NotNull Class<T> type, @Nullable Object instance) {
@NonNull
public <T> Optional<? extends T> resolveBean(@NonNull Class<T> type, @Nullable Object instance) {
for (Object inst : instancesStack) {
if (type.isInstance(inst)) {
return Optional.of((T) inst);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package io.freefair.android.injection.injector;

import org.jetbrains.annotations.NotNull;
import android.support.annotation.NonNull;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Properties;

import io.freefair.android.injection.annotation.Inject;
import io.freefair.android.injection.provider.BeanProvider;
import io.freefair.util.function.Optional;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
* @author Lars Grefer
Expand All @@ -37,18 +33,16 @@ public static RuntimeInjector getInstance() {

private RuntimeInjector() {
super((Object[]) null);

beanProviders.addLast(new NewInstanceProvider());
}

public void register(BeanProvider beanProvider) {
beanProviders.addFirst(beanProvider);
}

@NotNull
@NonNull
@Override
@SuppressWarnings("unchecked")
public <T> Optional<? extends T> resolveBean(@NotNull Class<T> type, Object instance) {
public <T> Optional<? extends T> resolveBean(@NonNull Class<T> type, Object instance) {

Optional<? extends T> optional = super.resolveBean(type, instance);
if (optional.isPresent()) {
Expand All @@ -73,58 +67,4 @@ public <T> Optional<? extends T> resolveBean(@NotNull Class<T> type, Object inst
return Optional.ofNullable(value);
}

@Slf4j
private static class NewInstanceProvider implements BeanProvider {

@Override
public boolean canProvideBean(Class<?> type) {
if (type.isPrimitive() || type.isAnnotation() || type.isArray() || type.isEnum() || type.isInterface()) {
return false;
}

if (Modifier.isAbstract(type.getModifiers())) {
return false;
}

try {
type.newInstance();
return true;
} catch (Exception ignored) {
return false;
}
}

@SuppressWarnings("unchecked")
@Override
public <T> T provideBean(Class<? super T> clazz, Object instance, Injector injector) {

T newInstance = null;
try {
newInstance = (T) clazz.newInstance();
} catch (Exception e) {
//Look for constructor annotated with @Inject
for (Constructor<?> constructor : clazz.getConstructors()) {
if (constructor.isAnnotationPresent(Inject.class)) {

//resolve constructor params;
Class<?>[] parameterTypes = constructor.getParameterTypes();
Object[] parameterValues = new Object[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
parameterValues[i] = injector.resolveBean(parameterTypes[i], null).orNull();
}

try {
newInstance = (T) constructor.newInstance(parameterValues);
} catch (Exception e1) {
NewInstanceProvider.log.error("Error while calling constructor " + constructor.toString(), e1);
}
}
}
}
if (newInstance != null) {
injector.inject(newInstance);
}
return newInstance;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import android.support.wearable.view.WatchViewStub;
import android.view.View;

import org.jetbrains.annotations.NotNull;

import io.freefair.util.function.Optional;

/**
Expand All @@ -23,7 +21,6 @@ public WatchViewStubActivityInjector(Activity activity, WatchViewStub stub, Obje
this.stub = stub;
}

@NotNull
@NonNull
@SuppressWarnings("unchecked")
@Override
Expand Down

0 comments on commit 1669ac9

Please sign in to comment.