Class ServiceLocatorTestRule<T>
- java.lang.Object
-
- org.junit.rules.ExternalResource
-
- org.jvnet.hk2.testing.junit.ServiceLocatorTestRule<T>
-
- Type Parameters:
T- the type of JUnit test thisServiceLocatorTestRuleis related to; consider making it an instance ofBinder
- All Implemented Interfaces:
Binder,org.junit.rules.TestRule
public class ServiceLocatorTestRule<T> extends org.junit.rules.ExternalResource implements Binder
AnExternalResource(and aBinder) that sets up and tears down an HK2ServiceLocatoron a per-test-class or per-test-method basis.- Since:
- 2.4.0-b33
- See Also:
ExternalResource,Binder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classServiceLocatorTestRule.ServiceLocatorIsolationAnenumdescribing possibleServiceLocatorisolation levels for JUnit tests.private static classServiceLocatorTestRule.VisitorA very, very special-purposeClassVisitorImplsuitable only for use by theServiceLocatorTestRuleclass to determine efficiently whether a givenClassis annotated withServiceor not.
-
Field Summary
Fields Modifier and Type Field Description private org.junit.runner.DescriptiondescriptionTheDescriptiondescribing the JUnit test method currently executing.private ServiceLocatorTestRule.ServiceLocatorIsolationisolationTheServiceLocatorTestRule.ServiceLocatorIsolationdesignating whether theServiceLocatorassociated with thisServiceLocatorTestRuleis set up for each test method or shared among them.private ServiceLocatorserviceLocatorTheServiceLocatorin effect for the currently executing JUnit test method.private TtestThe test instance instantiating thisServiceLocatorTestRule.private booleanverboseAbooleanindicating whether thisServiceLocatorTestRuleis configured to output additional information toSystem.out.
-
Constructor Summary
Constructors Constructor Description ServiceLocatorTestRule(T test)Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test inServiceLocatorTestRule.ServiceLocatorIsolation.PER_TESTisolation in non-verbose mode.ServiceLocatorTestRule(T test, boolean verbose)Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test inServiceLocatorTestRule.ServiceLocatorIsolation.PER_TESTisolation with the supplied verbosity.ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation)Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test in the givenServiceLocatorTestRule.ServiceLocatorIsolationin non-verbose mode.ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation, boolean verbose)Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test in the givenServiceLocatorTestRule.ServiceLocatorIsolation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafter()Shuts down and cleans up theServiceLocatorcreated by thisServiceLocatorTestRule.org.junit.runners.model.Statementapply(org.junit.runners.model.Statement statement, org.junit.runner.Description description)Overrides theExternalResource.apply(Statement, Description)method to save the suppliedDescriptionso that other methods in this class can refer to it.voidbefore()When necessary, calls thecreateServiceLocator(Description),configureServiceLocator(ServiceLocator, Description)andperformDependencyInjection(ServiceLocator, Object)methods in order to set up aServiceLocatorappropriate for the current JUnit test.voidbind(DynamicConfiguration dynamicConfiguration)Called at the appropriate time by theconfigureServiceLocator(ServiceLocator, Description)method in case subclasses wish to add services to theServiceLocatorbeing configured.private static voidbind(DynamicConfiguration configuration, java.io.BufferedReader reader)Reads the HK2 locator file represented by the suppliedBufferedReaderand binds theDescriptors it represents.private voidbind(DynamicConfiguration configuration, java.lang.reflect.AnnotatedElement element)Retrieves anyAnnotations found on the suppliedAnnotatedElementand calls thebind(DynamicConfiguration, Collection)method supplying them as the second parameter.private static voidbind(DynamicConfiguration configuration, java.lang.String locatorResourceName)Using the contextClassLoader, gets all classpath resources with the suppliedlocatorResourceName, and iterates through them, calling thebind(DynamicConfiguration, URL)method for each one.private static voidbind(DynamicConfiguration configuration, java.net.URL locatorResource)Opens anInputStreamto the suppliedURL, wraps aBufferedReaderaround it, and calls thebind(DynamicConfiguration, BufferedReader)method.private voidbind(DynamicConfiguration configuration, java.util.Collection<? extends java.lang.annotation.Annotation> annotations)Recursively examines theAnnotations supplied looking for instances of theClasses,InhabitantFilesandPackagesannotations, and, using the binding methods on the suppliedDynamicConfigurationbindsDescriptors for those "reachable" classes into the configuration.private voidbind(DynamicConfiguration configuration, java.util.Set<java.lang.String> packageNames)Binds allClasses "reachable" from the supplied packages.private voidbind(DynamicConfiguration configuration, Classes classes)Binds allClasses "reachable" from the suppliedClassesannotation.private static voidbind(DynamicConfiguration configuration, InhabitantFiles inhabitantFiles)Gathers all classpath resource names found in the suppliedInhabitantFilesannotation representing HK2 locator files and calls thebind(DynamicConfiguration, String)method for each such entry found.private voidbind(DynamicConfiguration configuration, Packages packages)Binds allClasses "reachable" from packages described by the suppliedPackagesannotation.protected voidconfigureServiceLocator(ServiceLocator serviceLocator, org.junit.runner.Description testDescription)Configures the suppliedServiceLocatorfor use by the test described by the suppliedDescription.protected ServiceLocatorcreateServiceLocator(org.junit.runner.Description testDescription)Creates and returns aServiceLocatorsuitable for the JUnit test described by the suppliedDescription.private static java.util.Set<java.lang.Class<?>>getClassesFromDirectory(java.util.Set<java.lang.String> packageNames, java.io.File directory, boolean verbose)Given aSetof package names and aFiledesignating an existing directory in the classpath that can be read, returns aSetofService-annotatedClasses that can be found there that belong to one of the supplied package names.private java.util.Set<java.lang.Class<?>>getClassesFromPackages(java.util.Set<java.lang.String> packageNames)Given aSetof package names, returns aSetofService-annotatedClasses found in those packages, whether they are located inZipFiles or directories on the classpath.private java.util.Set<java.lang.Class<?>>getClassesFromZipFile(java.util.Set<java.lang.String> packageNames, java.util.zip.ZipFile zipFile)Given aZipFile(which is usually a.jarfile) containing class files and aSetof package names, searches it to findService-annotatedClasses inside it that belong to one of the supplied package names and returns aSetof suchClasses.protected java.lang.StringgetClasspath()Returns aStringrepresenting the classpath to use to search for classes.private org.junit.runner.DescriptiongetDescription()Returns theDescriptiondescribing the currently executing JUnit test method.ServiceLocatorTestRule.ServiceLocatorIsolationgetServiceLocatorIsolation()Returns theServiceLocatorTestRule.ServiceLocatorIsolationfor thisServiceLocatorTestRule.protected java.lang.StringgetServiceLocatorName(org.junit.runner.Description testDescription)Returns a name for aServiceLocatorthat is appropriate for the suppliedDescription.private static booleanisBlacklisted(java.lang.annotation.Annotation annotation)Returnstrueif the suppliedAnnotationisnullor designates an annotation type that should be excluded from recursive annotation scanning performed by thebind(DynamicConfiguration, AnnotatedElement)method.private static booleanisBlacklisted(java.lang.Class<? extends java.lang.annotation.Annotation> c)Returnstrueif the suppliedClassisnullor designates an annotation type that should be excluded from recursive annotation scanning performed by thebind(DynamicConfiguration, AnnotatedElement)method.booleanisVerbose()protected voidperformDependencyInjection(ServiceLocator serviceLocator, T test)Performs HK2 dependency injection on thisServiceLocatorTestRuleand the supplied test instance, using the suppliedServiceLocatoras needed.voidshutdownAndDestroyServiceLocator(org.junit.runner.Description testDescription)Blindly calls theServiceLocator.shutdown()andServiceLocatorFactory.destroy(ServiceLocator)methods on thisServiceLocatorTestRule's associatedServiceLocatorif it is discovered to be non-null.
-
-
-
Field Detail
-
test
private final T test
The test instance instantiating thisServiceLocatorTestRule.This field is never
null.
-
isolation
private final ServiceLocatorTestRule.ServiceLocatorIsolation isolation
TheServiceLocatorTestRule.ServiceLocatorIsolationdesignating whether theServiceLocatorassociated with thisServiceLocatorTestRuleis set up for each test method or shared among them.This field is never
null.
-
verbose
private final boolean verbose
Abooleanindicating whether thisServiceLocatorTestRuleis configured to output additional information toSystem.out.- See Also:
isVerbose()
-
description
private org.junit.runner.Description description
TheDescriptiondescribing the JUnit test method currently executing.This field may be
null.- See Also:
apply(Statement, Description)
-
serviceLocator
private ServiceLocator serviceLocator
TheServiceLocatorin effect for the currently executing JUnit test method.This field may be
null.
-
-
Constructor Detail
-
ServiceLocatorTestRule
public ServiceLocatorTestRule(T test)
Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test inServiceLocatorTestRule.ServiceLocatorIsolation.PER_TESTisolation in non-verbose mode.- Parameters:
test- the JUnit test; must not benull- Throws:
java.lang.AssertionError- iftestisnull- See Also:
ServiceLocatorTestRule(Object, ServiceLocatorIsolation, boolean)
-
ServiceLocatorTestRule
public ServiceLocatorTestRule(T test, boolean verbose)
Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test inServiceLocatorTestRule.ServiceLocatorIsolation.PER_TESTisolation with the supplied verbosity.- Parameters:
test- the JUnit test; must not benullverbose- whether thisServiceLocatorTestRuleshould output additional information toSystem.out- Throws:
java.lang.AssertionError- iftestisnull- See Also:
ServiceLocatorTestRule(Object, ServiceLocatorIsolation, boolean)
-
ServiceLocatorTestRule
public ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation)
Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test in the givenServiceLocatorTestRule.ServiceLocatorIsolationin non-verbose mode.- Parameters:
test- the JUnit test; must not benullisolation- theServiceLocatorTestRule.ServiceLocatorIsolation; ifnullthenServiceLocatorTestRule.ServiceLocatorIsolation.PER_TESTwill be used instead- Throws:
java.lang.AssertionError- iftestisnull- See Also:
ServiceLocatorTestRule(Object, ServiceLocatorIsolation, boolean)
-
ServiceLocatorTestRule
public ServiceLocatorTestRule(T test, ServiceLocatorTestRule.ServiceLocatorIsolation isolation, boolean verbose)
Creates a newServiceLocatorTestRuleon behalf of the supplied JUnit test in the givenServiceLocatorTestRule.ServiceLocatorIsolation.- Parameters:
test- the JUnit test; must not benullisolation- theServiceLocatorTestRule.ServiceLocatorIsolation; ifnullthenServiceLocatorTestRule.ServiceLocatorIsolation.PER_TESTwill be used insteadverbose- whether thisServiceLocatorTestRuleshould output additional information toSystem.out- Throws:
java.lang.AssertionError- iftestisnull
-
-
Method Detail
-
getServiceLocatorIsolation
public final ServiceLocatorTestRule.ServiceLocatorIsolation getServiceLocatorIsolation()
Returns theServiceLocatorTestRule.ServiceLocatorIsolationfor thisServiceLocatorTestRule.This method never returns
null.- Returns:
- the
ServiceLocatorTestRule.ServiceLocatorIsolationfor thisServiceLocatorTestRule; nevernull
-
getDescription
private final org.junit.runner.Description getDescription()
Returns theDescriptiondescribing the currently executing JUnit test method.This method may return
null.- Returns:
- the
Descriptiondescribing the currently executing JUnit test method, ornull
-
isVerbose
public final boolean isVerbose()
- Returns:
trueif thisServiceLocatorTestRuleshould be verbose;falseotherwise- See Also:
ServiceLocatorUtilities.dumpAllDescriptors(ServiceLocator, PrintStream)
-
before
public void before() throws java.io.IOExceptionWhen necessary, calls thecreateServiceLocator(Description),configureServiceLocator(ServiceLocator, Description)andperformDependencyInjection(ServiceLocator, Object)methods in order to set up aServiceLocatorappropriate for the current JUnit test.- Overrides:
beforein classorg.junit.rules.ExternalResource- Throws:
java.io.IOException- if theconfigureServiceLocator(ServiceLocator, Description)method threw anIOException- See Also:
createServiceLocator(Description),configureServiceLocator(ServiceLocator, Description),performDependencyInjection(ServiceLocator, Object)
-
configureServiceLocator
protected void configureServiceLocator(ServiceLocator serviceLocator, org.junit.runner.Description testDescription) throws java.io.IOException
Configures the suppliedServiceLocatorfor use by the test described by the suppliedDescription.This implementation:
- Adds the
ErrorServiceImplclass to the suppliedServiceLocatorso that exceptions will be thrown from tests - Removes, via
DynamicConfiguration.addUnbindFilter(Filter), allDescriptors that haveDescription.classas one of their contracts - Adds a constant
descriptor in
Singletonscope with a name equal to the return value of theDescription.getDisplayName()method for the suppliedDescriptionso that the current test can inject theDescriptionfor the current method if it wishes - Adds any classes found in an optional
Classesannotation decorating the test class—if they are assignable toFactory.classthen they are added as factories - Adds any classes found in any packages listed in an optional
Packagesannotation decorating the test class, provided they are annotated withService - Reads any locator files listed in an optional
InhabitantFilesannotation decorating the test class and adds the services listed therein - Calls the
bind(DynamicConfiguration)method - Calls the
Binder.bind(DynamicConfiguration)method on the test if it is in fact an instance ofBinder
- Parameters:
serviceLocator- theServiceLocatorto configure; must not benulltestDescription- theDescriptiondescribing the particular test being run; must not benull- Throws:
java.lang.AssertionError- if eitherserviceLocatorortestDescriptionisnulljava.io.IOException- if there was an error looking for classes in packages or finding or reading locator files
- Adds the
-
bind
public void bind(DynamicConfiguration dynamicConfiguration)
Called at the appropriate time by theconfigureServiceLocator(ServiceLocator, Description)method in case subclasses wish to add services to theServiceLocatorbeing configured.Overrides of this method must not invoke the
DynamicConfiguration.commit()method.The default implementation of this method does nothing.
- Specified by:
bindin interfaceBinder- Parameters:
dynamicConfiguration- theDynamicConfigurationto manipulate in order to add or remove services from theServiceLocatorbeing configured; must not benull
-
getClassesFromPackages
private final java.util.Set<java.lang.Class<?>> getClassesFromPackages(java.util.Set<java.lang.String> packageNames) throws java.io.IOExceptionGiven aSetof package names, returns aSetofService-annotatedClasses found in those packages, whether they are located inZipFiles or directories on the classpath.This method never returns
null.- Parameters:
packageNames- the names of packages to search; may benullin which case an emptySetwill be returned- Returns:
- a non-
nullSetofClasses - Throws:
java.io.IOException- if there was any kind of error during package searching or file reading- See Also:
getClassesFromDirectory(Set, File, boolean),getClassesFromZipFile(Set, ZipFile)
-
getClassesFromZipFile
private final java.util.Set<java.lang.Class<?>> getClassesFromZipFile(java.util.Set<java.lang.String> packageNames, java.util.zip.ZipFile zipFile) throws java.io.IOExceptionGiven aZipFile(which is usually a.jarfile) containing class files and aSetof package names, searches it to findService-annotatedClasses inside it that belong to one of the supplied package names and returns aSetof suchClasses.- Parameters:
packageNames- aSetof package names; may benullin which case an emptySetwill be returnedzipFile- aZipFilehopefully containing class files; may benullin which case an emptySetwill be returned- Returns:
- a non-
nullSetofService-annotatedClasses - Throws:
java.io.IOException- if an error occurred reading theZipFile
-
getClasspath
protected java.lang.String getClasspath()
Returns aStringrepresenting the classpath to use to search for classes.This method never returns
null.Overrides of this method are permitted to return
null.The default implementation of this method returns the value of the
java.class.pathsystem property, or in the almost certainly catastrophic case where this is not set, the empty string.- Returns:
- a
Stringrepresenting the classpath, ornull
-
bind
private final void bind(DynamicConfiguration configuration, java.lang.reflect.AnnotatedElement element) throws java.io.IOException
Retrieves anyAnnotations found on the suppliedAnnotatedElementand calls thebind(DynamicConfiguration, Collection)method supplying them as the second parameter.- Parameters:
configuration- aDynamicConfigurationthat will perform the binding; must not benullelement- theAnnotatedElementthat has annotations; may benullin which case no action is performed- Throws:
java.lang.AssertionError- ifconfigurationisnulljava.io.IOException- if there was a problem reading class or locator files- See Also:
bind(DynamicConfiguration, Collection)
-
bind
private final void bind(DynamicConfiguration configuration, java.util.Collection<? extends java.lang.annotation.Annotation> annotations) throws java.io.IOException
Recursively examines theAnnotations supplied looking for instances of theClasses,InhabitantFilesandPackagesannotations, and, using the binding methods on the suppliedDynamicConfigurationbindsDescriptors for those "reachable" classes into the configuration.Recursively means that a user-defined
Annotationmight itself be annotated withClasses; in such a case theClassesannotation's contents will be found and processed. This effectively allows stereotyping theClasses,InhabitantFilesandPackagesannotations.- Parameters:
configuration- aDynamicConfigurationthat will perform the binding; must not benullannotations- aCollectionofAnnotations; may benullin which case no action will be performed- Throws:
java.lang.AssertionError- ifconfigurationisnulljava.io.IOException- if there was a problem reading class or locator files- See Also:
bind(DynamicConfiguration, Classes),bind(DynamicConfiguration, InhabitantFiles),bind(DynamicConfiguration, Packages)
-
bind
private final void bind(DynamicConfiguration configuration, Classes classes) throws java.io.IOException
Binds allClasses "reachable" from the suppliedClassesannotation.Each
Classfound as a member of the return value of theClasses.value()method is bound (even if it is an implementation ofFactory) and in turn is examined forAnnotations on itself.- Parameters:
configuration- aDynamicConfigurationthat will perform the binding; must not benullclasses- aClasses; may benullin which case no action will be performed- Throws:
java.lang.AssertionError- ifconfigurationisnulljava.io.IOException- if there was a problem reading class files or locator files- See Also:
DynamicConfiguration.addActiveDescriptor(Class),DynamicConfiguration.addActiveFactoryDescriptor(Class)
-
bind
private final void bind(DynamicConfiguration configuration, Packages packages) throws java.io.IOException
Binds allClasses "reachable" from packages described by the suppliedPackagesannotation.This method calls the
bind(DynamicConfiguration, Set)method, passing it aSetformed from the suppliedPackagesannotation's list of package names as the second parameter.- Parameters:
configuration- aDynamicConfigurationthat will perform the binding; must not benullpackages- aPackages; may benullin which case no action will be performed- Throws:
java.lang.AssertionError- ifconfigurationisnulljava.io.IOException- if there was a problem reading class files or locator files- See Also:
bind(DynamicConfiguration, Set)
-
bind
private final void bind(DynamicConfiguration configuration, java.util.Set<java.lang.String> packageNames) throws java.io.IOException
Binds allClasses "reachable" from the supplied packages.This method calls the
getClassesFromPackages(Set)method, and, for eachClassreturned, if it is an implementation ofFactorythen twoDescriptors are added to theDynamicConfigurationin the form of aFactoryDescriptorsaddition by way of theDynamicConfiguration.addActiveFactoryDescriptor(Class)method, or, if not, then a singleDescriptoris added to theDynamicConfiguration.- Parameters:
configuration- aDynamicConfigurationthat will perform the binding; must not benullpackageNames- aSetof package names; may benullin which case no action will be performed- Throws:
java.lang.AssertionError- ifconfigurationisnulljava.io.IOException- if there was a problem reading class files or locator files- See Also:
getClassesFromPackages(Set),DynamicConfiguration.addActiveDescriptor(Class),DynamicConfiguration.addActiveFactoryDescriptor(Class)
-
performDependencyInjection
protected void performDependencyInjection(ServiceLocator serviceLocator, T test)
Performs HK2 dependency injection on thisServiceLocatorTestRuleand the supplied test instance, using the suppliedServiceLocatoras needed.The default implementation of this method does the following:
- Calls
ServiceLocator.inject(Object)on the suppliedServiceLocator, if it is non-null, passing it thisServiceLocatorTestRule - Calls
ServiceLocator.postConstruct(Object)on the suppliedServiceLocator, if it is non-null, passing it thisServiceLocatorTestRule - Calls
ServiceLocator.inject(Object)on the suppliedServiceLocator, if it is non-null, passing it the suppliedtestinstance, if it is non-null - Calls
ServiceLocator.postConstruct(Object)on the suppliedServiceLocator, if it is non-null, passing it the suppliedtestinstance, if it is non-null
This method is guaranteed to be called after the
configureServiceLocator(ServiceLocator, Description)method with the sameServiceLocator.- Parameters:
serviceLocator- theServiceLocatorto use to perform injection; may benullin which case no action will be takentest- the test instance to inject; may benull- See Also:
bind(DynamicConfiguration),configureServiceLocator(ServiceLocator, Description)
- Calls
-
createServiceLocator
protected ServiceLocator createServiceLocator(org.junit.runner.Description testDescription)
Creates and returns aServiceLocatorsuitable for the JUnit test described by the suppliedDescription.This method never returns
null.Overrides of this method must not return
null.- Parameters:
testDescription- theDescriptiondescribing the currently executing JUnit test; must not benull- Returns:
- a non-
nullServiceLocator - Throws:
java.lang.AssertionError- iftestDescriptionisnull
-
getServiceLocatorName
protected java.lang.String getServiceLocatorName(org.junit.runner.Description testDescription)
Returns a name for aServiceLocatorthat is appropriate for the suppliedDescription.This method never returns
null.Overrides of this method must not return
null.- Parameters:
testDescription- theDescriptiondescribing the currently executing JUnit test; must not benull- Returns:
- a non-
nullStringthat will be used as a name for aServiceLocator - Throws:
java.lang.AssertionError- iftestDescriptionisnull, or if itsgetClassName()method returnsnullor if itsgetMethodName()method returnsnull
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement statement, org.junit.runner.Description description)Overrides theExternalResource.apply(Statement, Description)method to save the suppliedDescriptionso that other methods in this class can refer to it.This method never returns
null.Overrides of this method must not return
null.- Specified by:
applyin interfaceorg.junit.rules.TestRule- Overrides:
applyin classorg.junit.rules.ExternalResource- Parameters:
statement- theStatementthisServiceLocatorTestRulewill modify; passed unchanged to theExternalResource.apply(Statement, Description)methoddescription- theDescriptiondescribing the current JUnit test; must not benull; passed unchanged to theExternalResource.apply(Statement, Description)method- Returns:
- the return value of the
ExternalResource.apply(Statement, Description)method; nevernull
-
after
public void after()
Shuts down and cleans up theServiceLocatorcreated by thisServiceLocatorTestRule.This method performs the following actions in order:
- Calls the
ServiceLocator.preDestroy(Object)method on the existingServiceLocator, passing it the test instance supplied at construction time - Calls the
ServiceLocator.preDestroy(Object)method on the existingServiceLocator, passing it thisServiceLocatorTestRule - If the
ServiceLocatorisolation level isPER_TEST, calls theshutdownAndDestroyServiceLocator(Description)method with aDescriptioninstance that describes the JUnit test method that just executed
- Overrides:
afterin classorg.junit.rules.ExternalResource- See Also:
shutdownAndDestroyServiceLocator(Description)
- Calls the
-
shutdownAndDestroyServiceLocator
public void shutdownAndDestroyServiceLocator(org.junit.runner.Description testDescription)
Blindly calls theServiceLocator.shutdown()andServiceLocatorFactory.destroy(ServiceLocator)methods on thisServiceLocatorTestRule's associatedServiceLocatorif it is discovered to be non-null.- Parameters:
testDescription- aDescriptiondescribing the currently executing JUnit test, if any; ignored by this method's default implementation; may benull
-
getClassesFromDirectory
private static final java.util.Set<java.lang.Class<?>> getClassesFromDirectory(java.util.Set<java.lang.String> packageNames, java.io.File directory, boolean verbose) throws java.io.IOExceptionGiven aSetof package names and aFiledesignating an existing directory in the classpath that can be read, returns aSetofService-annotatedClasses that can be found there that belong to one of the supplied package names.This method never returns
null.- Parameters:
packageNames- aSetof package names; may benullin which case an emptySetwill be returneddirectory- aFiledesignating a directory; an emptySetwill be returned unless the directory so designated exists and is readableverbose- whether additional information should be output- Returns:
- a non-
nullSetofService-annotatedClasses - Throws:
java.io.IOException- if there was a problem reading files
-
isBlacklisted
private static final boolean isBlacklisted(java.lang.annotation.Annotation annotation)
Returnstrueif the suppliedAnnotationisnullor designates an annotation type that should be excluded from recursive annotation scanning performed by thebind(DynamicConfiguration, AnnotatedElement)method.- Parameters:
annotation- theAnnotationto consider; may benullin which casetruewill be returned- Returns:
trueif the suppliedAnnotationisnullor designates an annotation type that should be excluded from recursive annotation scanning performed by thebind(DynamicConfiguration, AnnotatedElement)method- See Also:
isBlacklisted(Class)
-
isBlacklisted
private static final boolean isBlacklisted(java.lang.Class<? extends java.lang.annotation.Annotation> c)
Returnstrueif the suppliedClassisnullor designates an annotation type that should be excluded from recursive annotation scanning performed by thebind(DynamicConfiguration, AnnotatedElement)method.- Parameters:
c- theClassto consider; may benullin which casetruewill be returned- Returns:
trueif the suppliedClassisnullor designates an annotation type that should be excluded from recursive annotation scanning performed by thebind(DynamicConfiguration, AnnotatedElement)method;falseotherwise
-
bind
private static final void bind(DynamicConfiguration configuration, InhabitantFiles inhabitantFiles) throws java.io.IOException
Gathers all classpath resource names found in the suppliedInhabitantFilesannotation representing HK2 locator files and calls thebind(DynamicConfiguration, String)method for each such entry found.- Parameters:
configuration- aDynamicConfigurationthat will perform the binding; must not benullinhabitantFiles- anInhabitantFiles; may benullin which case no action will be performed- Throws:
java.lang.AssertionError- ifconfigurationisnulljava.io.IOException- if there was a problem reading locator files- See Also:
bind(DynamicConfiguration, String)
-
bind
private static final void bind(DynamicConfiguration configuration, java.lang.String locatorResourceName) throws java.io.IOException
Using the contextClassLoader, gets all classpath resources with the suppliedlocatorResourceName, and iterates through them, calling thebind(DynamicConfiguration, URL)method for each one.- Parameters:
configuration- aDynamicConfigurationthat will perform the binding; must not benulllocatorResourceName- the name of a classpath resource identifying one or more HK2 locator files; may benullin which case no action will be performed- Throws:
java.lang.AssertionError- ifconfigurationisnulljava.io.IOException- if there was a problem reading locator files- See Also:
ClassLoader.getResources(String),Thread.getContextClassLoader(),bind(DynamicConfiguration, URL)
-
bind
private static final void bind(DynamicConfiguration configuration, java.net.URL locatorResource) throws java.io.IOException
Opens anInputStreamto the suppliedURL, wraps aBufferedReaderaround it, and calls thebind(DynamicConfiguration, BufferedReader)method.- Parameters:
configuration- aDynamicConfigurationthat will perform the binding; must not benulllocatorResource- aURLto an HK2 locator resource; may benullin which case no action will be taken- Throws:
java.lang.AssertionError- ifconfigurationisnulljava.io.IOException- if there was a problem reading locator files- See Also:
bind(DynamicConfiguration, BufferedReader)
-
bind
private static final void bind(DynamicConfiguration configuration, java.io.BufferedReader reader) throws java.io.IOException
Reads the HK2 locator file represented by the suppliedBufferedReaderand binds theDescriptors it represents.- Parameters:
configuration- aDynamicConfigurationthat will perform the binding; must not benullreader- aBufferedReaderreading a stream of descriptor information as parseable by theDescriptorImpl.readObject(BufferedReader)method; may benullin which case no action will be performed- Throws:
java.lang.AssertionError- ifconfigurationisnulljava.io.IOException- if there was a problem reading locator files- See Also:
DescriptorImpl.readObject(BufferedReader),DynamicConfiguration.bind(Descriptor)
-
-