Skip to content

Commit e029694

Browse files
TWiStErRobsjudd
authored andcommitted
Clean up synthetic accessor method rot (#2762)
* Clean up accessor method rotting. * Make tests pass on Windows (10, JDK 8x64) (with less ignores than before) * Fix error prone version
1 parent 3ed41ae commit e029694

File tree

22 files changed

+202
-167
lines changed

22 files changed

+202
-167
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ docs/**/*
2323

2424
# Intellij
2525
*.iml
26+
*.ipr
27+
*.iws
2628
.idea/**
2729
!.idea/codeStyleSettings.xml

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ buildscript {
1515
dependencies {
1616
classpath "com.android.tools.build:gradle:${ANDROID_GRADLE_VERSION}"
1717
if (!hasProperty('DISABLE_ERROR_PRONE')) {
18-
classpath "net.ltgt.gradle:gradle-errorprone-plugin:${ERROR_PRONE_VERSION}"
18+
classpath "net.ltgt.gradle:gradle-errorprone-plugin:${ERROR_PRONE_PLUGIN_VERSION}"
1919
}
2020
classpath "se.bjurr.violations:violations-gradle-plugin:${VIOLATIONS_PLUGIN_VERSION}"
2121
}

checkstyle.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373

7474
<!-- Allow common trailing comments used to describe suppressions -->
7575
<module name="TrailingComment">
76-
<property name="legalComment" value="Public API" />
76+
<property name="legalComment" value="^Public API.?$|^NOPMD .*$" />
7777
</module>
7878

7979
<!-- Checks for imports. -->

gradle.properties

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ JSR_305_VERSION=3.0.2
3232
AUTO_SERVICE_VERSION=1.0-rc3
3333
JAVAPOET_VERSION=1.9.0
3434

35-
PMD_VERSION=5.4.0
35+
PMD_VERSION=5.8.1
3636
FINDBUGS_VERSION=3.0.0
37-
ERROR_PRONE_VERSION=0.0.13
37+
ERROR_PRONE_VERSION=2.1.4-SNAPSHOT
38+
ERROR_PRONE_PLUGIN_VERSION=0.0.13
3839
VIOLATIONS_PLUGIN_VERSION=1.3
3940

4041
COMPILE_SDK_VERSION=27

integration/gifencoder/src/test/java/com/bumptech/glide/integration/gifencoder/ReEncodingGifResourceEncoderTest.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static com.google.common.truth.Truth.assertThat;
44
import static org.junit.Assert.assertFalse;
55
import static org.junit.Assert.assertTrue;
6-
import static org.junit.Assume.assumeTrue;
76
import static org.mockito.Matchers.any;
87
import static org.mockito.Matchers.anyInt;
98
import static org.mockito.Matchers.eq;
@@ -95,6 +94,8 @@ public void setUp() {
9594

9695
@After
9796
public void tearDown() {
97+
// GC before delete() to release files on Windows (https://stackoverflow.com/a/4213208/253468)
98+
System.gc();
9899
if (file.exists() && !file.delete()) {
99100
throw new RuntimeException("Failed to delete file");
100101
}
@@ -120,8 +121,6 @@ public void testEncodeStrategy_withEncodeTransformationFalse_returnsSource() {
120121
@Test
121122
public void testEncode_withEncodeTransformationFalse_writesSourceDataToStream()
122123
throws IOException {
123-
// Most likely an instance of http://stackoverflow.com/q/991489/253468
124-
assumeTrue(!System.getProperty("os.name").startsWith("Windows"));
125124
options.set(ReEncodingGifResourceEncoder.ENCODE_TRANSFORMATION, false);
126125
String expected = "testString";
127126
byte[] data = expected.getBytes("UTF-8");
@@ -134,7 +133,6 @@ public void testEncode_withEncodeTransformationFalse_writesSourceDataToStream()
134133
@Test
135134
public void testEncode_WithEncodeTransformationFalse_whenOsThrows_returnsFalse()
136135
throws IOException {
137-
138136
options.set(ReEncodingGifResourceEncoder.ENCODE_TRANSFORMATION, false);
139137
byte[] data = "testString".getBytes("UTF-8");
140138
when(gifDrawable.getBuffer()).thenReturn(ByteBuffer.wrap(data));
@@ -312,10 +310,7 @@ public void testRecyclesFrameResourceAfterWritingIfFrameResourceIsNotTransformed
312310
}
313311

314312
@Test
315-
public void testWritesBytesDirectlyToDiskIfTransformationIsUnitTransformation()
316-
throws IOException {
317-
// Most likely an instance of http://stackoverflow.com/q/991489/253468
318-
assumeTrue(!System.getProperty("os.name").startsWith("Windows"));
313+
public void testWritesBytesDirectlyToDiskIfTransformationIsUnitTransformation() {
319314
when(gifDrawable.getFrameTransformation()).thenReturn(UnitTransformation.<Bitmap>get());
320315
String expected = "expected";
321316
when(gifDrawable.getBuffer()).thenReturn(ByteBuffer.wrap(expected.getBytes()));

library/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ dependencies {
2020
testImplementation "org.robolectric:robolectric:${ROBOLECTRIC_VERSION}"
2121
testImplementation "com.squareup.okhttp3:mockwebserver:${MOCKWEBSERVER_VERSION}"
2222
testImplementation "com.android.support:support-v4:${ANDROID_SUPPORT_VERSION}"
23+
24+
if (project.plugins.hasPlugin('net.ltgt.errorprone')) {
25+
errorprone "com.google.errorprone:error_prone_core:${ERROR_PRONE_VERSION}"
26+
}
2327
}
2428

2529
android.testOptions.unitTests.all { Test testTask ->

library/pmd-ruleset.xml

+31-21
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
55

6-
<description>This ruleset was created from PMD.rul</description>
6+
<description>Check for flaws in Glide's codebase.</description>
77

88
<rule ref="rulesets/java/basic.xml">
99
<exclude name="AvoidBranchingStatementAsLastInLoop"/>
1010
</rule>
1111
<rule ref="rulesets/java/braces.xml"/>
12-
<rule ref="rulesets/java/strings.xml">
13-
<!-- TODO: This warns about annotations, apparently fixed in a later version. -->
14-
<exclude name="AvoidDuplicateLiterals"/>
12+
<rule ref="rulesets/java/strings.xml"/>
13+
<rule ref="rulesets/java/strings.xml/AvoidDuplicateLiterals">
14+
<properties>
15+
<property name="skipAnnotations" value="true" />
16+
</properties>
1517
</rule>
1618
<rule ref="rulesets/java/unusedcode.xml"/>
1719

@@ -28,31 +30,39 @@
2830
<exclude name="GodClass"/>
2931
</rule>
3032

33+
<rule ref="rulesets/java/design.xml/AccessorMethodGeneration"
34+
message="Avoid autogenerated methods to access private fields and methods of inner / outer classes.
35+
Use @Synthetic to flag members made more visible than necessary to prevent accessors.">
36+
<properties>
37+
<!-- Ignore references to `private static final * * = <literal>`
38+
Suppress via XPath: current node (access that generates the accessor) is .
39+
Check if there exists a FieldDeclaration (private static final)
40+
which has a VariableInitializer with a Literal
41+
and the name (@Image) of the declaration is the same as the accessed member.
42+
TODO calculated constants are false positive https://github.com/pmd/pmd/issues/808
43+
-->
44+
<property name="violationSuppressXPath" value="
45+
.[@Image =
46+
//FieldDeclaration[@Private = 'true' and @Static='true' and @Final='true']
47+
/VariableDeclarator[
48+
VariableInitializer/Expression/PrimaryExpression[not(PrimarySuffix)]/PrimaryPrefix/Literal
49+
]/VariableDeclaratorId/@Image
50+
]" />
51+
</properties>
52+
</rule>
53+
3154
<rule ref="rulesets/java/empty.xml/EmptyCatchBlock" message="Commented blocks are ok">
3255
<properties>
3356
<property name="allowCommentedBlocks" value="true"/>
3457
</properties>
3558
</rule>
3659

37-
38-
<!--Overrides default check to avoid violation when @Synthetic annotation is present-->
39-
<rule ref="rulesets/java/design.xml/UncommentedEmptyConstructor"
40-
message="Document empty constructor">
41-
60+
<!-- Configures check to avoid violation when @Synthetic annotation is present. -->
61+
<rule ref="rulesets/java/design.xml/UncommentedEmptyConstructor">
4262
<properties>
43-
<property name="xpath">
44-
<value>
45-
<![CDATA[
46-
//ConstructorDeclaration[@Private='false'][count(BlockStatement) = 0 and
47-
($ignoreExplicitConstructorInvocation = 'true' or not(ExplicitConstructorInvocation)) and @containsComment = 'false'
48-
and not(../Annotation/MarkerAnnotation/Name[@Image='Synthetic'])]
49-
]]>
50-
</value>
51-
</property>
52-
63+
<property name="violationSuppressXPath"
64+
value="../Annotation/MarkerAnnotation/Name[@Image='Synthetic']" />
5365
</properties>
54-
5566
</rule>
5667

57-
5868
</ruleset>

library/src/main/java/com/bumptech/glide/RequestBuilder.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.bumptech.glide.request.target.ViewTarget;
3131
import com.bumptech.glide.signature.ApplicationVersionSignature;
3232
import com.bumptech.glide.util.Preconditions;
33+
import com.bumptech.glide.util.Synthetic;
3334
import com.bumptech.glide.util.Util;
3435
import java.io.File;
3536
import java.net.URL;
@@ -572,7 +573,7 @@ public <Y extends Target<TranscodeType>> Y into(@NonNull Y target) {
572573
}
573574

574575
@NonNull
575-
private <Y extends Target<TranscodeType>> Y into(
576+
@Synthetic <Y extends Target<TranscodeType>> Y into(
576577
@NonNull Y target,
577578
@Nullable RequestListener<TranscodeType> targetListener) {
578579
return into(target, targetListener, getMutableOptions());

library/src/main/java/com/bumptech/glide/load/engine/ActiveResources.java

+22-17
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ EngineResource<?> get(Key key) {
9191
return active;
9292
}
9393

94-
private void cleanupActiveReference(@NonNull ResourceWeakReference ref) {
94+
@SuppressWarnings("WeakerAccess")
95+
@Synthetic void cleanupActiveReference(@NonNull ResourceWeakReference ref) {
9596
Util.assertMainThread();
9697
activeEngineResources.remove(ref.key);
9798

@@ -112,29 +113,33 @@ private ReferenceQueue<EngineResource<?>> getReferenceQueue() {
112113
@Override
113114
public void run() {
114115
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
115-
ResourceWeakReference ref;
116-
while (!isShutdown) {
117-
try {
118-
ref = (ResourceWeakReference) resourceReferenceQueue.remove();
119-
mainHandler.obtainMessage(MSG_CLEAN_REF, ref).sendToTarget();
120-
121-
// This section for testing only.
122-
DequeuedResourceCallback current = cb;
123-
if (current != null) {
124-
current.onResourceDequeued();
125-
}
126-
// End for testing only.
127-
} catch (InterruptedException e) {
128-
Thread.currentThread().interrupt();
129-
}
130-
}
116+
cleanReferenceQueue();
131117
}
132118
}, "glide-active-resources");
133119
cleanReferenceQueueThread.start();
134120
}
135121
return resourceReferenceQueue;
136122
}
137123

124+
@SuppressWarnings("WeakerAccess")
125+
@Synthetic void cleanReferenceQueue() {
126+
while (!isShutdown) {
127+
try {
128+
ResourceWeakReference ref = (ResourceWeakReference) resourceReferenceQueue.remove();
129+
mainHandler.obtainMessage(MSG_CLEAN_REF, ref).sendToTarget();
130+
131+
// This section for testing only.
132+
DequeuedResourceCallback current = cb;
133+
if (current != null) {
134+
current.onResourceDequeued();
135+
}
136+
// End for testing only.
137+
} catch (InterruptedException e) {
138+
Thread.currentThread().interrupt();
139+
}
140+
}
141+
}
142+
138143
@VisibleForTesting
139144
void setDequeuedResourceCallback(DequeuedResourceCallback cb) {
140145
this.cb = cb;

0 commit comments

Comments
 (0)