Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jun 21, 2023
1 parent 6c42f37 commit f67f98a
Showing 1 changed file with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ public int getOrder() {


@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
findLifecycleMetadata(beanDefinition, beanType);
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanClass, String beanName) {
findLifecycleMetadata(beanDefinition, beanClass);
}

@Override
Expand All @@ -173,8 +173,8 @@ public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registe
return null;
}

private LifecycleMetadata findLifecycleMetadata(RootBeanDefinition beanDefinition, Class<?> beanType) {
LifecycleMetadata metadata = findLifecycleMetadata(beanType);
private LifecycleMetadata findLifecycleMetadata(RootBeanDefinition beanDefinition, Class<?> beanClass) {
LifecycleMetadata metadata = findLifecycleMetadata(beanClass);
metadata.checkInitDestroyMethods(beanDefinition);
return metadata;
}
Expand Down Expand Up @@ -234,62 +234,62 @@ public boolean requiresDestruction(Object bean) {
}


private LifecycleMetadata findLifecycleMetadata(Class<?> clazz) {
private LifecycleMetadata findLifecycleMetadata(Class<?> beanClass) {
if (this.lifecycleMetadataCache == null) {
// Happens after deserialization, during destruction...
return buildLifecycleMetadata(clazz);
return buildLifecycleMetadata(beanClass);
}
// Quick check on the concurrent map first, with minimal locking.
LifecycleMetadata metadata = this.lifecycleMetadataCache.get(clazz);
LifecycleMetadata metadata = this.lifecycleMetadataCache.get(beanClass);
if (metadata == null) {
synchronized (this.lifecycleMetadataCache) {
metadata = this.lifecycleMetadataCache.get(clazz);
metadata = this.lifecycleMetadataCache.get(beanClass);
if (metadata == null) {
metadata = buildLifecycleMetadata(clazz);
this.lifecycleMetadataCache.put(clazz, metadata);
metadata = buildLifecycleMetadata(beanClass);
this.lifecycleMetadataCache.put(beanClass, metadata);
}
return metadata;
}
}
return metadata;
}

private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
if (!AnnotationUtils.isCandidateClass(clazz, List.of(this.initAnnotationType, this.destroyAnnotationType))) {
private LifecycleMetadata buildLifecycleMetadata(final Class<?> beanClass) {
if (!AnnotationUtils.isCandidateClass(beanClass, List.of(this.initAnnotationType, this.destroyAnnotationType))) {
return this.emptyLifecycleMetadata;
}

List<LifecycleMethod> initMethods = new ArrayList<>();
List<LifecycleMethod> destroyMethods = new ArrayList<>();
Class<?> targetClass = clazz;
Class<?> currentClass = beanClass;

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

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

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

return (initMethods.isEmpty() && destroyMethods.isEmpty() ? this.emptyLifecycleMetadata :
new LifecycleMetadata(clazz, initMethods, destroyMethods));
new LifecycleMetadata(beanClass, initMethods, destroyMethods));
}


Expand All @@ -311,7 +311,7 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound
*/
private class LifecycleMetadata {

private final Class<?> targetClass;
private final Class<?> beanClass;

private final Collection<LifecycleMethod> initMethods;

Expand All @@ -323,10 +323,10 @@ private class LifecycleMetadata {
@Nullable
private volatile Set<LifecycleMethod> checkedDestroyMethods;

public LifecycleMetadata(Class<?> targetClass, Collection<LifecycleMethod> initMethods,
public LifecycleMetadata(Class<?> beanClass, Collection<LifecycleMethod> initMethods,
Collection<LifecycleMethod> destroyMethods) {

this.targetClass = targetClass;
this.beanClass = beanClass;
this.initMethods = initMethods;
this.destroyMethods = destroyMethods;
}
Expand All @@ -339,7 +339,7 @@ public void checkInitDestroyMethods(RootBeanDefinition beanDefinition) {
beanDefinition.registerExternallyManagedInitMethod(methodIdentifier);
checkedInitMethods.add(lifecycleMethod);
if (logger.isTraceEnabled()) {
logger.trace("Registered init method on class [" + this.targetClass.getName() + "]: " + methodIdentifier);
logger.trace("Registered init method on class [" + this.beanClass.getName() + "]: " + methodIdentifier);
}
}
}
Expand All @@ -350,7 +350,7 @@ public void checkInitDestroyMethods(RootBeanDefinition beanDefinition) {
beanDefinition.registerExternallyManagedDestroyMethod(methodIdentifier);
checkedDestroyMethods.add(lifecycleMethod);
if (logger.isTraceEnabled()) {
logger.trace("Registered destroy method on class [" + this.targetClass.getName() + "]: " + methodIdentifier);
logger.trace("Registered destroy method on class [" + this.beanClass.getName() + "]: " + methodIdentifier);
}
}
}
Expand Down

0 comments on commit f67f98a

Please sign in to comment.