Java Code Examples for org.springframework.util.ReflectionUtils#doWithLocalMethods()

The following examples show how to use org.springframework.util.ReflectionUtils#doWithLocalMethods() . 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: MergedAnnotationsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void getWithExhaustiveFromBridgeMethod() {
	List<Method> methods = new ArrayList<>();
	ReflectionUtils.doWithLocalMethods(StringGenericParameter.class, method -> {
		if ("getFor".equals(method.getName())) {
			methods.add(method);
		}
	});
	Method bridgeMethod = methods.get(0).getReturnType().equals(Object.class)
			? methods.get(0)
			: methods.get(1);
	Method bridgedMethod = methods.get(0).getReturnType().equals(Object.class)
			? methods.get(1)
			: methods.get(0);
	assertThat(bridgeMethod.isBridge()).isTrue();
	assertThat(bridgedMethod.isBridge()).isFalse();
	MergedAnnotation<?> annotation = MergedAnnotations.from(bridgeMethod,
			SearchStrategy.EXHAUSTIVE).get(Order.class);
	assertThat(annotation.isPresent()).isTrue();
	assertThat(annotation.getAggregateIndex()).isEqualTo(0);
}
 
Example 2
Source File: MergedAnnotationsComposedOnSingleAnnotatedElementTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public Method getBridgeMethod() throws NoSuchMethodException {
	List<Method> methods = new ArrayList<>();
	ReflectionUtils.doWithLocalMethods(StringGenericParameter.class, method -> {
		if ("getFor".equals(method.getName())) {
			methods.add(method);
		}
	});
	Method bridgeMethod = methods.get(0).getReturnType().equals(Object.class)
			? methods.get(0)
			: methods.get(1);
	assertThat(bridgeMethod.isBridge()).isTrue();
	return bridgeMethod;
}
 
Example 3
Source File: InitDestroyAnnotationBeanPostProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
	if (!AnnotationUtils.isCandidateClass(clazz, Arrays.asList(this.initAnnotationType, this.destroyAnnotationType))) {
		return this.emptyLifecycleMetadata;
	}

	List<LifecycleElement> initMethods = new ArrayList<>();
	List<LifecycleElement> destroyMethods = new ArrayList<>();
	Class<?> targetClass = clazz;

	do {
		final List<LifecycleElement> currInitMethods = new ArrayList<>();
		final List<LifecycleElement> currDestroyMethods = new ArrayList<>();

		ReflectionUtils.doWithLocalMethods(targetClass, method -> {
			if (this.initAnnotationType != null && method.isAnnotationPresent(this.initAnnotationType)) {
				LifecycleElement element = new LifecycleElement(method);
				currInitMethods.add(element);
				if (logger.isTraceEnabled()) {
					logger.trace("Found init method on class [" + clazz.getName() + "]: " + method);
				}
			}
			if (this.destroyAnnotationType != null && method.isAnnotationPresent(this.destroyAnnotationType)) {
				currDestroyMethods.add(new LifecycleElement(method));
				if (logger.isTraceEnabled()) {
					logger.trace("Found destroy method on class [" + clazz.getName() + "]: " + method);
				}
			}
		});

		initMethods.addAll(0, currInitMethods);
		destroyMethods.addAll(currDestroyMethods);
		targetClass = targetClass.getSuperclass();
	}
	while (targetClass != null && targetClass != Object.class);

	return (initMethods.isEmpty() && destroyMethods.isEmpty() ? this.emptyLifecycleMetadata :
			new LifecycleMetadata(clazz, initMethods, destroyMethods));
}
 
Example 4
Source File: InitDestroyAnnotationBeanPostProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
	List<LifecycleElement> initMethods = new ArrayList<>();
	List<LifecycleElement> destroyMethods = new ArrayList<>();
	Class<?> targetClass = clazz;

	do {
		final List<LifecycleElement> currInitMethods = new ArrayList<>();
		final List<LifecycleElement> currDestroyMethods = new ArrayList<>();

		ReflectionUtils.doWithLocalMethods(targetClass, method -> {
			if (this.initAnnotationType != null && method.isAnnotationPresent(this.initAnnotationType)) {
				LifecycleElement element = new LifecycleElement(method);
				currInitMethods.add(element);
				if (logger.isTraceEnabled()) {
					logger.trace("Found init method on class [" + clazz.getName() + "]: " + method);
				}
			}
			if (this.destroyAnnotationType != null && method.isAnnotationPresent(this.destroyAnnotationType)) {
				currDestroyMethods.add(new LifecycleElement(method));
				if (logger.isTraceEnabled()) {
					logger.trace("Found destroy method on class [" + clazz.getName() + "]: " + method);
				}
			}
		});

		initMethods.addAll(0, currInitMethods);
		destroyMethods.addAll(currDestroyMethods);
		targetClass = targetClass.getSuperclass();
	}
	while (targetClass != null && targetClass != Object.class);

	return new LifecycleMetadata(clazz, initMethods, destroyMethods);
}
 
Example 5
Source File: CompiledFunctionFactory.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
private Method findFactoryMethod(Class<?> clazz) {
	AtomicReference<Method> method = new AtomicReference<>();
	ReflectionUtils.doWithLocalMethods(clazz, m -> {
		if (m.getName().equals("getResult")
				&& m.getReturnType().getName().startsWith("java.util.function")) {
			method.set(m);
		}
	});
	return method.get();
}
 
Example 6
Source File: AbstractByteCodeLoadingProxy.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
private Method findFactoryMethod(Class<?> clazz) {
	AtomicReference<Method> method = new AtomicReference<>();
	ReflectionUtils.doWithLocalMethods(clazz, m -> {
		if (m.getName().equals("getResult")
				&& m.getReturnType().getName().startsWith("java.util.function")) {
			method.set(m);
		}
	});
	return method.get();
}
 
Example 7
Source File: PersistenceAnnotationBeanPostProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private InjectionMetadata buildPersistenceMetadata(final Class<?> clazz) {
	if (!AnnotationUtils.isCandidateClass(clazz, Arrays.asList(PersistenceContext.class, PersistenceUnit.class))) {
		return InjectionMetadata.EMPTY;
	}

	List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
	Class<?> targetClass = clazz;

	do {
		final LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<>();

		ReflectionUtils.doWithLocalFields(targetClass, field -> {
			if (field.isAnnotationPresent(PersistenceContext.class) ||
					field.isAnnotationPresent(PersistenceUnit.class)) {
				if (Modifier.isStatic(field.getModifiers())) {
					throw new IllegalStateException("Persistence annotations are not supported on static fields");
				}
				currElements.add(new PersistenceElement(field, field, null));
			}
		});

		ReflectionUtils.doWithLocalMethods(targetClass, method -> {
			Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
			if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
				return;
			}
			if ((bridgedMethod.isAnnotationPresent(PersistenceContext.class) ||
					bridgedMethod.isAnnotationPresent(PersistenceUnit.class)) &&
					method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
				if (Modifier.isStatic(method.getModifiers())) {
					throw new IllegalStateException("Persistence annotations are not supported on static methods");
				}
				if (method.getParameterCount() != 1) {
					throw new IllegalStateException("Persistence annotation requires a single-arg method: " + method);
				}
				PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
				currElements.add(new PersistenceElement(method, bridgedMethod, pd));
			}
		});

		elements.addAll(0, currElements);
		targetClass = targetClass.getSuperclass();
	}
	while (targetClass != null && targetClass != Object.class);

	return InjectionMetadata.forElements(elements, clazz);
}
 
Example 8
Source File: PersistenceAnnotationBeanPostProcessor.java    From java-technology-stack with MIT License 4 votes vote down vote up
private InjectionMetadata buildPersistenceMetadata(final Class<?> clazz) {
	List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
	Class<?> targetClass = clazz;

	do {
		final LinkedList<InjectionMetadata.InjectedElement> currElements =
				new LinkedList<>();

		ReflectionUtils.doWithLocalFields(targetClass, field -> {
			if (field.isAnnotationPresent(PersistenceContext.class) ||
					field.isAnnotationPresent(PersistenceUnit.class)) {
				if (Modifier.isStatic(field.getModifiers())) {
					throw new IllegalStateException("Persistence annotations are not supported on static fields");
				}
				currElements.add(new PersistenceElement(field, field, null));
			}
		});

		ReflectionUtils.doWithLocalMethods(targetClass, method -> {
			Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
			if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
				return;
			}
			if ((bridgedMethod.isAnnotationPresent(PersistenceContext.class) ||
					bridgedMethod.isAnnotationPresent(PersistenceUnit.class)) &&
					method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
				if (Modifier.isStatic(method.getModifiers())) {
					throw new IllegalStateException("Persistence annotations are not supported on static methods");
				}
				if (method.getParameterCount() != 1) {
					throw new IllegalStateException("Persistence annotation requires a single-arg method: " + method);
				}
				PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
				currElements.add(new PersistenceElement(method, bridgedMethod, pd));
			}
		});

		elements.addAll(0, currElements);
		targetClass = targetClass.getSuperclass();
	}
	while (targetClass != null && targetClass != Object.class);

	return new InjectionMetadata(clazz, elements);
}
 
Example 9
Source File: InitDestroyAnnotationBeanPostProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
	final boolean debug = logger.isDebugEnabled();
	LinkedList<LifecycleElement> initMethods = new LinkedList<LifecycleElement>();
	LinkedList<LifecycleElement> destroyMethods = new LinkedList<LifecycleElement>();
	Class<?> targetClass = clazz;

	do {
		final LinkedList<LifecycleElement> currInitMethods = new LinkedList<LifecycleElement>();
		final LinkedList<LifecycleElement> currDestroyMethods = new LinkedList<LifecycleElement>();

		ReflectionUtils.doWithLocalMethods(targetClass, new ReflectionUtils.MethodCallback() {
			@Override
			public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
				if (initAnnotationType != null) {
					if (method.getAnnotation(initAnnotationType) != null) {
						LifecycleElement element = new LifecycleElement(method);
						currInitMethods.add(element);
						if (debug) {
							logger.debug("Found init method on class [" + clazz.getName() + "]: " + method);
						}
					}
				}
				if (destroyAnnotationType != null) {
					if (method.getAnnotation(destroyAnnotationType) != null) {
						currDestroyMethods.add(new LifecycleElement(method));
						if (debug) {
							logger.debug("Found destroy method on class [" + clazz.getName() + "]: " + method);
						}
					}
				}
			}
		});

		initMethods.addAll(0, currInitMethods);
		destroyMethods.addAll(currDestroyMethods);
		targetClass = targetClass.getSuperclass();
	}
	while (targetClass != null && targetClass != Object.class);

	return new LifecycleMetadata(clazz, initMethods, destroyMethods);
}
 
Example 10
Source File: PersistenceAnnotationBeanPostProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private InjectionMetadata buildPersistenceMetadata(final Class<?> clazz) {
	LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
	Class<?> targetClass = clazz;

	do {
		final LinkedList<InjectionMetadata.InjectedElement> currElements =
				new LinkedList<InjectionMetadata.InjectedElement>();

		ReflectionUtils.doWithLocalFields(targetClass, new ReflectionUtils.FieldCallback() {
			@Override
			public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
				if (field.isAnnotationPresent(PersistenceContext.class) ||
						field.isAnnotationPresent(PersistenceUnit.class)) {
					if (Modifier.isStatic(field.getModifiers())) {
						throw new IllegalStateException("Persistence annotations are not supported on static fields");
					}
					currElements.add(new PersistenceElement(field, field, null));
				}
			}
		});

		ReflectionUtils.doWithLocalMethods(targetClass, new ReflectionUtils.MethodCallback() {
			@Override
			public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
				Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
				if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
					return;
				}
				if ((bridgedMethod.isAnnotationPresent(PersistenceContext.class) ||
						bridgedMethod.isAnnotationPresent(PersistenceUnit.class)) &&
						method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
					if (Modifier.isStatic(method.getModifiers())) {
						throw new IllegalStateException("Persistence annotations are not supported on static methods");
					}
					if (method.getParameterTypes().length != 1) {
						throw new IllegalStateException("Persistence annotation requires a single-arg method: " + method);
					}
					PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
					currElements.add(new PersistenceElement(method, bridgedMethod, pd));
				}
			}
		});

		elements.addAll(0, currElements);
		targetClass = targetClass.getSuperclass();
	}
	while (targetClass != null && targetClass != Object.class);

	return new InjectionMetadata(clazz, elements);
}
 
Example 11
Source File: PersistenceAnnotationBeanPostProcessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private InjectionMetadata buildPersistenceMetadata(final Class<?> clazz) {
	LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
	Class<?> targetClass = clazz;

	do {
		final LinkedList<InjectionMetadata.InjectedElement> currElements =
				new LinkedList<InjectionMetadata.InjectedElement>();

		ReflectionUtils.doWithLocalFields(targetClass, new ReflectionUtils.FieldCallback() {
			@Override
			public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
				if (field.isAnnotationPresent(PersistenceContext.class) ||
						field.isAnnotationPresent(PersistenceUnit.class)) {
					if (Modifier.isStatic(field.getModifiers())) {
						throw new IllegalStateException("Persistence annotations are not supported on static fields");
					}
					currElements.add(new PersistenceElement(field, field, null));
				}
			}
		});

		ReflectionUtils.doWithLocalMethods(targetClass, new ReflectionUtils.MethodCallback() {
			@Override
			public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
				Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
				if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
					return;
				}
				if ((bridgedMethod.isAnnotationPresent(PersistenceContext.class) ||
						bridgedMethod.isAnnotationPresent(PersistenceUnit.class)) &&
						method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
					if (Modifier.isStatic(method.getModifiers())) {
						throw new IllegalStateException("Persistence annotations are not supported on static methods");
					}
					if (method.getParameterTypes().length != 1) {
						throw new IllegalStateException("Persistence annotation requires a single-arg method: " + method);
					}
					PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
					currElements.add(new PersistenceElement(method, bridgedMethod, pd));
				}
			}
		});

		elements.addAll(0, currElements);
		targetClass = targetClass.getSuperclass();
	}
	while (targetClass != null && targetClass != Object.class);

	return new InjectionMetadata(clazz, elements);
}
 
Example 12
Source File: InitDestroyAnnotationBeanPostProcessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
	final boolean debug = logger.isDebugEnabled();
	LinkedList<LifecycleElement> initMethods = new LinkedList<LifecycleElement>();
	LinkedList<LifecycleElement> destroyMethods = new LinkedList<LifecycleElement>();
	Class<?> targetClass = clazz;

	do {
		final LinkedList<LifecycleElement> currInitMethods = new LinkedList<LifecycleElement>();
		final LinkedList<LifecycleElement> currDestroyMethods = new LinkedList<LifecycleElement>();

		ReflectionUtils.doWithLocalMethods(targetClass, new ReflectionUtils.MethodCallback() {
			@Override
			public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
				if (initAnnotationType != null) {
					if (method.getAnnotation(initAnnotationType) != null) {
						LifecycleElement element = new LifecycleElement(method);
						currInitMethods.add(element);
						if (debug) {
							logger.debug("Found init method on class [" + clazz.getName() + "]: " + method);
						}
					}
				}
				if (destroyAnnotationType != null) {
					if (method.getAnnotation(destroyAnnotationType) != null) {
						currDestroyMethods.add(new LifecycleElement(method));
						if (debug) {
							logger.debug("Found destroy method on class [" + clazz.getName() + "]: " + method);
						}
					}
				}
			}
		});

		initMethods.addAll(0, currInitMethods);
		destroyMethods.addAll(currDestroyMethods);
		targetClass = targetClass.getSuperclass();
	}
	while (targetClass != null && targetClass != Object.class);

	return new LifecycleMetadata(clazz, initMethods, destroyMethods);
}