Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Forbid modules who extend from non-Object class types. #196

Merged
merged 1 commit into from Mar 27, 2013

Conversation

3 participants
Collaborator

JakeWharton commented Mar 27, 2013

Closes #188.

@JakeWharton JakeWharton and 1 other commented on an outdated diff Mar 27, 2013

...n/java/dagger/internal/codegen/ProvidesProcessor.java
- if (result.containsKey(moduleType)) continue;
- result.put(moduleType, new ArrayList<ExecutableElement>());
+ for (Element module : env.getElementsAnnotatedWith(Module.class)) {
+ if (module.getKind().equals(ElementKind.CLASS)) {
+ TypeElement moduleType = (TypeElement) module;
+
+ // Verify that all modules do not extend from non-Object types.
+ if (moduleType.getSuperclass().equals(objectType)) {
+ error("Modules may not extend from other classes: " + moduleType, moduleType);
+ }
+
+ String moduleName = moduleType.getQualifiedName().toString();
+ if (result.containsKey(moduleName)) continue;
+ result.put(moduleName, new ArrayList<ExecutableElement>());
+ } else {
+ throw new AssertionError();
@JakeWharton

JakeWharton Mar 27, 2013

Collaborator

Was unsure about this. Enforced by @Target on the annotation, right?

@JakeWharton

JakeWharton Mar 27, 2013

Collaborator

Should I flip the logic?

if (!module.getKind().equals(ElementKind.CLASS)) {
  throw new AssertionError();
}

TypeElement moduleType = ...
@JakeWharton

JakeWharton Mar 27, 2013

Collaborator

Ah, I guess it could be on an interface or enum. I'll mark as an error as well.

@swankjesse

swankjesse Mar 27, 2013

Owner

Yup. But flipping the logic is good.

@tbroyer tbroyer commented on the diff Mar 27, 2013

core/src/test/java/dagger/ModuleIncludesTest.java
@@ -181,4 +181,16 @@
public void childModuleMissingModuleAnnotation() {
ObjectGraph.create(new ChildModuleMissingModuleAnnotation());
}
+
+ @Module
+ static class ThreadModule extends Thread {}
+
+ @Test public void moduleExtendingClassThrowsException() {
@tbroyer

tbroyer Mar 27, 2013

Collaborator

Should the class be renamed as ModuleTest as this is no longer only testing for @Module(includes=…)?

@JakeWharton

JakeWharton Mar 27, 2013

Collaborator

Yep. Good call. Updated.

@swankjesse

swankjesse Mar 27, 2013

Owner

Push your changes?

@JakeWharton

JakeWharton Mar 27, 2013

Collaborator

Heh, I'm trying! Stupid bluetooth tether over cell network...

Owner

swankjesse commented Mar 27, 2013

LGTM

JakeWharton added a commit that referenced this pull request Mar 27, 2013

Merge pull request #196 from square/jw/composition
Forbid modules who extend from non-Object class types.

@JakeWharton JakeWharton merged commit 079a3dc into master Mar 27, 2013

1 check passed

default The Travis build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment