这更多是一种解决方法,但是我想如果没有更简单的方法来访问Guava版本,您可以这样做:
在大多数Guava版本中,都添加/删除了类/字段/方法。您可以尝试在堆转储中查找它们,并根据它们的存在确定Guava版本。
就像是:
public class ClassExistsPredicate implements Predicate<String> { private final HeapDump heapDump; public ClassExistsPredicate(HeapDump heapDump) { this.heapDump = heapDump; } @Override public boolean apply(String fullyQualifiedClassName) { // checks whether the given class exists in the heap dump return true; }}public enum GuavaVersion { R01, R02 { @Override Set<String> getAddedClasses() { return ImmutableSet.of("com.google.common.base.Foo"); } }, R03 { @Override Set<String> getAddedClasses() { return ImmutableSet.of("com.google.common.collect.ForwardingFooIterator"); } }, R04 { @Override Set<String> getAddedClasses() { return ImmutableSet.of("com.google.common.collect.FooFoo2"); } }; Set<String> getAddedClasses() { return ImmutableSet.of(); } public static GuavaVersion getGuavaVersionFor(HeapDump heapDump) { ClassExistsPredicate classExists = new ClassExistsPredicate(heapDump); for (GuavaVersion version : Lists.reverse(Arrays.asList(GuavaVersion.values()))) { if (Iterables.all(version.getAddedClasses(), classExists)) { return version; } } throw new RuntimeException("Unable to determine Guava version..."); }}显然,您应该缓存Guava版本号,因为计算可能会很慢…该方法可以扩展为考虑添加的方法/字段。
这种方法也可以与其他项目一起使用。



