类org.springframework.core.type.MethodMetadata源码实例Demo

下面列出了怎么用org.springframework.core.type.MethodMetadata的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: spring-init   文件: SimpleConditionService.java
@Override
public boolean matches(Class<?> factory, Class<?> type) {
	AnnotationMetadata metadata = getMetadata(factory);
	Set<MethodMetadata> assignable = new HashSet<>();
	for (MethodMetadata method : metadata.getAnnotatedMethods(Bean.class.getName())) {
		Class<?> candidate = ClassUtils.resolveClassName(method.getReturnTypeName(), this.classLoader);
		// Look for exact match first
		if (type.equals(candidate)) {
			return !this.evaluator.shouldSkip(method);
		}
		if (type.isAssignableFrom(candidate)) {
			assignable.add(method);
		}
	}
	if (assignable.size() == 1) {
		return !this.evaluator.shouldSkip(assignable.iterator().next());
	}
	// TODO: fail if size() > 1
	Class<?> base = factory.getSuperclass();
	if (AnnotationUtils.isAnnotationDeclaredLocally(Configuration.class, base)) {
		return matches(base, type);
	}
	return false;
}
 
源代码2 项目: COLA   文件: AbstractRecordController.java
protected String getClassName(Object bean, BeanDefinition beanDefinition){
    String className = null;
    if(bean instanceof FactoryBean){
        className = ((FactoryBean)bean).getObjectType().getName();
    }else if(beanDefinition instanceof AnnotatedBeanDefinition){
        MethodMetadata methodMetadata = ((AnnotatedBeanDefinition)beanDefinition).getFactoryMethodMetadata();

        if(methodMetadata != null){
            className = methodMetadata.getReturnTypeName();
        }else{
            className = ((AnnotatedBeanDefinition)beanDefinition).getMetadata().getClassName();
        }
    }else if(beanDefinition instanceof RootBeanDefinition){
        className = bean.getClass().getName();
    }else if(bean instanceof Proxy){
        className = getClassNameFromProxy(bean);
    }else{
        className = beanDefinition.getBeanClassName();
    }
    return className;
}
 
/**
 * Gets the annotation attributes {@link RestTemplate} bean being annotated
 * {@link DubboTransported @DubboTransported}.
 * @param beanName the bean name of {@link LoadBalanced @LoadBalanced}
 * {@link RestTemplate}
 * @param attributesResolver {@link DubboTransportedAttributesResolver}
 * @return non-null {@link Map}
 */
private Map<String, Object> getDubboTranslatedAttributes(String beanName,
		DubboTransportedAttributesResolver attributesResolver) {
	Map<String, Object> attributes = Collections.emptyMap();
	BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
	if (beanDefinition instanceof AnnotatedBeanDefinition) {
		AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition;
		MethodMetadata factoryMethodMetadata = annotatedBeanDefinition
				.getFactoryMethodMetadata();
		attributes = factoryMethodMetadata != null ? Optional
				.ofNullable(factoryMethodMetadata
						.getAnnotationAttributes(DUBBO_TRANSPORTED_CLASS_NAME))
				.orElse(attributes) : Collections.emptyMap();
	}
	return attributesResolver.resolve(attributes);
}
 
public static Type findType(BeanFactory beanFactory, String name) {
	ConfigurableListableBeanFactory registry = (ConfigurableListableBeanFactory) beanFactory;
	AbstractBeanDefinition definition = (AbstractBeanDefinition) registry.getBeanDefinition(name);

	Object source = definition.getSource();

	Type param = null;
	if (source instanceof MethodMetadata) {
		param = findBeanType(definition, ((MethodMetadata) source).getDeclaringClassName(), ((MethodMetadata) source).getMethodName());
	}
	else if (source instanceof Resource) {
		param = registry.getType(name);
	}
	else {
		ResolvableType type = (ResolvableType) getField(definition, "targetType");
		if (type != null) {
			param = type.getType();
		}
	}
	return param;
}
 
private PollableBean extractPollableAnnotation(StreamFunctionProperties functionProperties, GenericApplicationContext context,
		BindableFunctionProxyFactory proxyFactory) {
	// here we need to ensure that for cases where composition is defined we only look for supplier method to find Pollable annotation.
	String supplierFunctionName = StringUtils
			.delimitedListToStringArray(proxyFactory.getFunctionDefinition().replaceAll(",", "|").trim(), "|")[0];
	BeanDefinition bd = context.getBeanDefinition(supplierFunctionName);
	if (!(bd instanceof RootBeanDefinition)) {
		return null;
	}

	Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
	if (factoryMethod == null) {
		Object source = bd.getSource();
		if (source instanceof MethodMetadata) {
			Class<?> factory = ClassUtils.resolveClassName(((MethodMetadata) source).getDeclaringClassName(), null);
			Class<?>[] params = FunctionContextUtils.getParamTypesFromBeanDefinitionFactory(factory, (RootBeanDefinition) bd);
			factoryMethod = ReflectionUtils.findMethod(factory, ((MethodMetadata) source).getMethodName(), params);
		}
	}
	Assert.notNull(factoryMethod, "Failed to introspect factory method since it was not discovered for function '"
			+ functionProperties.getDefinition() + "'");
	return factoryMethod.getReturnType().isAssignableFrom(Supplier.class)
			? AnnotationUtils.findAnnotation(factoryMethod, PollableBean.class)
					: null;
}
 
源代码6 项目: spring-cloud-config   文件: CompositeUtils.java
/**
 * Given a Factory Name return the generic type parameters of the factory (The actual
 * repository class, and its properties class).
 * @param beanFactory Spring Bean Factory
 * @param factoryName name of the factory
 * @return generic type params of the factory
 */
public static Type[] getEnvironmentRepositoryFactoryTypeParams(
		ConfigurableListableBeanFactory beanFactory, String factoryName) {
	MethodMetadata methodMetadata = (MethodMetadata) beanFactory
			.getBeanDefinition(factoryName).getSource();
	Class<?> factoryClass = null;
	try {
		factoryClass = Class.forName(methodMetadata.getReturnTypeName());
	}
	catch (ClassNotFoundException e) {
		throw new IllegalStateException(e);
	}
	Optional<AnnotatedType> annotatedFactoryType = Arrays
			.stream(factoryClass.getAnnotatedInterfaces()).filter(i -> {
				ParameterizedType parameterizedType = (ParameterizedType) i.getType();
				return parameterizedType.getRawType()
						.equals(EnvironmentRepositoryFactory.class);
			}).findFirst();
	ParameterizedType factoryParameterizedType = (ParameterizedType) annotatedFactoryType
			.orElse(factoryClass.getAnnotatedSuperclass()).getType();
	return factoryParameterizedType.getActualTypeArguments();
}
 
/**
 * Register default methods on interfaces implemented by the configuration class.
 */
private void processInterfaces(ConfigurationClass configClass, SourceClass sourceClass) throws IOException {
	for (SourceClass ifc : sourceClass.getInterfaces()) {
		Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(ifc);
		for (MethodMetadata methodMetadata : beanMethods) {
			if (!methodMetadata.isAbstract()) {
				// A default method or other concrete method on a Java 8+ interface...
				configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
			}
		}
		processInterfaces(configClass, ifc);
	}
}
 
/**
 * Retrieve the metadata for all <code>@Bean</code> methods.
 */
private Set<MethodMetadata> retrieveBeanMethodMetadata(SourceClass sourceClass) {
	AnnotationMetadata original = sourceClass.getMetadata();
	Set<MethodMetadata> beanMethods = original.getAnnotatedMethods(Bean.class.getName());
	if (beanMethods.size() > 1 && original instanceof StandardAnnotationMetadata) {
		// Try reading the class file via ASM for deterministic declaration order...
		// Unfortunately, the JVM's standard reflection returns methods in arbitrary
		// order, even between different runs of the same application on the same JVM.
		try {
			AnnotationMetadata asm =
					this.metadataReaderFactory.getMetadataReader(original.getClassName()).getAnnotationMetadata();
			Set<MethodMetadata> asmMethods = asm.getAnnotatedMethods(Bean.class.getName());
			if (asmMethods.size() >= beanMethods.size()) {
				Set<MethodMetadata> selectedMethods = new LinkedHashSet<>(asmMethods.size());
				for (MethodMetadata asmMethod : asmMethods) {
					for (MethodMetadata beanMethod : beanMethods) {
						if (beanMethod.getMethodName().equals(asmMethod.getMethodName())) {
							selectedMethods.add(beanMethod);
							break;
						}
					}
				}
				if (selectedMethods.size() == beanMethods.size()) {
					// All reflection-detected methods found in ASM method set -> proceed
					beanMethods = selectedMethods;
				}
			}
		}
		catch (IOException ex) {
			logger.debug("Failed to read class file via ASM for determining @Bean method order", ex);
			// No worries, let's continue with the reflection metadata we started with...
		}
	}
	return beanMethods;
}
 
SimpleAnnotationMetadata(String className, int access, @Nullable String enclosingClassName,
		@Nullable String superClassName, boolean independentInnerClass, String[] interfaceNames,
		String[] memberClassNames, MethodMetadata[] annotatedMethods, MergedAnnotations annotations) {

	this.className = className;
	this.access = access;
	this.enclosingClassName = enclosingClassName;
	this.superClassName = superClassName;
	this.independentInnerClass = independentInnerClass;
	this.interfaceNames = interfaceNames;
	this.memberClassNames = memberClassNames;
	this.annotatedMethods = annotatedMethods;
	this.annotations = annotations;
}
 
@Override
public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
	Set<MethodMetadata> annotatedMethods = null;
	for (int i = 0; i < this.annotatedMethods.length; i++) {
		if (this.annotatedMethods[i].isAnnotated(annotationName)) {
			if (annotatedMethods == null) {
				annotatedMethods = new LinkedHashSet<>(4);
			}
			annotatedMethods.add(this.annotatedMethods[i]);
		}
	}
	return annotatedMethods != null ? annotatedMethods : Collections.emptySet();
}
 
@Override
public void visitEnd() {
	String[] memberClassNames = StringUtils.toStringArray(this.memberClassNames);
	MethodMetadata[] annotatedMethods = this.annotatedMethods.toArray(new MethodMetadata[0]);
	MergedAnnotations annotations = MergedAnnotations.of(this.annotations);
	this.metadata = new SimpleAnnotationMetadata(this.className, this.access,
			this.enclosingClassName, this.superClassName, this.independentInnerClass,
			this.interfaceNames, memberClassNames, annotatedMethods, annotations);
}
 
@Override
public boolean hasAnnotatedMethods(String annotationName) {
	for (MethodMetadata methodMetadata : this.methodMetadataSet) {
		if (methodMetadata.isAnnotated(annotationName)) {
			return true;
		}
	}
	return false;
}
 
@Override
public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
	Set<MethodMetadata> annotatedMethods = new LinkedHashSet<>(4);
	for (MethodMetadata methodMetadata : this.methodMetadataSet) {
		if (methodMetadata.isAnnotated(annotationName)) {
			annotatedMethods.add(methodMetadata);
		}
	}
	return annotatedMethods;
}
 
public MethodMetadataReadingVisitor(String methodName, int access, String declaringClassName,
		String returnTypeName, @Nullable ClassLoader classLoader, Set<MethodMetadata> methodMetadataSet) {

	super(SpringAsmInfo.ASM_VERSION);
	this.methodName = methodName;
	this.access = access;
	this.declaringClassName = declaringClassName;
	this.returnTypeName = returnTypeName;
	this.classLoader = classLoader;
	this.methodMetadataSet = methodMetadataSet;
}
 
/**
 * Register default methods on interfaces implemented by the configuration class.
 */
private void processInterfaces(ConfigurationClass configClass, SourceClass sourceClass) throws IOException {
	for (SourceClass ifc : sourceClass.getInterfaces()) {
		Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(ifc);
		for (MethodMetadata methodMetadata : beanMethods) {
			if (!methodMetadata.isAbstract()) {
				// A default method or other concrete method on a Java 8+ interface...
				configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
			}
		}
		processInterfaces(configClass, ifc);
	}
}
 
/**
 * Retrieve the metadata for all <code>@Bean</code> methods.
 */
private Set<MethodMetadata> retrieveBeanMethodMetadata(SourceClass sourceClass) {
	AnnotationMetadata original = sourceClass.getMetadata();
	Set<MethodMetadata> beanMethods = original.getAnnotatedMethods(Bean.class.getName());
	if (beanMethods.size() > 1 && original instanceof StandardAnnotationMetadata) {
		// Try reading the class file via ASM for deterministic declaration order...
		// Unfortunately, the JVM's standard reflection returns methods in arbitrary
		// order, even between different runs of the same application on the same JVM.
		try {
			AnnotationMetadata asm =
					this.metadataReaderFactory.getMetadataReader(original.getClassName()).getAnnotationMetadata();
			Set<MethodMetadata> asmMethods = asm.getAnnotatedMethods(Bean.class.getName());
			if (asmMethods.size() >= beanMethods.size()) {
				Set<MethodMetadata> selectedMethods = new LinkedHashSet<>(asmMethods.size());
				for (MethodMetadata asmMethod : asmMethods) {
					for (MethodMetadata beanMethod : beanMethods) {
						if (beanMethod.getMethodName().equals(asmMethod.getMethodName())) {
							selectedMethods.add(beanMethod);
							break;
						}
					}
				}
				if (selectedMethods.size() == beanMethods.size()) {
					// All reflection-detected methods found in ASM method set -> proceed
					beanMethods = selectedMethods;
				}
			}
		}
		catch (IOException ex) {
			logger.debug("Failed to read class file via ASM for determining @Bean method order", ex);
			// No worries, let's continue with the reflection metadata we started with...
		}
	}
	return beanMethods;
}
 
@Override
public boolean hasAnnotatedMethods(String annotationName) {
	for (MethodMetadata methodMetadata : this.methodMetadataSet) {
		if (methodMetadata.isAnnotated(annotationName)) {
			return true;
		}
	}
	return false;
}
 
@Override
public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
	Set<MethodMetadata> annotatedMethods = new LinkedHashSet<>(4);
	for (MethodMetadata methodMetadata : this.methodMetadataSet) {
		if (methodMetadata.isAnnotated(annotationName)) {
			annotatedMethods.add(methodMetadata);
		}
	}
	return annotatedMethods;
}
 
public MethodMetadataReadingVisitor(String methodName, int access, String declaringClassName,
		String returnTypeName, @Nullable ClassLoader classLoader, Set<MethodMetadata> methodMetadataSet) {

	super(SpringAsmInfo.ASM_VERSION);
	this.methodName = methodName;
	this.access = access;
	this.declaringClassName = declaringClassName;
	this.returnTypeName = returnTypeName;
	this.classLoader = classLoader;
	this.methodMetadataSet = methodMetadataSet;
}
 
public boolean hasAnnotatedMethods(String annotationType) {
    for (MethodMetadata method : this.methodMetadataSet) {
        if (method.isAnnotated(annotationType)) {
            return true;
        }
    }
    return false;
}
 
public Set<MethodMetadata> getAnnotatedMethods(String annotationType) {
    Set<MethodMetadata> annotatedMethods = new LinkedHashSet<MethodMetadata>();
    for (MethodMetadata method : this.methodMetadataSet) {
        if (method.isAnnotated(annotationType)) {
            annotatedMethods.add(method);
        }
    }
    return annotatedMethods;
}
 
private boolean isMatch(BeanDefinition beanDefinition, String beanName) {
    String className = beanDefinition.getBeanClassName();
    if (className == null) {
        if (beanDefinition.getFactoryMethodName() != null &&
                beanDefinition.getFactoryMethodName().contentEquals(beanName)) {
            Object source = beanDefinition.getSource();
            if (source instanceof MethodMetadata) {
                String returnType = ((MethodMetadata) source).getReturnTypeName();
                if (returnType.contentEquals("javax.sql.DataSource")) {
                    return true;
                }
                if (returnType.startsWith("org.springframework") || returnType.startsWith("io.micrometer")
                        || returnType.startsWith("com.fasterxml.") || returnType.startsWith("org.hibernate.")) {
                    return false;
                }
                className = returnType;
            }
        }
    }

    if (className != null) {
        try {
            final Class<?> beanClass = Class.forName(className);
            if (DataSource.class.isAssignableFrom(beanClass)
                    || BaseConnectionFactory.class.isAssignableFrom(beanClass)) {
                return true;
            }
        } catch (ClassNotFoundException e) {
        }
    }
    return false;
}
 
源代码23 项目: lams   文件: ConfigurationClassParser.java
/**
 * Register default methods on interfaces implemented by the configuration class.
 */
private void processInterfaces(ConfigurationClass configClass, SourceClass sourceClass) throws IOException {
	for (SourceClass ifc : sourceClass.getInterfaces()) {
		Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(ifc);
		for (MethodMetadata methodMetadata : beanMethods) {
			if (!methodMetadata.isAbstract()) {
				// A default method or other concrete method on a Java 8+ interface...
				configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
			}
		}
		processInterfaces(configClass, ifc);
	}
}
 
源代码24 项目: lams   文件: ConfigurationClassParser.java
/**
 * Retrieve the metadata for all <code>@Bean</code> methods.
 */
private Set<MethodMetadata> retrieveBeanMethodMetadata(SourceClass sourceClass) {
	AnnotationMetadata original = sourceClass.getMetadata();
	Set<MethodMetadata> beanMethods = original.getAnnotatedMethods(Bean.class.getName());
	if (beanMethods.size() > 1 && original instanceof StandardAnnotationMetadata) {
		// Try reading the class file via ASM for deterministic declaration order...
		// Unfortunately, the JVM's standard reflection returns methods in arbitrary
		// order, even between different runs of the same application on the same JVM.
		try {
			AnnotationMetadata asm =
					this.metadataReaderFactory.getMetadataReader(original.getClassName()).getAnnotationMetadata();
			Set<MethodMetadata> asmMethods = asm.getAnnotatedMethods(Bean.class.getName());
			if (asmMethods.size() >= beanMethods.size()) {
				Set<MethodMetadata> selectedMethods = new LinkedHashSet<MethodMetadata>(asmMethods.size());
				for (MethodMetadata asmMethod : asmMethods) {
					for (MethodMetadata beanMethod : beanMethods) {
						if (beanMethod.getMethodName().equals(asmMethod.getMethodName())) {
							selectedMethods.add(beanMethod);
							break;
						}
					}
				}
				if (selectedMethods.size() == beanMethods.size()) {
					// All reflection-detected methods found in ASM method set -> proceed
					beanMethods = selectedMethods;
				}
			}
		}
		catch (IOException ex) {
			logger.debug("Failed to read class file via ASM for determining @Bean method order", ex);
			// No worries, let's continue with the reflection metadata we started with...
		}
	}
	return beanMethods;
}
 
源代码25 项目: lams   文件: AnnotationMetadataReadingVisitor.java
@Override
public boolean hasAnnotatedMethods(String annotationName) {
	for (MethodMetadata methodMetadata : this.methodMetadataSet) {
		if (methodMetadata.isAnnotated(annotationName)) {
			return true;
		}
	}
	return false;
}
 
源代码26 项目: lams   文件: AnnotationMetadataReadingVisitor.java
@Override
public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
	Set<MethodMetadata> annotatedMethods = new LinkedHashSet<MethodMetadata>(4);
	for (MethodMetadata methodMetadata : this.methodMetadataSet) {
		if (methodMetadata.isAnnotated(annotationName)) {
			annotatedMethods.add(methodMetadata);
		}
	}
	return annotatedMethods;
}
 
源代码27 项目: lams   文件: MethodMetadataReadingVisitor.java
public MethodMetadataReadingVisitor(String methodName, int access, String declaringClassName,
		String returnTypeName, ClassLoader classLoader, Set<MethodMetadata> methodMetadataSet) {

	super(SpringAsmInfo.ASM_VERSION);
	this.methodName = methodName;
	this.access = access;
	this.declaringClassName = declaringClassName;
	this.returnTypeName = returnTypeName;
	this.classLoader = classLoader;
	this.methodMetadataSet = methodMetadataSet;
}
 
@Override
public boolean hasAnnotatedMethods(String annotationName) {
	for (MethodMetadata methodMetadata : this.methodMetadataSet) {
		if (methodMetadata.isAnnotated(annotationName)) {
			return true;
		}
	}
	return false;
}
 
@Override
public Set<MethodMetadata> getAnnotatedMethods(String annotationName) {
	Set<MethodMetadata> annotatedMethods = new LinkedHashSet<MethodMetadata>(4);
	for (MethodMetadata methodMetadata : this.methodMetadataSet) {
		if (methodMetadata.isAnnotated(annotationName)) {
			annotatedMethods.add(methodMetadata);
		}
	}
	return annotatedMethods;
}
 
public MethodMetadataReadingVisitor(String methodName, int access, String declaringClassName,
		String returnTypeName, ClassLoader classLoader, Set<MethodMetadata> methodMetadataSet) {

	super(SpringAsmInfo.ASM_VERSION);
	this.methodName = methodName;
	this.access = access;
	this.declaringClassName = declaringClassName;
	this.returnTypeName = returnTypeName;
	this.classLoader = classLoader;
	this.methodMetadataSet = methodMetadataSet;
}
 
 类所在包
 同包方法