Skip to content

Commit

Permalink
Merge pull request square#163 from cgruber/compiletimecheckcirculardeps
Browse files Browse the repository at this point in the history
Add cycle detection at compile-time using the problem detector, plus test.
  • Loading branch information
swankjesse committed Feb 16, 2013
2 parents a061bbd + aadcf07 commit 8b8bbfb
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
1 change: 1 addition & 0 deletions compiler/src/it/cyclic-deps/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.buildResult=failure
49 changes: 49 additions & 0 deletions compiler/src/it/cyclic-deps/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2013 Square, Inc.
Copyright (C) 2013 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.
-->
<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.squareup.dagger.tests</groupId>
<version>@dagger.version@</version>
<packaging>jar</packaging>
<name>Dagger Integration Test Basic</name>
<dependencies>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>dagger</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>dagger-compiler</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration><source>1.5</source><target>1.5</target></configuration>
</plugin>
</plugins>
</build>
<artifactId>cyclic-deps</artifactId>
</project>
58 changes: 58 additions & 0 deletions compiler/src/it/cyclic-deps/src/main/java/test/TestApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2013 Google, Inc.
* 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;

class TestApp implements Runnable {

static class Foo {
@Inject Foo(@SuppressWarnings("unused") Bar b) { }
}

static class Bar {
@Inject Bar(@SuppressWarnings("unused") Blah b) { }
}

static class Blah {
@Inject Blah(@SuppressWarnings("unused") Foo f) { }
}

static class EntryPoint {
@Inject Foo f;
}

@Module(entryPoints = EntryPoint.class)
static class TestModule {

}

static class A { }
static class B { }
static class C { }
static class D { }
@Module(entryPoints = D.class)
static class CyclicModule {
@Provides A a(@SuppressWarnings("unused") D d) { }
@Provides B b(@SuppressWarnings("unused") A a) { }
@Provides C c(@SuppressWarnings("unused") B b) { }
@Provides D d(@SuppressWarnings("unused") C c) { }
}
}
9 changes: 9 additions & 0 deletions compiler/src/it/cyclic-deps/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import dagger.testing.it.BuildLogValidator;
import java.io.File;

File buildLog = new File(basedir, "build.log");
new BuildLogValidator().assertHasText(buildLog, new String[]{
"TestApp.java:[43", "Graph validation", "Dependency cycle"});
new BuildLogValidator().assertHasText(buildLog, new String[]{
"TestApp.java:[52", "Graph validation", "Dependency cycle"});

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import dagger.Provides;
import dagger.internal.Binding;
import dagger.internal.Linker;
import dagger.internal.ProblemDetector;
import dagger.internal.SetBinding;
import java.io.IOException;
import java.io.Writer;
Expand Down Expand Up @@ -64,6 +65,10 @@ public final class FullGraphProcessor extends AbstractProcessor {
// Storing module names for later retrieval as the element instance is invalidated across
// passes.
for (Element e : env.getElementsAnnotatedWith(Module.class)) {
if (!(e instanceof TypeElement)) {
error("@Module applies to a type, " + e.getSimpleName() + " is a " + e.getKind(), e);
continue;
}
delayedModuleNames.add(((TypeElement) e).getQualifiedName().toString());
}
return true;
Expand All @@ -81,10 +86,16 @@ public final class FullGraphProcessor extends AbstractProcessor {
}
TypeElement moduleType = (TypeElement) element;
Map<String, Binding<?>> bindings = processCompleteModule(moduleType);
try {
new ProblemDetector().detectProblems(bindings.values());
} catch (IllegalStateException e) {
error("Graph validation failed: " + e.getMessage(), moduleType);
continue;
}
try {
writeDotFile(moduleType, bindings);
} catch (IOException e) {
error("Graph processing failed: " + e, moduleType);
error("Graph visualization failed: " + e, moduleType);
}
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/dagger/internal/ProblemDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void detectCircularDependencies(Collection<Binding<?>> bindings, List<Bin
}

static class ArraySet<T> extends AbstractSet<T> {
private ArrayList<T> list = new ArrayList<T>();
private final ArrayList<T> list = new ArrayList<T>();

@Override public boolean add(T t) {
list.add(t);
Expand Down

0 comments on commit 8b8bbfb

Please sign in to comment.