Skip to content

Commit

Permalink
Shared empty InjectionMetadata/LifecycleMetadata instance
Browse files Browse the repository at this point in the history
Closes gh-22570
  • Loading branch information
jhoeller committed Mar 12, 2019
1 parent 62c9237 commit c0ddaae
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz

private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
if (!AnnotationUtils.isCandidateClass(clazz, this.autowiredAnnotationTypes)) {
return new InjectionMetadata(clazz, Collections.emptyList());
return InjectionMetadata.EMPTY;
}

List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
Expand Down Expand Up @@ -496,7 +496,7 @@ private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
}
while (targetClass != null && targetClass != Object.class);

return new InjectionMetadata(clazz, elements);
return InjectionMetadata.forElements(elements, clazz);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@
public class InitDestroyAnnotationBeanPostProcessor
implements DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, PriorityOrdered, Serializable {

private final transient LifecycleMetadata emptyLifecycleMetadata =
new LifecycleMetadata(Object.class, Collections.emptyList(), Collections.emptyList()) {
@Override
public void checkConfigMembers(RootBeanDefinition beanDefinition) {
}
@Override
public void invokeInitMethods(Object target, String beanName) {
}
@Override
public void invokeDestroyMethods(Object target, String beanName) {
}
@Override
public boolean hasDestroyMethods() {
return false;
}
};


protected transient Log logger = LogFactory.getLog(getClass());

@Nullable
Expand Down Expand Up @@ -200,7 +218,7 @@ private LifecycleMetadata findLifecycleMetadata(Class<?> clazz) {

private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
if (!AnnotationUtils.isCandidateClass(clazz, Arrays.asList(this.initAnnotationType, this.destroyAnnotationType))) {
return new LifecycleMetadata(clazz, Collections.emptyList(), Collections.emptyList());
return this.emptyLifecycleMetadata;
}

List<LifecycleElement> initMethods = new ArrayList<>();
Expand Down Expand Up @@ -233,7 +251,8 @@ private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
}
while (targetClass != null && targetClass != Object.class);

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


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

Expand All @@ -47,6 +48,23 @@
*/
public class InjectionMetadata {

/**
* An empty {@code InjectionMetadata} instance with no-op callbacks.
* @since 5.2
*/
public static final InjectionMetadata EMPTY = new InjectionMetadata(Object.class, Collections.emptyList()) {
@Override
public void checkConfigMembers(RootBeanDefinition beanDefinition) {
}
@Override
public void inject(Object target, @Nullable String beanName, @Nullable PropertyValues pvs) {
}
@Override
public void clear(@Nullable PropertyValues pvs) {
}
};


private static final Log logger = LogFactory.getLog(InjectionMetadata.class);

private final Class<?> targetClass;
Expand All @@ -57,6 +75,14 @@ public class InjectionMetadata {
private volatile Set<InjectedElement> checkedElements;


/**
* Create a new {@code InjectionMetadata instance}.
* <p>Preferably use {@link #forElements} for reusing the {@link #EMPTY}
* instance in case of no elements.
* @param targetClass the target class
* @param elements the associated elements to inject
* @see #forElements
*/
public InjectionMetadata(Class<?> targetClass, Collection<InjectedElement> elements) {
this.targetClass = targetClass;
this.injectedElements = elements;
Expand Down Expand Up @@ -108,6 +134,24 @@ public void clear(@Nullable PropertyValues pvs) {
}


/**
* Return an {@code InjectionMetadata} instance, possibly for empty elements.
* @param elements the elements to inject (possibly empty)
* @param clazz the target class
* @return a new {@code InjectionMetadata} instance,
* or {@link #EMPTY} in case of no elements
* @since 5.2
*/
public static InjectionMetadata forElements(Collection<InjectedElement> elements, Class<?> clazz) {
return (elements.isEmpty() ? InjectionMetadata.EMPTY : new InjectionMetadata(clazz, elements));
}

/**
* Check whether the given injection metadata needs to be refreshed.
* @param metadata the existing metadata instance
* @param clazz the current target class
* @return {@code true} indicating a refresh, {@code false} otherwise
*/
public static boolean needsRefresh(@Nullable InjectionMetadata metadata, Class<?> clazz) {
return (metadata == null || metadata.targetClass != clazz);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ private InjectionMetadata findResourceMetadata(String beanName, final Class<?> c

private InjectionMetadata buildResourceMetadata(final Class<?> clazz) {
if (!AnnotationUtils.isCandidateClass(clazz, resourceAnnotationTypes)) {
return new InjectionMetadata(clazz, Collections.emptyList());
return InjectionMetadata.EMPTY;
}

List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
Expand Down Expand Up @@ -448,7 +448,7 @@ else if (bridgedMethod.isAnnotationPresent(Resource.class)) {
}
while (targetClass != null && targetClass != Object.class);

return new InjectionMetadata(clazz, elements);
return InjectionMetadata.forElements(elements, clazz);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -416,7 +415,7 @@ private InjectionMetadata findPersistenceMetadata(String beanName, final Class<?

private InjectionMetadata buildPersistenceMetadata(final Class<?> clazz) {
if (!AnnotationUtils.isCandidateClass(clazz, Arrays.asList(PersistenceContext.class, PersistenceUnit.class))) {
return new InjectionMetadata(clazz, Collections.emptyList());
return InjectionMetadata.EMPTY;
}

List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
Expand Down Expand Up @@ -459,7 +458,7 @@ private InjectionMetadata buildPersistenceMetadata(final Class<?> clazz) {
}
while (targetClass != null && targetClass != Object.class);

return new InjectionMetadata(clazz, elements);
return InjectionMetadata.forElements(elements, clazz);
}

/**
Expand Down

0 comments on commit c0ddaae

Please sign in to comment.