java:基于guava ClassPath工具实现基于包名(package)的类扫描

google的guava库提供了一个类路径扫描的实用工具ClassPath(参见说明: https://github.com/google/guava/wiki/ReflectionExplained#classpath)工具,适用于非android的Java平台搜索类。基于它可以设计一个过滤包名的搜索工具。

导入依赖库

xml 复制代码
<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>20.0</version>
</dependency>

实现代码:GuavaPackageScanner.java

java 复制代码
import java.io.IOException;
import java.util.LinkedHashSet;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ClassInfo;

public class GuavaPackageScanner  {
	private final ClassPath classpath ;
	private final ClassLoader classLoader;
	private volatile ImmutableSet<Class<?>> classes;
	public GuavaPackageScanner() {
		try {
			this.classLoader = Thread.currentThread().getContextClassLoader();
			classpath = ClassPath.from(classLoader);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	private ImmutableSet<Class<?>> getClasses() {
		if(null == classes) {
			synchronized (this) {
				if(null == classes) {
					classes = FluentIterable.from(classpath.getResources())
							.filter(ClassInfo.class)
							.transform(new ClassInfoClassBuilder())
							.filter(Predicates.notNull())
							.toSet();
					System.out.printf("%d classes loaded\n",classes.size());
				}
			}
		}
		return classes;
	}

	public LinkedHashSet<Class<?>> getClasses(final String packagename) throws IOException {
		return Sets.newLinkedHashSet(
		Iterables.filter(getClasses(), new PackageFilter(packagename)));
	}
	private class ClassInfoClassBuilder implements Function<ClassInfo,Class<?>>{

		@Override
		public Class<?> apply(ClassInfo input) {
			try {
				return Class.forName(input.getName(), false, classLoader);
			} catch (Throwable e) {
				
			}
			return null;
		}};
	private static class PackageFilter implements Predicate<Class<?>>{

		private String packagename;

		public PackageFilter(String packagename) {
			this.packagename = packagename;
		}

		@Override
		public boolean apply(Class<?> input) {
			return input.getPackage().getName().startsWith(packagename);
		}
	}
}

调用示例:

PackageScannerTest.java

css 复制代码
import static org.junit.Assert.*;

import java.util.LinkedHashSet;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PackageScannerTest {
	public static final GuavaPackageScanner GUAVA_PACKAGE_SCANNER = new GuavaPackageScanner();
	@Test
	public void test6GuavaGetClasses() {
		try {
			/** 搜索com.google.common.base包下的所有类输出类名 */ 
			LinkedHashSet<Class<?>> urls = GUAVA_PACKAGE_SCANNER.getClasses("com.google.common.base");
			for(Class<?> url:urls) {
				System.out.printf("class:%s\n",url);
			}
			System.out.printf("class count:%d\n",urls.size());
		} catch (Throwable e) {
			e.printStackTrace();
			fail();
		}
	}

}

输出:

3677 classes loaded

class:class com.google.common.base.Absent

class:class com.google.common.base.AbstractIterator 1 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . A b s t r a c t I t e r a t o r 1 class:class com.google.common.base.AbstractIterator 1class:classcom.google.common.base.AbstractIteratorState

class:class com.google.common.base.AbstractIterator

class:class com.google.common.base.Ascii

class:class com.google.common.base.CaseFormat$1

class:class com.google.common.base.CaseFormat$2

class:class com.google.common.base.CaseFormat$3

class:class com.google.common.base.CaseFormat$4

class:class com.google.common.base.CaseFormat 5 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C a s e F o r m a t 5 class:class com.google.common.base.CaseFormat 5class:classcom.google.common.base.CaseFormatStringConverter

class:class com.google.common.base.CaseFormat

class:class com.google.common.base.CharMatcher 1 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r 1 class:class com.google.common.base.CharMatcher 1class:classcom.google.common.base.CharMatcherAnd

class:class com.google.common.base.CharMatcher A n y c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r Any class:class com.google.common.base.CharMatcher Anyclass:classcom.google.common.base.CharMatcherAnyOf

class:class com.google.common.base.CharMatcher A s c i i c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r Ascii class:class com.google.common.base.CharMatcher Asciiclass:classcom.google.common.base.CharMatcherBitSetMatcher

class:class com.google.common.base.CharMatcher B r e a k i n g W h i t e s p a c e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r BreakingWhitespace class:class com.google.common.base.CharMatcher BreakingWhitespaceclass:classcom.google.common.base.CharMatcherDigit

class:class com.google.common.base.CharMatcher F a s t M a t c h e r c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r FastMatcher class:class com.google.common.base.CharMatcher FastMatcherclass:classcom.google.common.base.CharMatcherForPredicate

class:class com.google.common.base.CharMatcher I n R a n g e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r InRange class:class com.google.common.base.CharMatcher InRangeclass:classcom.google.common.base.CharMatcherInvisible

class:class com.google.common.base.CharMatcher I s c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r Is class:class com.google.common.base.CharMatcher Isclass:classcom.google.common.base.CharMatcherIsEither

class:class com.google.common.base.CharMatcher I s N o t c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r IsNot class:class com.google.common.base.CharMatcher IsNotclass:classcom.google.common.base.CharMatcherJavaDigit

class:class com.google.common.base.CharMatcher J a v a I s o C o n t r o l c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r JavaIsoControl class:class com.google.common.base.CharMatcher JavaIsoControlclass:classcom.google.common.base.CharMatcherJavaLetter

class:class com.google.common.base.CharMatcher J a v a L e t t e r O r D i g i t c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r JavaLetterOrDigit class:class com.google.common.base.CharMatcher JavaLetterOrDigitclass:classcom.google.common.base.CharMatcherJavaLowerCase

class:class com.google.common.base.CharMatcher J a v a U p p e r C a s e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r JavaUpperCase class:class com.google.common.base.CharMatcher JavaUpperCaseclass:classcom.google.common.base.CharMatcherNamedFastMatcher

class:class com.google.common.base.CharMatcher N e g a t e d c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r Negated class:class com.google.common.base.CharMatcher Negatedclass:classcom.google.common.base.CharMatcherNegatedFastMatcher

class:class com.google.common.base.CharMatcher N o n e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r None class:class com.google.common.base.CharMatcher Noneclass:classcom.google.common.base.CharMatcherOr

class:class com.google.common.base.CharMatcher R a n g e s M a t c h e r c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C h a r M a t c h e r RangesMatcher class:class com.google.common.base.CharMatcher RangesMatcherclass:classcom.google.common.base.CharMatcherSingleWidth

class:class com.google.common.base.CharMatcher$Whitespace

class:class com.google.common.base.CharMatcher

class:class com.google.common.base.Charsets

class:class com.google.common.base.CommonMatcher

class:class com.google.common.base.CommonPattern

class:class com.google.common.base.Converter$1$1

class:class com.google.common.base.Converter 1 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C o n v e r t e r 1 class:class com.google.common.base.Converter 1class:classcom.google.common.base.ConverterConverterComposition

class:class com.google.common.base.Converter F u n c t i o n B a s e d C o n v e r t e r c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C o n v e r t e r FunctionBasedConverter class:class com.google.common.base.Converter FunctionBasedConverterclass:classcom.google.common.base.ConverterIdentityConverter

class:class com.google.common.base.Converter R e v e r s e C o n v e r t e r c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . C o n v e r t e r c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . D e f a u l t s c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . E n u m s ReverseConverter class:class com.google.common.base.Converter class:class com.google.common.base.Defaults class:class com.google.common.base.Enums ReverseConverterclass:classcom.google.common.base.Converterclass:classcom.google.common.base.Defaultsclass:classcom.google.common.base.EnumsStringConverter

class:class com.google.common.base.Enums

class:class com.google.common.base.Equivalence 1 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . E q u i v a l e n c e 1 class:class com.google.common.base.Equivalence 1class:classcom.google.common.base.EquivalenceEquals

class:class com.google.common.base.Equivalence E q u i v a l e n t T o P r e d i c a t e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . E q u i v a l e n c e EquivalentToPredicate class:class com.google.common.base.Equivalence EquivalentToPredicateclass:classcom.google.common.base.EquivalenceIdentity

class:class com.google.common.base.Equivalence W r a p p e r c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . E q u i v a l e n c e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . E x t r a O b j e c t s M e t h o d s F o r W e b c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . F i n a l i z a b l e P h a n t o m R e f e r e n c e c l a s s : i n t e r f a c e c o m . g o o g l e . c o m m o n . b a s e . F i n a l i z a b l e R e f e r e n c e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . F i n a l i z a b l e R e f e r e n c e Q u e u e Wrapper class:class com.google.common.base.Equivalence class:class com.google.common.base.ExtraObjectsMethodsForWeb class:class com.google.common.base.FinalizablePhantomReference class:interface com.google.common.base.FinalizableReference class:class com.google.common.base.FinalizableReferenceQueue Wrapperclass:classcom.google.common.base.Equivalenceclass:classcom.google.common.base.ExtraObjectsMethodsForWebclass:classcom.google.common.base.FinalizablePhantomReferenceclass:interfacecom.google.common.base.FinalizableReferenceclass:classcom.google.common.base.FinalizableReferenceQueueDecoupledLoader

class:class com.google.common.base.FinalizableReferenceQueue D i r e c t L o a d e r c l a s s : i n t e r f a c e c o m . g o o g l e . c o m m o n . b a s e . F i n a l i z a b l e R e f e r e n c e Q u e u e DirectLoader class:interface com.google.common.base.FinalizableReferenceQueue DirectLoaderclass:interfacecom.google.common.base.FinalizableReferenceQueueFinalizerLoader

class:class com.google.common.base.FinalizableReferenceQueue$SystemLoader

class:class com.google.common.base.FinalizableReferenceQueue

class:class com.google.common.base.FinalizableSoftReference

class:class com.google.common.base.FinalizableWeakReference

class:interface com.google.common.base.Function

class:class com.google.common.base.FunctionalEquivalence

class:class com.google.common.base.Functions 1 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . F u n c t i o n s 1 class:class com.google.common.base.Functions 1class:classcom.google.common.base.FunctionsConstantFunction

class:class com.google.common.base.Functions F o r M a p W i t h D e f a u l t c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . F u n c t i o n s ForMapWithDefault class:class com.google.common.base.Functions ForMapWithDefaultclass:classcom.google.common.base.FunctionsFunctionComposition

class:class com.google.common.base.Functions F u n c t i o n F o r M a p N o D e f a u l t c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . F u n c t i o n s FunctionForMapNoDefault class:class com.google.common.base.Functions FunctionForMapNoDefaultclass:classcom.google.common.base.FunctionsIdentityFunction

class:class com.google.common.base.Functions P r e d i c a t e F u n c t i o n c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . F u n c t i o n s PredicateFunction class:class com.google.common.base.Functions PredicateFunctionclass:classcom.google.common.base.FunctionsSupplierFunction

class:class com.google.common.base.Functions T o S t r i n g F u n c t i o n c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . F u n c t i o n s c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . J d k P a t t e r n ToStringFunction class:class com.google.common.base.Functions class:class com.google.common.base.JdkPattern ToStringFunctionclass:classcom.google.common.base.Functionsclass:classcom.google.common.base.JdkPatternJdkMatcher

class:class com.google.common.base.JdkPattern

class:class com.google.common.base.Joiner$1

class:class com.google.common.base.Joiner$2

class:class com.google.common.base.Joiner 3 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . J o i n e r 3 class:class com.google.common.base.Joiner 3class:classcom.google.common.base.JoinerMapJoiner

class:class com.google.common.base.Joiner

class:class com.google.common.base.MoreObjects 1 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . M o r e O b j e c t s 1 class:class com.google.common.base.MoreObjects 1class:classcom.google.common.base.MoreObjectsToStringHelper V a l u e H o l d e r c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . M o r e O b j e c t s ValueHolder class:class com.google.common.base.MoreObjects ValueHolderclass:classcom.google.common.base.MoreObjectsToStringHelper

class:class com.google.common.base.MoreObjects

class:class com.google.common.base.Objects 1 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . O b j e c t s 1 class:class com.google.common.base.Objects 1class:classcom.google.common.base.ObjectsToStringHelper V a l u e H o l d e r c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . O b j e c t s ValueHolder class:class com.google.common.base.Objects ValueHolderclass:classcom.google.common.base.ObjectsToStringHelper

class:class com.google.common.base.Objects

class:class com.google.common.base.Optional$1$1

class:class com.google.common.base.Optional$1

class:class com.google.common.base.Optional

class:class com.google.common.base.PairwiseEquivalence

class:interface com.google.common.base.PatternCompiler

class:class com.google.common.base.Platform 1 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . P l a t f o r m 1 class:class com.google.common.base.Platform 1class:classcom.google.common.base.PlatformJdkPatternCompiler

class:class com.google.common.base.Platform

class:class com.google.common.base.Preconditions

class:interface com.google.common.base.Predicate

class:class com.google.common.base.Predicates 1 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . P r e d i c a t e s 1 class:class com.google.common.base.Predicates 1class:classcom.google.common.base.PredicatesAndPredicate

class:class com.google.common.base.Predicates C o m p o s i t i o n P r e d i c a t e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . P r e d i c a t e s CompositionPredicate class:class com.google.common.base.Predicates CompositionPredicateclass:classcom.google.common.base.PredicatesContainsPatternFromStringPredicate

class:class com.google.common.base.Predicates C o n t a i n s P a t t e r n P r e d i c a t e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . P r e d i c a t e s ContainsPatternPredicate class:class com.google.common.base.Predicates ContainsPatternPredicateclass:classcom.google.common.base.PredicatesInPredicate

class:class com.google.common.base.Predicates I n s t a n c e O f P r e d i c a t e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . P r e d i c a t e s InstanceOfPredicate class:class com.google.common.base.Predicates InstanceOfPredicateclass:classcom.google.common.base.PredicatesIsEqualToPredicate

class:class com.google.common.base.Predicates N o t P r e d i c a t e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . P r e d i c a t e s NotPredicate class:class com.google.common.base.Predicates NotPredicateclass:classcom.google.common.base.PredicatesObjectPredicate 1 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . P r e d i c a t e s 1 class:class com.google.common.base.Predicates 1class:classcom.google.common.base.PredicatesObjectPredicate 2 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . P r e d i c a t e s 2 class:class com.google.common.base.Predicates 2class:classcom.google.common.base.PredicatesObjectPredicate 3 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . P r e d i c a t e s 3 class:class com.google.common.base.Predicates 3class:classcom.google.common.base.PredicatesObjectPredicate 4 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . P r e d i c a t e s 4 class:class com.google.common.base.Predicates 4class:classcom.google.common.base.PredicatesObjectPredicate

class:class com.google.common.base.Predicates O r P r e d i c a t e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . P r e d i c a t e s OrPredicate class:class com.google.common.base.Predicates OrPredicateclass:classcom.google.common.base.PredicatesSubtypeOfPredicate

class:class com.google.common.base.Predicates

class:class com.google.common.base.Present

class:class com.google.common.base.SmallCharMatcher

class:class com.google.common.base.Splitter$1$1

class:class com.google.common.base.Splitter$1

class:class com.google.common.base.Splitter$2$1

class:class com.google.common.base.Splitter$2

class:class com.google.common.base.Splitter$3$1

class:class com.google.common.base.Splitter$3

class:class com.google.common.base.Splitter$4$1

class:class com.google.common.base.Splitter$4

class:class com.google.common.base.Splitter 5 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . S p l i t t e r 5 class:class com.google.common.base.Splitter 5class:classcom.google.common.base.SplitterMapSplitter

class:class com.google.common.base.Splitter S p l i t t i n g I t e r a t o r c l a s s : i n t e r f a c e c o m . g o o g l e . c o m m o n . b a s e . S p l i t t e r SplittingIterator class:interface com.google.common.base.Splitter SplittingIteratorclass:interfacecom.google.common.base.SplitterStrategy

class:class com.google.common.base.Splitter

class:class com.google.common.base.StandardSystemProperty

class:class com.google.common.base.Stopwatch 1 c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . S t o p w a t c h c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . S t r i n g s c l a s s : i n t e r f a c e c o m . g o o g l e . c o m m o n . b a s e . S u p p l i e r c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . S u p p l i e r s 1 class:class com.google.common.base.Stopwatch class:class com.google.common.base.Strings class:interface com.google.common.base.Supplier class:class com.google.common.base.Suppliers 1class:classcom.google.common.base.Stopwatchclass:classcom.google.common.base.Stringsclass:interfacecom.google.common.base.Supplierclass:classcom.google.common.base.SuppliersExpiringMemoizingSupplier

class:class com.google.common.base.Suppliers M e m o i z i n g S u p p l i e r c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . S u p p l i e r s MemoizingSupplier class:class com.google.common.base.Suppliers MemoizingSupplierclass:classcom.google.common.base.SuppliersSupplierComposition

class:interface com.google.common.base.Suppliers S u p p l i e r F u n c t i o n c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . S u p p l i e r s SupplierFunction class:class com.google.common.base.Suppliers SupplierFunctionclass:classcom.google.common.base.SuppliersSupplierFunctionImpl

class:class com.google.common.base.Suppliers S u p p l i e r O f I n s t a n c e c l a s s : c l a s s c o m . g o o g l e . c o m m o n . b a s e . S u p p l i e r s SupplierOfInstance class:class com.google.common.base.Suppliers SupplierOfInstanceclass:classcom.google.common.base.SuppliersThreadSafeSupplier

class:class com.google.common.base.Suppliers

class:class com.google.common.base.Throwables$1

class:class com.google.common.base.Throwables

class:class com.google.common.base.Ticker$1

class:class com.google.common.base.Ticker

class:class com.google.common.base.Utf8

class:class com.google.common.base.Verify

class:class com.google.common.base.VerifyException

class:class com.google.common.base.internal.Finalizer

class:interface com.google.common.base.package-info

class count:162

相关推荐
神仙别闹3 分钟前
基于java的改良版超级玛丽小游戏
java
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭27 分钟前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫43 分钟前
泛型(2)
java
超爱吃士力架1 小时前
邀请逻辑
java·linux·后端
南宫生1 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石1 小时前
12/21java基础
java
李小白661 小时前
Spring MVC(上)
java·spring·mvc
GoodStudyAndDayDayUp1 小时前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea
装不满的克莱因瓶2 小时前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
n北斗2 小时前
常用类晨考day15
java