Skip to content

Commit

Permalink
Revert "Remove runtime static injector."
Browse files Browse the repository at this point in the history
This reverts commit 3c7a5aa.
  • Loading branch information
JakeWharton committed May 2, 2016
1 parent eefb3e4 commit 7fd458a
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import dagger.internal.Binding;
import dagger.internal.Loader;
import dagger.internal.ModuleAdapter;
import dagger.internal.StaticInjection;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -136,4 +137,8 @@ private static TypeElement getTypeElement(Elements elements, CharSequence classN
@Override public <T> ModuleAdapter<T> getModuleAdapter(Class<T> moduleClass) {
throw new UnsupportedOperationException();
}

@Override public StaticInjection getStaticInjection(Class<?> injectedClass) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2012 Square, Inc.
*
* 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 dagger.internal.codegen;

import dagger.internal.Linker;
import dagger.internal.StaticInjection;
import javax.inject.Inject;
import javax.lang.model.element.Element;

import static dagger.internal.codegen.Util.isStatic;

public final class GraphAnalysisStaticInjection extends StaticInjection {

private final Element enclosingClass;

public GraphAnalysisStaticInjection(Element enclosingClass) {
this.enclosingClass = enclosingClass;
}

@Override public void attach(Linker linker) {
for (Element enclosedElement : enclosingClass.getEnclosedElements()) {
if (enclosedElement.getKind().isField() && isStatic(enclosedElement)) {
Inject injectAnnotation = enclosedElement.getAnnotation(Inject.class);
if (injectAnnotation != null) {
String key = GeneratorKeys.get(enclosedElement.asType());
linker.requestBinding(key, enclosingClass.toString(),
getClass().getClassLoader());
}
}
}
}

@Override public void inject() {
throw new UnsupportedOperationException();
}
}
11 changes: 11 additions & 0 deletions core/src/main/java/dagger/internal/FailoverLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@


import dagger.internal.loaders.ReflectiveAtInjectBinding;
import dagger.internal.loaders.ReflectiveStaticInjection;

import static dagger.internal.loaders.GeneratedAdapters.INJECT_ADAPTER_SUFFIX;
import static dagger.internal.loaders.GeneratedAdapters.MODULE_ADAPTER_SUFFIX;
import static dagger.internal.loaders.GeneratedAdapters.STATIC_INJECTION_SUFFIX;

/**
* Handles loading/finding of modules, injection bindings, and static injections by use of a
Expand Down Expand Up @@ -71,4 +73,13 @@ public final class FailoverLoader extends Loader {
}
return ReflectiveAtInjectBinding.create(type, mustHaveInjections);
}

@Override public StaticInjection getStaticInjection(Class<?> injectedClass) {
StaticInjection result = instantiate(
injectedClass.getName().concat(STATIC_INJECTION_SUFFIX), injectedClass.getClassLoader());
if (result != null) {
return result;
}
return ReflectiveStaticInjection.create(injectedClass);
}
}
7 changes: 6 additions & 1 deletion core/src/main/java/dagger/internal/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public abstract Binding<?> getAtInjectBinding(
*/
public abstract <T> ModuleAdapter<T> getModuleAdapter(Class<T> moduleClass);

/**
* Returns the static injection for {@code injectedClass}.
*/
public abstract StaticInjection getStaticInjection(Class<?> injectedClass);

/**
* Loads a class from a {@code ClassLoader}-specific cache if it's already there, or
* loads it from the given {@code ClassLoader} and caching it for future requests. Failures
Expand Down Expand Up @@ -85,4 +90,4 @@ protected <T> T instantiate(String name, ClassLoader classLoader) {
}
}

}
}
29 changes: 29 additions & 0 deletions core/src/main/java/dagger/internal/StaticInjection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2012 Square Inc.
* Copyright (C) 2012 Google Inc.
*
* 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 dagger.internal;


/**
* Injects the static fields of a class.
*/
public abstract class StaticInjection {

public abstract void attach(Linker linker);

public abstract void inject();

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class GeneratedAdapters {
private static final String SEPARATOR = "$$";
public static final String INJECT_ADAPTER_SUFFIX = SEPARATOR + "InjectAdapter";
public static final String MODULE_ADAPTER_SUFFIX = SEPARATOR + "ModuleAdapter";
public static final String STATIC_INJECTION_SUFFIX = SEPARATOR + "StaticInjection";

private GeneratedAdapters() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2012 Square, Inc.
*
* 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 dagger.internal.loaders;

import dagger.internal.Binding;
import dagger.internal.Keys;
import dagger.internal.Linker;
import dagger.internal.StaticInjection;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;

/**
* Uses reflection to inject the static fields of a class.
*/
public final class ReflectiveStaticInjection extends StaticInjection {
private final ClassLoader loader;
private final Field[] fields;
private Binding<?>[] bindings;

private ReflectiveStaticInjection(ClassLoader loader, Field[] fields) {
this.fields = fields;
this.loader = loader;
}

@Override public void attach(Linker linker) {
bindings = new Binding<?>[fields.length];
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
String key = Keys.get(field.getGenericType(), field.getAnnotations(), field);
bindings[i] = linker.requestBinding(key, field, loader);
}
}

@Override public void inject() {
try {
for (int f = 0; f < fields.length; f++) {
fields[f].set(null, bindings[f].get());
}
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}

public static StaticInjection create(Class<?> injectedClass) {
List<Field> fields = new ArrayList<Field>();
for (Field field : injectedClass.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()) && field.isAnnotationPresent(Inject.class)) {
field.setAccessible(true);
fields.add(field);
}
}
if (fields.isEmpty()) {
throw new IllegalArgumentException("No static injections: " + injectedClass.getName());
}
return new ReflectiveStaticInjection(injectedClass.getClassLoader(),
fields.toArray(new Field[fields.size()]));
}
}
5 changes: 5 additions & 0 deletions core/src/test/java/dagger/internal/TestingLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


import dagger.internal.loaders.ReflectiveAtInjectBinding;
import dagger.internal.loaders.ReflectiveStaticInjection;

/**
* A test-only loader that merely uses reflection to test internals.
Expand All @@ -42,4 +43,8 @@ public final class TestingLoader extends Loader {
String.format("Could not find %s needed for binding %s", className, key), e);
}
}

@Override public StaticInjection getStaticInjection(Class<?> injectedClass) {
return ReflectiveStaticInjection.create(injectedClass);
}
}

0 comments on commit 7fd458a

Please sign in to comment.