forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Remove runtime static injector."
This reverts commit 3c7a5aa.
- Loading branch information
1 parent
eefb3e4
commit 7fd458a
Showing
8 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
compiler/src/main/java/dagger/internal/codegen/GraphAnalysisStaticInjection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
core/src/main/java/dagger/internal/loaders/ReflectiveStaticInjection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters