Java Code Examples for org.springframework.util.ClassUtils#getAllInterfaces()

The following examples show how to use org.springframework.util.ClassUtils#getAllInterfaces() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericsUtils.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
/**
 * Return the generic type of PollableSource to determine if it is appropriate for the
 * binder. e.g., with PollableMessageSource extends
 * PollableSource<MessageHandler> and AbstractMessageChannelBinder implements
 * PollableConsumerBinder<MessageHandler, C> We're checking that the the generic
 * type (MessageHandler) matches.
 * @param binderInstance the binder.
 * @param bindingTargetType the binding target type.
 * @return true if found, false otherwise.
 */
@SuppressWarnings("rawtypes")
public static boolean checkCompatiblePollableBinder(Binder binderInstance,
		Class<?> bindingTargetType) {
	Class<?>[] binderInterfaces = ClassUtils.getAllInterfaces(binderInstance);
	for (Class<?> intf : binderInterfaces) {
		if (PollableConsumerBinder.class.isAssignableFrom(intf)) {
			Class<?>[] targetInterfaces = ClassUtils
					.getAllInterfacesForClass(bindingTargetType);
			Class<?> psType = findPollableSourceType(targetInterfaces);
			if (psType != null) {
				return getParameterType(binderInstance.getClass(), intf, 0)
						.isAssignableFrom(psType);
			}
		}
	}
	return false;
}
 
Example 2
Source File: ThriftAutoConfiguration.java    From spring-thrift-starter with MIT License 4 votes vote down vote up
protected void register(ServletContext servletContext, String[] urls, Class<? extends TProtocolFactory> factory, Object handler) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException {
    Class<?>[] handlerInterfaces = ClassUtils.getAllInterfaces(handler);

    Class ifaceClass = null;
    Class<TProcessor> processorClass = null;
    Class serviceClass = null;

    for (Class<?> handlerInterfaceClass : handlerInterfaces) {
        if (!handlerInterfaceClass.getName().endsWith("$Iface")) {
            continue;
        }

        serviceClass = handlerInterfaceClass.getDeclaringClass();

        if (serviceClass == null) {
            continue;
        }

        for (Class<?> innerClass : serviceClass.getDeclaredClasses()) {
            if (!innerClass.getName().endsWith("$Processor")) {
                continue;
            }

            if (!TProcessor.class.isAssignableFrom(innerClass)) {
                continue;
            }

            if (ifaceClass != null) {
                throw new IllegalStateException("Multiple Thrift Ifaces defined on handler");
            }

            ifaceClass = handlerInterfaceClass;
            processorClass = (Class<TProcessor>) innerClass;
            break;
        }
    }

    if (ifaceClass == null) {
        throw new IllegalStateException("No Thrift Ifaces found on handler");
    }

    handler = wrapHandler(ifaceClass, handler);

    Constructor<TProcessor> processorConstructor = processorClass.getConstructor(ifaceClass);

    TProcessor processor = BeanUtils.instantiateClass(processorConstructor, handler);

    TServlet servlet;
    if (TProtocolFactory.class.equals(factory)) {
        servlet = getServlet(processor, protocolFactory);
    } else {
        servlet = getServlet(processor, factory.newInstance());
    }

    String servletBeanName = handler.getClass().getSimpleName() + "Servlet";

    ServletRegistration.Dynamic registration = servletContext.addServlet(servletBeanName, servlet);

    if (urls != null && urls.length > 0) {
        registration.addMapping(urls);
    } else {
        registration.addMapping("/" + serviceClass.getSimpleName());
    }
}
 
Example 3
Source File: TransactionAwareRepository.java    From mycollab with GNU Affero General Public License v3.0 4 votes vote down vote up
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

            // Invocation on Repository interface coming in...
            // check method invocation
            if (method.getName().equals("login")) {
                boolean matched = false;

                // check method signature
                Class<?>[] paramTypes = method.getParameterTypes();

                // a. login()
                if (paramTypes.length == 0) {
                    // match the sessionFactory definitions
                    matched = (sessionFactory.getWorkspaceName() == null && sessionFactory.getCredentials() == null);
                } else if (paramTypes.length == 1) {
                    // b. login(java.lang.String workspaceName)
                    if (paramTypes[0] == String.class)
                        matched = ObjectUtils.nullSafeEquals(args[0], sessionFactory.getWorkspaceName());
                    // c. login(Credentials credentials)
                    if (Credentials.class.isAssignableFrom(paramTypes[0]))
                        matched = ObjectUtils.nullSafeEquals(args[0], sessionFactory.getCredentials());
                } else if (paramTypes.length == 2) {
                    // d. login(Credentials credentials, java.lang.String
                    // workspaceName)
                    matched = ObjectUtils.nullSafeEquals(args[0], sessionFactory.getCredentials())
                            && ObjectUtils.nullSafeEquals(args[1], sessionFactory.getWorkspaceName());
                }

                if (matched) {
                    Session session = SessionFactoryUtils.getSession(sessionFactory, isAllowCreate());
                    Class<?>[] ifcs = ClassUtils.getAllInterfaces(session);
                    return Proxy.newProxyInstance(getClass().getClassLoader(), ifcs,
                            new TransactionAwareInvocationHandler(session, sessionFactory));
                }
            } else if (method.getName().equals("equals")) {
                // Only consider equal when proxies are identical.
                return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);
            } else if (method.getName().equals("hashCode")) {
                // Use hashCode of Repository proxy.
                return hashCode();
            }

            Repository target = getTargetRepository();

            // Invoke method on target Repository.
            try {
                return method.invoke(target, args);
            } catch (InvocationTargetException ex) {
                throw ex.getTargetException();
            }
        }