下面列出了org.springframework.util.ClassUtils#getAllInterfacesForClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Checks to see if the given method is declared in a managed
* interface for the given bean.
*/
private boolean isDeclaredInInterface(Method method, String beanKey) {
Class<?>[] ifaces = null;
if (this.resolvedInterfaceMappings != null) {
ifaces = this.resolvedInterfaceMappings.get(beanKey);
}
if (ifaces == null) {
ifaces = this.managedInterfaces;
if (ifaces == null) {
ifaces = ClassUtils.getAllInterfacesForClass(method.getDeclaringClass());
}
}
for (Class<?> ifc : ifaces) {
for (Method ifcMethod : ifc.getMethods()) {
if (ifcMethod.getName().equals(method.getName()) &&
Arrays.equals(ifcMethod.getParameterTypes(), method.getParameterTypes())) {
return true;
}
}
}
return false;
}
private Class<?> determineCommonType(Class<?> oldType, Class<?> newType) {
if (oldType == null) {
return newType;
}
if (oldType.isAssignableFrom(newType)) {
return oldType;
}
Class<?> nextType = newType;
while (nextType != Object.class) {
if (nextType.isAssignableFrom(oldType)) {
return nextType;
}
nextType = nextType.getSuperclass();
}
Class<?>[] interfaces = ClassUtils.getAllInterfacesForClass(newType);
for (Class<?> nextInterface : interfaces) {
if (nextInterface.isAssignableFrom(oldType)) {
return nextInterface;
}
}
return Object.class;
}
/**
* Checks to see if the given method is declared in a managed
* interface for the given bean.
*/
private boolean isDeclaredInInterface(Method method, String beanKey) {
Class<?>[] ifaces = null;
if (this.resolvedInterfaceMappings != null) {
ifaces = this.resolvedInterfaceMappings.get(beanKey);
}
if (ifaces == null) {
ifaces = this.managedInterfaces;
if (ifaces == null) {
ifaces = ClassUtils.getAllInterfacesForClass(method.getDeclaringClass());
}
}
for (Class<?> ifc : ifaces) {
for (Method ifcMethod : ifc.getMethods()) {
if (ifcMethod.getName().equals(method.getName()) &&
Arrays.equals(ifcMethod.getParameterTypes(), method.getParameterTypes())) {
return true;
}
}
}
return false;
}
/**
* Searches for the generic {@link Method} declaration whose erased signature
* matches that of the supplied bridge method.
* @throws IllegalStateException if the generic declaration cannot be found
*/
@Nullable
private static Method findGenericDeclaration(Method bridgeMethod) {
// Search parent types for method that has same signature as bridge.
Class<?> superclass = bridgeMethod.getDeclaringClass().getSuperclass();
while (superclass != null && Object.class != superclass) {
Method method = searchForMatch(superclass, bridgeMethod);
if (method != null && !method.isBridge()) {
return method;
}
superclass = superclass.getSuperclass();
}
Class<?>[] interfaces = ClassUtils.getAllInterfacesForClass(bridgeMethod.getDeclaringClass());
return searchInterfaces(interfaces, bridgeMethod);
}
@RequestMapping(value = "getAllService", method = RequestMethod.GET)
public List<Map<String, Object>> getAllService() throws Exception {
List<Map<String, Object>> services = Lists.newArrayList();
try {
Collection<Object> instances = getTypedBeansWithAnnotation(SalukiService.class);
for (Object instance : instances) {
Object target = GrpcAop.getTarget(instance);
Class<?>[] interfaces = ClassUtils.getAllInterfacesForClass(target.getClass());
Class<?> clzz = interfaces[0];
Map<String, Object> serviceMap = Maps.newHashMap();
serviceMap.put("simpleName", clzz.getSimpleName());
serviceMap.put("name", clzz.getName());
ServiceDefinition sd = Jaket.build(clzz);
List<MethodDefinition> methodDefines = sd.getMethods();
List<String> functions = Lists.newArrayList();
for (MethodDefinition methodDefine : methodDefines) {
functions.add(methodDefine.getName());
}
serviceMap.put("functions", functions);
services.add(serviceMap);
}
return services;
} catch (Exception e) {
throw e;
}
}
/**
* Create a refreshable proxy for the given AOP TargetSource.
* @param ts the refreshable TargetSource
* @param interfaces the proxy interfaces (may be {@code null} to
* indicate proxying of all interfaces implemented by the target class)
* @return the generated proxy
* @see RefreshableScriptTargetSource
*/
protected Object createRefreshableProxy(TargetSource ts, Class<?>[] interfaces, boolean proxyTargetClass) {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTargetSource(ts);
ClassLoader classLoader = this.beanClassLoader;
if (interfaces == null) {
interfaces = ClassUtils.getAllInterfacesForClass(ts.getTargetClass(), this.beanClassLoader);
}
proxyFactory.setInterfaces(interfaces);
if (proxyTargetClass) {
classLoader = null; // force use of Class.getClassLoader()
proxyFactory.setProxyTargetClass(true);
}
DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts);
introduction.suppressInterface(TargetSource.class);
proxyFactory.addAdvice(introduction);
return proxyFactory.getProxy(classLoader);
}
/**
* Checks to see if the given method is declared in a managed
* interface for the given bean.
*/
private boolean isDeclaredInInterface(Method method, String beanKey) {
Class<?>[] ifaces = null;
if (this.resolvedInterfaceMappings != null) {
ifaces = this.resolvedInterfaceMappings.get(beanKey);
}
if (ifaces == null) {
ifaces = this.managedInterfaces;
if (ifaces == null) {
ifaces = ClassUtils.getAllInterfacesForClass(method.getDeclaringClass());
}
}
for (Class<?> ifc : ifaces) {
for (Method ifcMethod : ifc.getMethods()) {
if (ifcMethod.getName().equals(method.getName()) &&
Arrays.equals(ifcMethod.getParameterTypes(), method.getParameterTypes())) {
return true;
}
}
}
return false;
}
private Class<?> determineCommonType(Class<?> oldType, Class<?> newType) {
if (oldType == null) {
return newType;
}
if (oldType.isAssignableFrom(newType)) {
return oldType;
}
Class<?> nextType = newType;
while (nextType != Object.class) {
if (nextType.isAssignableFrom(oldType)) {
return nextType;
}
nextType = nextType.getSuperclass();
}
Class<?>[] interfaces = ClassUtils.getAllInterfacesForClass(newType);
for (Class<?> nextInterface : interfaces) {
if (nextInterface.isAssignableFrom(oldType)) {
return nextInterface;
}
}
return Object.class;
}
/**
* Create a refreshable proxy for the given AOP TargetSource.
* @param ts the refreshable TargetSource
* @param interfaces the proxy interfaces (may be {@code null} to
* indicate proxying of all interfaces implemented by the target class)
* @return the generated proxy
* @see RefreshableScriptTargetSource
*/
protected Object createRefreshableProxy(TargetSource ts, Class<?>[] interfaces, boolean proxyTargetClass) {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTargetSource(ts);
ClassLoader classLoader = this.beanClassLoader;
if (interfaces == null) {
interfaces = ClassUtils.getAllInterfacesForClass(ts.getTargetClass(), this.beanClassLoader);
}
proxyFactory.setInterfaces(interfaces);
if (proxyTargetClass) {
classLoader = null; // force use of Class.getClassLoader()
proxyFactory.setProxyTargetClass(true);
}
DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts);
introduction.suppressInterface(TargetSource.class);
proxyFactory.addAdvice(introduction);
return proxyFactory.getProxy(classLoader);
}
private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws NamingException {
// Create a JndiObjectTargetSource that mirrors the JndiObjectFactoryBean's configuration.
JndiObjectTargetSource targetSource = new JndiObjectTargetSource();
targetSource.setJndiTemplate(jof.getJndiTemplate());
String jndiName = jof.getJndiName();
Assert.state(jndiName != null, "No JNDI name specified");
targetSource.setJndiName(jndiName);
targetSource.setExpectedType(jof.getExpectedType());
targetSource.setResourceRef(jof.isResourceRef());
targetSource.setLookupOnStartup(jof.lookupOnStartup);
targetSource.setCache(jof.cache);
targetSource.afterPropertiesSet();
// Create a proxy with JndiObjectFactoryBean's proxy interface and the JndiObjectTargetSource.
ProxyFactory proxyFactory = new ProxyFactory();
if (jof.proxyInterfaces != null) {
proxyFactory.setInterfaces(jof.proxyInterfaces);
}
else {
Class<?> targetClass = targetSource.getTargetClass();
if (targetClass == null) {
throw new IllegalStateException(
"Cannot deactivate 'lookupOnStartup' without specifying a 'proxyInterface' or 'expectedType'");
}
Class<?>[] ifcs = ClassUtils.getAllInterfacesForClass(targetClass, jof.beanClassLoader);
for (Class<?> ifc : ifcs) {
if (Modifier.isPublic(ifc.getModifiers())) {
proxyFactory.addInterface(ifc);
}
}
}
if (jof.exposeAccessContext) {
proxyFactory.addAdvice(new JndiContextExposingInterceptor(jof.getJndiTemplate()));
}
proxyFactory.setTargetSource(targetSource);
return proxyFactory.getProxy(jof.beanClassLoader);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Invocation on PersistenceManagerFactory interface coming in...
if (method.getName().equals("equals")) {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
}
else if (method.getName().equals("hashCode")) {
// Use hashCode of PersistenceManagerFactory proxy.
return System.identityHashCode(proxy);
}
else if (method.getName().equals("getPersistenceManager")) {
PersistenceManagerFactory target = getTargetPersistenceManagerFactory();
PersistenceManager pm =
PersistenceManagerFactoryUtils.doGetPersistenceManager(target, isAllowCreate());
Class<?>[] ifcs = ClassUtils.getAllInterfacesForClass(pm.getClass(), pm.getClass().getClassLoader());
return Proxy.newProxyInstance(
pm.getClass().getClassLoader(), ifcs, new PersistenceManagerInvocationHandler(pm, target));
}
// Invoke method on target PersistenceManagerFactory.
try {
return method.invoke(getTargetPersistenceManagerFactory(), args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
@Nonnull
private Class findJmxInterface(String beanKey, Class<?> beanClass) {
Class cachedInterface = interfaceCache.get(beanKey);
if (cachedInterface != null) {
return cachedInterface;
}
Class mbeanInterface = JmxUtils.getMBeanInterface(beanClass);
if (mbeanInterface != null) { // found with MBean ending
interfaceCache.put(beanKey, mbeanInterface);
return mbeanInterface;
}
Class[] ifaces = ClassUtils.getAllInterfacesForClass(beanClass);
for (Class ifc : ifaces) {
ManagedResource metadata = attributeSource.getManagedResource(ifc);
if (metadata != null) { // found with @ManagedResource annotation
interfaceCache.put(beanKey, ifc);
return ifc;
}
}
throw new IllegalArgumentException(String.format(
"Bean %s doesn't implement management interfaces. Management interface should either follow naming scheme or be annotated by @ManagedResource",
beanKey));
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
for (Class<?> interfaceType : ClassUtils.getAllInterfacesForClass(sourceType.getType())) {
if (conversionService.canConvert(TypeDescriptor.valueOf(interfaceType), targetType)) {
return false;
}
}
return true;
}
private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws NamingException {
// Create a JndiObjectTargetSource that mirrors the JndiObjectFactoryBean's configuration.
JndiObjectTargetSource targetSource = new JndiObjectTargetSource();
targetSource.setJndiTemplate(jof.getJndiTemplate());
targetSource.setJndiName(jof.getJndiName());
targetSource.setExpectedType(jof.getExpectedType());
targetSource.setResourceRef(jof.isResourceRef());
targetSource.setLookupOnStartup(jof.lookupOnStartup);
targetSource.setCache(jof.cache);
targetSource.afterPropertiesSet();
// Create a proxy with JndiObjectFactoryBean's proxy interface and the JndiObjectTargetSource.
ProxyFactory proxyFactory = new ProxyFactory();
if (jof.proxyInterfaces != null) {
proxyFactory.setInterfaces(jof.proxyInterfaces);
}
else {
Class<?> targetClass = targetSource.getTargetClass();
if (targetClass == null) {
throw new IllegalStateException(
"Cannot deactivate 'lookupOnStartup' without specifying a 'proxyInterface' or 'expectedType'");
}
Class<?>[] ifcs = ClassUtils.getAllInterfacesForClass(targetClass, jof.beanClassLoader);
for (Class<?> ifc : ifcs) {
if (Modifier.isPublic(ifc.getModifiers())) {
proxyFactory.addInterface(ifc);
}
}
}
if (jof.exposeAccessContext) {
proxyFactory.addAdvice(new JndiContextExposingInterceptor(jof.getJndiTemplate()));
}
proxyFactory.setTargetSource(targetSource);
return proxyFactory.getProxy(jof.beanClassLoader);
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
for (Class<?> interfaceType : ClassUtils.getAllInterfacesForClass(sourceType.getType())) {
if (this.conversionService.canConvert(TypeDescriptor.valueOf(interfaceType), targetType)) {
return false;
}
}
return true;
}
/**
* Set the target JDO PersistenceManagerFactory that this proxy should
* delegate to. This should be the raw PersistenceManagerFactory, as
* accessed by JdoTransactionManager.
* @see org.springframework.orm.jdo.JdoTransactionManager
*/
public void setTargetPersistenceManagerFactory(PersistenceManagerFactory target) {
Assert.notNull(target, "Target PersistenceManagerFactory must not be null");
this.target = target;
Class<?>[] ifcs = ClassUtils.getAllInterfacesForClass(target.getClass(), target.getClass().getClassLoader());
this.proxy = (PersistenceManagerFactory) Proxy.newProxyInstance(
target.getClass().getClassLoader(), ifcs, new PersistenceManagerFactoryInvocationHandler());
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Invocation on PersistenceManagerFactory interface coming in...
if (method.getName().equals("equals")) {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
}
else if (method.getName().equals("hashCode")) {
// Use hashCode of PersistenceManagerFactory proxy.
return System.identityHashCode(proxy);
}
else if (method.getName().equals("getPersistenceManager")) {
PersistenceManagerFactory target = getTargetPersistenceManagerFactory();
PersistenceManager pm =
PersistenceManagerFactoryUtils.doGetPersistenceManager(target, isAllowCreate());
Class<?>[] ifcs = ClassUtils.getAllInterfacesForClass(pm.getClass(), pm.getClass().getClassLoader());
return Proxy.newProxyInstance(
pm.getClass().getClassLoader(), ifcs, new PersistenceManagerInvocationHandler(pm, target));
}
// Invoke method on target PersistenceManagerFactory.
try {
return method.invoke(getTargetPersistenceManagerFactory(), args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
private static Object createJndiObjectProxy(JndiObjectFactoryBean jof) throws NamingException {
// Create a JndiObjectTargetSource that mirrors the JndiObjectFactoryBean's configuration.
JndiObjectTargetSource targetSource = new JndiObjectTargetSource();
targetSource.setJndiTemplate(jof.getJndiTemplate());
targetSource.setJndiName(jof.getJndiName());
targetSource.setExpectedType(jof.getExpectedType());
targetSource.setResourceRef(jof.isResourceRef());
targetSource.setLookupOnStartup(jof.lookupOnStartup);
targetSource.setCache(jof.cache);
targetSource.afterPropertiesSet();
// Create a proxy with JndiObjectFactoryBean's proxy interface and the JndiObjectTargetSource.
ProxyFactory proxyFactory = new ProxyFactory();
if (jof.proxyInterfaces != null) {
proxyFactory.setInterfaces(jof.proxyInterfaces);
}
else {
Class<?> targetClass = targetSource.getTargetClass();
if (targetClass == null) {
throw new IllegalStateException(
"Cannot deactivate 'lookupOnStartup' without specifying a 'proxyInterface' or 'expectedType'");
}
Class<?>[] ifcs = ClassUtils.getAllInterfacesForClass(targetClass, jof.beanClassLoader);
for (Class<?> ifc : ifcs) {
if (Modifier.isPublic(ifc.getModifiers())) {
proxyFactory.addInterface(ifc);
}
}
}
if (jof.exposeAccessContext) {
proxyFactory.addAdvice(new JndiContextExposingInterceptor(jof.getJndiTemplate()));
}
proxyFactory.setTargetSource(targetSource);
return proxyFactory.getProxy(jof.beanClassLoader);
}
/**
* Checks to see if the given method is declared in a managed
* interface for the given bean.
*/
private boolean isDeclaredInInterface(Method method, String beanKey) {
Class<?>[] ifaces = null;
if (this.resolvedInterfaceMappings != null) {
ifaces = this.resolvedInterfaceMappings.get(beanKey);
}
if (ifaces == null) {
ifaces = this.managedInterfaces;
if (ifaces == null) {
ifaces = ClassUtils.getAllInterfacesForClass(method.getDeclaringClass());
}
}
if (ifaces != null) {
for (Class<?> ifc : ifaces) {
for (Method ifcMethod : ifc.getMethods()) {
if (ifcMethod.getName().equals(method.getName()) &&
Arrays.equals(ifcMethod.getParameterTypes(), method.getParameterTypes())) {
return true;
}
}
}
}
return false;
}
@Override
public void run(String... arg0) throws Exception {
System.out.println("Starting GRPC Server ...");
RpcServiceConfig rpcSerivceConfig = new RpcServiceConfig();
this.addRegistyAddress(rpcSerivceConfig);
rpcSerivceConfig.setApplication(applicationName);
this.addHostAndPort(rpcSerivceConfig);
rpcSerivceConfig.setMonitorinterval(grpcProperties.getMonitorinterval());
Collection<Object> instances = getTypedBeansWithAnnotation(SalukiService.class);
if (instances.size() > 0) {
try {
for (Object instance : instances) {
Object target = GrpcAop.getTarget(instance);
SalukiService serviceAnnotation = target.getClass().getAnnotation(SalukiService.class);
String serviceName = serviceAnnotation.service();
Set<String> serviceNames = Sets.newHashSet();
if (StringUtils.isBlank(serviceName)) {
if (this.isGrpcServer(target)) {
throw new java.lang.IllegalArgumentException(
"you use grpc stub service,must set service name,service instance is" + target);
} else {
Class<?>[] interfaces = ClassUtils.getAllInterfacesForClass(target.getClass());
for (Class<?> interfaceClass : interfaces) {
String interfaceName = interfaceClass.getName();
if (!StringUtils.startsWith(interfaceName, "org.springframework")) {
serviceNames.add(interfaceName);
}
}
}
} else {
serviceNames.add(serviceName);
}
for (String realServiceName : serviceNames) {
rpcSerivceConfig.addServiceDefinition(realServiceName, getGroup(serviceAnnotation),
getVersion(serviceAnnotation), instance);
}
}
} finally {
Object healthInstance = new HealthImpl(applicationContext);
BeanDefinitionRegistry beanDefinitonRegistry =
(BeanDefinitionRegistry) applicationContext.getBeanFactory();
BeanDefinitionBuilder beanDefinitionBuilder =
BeanDefinitionBuilder.genericBeanDefinition(Health.class);
beanDefinitonRegistry.registerBeanDefinition(Health.class.getName(),
beanDefinitionBuilder.getRawBeanDefinition());
applicationContext.getBeanFactory().registerSingleton(Health.class.getName(),
healthInstance);
String group = grpcProperties.getGroup() != null ? grpcProperties.getGroup() : "default";
String version =
grpcProperties.getVersion() != null ? grpcProperties.getVersion() : "1.0.0";
rpcSerivceConfig.addServiceDefinition(Health.class.getName(), group, version,
healthInstance);
}
}
this.rpcService = rpcSerivceConfig;
rpcSerivceConfig.export();
System.out.println("****************");
System.out.println(String.format("GRPC server has started!You can do test by %s",
"http://localhost:" + httpPort + "/doc"));
System.out.println("****************");
rpcSerivceConfig.await();
}