Skip to content

Commit 799d925

Browse files
authored
Remove dependency on org.reflections:reflections (#654)
1 parent 5328777 commit 799d925

11 files changed

Lines changed: 51 additions & 73 deletions

File tree

google-ads-codegen/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ dependencies {
2525
api project(":google-ads-stubs-lib")
2626
implementation 'org.slf4j:slf4j-api:1.7.25'
2727
implementation 'com.squareup:javapoet:1.11.1'
28-
implementation 'org.reflections:reflections:0.9.11'
2928
testImplementation 'com.google.api:gax-grpc:1.65.1:testlib'
3029
testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0'
3130
testImplementation 'org.mockito:mockito-all:1.9.5'
@@ -64,4 +63,4 @@ publishing {
6463
"Generated code (stus) and generators for library")
6564
}
6665
}
67-
}
66+
}

google-ads-codegen/src/main/java/com/google/ads/googleads/codegen/RunCodeGenerators.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@
1515
package com.google.ads.googleads.codegen;
1616

1717
import com.google.ads.googleads.lib.stubs.annotations.VersionDescriptor;
18+
import com.google.common.base.Preconditions;
1819
import com.google.common.collect.ImmutableList;
1920
import com.google.common.collect.ImmutableMap;
21+
import com.google.common.reflect.ClassPath;
22+
import com.google.common.reflect.ClassPath.ClassInfo;
2023
import java.io.File;
24+
import java.io.IOException;
2125
import java.util.List;
2226
import java.util.Map;
2327
import java.util.Set;
2428
import java.util.stream.Collectors;
25-
import org.reflections.Reflections;
26-
import org.reflections.scanners.SubTypesScanner;
27-
import org.reflections.scanners.TypeAnnotationsScanner;
28-
import org.reflections.util.ClasspathHelper;
29-
import org.reflections.util.ConfigurationBuilder;
3029

3130
/**
3231
* Generates various classes which require knowing the available Google Ads API major versions.
@@ -56,12 +55,24 @@ public static void main(String[] args) {
5655
* major version by the GAPIC generator pipeline.
5756
*/
5857
private static Set<Class<?>> loadAnnotatedClasses() {
59-
Reflections reflections =
60-
new Reflections(
61-
new ConfigurationBuilder()
62-
.addUrls(ClasspathHelper.forPackage("com.google.ads.googleads"))
63-
.addScanners(new TypeAnnotationsScanner(), new SubTypesScanner()));
64-
return reflections.getTypesAnnotatedWith(VersionDescriptor.class);
58+
try {
59+
Set<Class<?>> annotatedClasses =
60+
ClassPath.from(ClassLoader.getSystemClassLoader())
61+
.getTopLevelClassesRecursive("com.google.ads.googleads")
62+
.stream()
63+
.map(ClassInfo::load)
64+
.filter(c -> c.getAnnotation(VersionDescriptor.class) != null)
65+
.collect(Collectors.toSet());
66+
// Verifies annotated classes were found.
67+
Preconditions.checkState(
68+
!annotatedClasses.isEmpty(),
69+
"No types found with annotation: %s",
70+
VersionDescriptor.class);
71+
return annotatedClasses;
72+
} catch (IOException e) {
73+
throw new RuntimeException(
74+
"Failed to find classes annotated with " + VersionDescriptor.class.getSimpleName(), e);
75+
}
6576
}
6677

6778
/** Pulls the @VersionDescriptor instance from annotated classes. */

google-ads-examples/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ dependencies {
4646
implementation 'com.beust:jcommander:1.72'
4747
implementation 'joda-time:joda-time:2.8.2'
4848
runtimeOnly 'org.apache.logging.log4j:log4j-slf4j-impl:2.16.0'
49-
testImplementation 'org.reflections:reflections:0.9.11'
5049
testImplementation 'org.mockito:mockito-core:2.27.0'
5150
testImplementation 'org.hamcrest:hamcrest:2.2'
5251
}

google-ads-examples/src/test/java/com/google/ads/googleads/examples/utils/ValidateParametersTest.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,45 @@
2020
import static org.junit.Assert.assertEquals;
2121

2222
import com.beust.jcommander.Parameter;
23+
import com.google.common.collect.ImmutableList;
24+
import com.google.common.reflect.ClassPath;
25+
import com.google.common.reflect.ClassPath.ClassInfo;
26+
import java.io.IOException;
2327
import java.lang.reflect.Field;
2428
import java.lang.reflect.Modifier;
29+
import java.util.Arrays;
2530
import java.util.Set;
31+
import java.util.stream.Collectors;
2632
import org.junit.Test;
2733
import org.junit.runner.RunWith;
2834
import org.junit.runners.JUnit4;
29-
import org.reflections.Reflections;
30-
import org.reflections.scanners.FieldAnnotationsScanner;
3135

3236
@RunWith(JUnit4.class)
3337
public class ValidateParametersTest {
3438

3539
@Test
36-
public void ensure_no_final_parmeters() {
40+
public void ensure_no_final_parameters() throws IOException {
3741
// Avoids silent failures from JCommander when parsing flags if the field is final.
38-
Reflections reflections =
39-
new Reflections("com.google.ads.googleads.examples", new FieldAnnotationsScanner());
40-
Set<Field> annotatedFields = reflections.getFieldsAnnotatedWith(Parameter.class);
41-
assertThat("parameters", annotatedFields.size(), is(greaterThan(0)));
42+
Set<Field> annotatedFields =
43+
ClassPath.from(getClass().getClassLoader())
44+
.getTopLevelClassesRecursive("com.google.ads.googleads.examples")
45+
.stream()
46+
.map(ClassInfo::load)
47+
.flatMap(
48+
c ->
49+
ImmutableList.<Class<?>>builder()
50+
.add(c)
51+
.addAll(Arrays.asList(c.getDeclaredClasses()))
52+
.build()
53+
.stream())
54+
.flatMap(c -> Arrays.stream(c.getDeclaredFields()))
55+
.filter(f -> f.getAnnotation(Parameter.class) != null)
56+
.collect(Collectors.toSet());
57+
58+
// Asserts that the above discovered at least 300 annotated fields. Examples contain closer to
59+
// 400 such fields as of the writing of this test, but this leaves some room in case we decide
60+
// to remove some examples.
61+
assertThat("parameters", annotatedFields.size(), is(greaterThan(300)));
4262
for (Field field : annotatedFields) {
4363
assertEquals("field is final: " + field, 0, field.getModifiers() & Modifier.FINAL);
4464
}

third_party/LICENSES.html

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -121,30 +121,6 @@ <h3>License text URL</h3>
121121
<a href='http://www.apache.org/licenses/LICENSE-2.0.txt'>Show license agreement</a>
122122
</td>
123123
</tr>
124-
<tr>
125-
<td class='dependencies'>org.javassist:javassist:3.21.0-GA</td>
126-
<td class='licenseName'>javassist-3.21.0-GA.jar</td>
127-
<td class='licenseName'>MPL 1.1</td>
128-
<td class='license'>
129-
<a href='http://www.mozilla.org/MPL/MPL-1.1.html'>Show license agreement</a>
130-
</td>
131-
</tr>
132-
<tr>
133-
<td class='dependencies'>org.javassist:javassist:3.21.0-GA</td>
134-
<td class='licenseName'>javassist-3.21.0-GA.jar</td>
135-
<td class='licenseName'>LGPL 2.1</td>
136-
<td class='license'>
137-
<a href='http://www.gnu.org/licenses/lgpl-2.1.html'>Show license agreement</a>
138-
</td>
139-
</tr>
140-
<tr>
141-
<td class='dependencies'>org.javassist:javassist:3.21.0-GA</td>
142-
<td class='licenseName'>javassist-3.21.0-GA.jar</td>
143-
<td class='licenseName'>Apache License 2.0</td>
144-
<td class='license'>
145-
<a href='http://www.apache.org/licenses/'>Show license agreement</a>
146-
</td>
147-
</tr>
148124
<tr>
149125
<td class='dependencies'>com.google.protobuf:protobuf-java-util:3.18.1</td>
150126
<td class='licenseName'>protobuf-java-util-3.18.1.jar</td>
@@ -201,22 +177,6 @@ <h3>License text URL</h3>
201177
<a href='https://raw.githubusercontent.com/ThreeTen/threetenbp/master/LICENSE.txt'>Show license agreement</a>
202178
</td>
203179
</tr>
204-
<tr>
205-
<td class='dependencies'>org.reflections:reflections:0.9.11</td>
206-
<td class='licenseName'>reflections-0.9.11.jar</td>
207-
<td class='licenseName'>WTFPL</td>
208-
<td class='license'>
209-
<a href='http://www.wtfpl.net/'>Show license agreement</a>
210-
</td>
211-
</tr>
212-
<tr>
213-
<td class='dependencies'>org.reflections:reflections:0.9.11</td>
214-
<td class='licenseName'>reflections-0.9.11.jar</td>
215-
<td class='licenseName'>The New BSD License</td>
216-
<td class='license'>
217-
<a href='http://www.opensource.org/licenses/bsd-license.html'>Show license agreement</a>
218-
</td>
219-
</tr>
220180
<tr>
221181
<td class='dependencies'>com.google.guava:failureaccess:1.0.1</td>
222182
<td class='licenseName'>failureaccess-1.0.1.jar</td>

third_party/LICENSES.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

third_party/LICENSES.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@
3535
<file>auto-service-1.0-rc2.jar</file>
3636
<license name='Apache 2.0' url='http://www.apache.org/licenses/LICENSE-2.0.txt' />
3737
</dependency>
38-
<dependency name='org.javassist:javassist:3.21.0-GA'>
39-
<file>javassist-3.21.0-GA.jar</file>
40-
<license name='MPL 1.1' url='http://www.mozilla.org/MPL/MPL-1.1.html' />
41-
<license name='LGPL 2.1' url='http://www.gnu.org/licenses/lgpl-2.1.html' />
42-
<license name='Apache License 2.0' url='http://www.apache.org/licenses/' />
43-
</dependency>
4438
<dependency name='com.google.protobuf:protobuf-java-util:3.18.1'>
4539
<file>protobuf-java-util-3.18.1.jar</file>
4640
<license name='3-Clause BSD License' url='https://opensource.org/licenses/BSD-3-Clause' />
@@ -69,11 +63,6 @@
6963
<file>threetenbp-1.5.1.jar</file>
7064
<license name='BSD 3-clause' url='https://raw.githubusercontent.com/ThreeTen/threetenbp/master/LICENSE.txt' />
7165
</dependency>
72-
<dependency name='org.reflections:reflections:0.9.11'>
73-
<file>reflections-0.9.11.jar</file>
74-
<license name='WTFPL' url='http://www.wtfpl.net/' />
75-
<license name='The New BSD License' url='http://www.opensource.org/licenses/bsd-license.html' />
76-
</dependency>
7766
<dependency name='com.google.guava:failureaccess:1.0.1'>
7867
<file>failureaccess-1.0.1.jar</file>
7968
<license name='The Apache Software License, Version 2.0' url='http://www.apache.org/licenses/LICENSE-2.0.txt' />
-512 KB
Binary file not shown.
-718 KB
Binary file not shown.
-54 KB
Binary file not shown.

0 commit comments

Comments
 (0)