Created
July 27, 2016 18:06
-
-
Save vorburger/bdc1e08226425dc0a30e917b85259205 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved. | |
* | |
* This program and the accompanying materials are made available under the | |
* terms of the Eclipse Public License v1.0 which accompanies this distribution, | |
* and is available at http://www.eclipse.org/legal/epl-v10.html | |
*/ | |
package org.opendaylight.netvirt.aclservice.tests.utils.inject; | |
import java.lang.reflect.Method; | |
import javax.inject.Provider; | |
/** | |
* Utility for {@link InstanceProvidedBy}. | |
* | |
* <p> | |
* Used internally by {@link ProviderRunner}; not typically intended for direct | |
* end-user usage. | |
* | |
* @author Michael Vorburger | |
*/ | |
public class InstanceProvidedByUtil { | |
public Object getInstance(Class<?> classAnnotatedWithInjectorProvider) | |
throws InstanceProvidedByAnnotationException, ReflectiveOperationException { | |
InstanceProvidedBy annotation = classAnnotatedWithInjectorProvider.getAnnotation(InstanceProvidedBy.class); | |
if (annotation == null) { | |
throw new InstanceProvidedByAnnotationException(classAnnotatedWithInjectorProvider | |
+ " must be annotated with " + InstanceProvidedBy.class.getName()); | |
} | |
Class<? extends Provider<?>> providerClass = annotation.value(); | |
return getInstance(classAnnotatedWithInjectorProvider, providerClass); | |
} | |
protected Object getInstance(Class<?> classAnnotatedWithInjectorProvider, | |
Class<? extends Provider<?>> providerClass) | |
throws InstanceProvidedByAnnotationException, ReflectiveOperationException { | |
Provider<?> provider = getProvider(providerClass); | |
Object instance = provider.get(); | |
if (!classAnnotatedWithInjectorProvider.equals(instance.getClass())) { | |
throw new InstanceProvidedByAnnotationException( | |
classAnnotatedWithInjectorProvider + " @" + InstanceProvidedBy.class.getSimpleName() + "'s " | |
+ providerClass + "'s get() created an instance of " + instance.getClass().getName() | |
+ " instead of an " + classAnnotatedWithInjectorProvider); | |
} | |
return instance; | |
} | |
protected Provider<?> getProvider(Class<? extends Provider<?>> providerClass) throws ReflectiveOperationException { | |
// Could here easily also first check if providerClass has a visible | |
// default constructor, not needed with Dagger; but could be useful for | |
// other usages later | |
Method providerClassStaticCreateMethod = providerClass.getMethod("create"); | |
Provider<?> provider = (Provider<?>) providerClassStaticCreateMethod.invoke(null); | |
return provider; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment