Skip to content

Commit 80338fc

Browse files
authored
Port removal of reflections from master-2.0 (#144)
1 parent 7b5ab68 commit 80338fc

File tree

4 files changed

+44
-244
lines changed

4 files changed

+44
-244
lines changed

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
<metrics.jvm.extra.version>0.10.0</metrics.jvm.extra.version>
9999
<oval.version>1.90</oval.version>
100100
<protobuf.version>3.6.0</protobuf.version>
101-
<reflections.version>0.9.11</reflections.version>
102101
<scala.version>2.11</scala.version>
103102
<scala.library.version>2.11.7</scala.library.version>
104103
<slf4j.version>1.7.25</slf4j.version>
@@ -595,11 +594,6 @@
595594
<artifactId>fastutil</artifactId>
596595
<version>${fastutil.version}</version>
597596
</dependency>
598-
<dependency>
599-
<groupId>org.reflections</groupId>
600-
<artifactId>reflections</artifactId>
601-
<version>${reflections.version}</version>
602-
</dependency>
603597
<dependency>
604598
<groupId>org.aspectj</groupId>
605599
<artifactId>aspectjrt</artifactId>

src/main/java/com/arpnetworking/tsdcore/statistics/StatisticFactory.java

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717

1818
import com.arpnetworking.steno.Logger;
1919
import com.arpnetworking.steno.LoggerFactory;
20-
import com.arpnetworking.utility.InterfaceDatabase;
21-
import com.arpnetworking.utility.ReflectionsDatabase;
22-
import com.google.common.collect.ImmutableMap;
2320
import com.google.common.collect.ImmutableSet;
2421
import com.google.common.collect.Maps;
25-
import com.google.common.collect.Sets;
22+
import com.google.common.reflect.ClassPath;
2623

24+
import java.io.IOException;
2725
import java.lang.reflect.Constructor;
2826
import java.lang.reflect.InvocationTargetException;
2927
import java.lang.reflect.Modifier;
30-
import java.util.Map;
3128
import java.util.Optional;
32-
import java.util.Set;
29+
import java.util.concurrent.ConcurrentMap;
3330

3431
/**
3532
* Creates statistics.
@@ -43,7 +40,7 @@ public class StatisticFactory {
4340
* Get a statistic by name.
4441
*
4542
* @param name The name of the desired statistic.
46-
* @return A new <code>Statistic</code>.
43+
* @return A new {@link Statistic}.
4744
*/
4845
public Statistic getStatistic(final String name) {
4946
final Optional<Statistic> statistic = tryGetStatistic(name);
@@ -63,77 +60,65 @@ public Optional<Statistic> tryGetStatistic(final String name) {
6360
return Optional.ofNullable(STATISTICS_BY_NAME_AND_ALIAS.get(name));
6461
}
6562

66-
/**
67-
* Get all registered <code>Statistic</code> instances.
68-
*
69-
* @return A new <code>Statistic</code>.
70-
*/
71-
public ImmutableSet<Statistic> getAllStatistics() {
72-
return ALL_STATISTICS;
73-
}
74-
75-
/**
76-
* Creates a statistic from a name.
77-
*
78-
* @param statistic The name of the desired statistic.
79-
* @return A new <code>Statistic</code>.
80-
* @deprecated Use <code>getStatistic</code> instead.
81-
*/
82-
@Deprecated
83-
public Optional<Statistic> createStatistic(final String statistic) {
84-
return Optional.ofNullable(STATISTICS_BY_NAME_AND_ALIAS.get(statistic));
63+
private static void checkedPut(final ConcurrentMap<String, Statistic> map, final Statistic statistic) {
64+
checkedPut(map, statistic, statistic.getName());
65+
for (final String alias : statistic.getAliases()) {
66+
checkedPut(map, statistic, alias);
67+
}
8568
}
8669

87-
private static void checkedPut(final Map<String, Statistic> map, final Statistic statistic, final String key) {
70+
private static void checkedPut(final ConcurrentMap<String, Statistic> map, final Statistic statistic, final String key) {
8871
final Statistic existingStatistic = map.get(key);
8972
if (existingStatistic != null) {
9073
if (!existingStatistic.equals(statistic)) {
9174
LOGGER.error()
92-
.setMessage("Statistic already registered")
93-
.addData("key", key)
94-
.addData("existing", existingStatistic)
95-
.addData("new", statistic)
96-
.log();
75+
.setMessage("Statistic already registered")
76+
.addData("key", key)
77+
.addData("existing", existingStatistic)
78+
.addData("new", statistic)
79+
.log();
9780
}
9881
return;
9982
}
10083
map.put(key, statistic);
10184
}
10285

103-
private static final ImmutableMap<String, Statistic> STATISTICS_BY_NAME_AND_ALIAS;
104-
private static final ImmutableSet<Statistic> ALL_STATISTICS;
105-
private static final InterfaceDatabase INTERFACE_DATABASE = ReflectionsDatabase.newInstance();
86+
private static final ConcurrentMap<String, Statistic> STATISTICS_BY_NAME_AND_ALIAS;
10687
private static final Logger LOGGER = LoggerFactory.getLogger(StatisticFactory.class);
10788

10889
static {
10990
// NOTE: Do not put log messages in static blocks since they can lock the logger thread!
110-
final Map<String, Statistic> statisticByNameAndAlias = Maps.newHashMap();
111-
final Set<Statistic> allStatistics = Sets.newHashSet();
112-
final Set<Class<? extends Statistic>> statisticClasses = INTERFACE_DATABASE.findClassesWithInterface(Statistic.class);
113-
for (final Class<? extends Statistic> statisticClass : statisticClasses) {
114-
if (!statisticClass.isInterface() && !Modifier.isAbstract(statisticClass.getModifiers())) {
115-
try {
116-
final Constructor<? extends Statistic> constructor = statisticClass.getDeclaredConstructor();
117-
if (!constructor.isAccessible()) {
118-
constructor.setAccessible(true);
119-
}
120-
final Statistic statistic = constructor.newInstance();
121-
allStatistics.add(statistic);
122-
checkedPut(statisticByNameAndAlias, statistic, statistic.getName());
123-
for (final String alias : statistic.getAliases()) {
124-
checkedPut(statisticByNameAndAlias, statistic, alias);
91+
final ConcurrentMap<String, Statistic> statisticByNameAndAlias = Maps.newConcurrentMap();
92+
try {
93+
final ImmutableSet<ClassPath.ClassInfo> statisticClasses = ClassPath.from(StatisticFactory.class.getClassLoader())
94+
.getTopLevelClasses("com.arpnetworking.tsdcore.statistics");
95+
for (final ClassPath.ClassInfo statisticClassInfo : statisticClasses) {
96+
final Class<?> statisticClass = statisticClassInfo.load();
97+
if (!statisticClass.isInterface() && !Modifier.isAbstract(statisticClass.getModifiers())
98+
&& Statistic.class.isAssignableFrom(statisticClass)) {
99+
try {
100+
// The constructor type is implied by the assignability
101+
// of the statisticClass to the Statistic interface
102+
@SuppressWarnings("unchecked")
103+
final Constructor<? extends Statistic> constructor =
104+
(Constructor<? extends Statistic>) statisticClass.getDeclaredConstructor();
105+
if (!constructor.isAccessible()) {
106+
constructor.setAccessible(true);
107+
}
108+
checkedPut(statisticByNameAndAlias, constructor.newInstance());
109+
} catch (final InvocationTargetException | NoSuchMethodException
110+
| InstantiationException | IllegalAccessException e) {
111+
LOGGER.warn()
112+
.setMessage("Unable to load statistic")
113+
.addData("class", statisticClass)
114+
.setThrowable(e)
115+
.log();
125116
}
126-
} catch (final InvocationTargetException | NoSuchMethodException
127-
| InstantiationException | IllegalAccessException e) {
128-
LOGGER.warn()
129-
.setMessage("Unable to load statistic")
130-
.addData("class", statisticClass)
131-
.setThrowable(e)
132-
.log();
133117
}
134118
}
119+
} catch (final IOException e) {
120+
throw new RuntimeException("Statistic discovery failed", e);
135121
}
136-
STATISTICS_BY_NAME_AND_ALIAS = ImmutableMap.copyOf(statisticByNameAndAlias);
137-
ALL_STATISTICS = ImmutableSet.copyOf(allStatistics);
122+
STATISTICS_BY_NAME_AND_ALIAS = statisticByNameAndAlias;
138123
}
139124
}

src/main/java/com/arpnetworking/utility/ReflectionsDatabase.java

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/test/java/com/arpnetworking/utility/ReflectionsDatabaseTest.java

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)