Skip to content

Commit b698d53

Browse files
falhassenglide-copybara-robot
authored andcommitted
This PR removes jarjar from Glide, and removes the outdated Java 1.7 source target rules. Repackaging is no longer needed, and was based on outdated work, and the annotation processor has been updated to work without the need for Sun imports (no longer accessible in Java 1.8+).
PiperOrigin-RevId: 798401559
1 parent 900f060 commit b698d53

File tree

2 files changed

+32
-148
lines changed

2 files changed

+32
-148
lines changed

annotation/compiler/build.gradle

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,9 @@ import proguard.gradle.ProGuardTask
22

33
apply plugin: 'java'
44

5-
// This package is stuck at java 7 for as long as we use jarjar.
6-
// We should re-evaluate whether we need to continue to worry
7-
// about this.
8-
java {
9-
sourceCompatibility JavaVersion.VERSION_1_7
10-
targetCompatibility JavaVersion.VERSION_1_7
11-
}
12-
13-
configurations {
14-
// adapted from https://android.googlesource.com/platform/frameworks/testing/+/976c423/espresso/espresso-lib/build.gradle
15-
// compileOnly dependencies will be repackaged, see rules in jarjar ant task below
16-
jarjar
17-
}
18-
195
dependencies {
20-
jarjar "com.googlecode.jarjar:jarjar:1.3"
21-
22-
compileOnly libs.javapoet
6+
implementation libs.javapoet
7+
implementation libs.guava
238
compileOnly libs.autoservice
249
compileOnly libs.findbugs.jsr305
2510
implementation project(':annotation')
@@ -30,84 +15,4 @@ javadoc {
3015
failOnError = false
3116
}
3217

33-
// TODO: Figure out a way to get the annotation processor tests running and re-enable this.
34-
// Make sure running `gradlew :annotation:compiler:check` actually does full quality control.
35-
//test.dependsOn ':annotation:compiler:test:test'
36-
37-
def packagingFolder = file("${buildDir}/intermediates")
38-
def repackagedJar = file("${packagingFolder}/repackaged.jar")
39-
def proguardedJar = file("${packagingFolder}/proguarded.jar")
40-
41-
task compiledJar(type: Jar, dependsOn: classes) {
42-
destinationDirectory.set(packagingFolder)
43-
archiveFileName.set('compiled.jar')
44-
from sourceSets.main.output
45-
}
46-
47-
// Repackage compileOnly dependencies to avoid namespace collisions.
48-
task jarjar(dependsOn: [tasks.compiledJar, configurations.compileClasspath]) {
49-
// Set up inputs and outputs to only rebuild when necessary (code change, dependency change).
50-
inputs.files compiledJar
51-
inputs.files configurations.compileClasspath
52-
outputs.file repackagedJar
53-
54-
doFirst {
55-
ant {
56-
taskdef name: 'jarjar',
57-
classname: 'com.tonicsystems.jarjar.JarJarTask',
58-
classpath: configurations.jarjar.asPath
59-
60-
jarjar(jarfile: repackagedJar) {
61-
configurations.compileClasspath.resolve().each {
62-
zipfileset(src: it.absolutePath, excludes: [
63-
'META-INF/maven/**',
64-
'META-INF/services/javax.annotation.processing.Processor'
65-
].join(','))
66-
}
67-
zipfileset(src: tasks.compiledJar.archivePath)
68-
def repackageIntoGlide = 'com.bumptech.glide.repackaged.@0'
69-
rule result: repackageIntoGlide, pattern: 'com.squareup.javapoet.**'
70-
rule result: repackageIntoGlide, pattern: 'com.google.auto.**'
71-
rule result: repackageIntoGlide, pattern: 'com.google.common.**'
72-
rule result: repackageIntoGlide, pattern: 'com.google.thirdparty.publicsuffix.**'
73-
}
74-
}
75-
}
76-
}
77-
78-
// Proguard repackaged dependencies to reduce the binary size.
79-
task proguard(type: ProGuardTask, dependsOn: tasks.jarjar) {
80-
configuration 'proguard.pro'
81-
82-
injars repackagedJar
83-
outjars proguardedJar
84-
85-
libraryjars files(configurations.compileClasspath.collect())
86-
// From http://cr.openjdk.java.net/~mr/jigsaw/ea/module-summary.html
87-
for (jmod in [
88-
"java.base",
89-
"java.logging",
90-
"java.compiler",
91-
"jdk.compiler",
92-
"jdk.unsupported"]) {
93-
libraryjars(
94-
"${System.getProperty('java.home')}/jmods/${jmod}.jmod",
95-
jarfilter: '!**.jar',
96-
filter: '!module-info.class')
97-
}
98-
}
99-
100-
// Replace the contents of the standard jar task with those from our our compiled, repackaged and
101-
// proguarded jar. Replacing the task itself is possible and looks simpler, but requires
102-
// reconstructing the task dependency chain and is more complex in practice.
103-
jar {
104-
dependsOn proguard
105-
from zipTree(proguardedJar)
106-
exclude { entry ->
107-
sourceSets.main.output.files*.absolutePath.any {
108-
entry.file.absolutePath.startsWith it
109-
}
110-
}
111-
}
112-
11318
apply from: "${rootProject.projectDir}/scripts/upload.gradle"

annotation/compiler/src/main/java/com/bumptech/glide/annotation/compiler/ProcessorUtil.java

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.google.common.collect.FluentIterable;
1111
import com.google.common.collect.ImmutableBiMap;
1212
import com.google.common.collect.ImmutableList;
13+
import com.google.common.collect.ImmutableSet;
1314
import com.google.common.collect.Lists;
1415
import com.squareup.javapoet.AnnotationSpec;
1516
import com.squareup.javapoet.ClassName;
@@ -20,18 +21,13 @@
2021
import com.squareup.javapoet.TypeName;
2122
import com.squareup.javapoet.TypeSpec;
2223
import com.squareup.javapoet.TypeVariableName;
23-
import com.sun.tools.javac.code.Attribute;
24-
import com.sun.tools.javac.code.Type.ClassType;
2524
import java.lang.annotation.Annotation;
26-
import java.lang.reflect.InvocationTargetException;
27-
import java.lang.reflect.Method;
2825
import java.util.ArrayList;
2926
import java.util.Collection;
30-
import java.util.Collections;
3127
import java.util.HashSet;
3228
import java.util.LinkedHashSet;
3329
import java.util.List;
34-
import java.util.Map;
30+
import java.util.Locale;
3531
import java.util.Set;
3632
import javax.annotation.Nullable;
3733
import javax.annotation.processing.ProcessingEnvironment;
@@ -46,6 +42,8 @@
4642
import javax.lang.model.element.TypeElement;
4743
import javax.lang.model.element.TypeParameterElement;
4844
import javax.lang.model.element.VariableElement;
45+
import javax.lang.model.type.DeclaredType;
46+
import javax.lang.model.type.TypeKind;
4947
import javax.lang.model.type.TypeMirror;
5048
import javax.lang.model.type.TypeVariable;
5149
import javax.lang.model.util.ElementFilter;
@@ -408,7 +406,7 @@ private static String computeParameterName(VariableElement parameter, TypeName t
408406
}
409407
}
410408
if (allCaps) {
411-
name = rawClassName.toLowerCase();
409+
name = rawClassName.toLowerCase(Locale.ROOT);
412410
} else {
413411
int indexOfLastWordStart = 0;
414412
char[] chars = rawClassName.toCharArray();
@@ -431,7 +429,7 @@ private static String computeParameterName(VariableElement parameter, TypeName t
431429

432430
private static String getSmartPrimitiveParameterName(VariableElement parameter) {
433431
for (AnnotationMirror annotation : parameter.getAnnotationMirrors()) {
434-
String annotationName = annotation.getAnnotationType().toString().toUpperCase();
432+
String annotationName = annotation.getAnnotationType().toString().toUpperCase(Locale.ROOT);
435433
if (annotationName.endsWith("RES")) {
436434
// Catch annotations like StringRes
437435
return "id";
@@ -539,7 +537,7 @@ List<ExecutableElement> findStaticMethods(TypeElement clazz) {
539537
.toList();
540538
}
541539

542-
Set<String> findClassValuesFromAnnotationOnClassAsNames(
540+
ImmutableSet<String> findClassValuesFromAnnotationOnClassAsNames(
543541
Element clazz, Class<? extends Annotation> annotationClass) {
544542
String annotationClassName = annotationClass.getName();
545543
AnnotationValue excludedModuleAnnotationValue = null;
@@ -549,67 +547,48 @@ Set<String> findClassValuesFromAnnotationOnClassAsNames(
549547
if (!annotationClassName.equals(annotationMirror.getAnnotationType().toString())) {
550548
continue;
551549
}
552-
Set<? extends Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> values =
553-
annotationMirror.getElementValues().entrySet();
554-
// Excludes has only one value. If we ever change that, we'd need to iterate over all
555-
// values in the entry set and compare the keys to whatever our Annotation's attribute is
556-
// (usually value).
557-
if (values.size() != 1) {
558-
throw new IllegalArgumentException("Expected single value, but found: " + values);
550+
551+
var entries = annotationMirror.getElementValues().entrySet();
552+
if (entries.size() != 1) {
553+
throw new IllegalArgumentException("Expected single value, but found: " + entries);
559554
}
560-
excludedModuleAnnotationValue = values.iterator().next().getValue();
561-
if (excludedModuleAnnotationValue == null
562-
|| excludedModuleAnnotationValue instanceof Attribute.UnresolvedClass) {
555+
excludedModuleAnnotationValue = entries.iterator().next().getValue();
556+
if (excludedModuleAnnotationValue == null) {
563557
throw new IllegalArgumentException(
564558
"Failed to find value for: "
565559
+ annotationClass
566560
+ " from mirrors: "
567561
+ clazz.getAnnotationMirrors());
568562
}
569563
}
564+
570565
if (excludedModuleAnnotationValue == null) {
571-
return Collections.emptySet();
566+
return ImmutableSet.of();
572567
}
568+
573569
Object value = excludedModuleAnnotationValue.getValue();
574570
if (value instanceof List) {
575-
List<?> values = (List<?>) value;
576-
Set<String> result = new HashSet<>(values.size());
577-
for (Object current : values) {
578-
result.add(getExcludedModuleClassFromAnnotationAttribute(clazz, current));
571+
LinkedHashSet<String> out = new LinkedHashSet<>();
572+
for (Object o : (List<?>) value) {
573+
AnnotationValue av = (AnnotationValue) o;
574+
out.add(qualifiedNameFromTypeMirror((TypeMirror) av.getValue()));
579575
}
580-
return result;
576+
return ImmutableSet.copyOf(out);
581577
} else {
582-
ClassType classType = (ClassType) value;
583-
return Collections.singleton(classType.toString());
578+
return ImmutableSet.of(qualifiedNameFromTypeMirror((TypeMirror) value));
584579
}
585580
}
586581

587-
// We should be able to cast to Attribute.Class rather than use reflection, but there are some
588-
// compilers that seem to break when we do so. See #2673 for an example.
589-
private static String getExcludedModuleClassFromAnnotationAttribute(
590-
Element clazz, Object attribute) {
591-
if (attribute.getClass().getSimpleName().equals("UnresolvedClass")) {
592-
throw new IllegalArgumentException(
593-
"Failed to parse @Excludes for: "
594-
+ clazz
595-
+ ", one or more excluded Modules could not be found at compile time. Ensure that all"
596-
+ "excluded Modules are included in your classpath.");
582+
static String qualifiedNameFromTypeMirror(TypeMirror type) {
583+
if (type.getKind() == TypeKind.ERROR) {
584+
throw new IllegalArgumentException("Unresolved class type in annotation: " + type);
597585
}
598-
Method[] methods = attribute.getClass().getDeclaredMethods();
599-
if (methods == null || methods.length == 0) {
600-
throw new IllegalArgumentException(
601-
"Failed to parse @Excludes for: " + clazz + ", invalid exclude: " + attribute);
602-
}
603-
for (Method method : methods) {
604-
if (method.getName().equals("getValue")) {
605-
try {
606-
return method.invoke(attribute).toString();
607-
} catch (IllegalAccessException | InvocationTargetException e) {
608-
throw new IllegalArgumentException("Failed to parse @Excludes for: " + clazz, e);
609-
}
610-
}
586+
if (type.getKind() == TypeKind.DECLARED) {
587+
DeclaredType dt = (DeclaredType) type;
588+
TypeElement te = (TypeElement) dt.asElement();
589+
return te.getQualifiedName().toString();
611590
}
612-
throw new IllegalArgumentException("Failed to parse @Excludes for: " + clazz);
591+
return type.toString();
613592
}
614593

615594
private enum MethodType {

0 commit comments

Comments
 (0)