/** * @see ReflectionHelper#getNameFromAllQualifiers(Set, AnnotatedElement) */ public static String getNameFromAllQualifiers(Set<Annotation> qualifiers, AnnotatedElement element) { return ReflectionHelper.getNameFromAllQualifiers(qualifiers, element); }
/** * This method returns the class associated with the type of the * factory * * @param factoryClass The non-null factory class. May not be null * @return the CLASS version of what the factory produces. Will * not be null * @throws MultiException if there was an error analyzing the class */ private static Class<?> getFactoryProductionClass(Class<?> factoryClass) { Type factoryProvidedType = getFactoryProductionType(factoryClass); Class<?> retVal = ReflectionHelper.getRawClass(factoryProvidedType); if (retVal != null) return retVal; throw new MultiException(new AssertionError("Could not find true produced type of factory " + factoryClass.getName())); }
/** * Gets all the qualifiers from the object * * @param t The object to analyze * @return The set of qualifiers. Will not return null but may return an empty set */ public static Set<Annotation> getQualifiersFromObject(Object t) { if (t == null) return Collections.emptySet(); return getQualifierAnnotations(t.getClass()); }
private static void addAllInterfaceContracts(Type interfaceType, LinkedHashSet<Type> addToMe) { Class<?> interfaceClass = ReflectionHelper.getRawClass(interfaceType); if (interfaceClass == null) return; if (addToMe.contains(interfaceType)) return; addToMe.add(interfaceType); for (Type extendedInterfaces : interfaceClass.getGenericInterfaces()) { addAllInterfaceContracts(extendedInterfaces, addToMe); } }
/** * Returns the type closure, as restricted by the classes listed in the * set of contracts implemented * * @param ofType The type to check * @param contracts The contracts this type is allowed to handle * @return The type closure restricted to the contracts */ public static Set<Type> getTypeClosure(Type ofType, Set<String> contracts) { Set<Type> closure = getTypeClosure(ofType); HashSet<Type> retVal = new HashSet<Type>(); for (Type t : closure) { Class<?> rawClass = ReflectionHelper.getRawClass(t); if (rawClass == null) continue; if (contracts.contains(rawClass.getName())) { retVal.add(t); } } return retVal; }
checkState(); Type requiredType = injectee.getRequiredType(); Class<?> rawType = ReflectionHelper.getRawClass(requiredType); if (rawType == null) { throw new MultiException(new IllegalArgumentException( "Invalid injectee with required type of " + injectee.getRequiredType() + " passed to getInjecteeDescriptor")); (ReflectionHelper.getFirstTypeArgument(requiredType)), injectee.getRequiredQualifiers(), injectee.getUnqualified(), injectee, ReflectionHelper.getFirstTypeArgument(requiredType), injectee.getRequiredQualifiers()); Set<Annotation> qualifiersAsSet = injectee.getRequiredQualifiers(); String name = ReflectionHelper.getNameFromAllQualifiers(qualifiersAsSet, injectee.getParent());
@Override public Object resolve(Injectee injectee, ServiceHandle<?> root) { final String factoryName = ReflectionHelper.getNameFromAllQualifiers( injectee.getRequiredQualifiers(), injectee.getParent()); final ConnectionSource connectionSource = persistenceSessionManager.get().getPersistenceContext( new PersistenceContextKey(new PersistenceContextFactoryKey(factoryName)), ConnectionSource.class, injectee.getInjecteeDescriptor().getScope()); return connectionSource; }
/** * This method returns the class associated with the type of the factory. * * @param descriptor The descriptor (reified and not null) that will be used to find the implementation. * @return the CLASS version of what the factory produces. Will not be null. * @throws MultiException if there was an error analyzing the class. */ private static Class<?> getFactoryProductionClass(ActiveDescriptor<?> descriptor) { Class<?> factoryClass = descriptor.getImplementationClass(); Type factoryProvidedType = getFactoryProductionType(factoryClass); Class<?> retVal = ReflectionHelper.getRawClass(factoryProvidedType); if (retVal == null && descriptor.getContractTypes().size() == 1) { // If the type was not resolved from a generic type, try to use // contract type if only one was registered. Type contract = descriptor.getContractTypes().iterator().next(); retVal = ReflectionHelper.getRawClass(contract); } if (retVal == null) { throw new MultiException(new AssertionError("Could not find true produced type of factory " + factoryClass.getName())); } return retVal; }
private Object internalInvoke(Object target, Method method, Method proceed, Object[] params) throws Throwable { Context<?> context; Object service; context = locator.resolveContext(descriptor.getScopeAnnotation()); service = context.findOrCreate(descriptor, root); if (service == null) { throw new MultiException(new IllegalStateException("Proxiable context " + context + " findOrCreate returned a null for descriptor " + descriptor + " and handle " + root)); } if (method.getName().equals(PROXY_MORE_METHOD_NAME)) { // We did what we came here to do return service; } if (isEquals(method) && (params.length == 1) && (params[0] != null) && (params[0] instanceof ProxyCtl)) { ProxyCtl equalsProxy = (ProxyCtl) params[0]; params = new Object[1]; params[0] = equalsProxy.__make(); } return ReflectionHelper.invoke(service, method, params, locator.getNeutralContextClassLoader()); }
@SuppressWarnings("unchecked") @Override public <T> T getService(ActiveDescriptor<T> activeDescriptor, ServiceHandle<?> root, Injectee originalRequest) throws MultiException { checkState(); Type contractOrImpl = (originalRequest == null) ? null : originalRequest.getRequiredType(); Class<?> rawClass = ReflectionHelper.getRawClass(contractOrImpl); if (root == null) { ServiceHandleImpl<T> tmpRoot = new ServiceHandleImpl<T>(this, activeDescriptor, originalRequest); return Utilities.createService(activeDescriptor, originalRequest, this, tmpRoot, rawClass); } ServiceHandleImpl<?> rootImpl = (ServiceHandleImpl<?>) root; ServiceHandleImpl<T> subHandle = internalGetServiceHandle(activeDescriptor, contractOrImpl, originalRequest); if (PerLookup.class.equals(activeDescriptor.getScopeAnnotation())) { rootImpl.addSubHandle(subHandle); } rootImpl.pushInjectee(originalRequest); try { return subHandle.getService((ServiceHandle<T>) root); } finally { rootImpl.popInjectee(); } }
/* package */ T getService(ServiceHandle<T> handle) { if (root instanceof Closeable) { Closeable closeable = (Closeable) root; if (closeable.isClosed()) { throw new IllegalStateException("This service has been unbound: " + root); } } synchronized (lock) { if (serviceDestroyed) throw new IllegalStateException("Service has been disposed"); if (serviceSet) return service; Injectee injectee = getLastInjectee(); Class<?> requiredClass = (injectee == null) ? null : ReflectionHelper.getRawClass(injectee.getRequiredType()); service = Utilities.createService(root, injectee, locator, handle, requiredClass); serviceSet = true; return service; } }
@SuppressWarnings("unchecked") private static ParameterizedType fillInPT(ParameterizedType pt, HashMap<Class<?>, ParameterizedType> class2TypeMap) { if (isFilledIn(pt)) return pt; newActualArguments[outerIndex] = fillInPT((ParameterizedType) fillMeIn, class2TypeMap); continue; ParameterizedTypeImpl pti = new ParameterizedTypeImpl(getRawClass(pt), newActualArguments); return pti;
qualifiers = ReflectionHelper.getQualifierAnnotations(clazz); name = ReflectionHelper.getNameFromAllQualifiers(qualifiers, clazz); if (serviceMetadata != null) { try { ReflectionHelper.readMetadataMap(serviceMetadata, metadata); ReflectionHelper.parseServiceMetadataString(serviceMetadata, metadata);
/** * Post constructs the given object * * @param postMe post constructs the thing * @param locator The non-null service locator associated with the operation (for finding the strategy) * @param strategy The strategy to use for analyzing the class */ public static void justPostConstruct(Object postMe, ServiceLocatorImpl locator, String strategy) { if (postMe == null) throw new IllegalArgumentException(); Collector collector = new Collector(); ClassAnalyzer analyzer = getClassAnalyzer(locator, strategy, collector); collector.throwIfErrors(); Class<?> baseClass = postMe.getClass(); Method postConstruct = getPostConstruct(baseClass, analyzer, collector); collector.throwIfErrors(); if (postConstruct == null) return; try { ReflectionHelper.invoke(postMe, postConstruct, new Object[0], locator.getNeutralContextClassLoader()); } catch (Throwable e) { throw new MultiException(e); } }
private void methodMe(Map<SystemInjecteeImpl, Object> resolved, T t) throws Throwable { for (ResolutionInfo ri : myInitializers) { Method m = (Method) ri.baseElement; List<SystemInjecteeImpl> injectees = ri.injectees; Object args[] = new Object[injectees.size()]; for (Injectee injectee : injectees) { args[injectee.getPosition()] = resolved.get(injectee); } ReflectionHelper.invoke(t, m, args, locator.getNeutralContextClassLoader()); } }
if (clazz == null) return new DescriptorImpl(); Set<String> contracts = ReflectionHelper.getContractsFromClass(clazz, Contract.class); String name = ReflectionHelper.getName(clazz); String scope = ReflectionHelper.getScopeFromClass(clazz, ServiceLocatorUtilities.getPerLookupAnnotation()).annotationType().getName(); Set<String> qualifiers = ReflectionHelper.getQualifiersFromClass(clazz); DescriptorType type = DescriptorType.CLASS; if (Factory.class.isAssignableFrom(clazz)) {
ReflectionHelper.readSet(rightHandSide, contracts); ReflectionHelper.readSet(rightHandSide, localQualifiers); if (!localQualifiers.isEmpty()) qualifiers = localQualifiers; ReflectionHelper.readMetadataMap(rightHandSide, localMetadatas); if (!localMetadatas.isEmpty()) metadatas = localMetadatas;
private static boolean isProperField(Field field) { if (ReflectionHelper.isStatic(field)) return false; if (isFinal(field)) return false; Class<?> type = field.getType(); return !type.isAnnotation(); }
/** * Resolves the generic type of a field given the actual class being instantiated * * @param topclass The instantiation class. Must not be null * @param field The non-null field whose type to resolve * @return The resolved field type by way of its subclasses. Will not return * null, but may return the original fields generic type */ public static Type resolveField(Class<?> topclass, Field field) { return resolveMember(topclass, field.getGenericType(), field.getDeclaringClass()); }
/** * Gets all the qualifiers from the object * * @param clazz The class to analyze * @return The set of qualifiers. Will not return null but may return an empty set */ public static Set<String> getQualifiersFromClass(Class<?> clazz) { Set<String> retVal = new LinkedHashSet<String>(); if (clazz == null) return retVal; for (Annotation annotation : clazz.getAnnotations()) { if (isAnnotationAQualifier(annotation)) { retVal.add(annotation.annotationType().getName()); } } while (clazz != null) { for (Class<?> iFace : clazz.getInterfaces()) { for (Annotation annotation : iFace.getAnnotations()) { if (isAnnotationAQualifier(annotation)) { retVal.add(annotation.annotationType().getName()); } } } clazz = clazz.getSuperclass(); } return retVal; }