Skip to content

Commit

Permalink
Merge pull request google#273 from cgruber/prohibitsingletononconstru…
Browse files Browse the repository at this point in the history
…ctor

Prohibit singleton annotations in constructors.
  • Loading branch information
cgruber committed Jun 12, 2013
2 parents 2477211 + 886ce2b commit 39eef5a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.buildResult=failure
51 changes: 51 additions & 0 deletions compiler/src/it/singleton-on-constructor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<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>singleton-on-constructor</artifactId>
<version>HEAD-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>@dagger.groupId@</groupId>
<artifactId>dagger</artifactId>
<version>@dagger.version@</version>
</dependency>
<dependency>
<groupId>@dagger.groupId@</groupId>
<artifactId>dagger-compiler</artifactId>
<version>@dagger.version@</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 javax.inject.Inject;
import javax.inject.Singleton;

class TestApp {
@Singleton
@Inject
public TestApp() { }

@Module(injects=TestApp.class)
static class TestAppModule { }
}
7 changes: 7 additions & 0 deletions compiler/src/it/singleton-on-constructor/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import dagger.testing.it.BuildLogValidator;
import java.io.File;

File buildLog = new File(basedir, "build.log");
new BuildLogValidator().assertHasText(buildLog, new String[]{
"Singleton annotations have no effect on constructors.",
"Did you mean to annotate the class? test.TestApp"});
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ static GraphAnalysisInjectBinding create(TypeElement type, boolean mustHaveInjec
ExecutableElement constructor = (ExecutableElement) enclosed;
List<? extends VariableElement> parameters = constructor.getParameters();
if (hasAtInject(enclosed)) {
if (hasAtSingleton(enclosed)) {
throw new IllegalArgumentException("Singleton annotations have no effect on "
+ "constructors. Did you mean to annotate the class? "
+ type.getQualifiedName().toString());
}
if (hasInjectConstructor) {
throw new IllegalArgumentException("Too many injectable constructors on "
+ type.getQualifiedName().toString());
Expand Down Expand Up @@ -111,6 +116,10 @@ private static boolean hasAtInject(Element enclosed) {
return enclosed.getAnnotation(Inject.class) != null;
}

private static boolean hasAtSingleton(Element enclosed) {
return enclosed.getAnnotation(Singleton.class) != null;
}

@Override public void attach(Linker linker) {
String requiredBy = type.getQualifiedName().toString();
for (int i = 0; i < keys.size(); i++) {
Expand Down

0 comments on commit 39eef5a

Please sign in to comment.