Skip to content

Commit

Permalink
Fix best guess related APT compiler crash with underscore in package …
Browse files Browse the repository at this point in the history
…name.
  • Loading branch information
TWiStErRob authored and sjudd committed Mar 2, 2018
1 parent a1f5f10 commit 99723e3
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,12 @@ private MethodSpec generateConstructor(ClassName appGlideModule,
"Discovered AppGlideModule from annotation: " + appGlideModule);
// Excluded GlideModule classes from the manifest are logged in Glide's singleton.
for (String glideModule : libraryGlideModuleClassNames) {
ClassName moduleClassName = ClassName.bestGuess(glideModule);
if (excludedGlideModuleClassNames.contains(glideModule)) {
constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG,
"AppGlideModule excludes LibraryGlideModule from annotation: " + moduleClassName);
"AppGlideModule excludes LibraryGlideModule from annotation: " + glideModule);
} else {
constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG,
"Discovered LibraryGlideModule from annotation: " + moduleClassName);
"Discovered LibraryGlideModule from annotation: " + glideModule);
}
}
constructorBuilder.endControlFlow();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.bumptech.glide.annotation.compiler;

import static com.bumptech.glide.annotation.compiler.test.Util.appResource;
import static com.bumptech.glide.annotation.compiler.test.Util.asUnixChars;
import static com.bumptech.glide.annotation.compiler.test.Util.glide;
import static com.bumptech.glide.annotation.compiler.test.Util.subpackage;
import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;

import com.bumptech.glide.annotation.compiler.test.ReferencedResource;
import com.bumptech.glide.annotation.compiler.test.RegenerateResourcesRule;
import com.bumptech.glide.annotation.compiler.test.Util;
import com.google.testing.compile.Compilation;
import java.io.IOException;
import javax.tools.JavaFileObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Tests AppGlideModules that use the @Excludes annotation
* with a single excluded Module class in a strangely named subpackage.
*/
@RunWith(JUnit4.class)
public class AppGlideModuleWithLibraryInPackageTest {
@Rule public final RegenerateResourcesRule regenerateResourcesRule =
new RegenerateResourcesRule(getClass());
private Compilation compilation;

@Before
public void setUp() {
compilation =
javac()
.withProcessors(new GlideAnnotationProcessor())
.compile(
forResource("AppModuleWithLibraryInPackage.java"),
forResource("LibraryModuleInPackage.java"));
assertThat(compilation).succeededWithoutWarnings();
}

@Test
@ReferencedResource
public void compilation_generatesExpectedGlideOptionsClass() throws IOException {
assertThat(compilation)
.generatedSourceFile(subpackage("GlideOptions"))
.contentsAsUtf8String()
.isEqualTo(asUnixChars(appResource("GlideOptions.java").getCharContent(true)));
}

@Test
@ReferencedResource
public void compilation_generatesExpectedGlideRequestClass() throws IOException {
assertThat(compilation)
.generatedSourceFile(subpackage("GlideRequest"))
.contentsAsUtf8String()
.isEqualTo(asUnixChars(appResource("GlideRequest.java").getCharContent(true)));
}

@Test
@ReferencedResource
public void compilation_generatesExpectedGlideRequestsClass() throws IOException {
assertThat(compilation)
.generatedSourceFile(subpackage("GlideRequests"))
.contentsAsUtf8String()
.isEqualTo(asUnixChars(appResource("GlideRequests.java").getCharContent(true)));
}

@Test
@ReferencedResource
public void compilationGeneratesExpectedGlideAppClass() throws IOException {
assertThat(compilation)
.generatedSourceFile(subpackage("GlideApp"))
.contentsAsUtf8String()
.isEqualTo(asUnixChars(appResource("GlideApp.java").getCharContent(true)));
}

@Test
public void compilation_generatesExpectedGeneratedAppGlideModuleImpl() throws IOException {
assertThat(compilation)
.generatedSourceFile(glide("GeneratedAppGlideModuleImpl"))
.contentsAsUtf8String()
.isEqualTo(
asUnixChars(forResource("GeneratedAppGlideModuleImpl.java").getCharContent(true)));
}

@Test
@ReferencedResource
public void compilation_generatesExpectedGeneratedRequestManagerFactory() throws IOException {
assertThat(compilation)
.generatedSourceFile(glide("GeneratedRequestManagerFactory"))
.contentsAsUtf8String()
.isEqualTo(
asUnixChars(appResource("GeneratedRequestManagerFactory.java").getCharContent(true)));
}

private JavaFileObject forResource(String name) {
return Util.forResource(getClass().getSimpleName(), name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.bumptech.glide.test;

import com.bumptech.glide.annotation.Excludes;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.test._package.LibraryModuleInPackage;

@GlideModule
@Excludes(LibraryModuleInPackage.class)
public final class AppModuleWithLibraryInPackage extends AppGlideModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.bumptech.glide;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import com.bumptech.glide.test.AppModuleWithLibraryInPackage;
import java.lang.Class;
import java.lang.Override;
import java.lang.SuppressWarnings;
import java.util.HashSet;
import java.util.Set;

@SuppressWarnings("deprecation")
final class GeneratedAppGlideModuleImpl extends GeneratedAppGlideModule {
private final AppModuleWithLibraryInPackage appGlideModule;

GeneratedAppGlideModuleImpl() {
appGlideModule = new AppModuleWithLibraryInPackage();
if (Log.isLoggable("Glide", Log.DEBUG)) {
Log.d("Glide", "Discovered AppGlideModule from annotation: com.bumptech.glide.test.AppModuleWithLibraryInPackage");
Log.d("Glide", "AppGlideModule excludes LibraryGlideModule from annotation: com.bumptech.glide.test._package.LibraryModuleInPackage");
}
}

@Override
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
appGlideModule.applyOptions(context, builder);
}

@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide,
@NonNull Registry registry) {
appGlideModule.registerComponents(context, glide, registry);
}

@Override
public boolean isManifestParsingEnabled() {
return appGlideModule.isManifestParsingEnabled();
}

@Override
@NonNull
public Set<Class<?>> getExcludedModuleClasses() {
Set<Class<?>> excludedClasses = new HashSet<Class<?>>();
excludedClasses.add(com.bumptech.glide.test._package.LibraryModuleInPackage.class);
return excludedClasses;
}

@Override
@NonNull
GeneratedRequestManagerFactory getRequestManagerFactory() {
return new GeneratedRequestManagerFactory();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// _ in the name is important otherwise everything would work
package com.bumptech.glide.test._package;

import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.LibraryGlideModule;

@GlideModule
public final class LibraryModuleInPackage extends LibraryGlideModule {}

0 comments on commit 99723e3

Please sign in to comment.