Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Commit

Permalink
Forbid injection on private fields.
Browse files Browse the repository at this point in the history
Closes #181. Closes #113.
  • Loading branch information
JakeWharton committed Mar 26, 2013
1 parent cdb58e6 commit 9a3102d
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 4 deletions.
1 change: 1 addition & 0 deletions compiler/src/it/private-field-inject/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.buildResult=failure
53 changes: 53 additions & 0 deletions compiler/src/it/private-field-inject/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2013 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.
-->
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.dagger.tests</groupId>
<artifactId>private-field-inject</artifactId>
<version>HEAD-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>@dagger.groupId@</groupId>
<artifactId>dagger</artifactId>
<version>@dagger.version@</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<!-- workaround for http://jira.codehaus.org/browse/MCOMPILER-202 -->
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
<dependencies>
<dependency>
<groupId>@dagger.groupId@</groupId>
<artifactId>dagger-compiler</artifactId>
<version>@dagger.version@</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (C) 2013 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 test;

import dagger.Module;

import dagger.ObjectGraph;
import dagger.Provides;

import javax.inject.Inject;
import java.lang.Override;

class TestApp {
@Inject private Object nope;
}
6 changes: 6 additions & 0 deletions compiler/src/it/private-field-inject/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dagger.testing.it.BuildLogValidator;
import java.io.File;

File buildLog = new File(basedir, "build.log");
new BuildLogValidator().assertHasText(buildLog, new String[]{
"Can't inject a private field or constructor: test.TestApp.nope"});
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,21 @@ private Set<String> getInjectedClassNames(RoundEnvironment env) {
// First gather the set of classes that have @Inject-annotated members.
Set<String> injectedTypeNames = new LinkedHashSet<String>();
for (Element element : env.getElementsAnnotatedWith(Inject.class)) {
Element enclosingType = element.getEnclosingElement();
if (!validateInjectable(enclosingType)) {
if (!validateInjectable(element)) {
continue;
}
injectedTypeNames.add(CodeGen.rawTypeToString(enclosingType.asType(), '.'));
injectedTypeNames.add(CodeGen.rawTypeToString(element.getEnclosingElement().asType(), '.'));
}
return injectedTypeNames;
}

private boolean validateInjectable(Element injectableType) {
private boolean validateInjectable(Element injectable) {
if (injectable.getModifiers().contains(Modifier.PRIVATE)) {
error("Can't inject a private field or constructor: " + injectable, injectable);
return false;
}

Element injectableType = injectable.getEnclosingElement();
ElementKind elementKind = injectableType.getEnclosingElement().getKind();
boolean isClassOrInterface = elementKind.isClass() || elementKind.isInterface();
boolean isStatic = injectableType.getModifiers().contains(Modifier.STATIC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ public static <T> Binding<T> create(Class<T> type, boolean mustBeInjectable) {
if (!field.isAnnotationPresent(Inject.class) || Modifier.isStatic(field.getModifiers())) {
continue;
}
if ((field.getModifiers() & Modifier.PRIVATE) != 0) {
throw new IllegalStateException("Can't inject private field: " + field);
}
field.setAccessible(true);
injectedFields.add(field);
keys.add(Keys.get(field.getGenericType(), field.getAnnotations(), field));
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/java/dagger/InjectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -750,4 +750,19 @@ static class ExtensionModule { }
assertThat(extension.get(SingletonLinkedFromExtension.class).c).isSameAs(root.get(C.class));
}

@Test(expected = IllegalStateException.class)
public void privateFieldsFail() {
class Test {
@Inject private Object nope;
}

@Module(entryPoints = Test.class)
class TestModule {
@Provides Object provideObject() {
return null;
}
}

ObjectGraph.create(new TestModule()).inject(new Test());
}
}

0 comments on commit 9a3102d

Please sign in to comment.