Skip to content

Commit

Permalink
CAUSEWAY-2297: optimization: check for internal bean semantics directly
Browse files Browse the repository at this point in the history
in _ClassCache
  • Loading branch information
andi-huber committed Nov 11, 2024
1 parent 8cb9e4c commit d13ecfc
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 22 deletions.
1 change: 1 addition & 0 deletions commons/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
exports org.apache.causeway.commons.tabular;
// internals exported as well
exports org.apache.causeway.commons.internal;
exports org.apache.causeway.commons.internal.annotations;
exports org.apache.causeway.commons.internal.assertions;
exports org.apache.causeway.commons.internal.base;
exports org.apache.causeway.commons.internal.binding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.causeway.core.config.beans;
package org.apache.causeway.commons.internal.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -35,12 +36,14 @@
import jakarta.xml.bind.annotation.XmlRootElement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.util.ReflectionUtils;

import org.apache.causeway.commons.collections.Can;
import org.apache.causeway.commons.functional.Try;
import org.apache.causeway.commons.internal._Constants;
import org.apache.causeway.commons.internal.annotations.BeanInternal;
import org.apache.causeway.commons.internal.base._Casts;
import org.apache.causeway.commons.internal.base._NullSafe;
import org.apache.causeway.commons.internal.base._Strings;
Expand Down Expand Up @@ -100,6 +103,13 @@ public void add(final Class<?> type) {
public boolean hasJaxbRootElementSemantics(final Class<?> type) {
return classModel(type).hasJaxbRootElementSemantics;
}

/**
* whether type is annotated with {@link BeanInternal} or {@link Configuration}
*/
public boolean hasInternalBeanSemantics(final Class<?> type) {
return classModel(type).hasInternalBeanSemantics;
}

/**
* whether type is annotated with {@link Named}
Expand Down Expand Up @@ -248,21 +258,44 @@ public Optional<String> lookupAttribute(

// -- IMPLEMENATION DETAILS

@RequiredArgsConstructor
private static class ClassModel {
private final Can<Field> declaredFields;
private final boolean hasJaxbRootElementSemantics;
private final boolean isAnnotatedWithNamed;

private final Map<ConstructorKey, ResolvedConstructor> publicConstructorsByKey = new HashMap<>();
private final Map<ConstructorKey, ResolvedConstructor> constructorsWithInjectSemanticsByKey = new HashMap<>();

private final Map<MethodKey, ResolvedMethod> resolvedMethodsByKey = new HashMap<>();
private final Map<MethodKey, ResolvedMethod> publicMethodsByKey = new HashMap<>();
private final Map<MethodKey, ResolvedMethod> postConstructMethodsByKey = new HashMap<>();

private final Map<String, Can<ResolvedMethod>> declaredMethodsByAttribute = new HashMap<>();
private final Map<String, String> attributeMap = new ConcurrentHashMap<>();
private record ClassModel(Can<Field> declaredFields,
boolean hasInternalBeanSemantics,
boolean hasJaxbRootElementSemantics,
boolean isAnnotatedWithNamed,

Map<ConstructorKey, ResolvedConstructor> publicConstructorsByKey,
Map<ConstructorKey, ResolvedConstructor> constructorsWithInjectSemanticsByKey,

Map<MethodKey, ResolvedMethod> resolvedMethodsByKey,
Map<MethodKey, ResolvedMethod> publicMethodsByKey,
Map<MethodKey, ResolvedMethod> postConstructMethodsByKey,

Map<String, Can<ResolvedMethod>> declaredMethodsByAttribute,
Map<String, String> attributeMap
) {

ClassModel(Can<Field> declaredFields,
boolean hasInternalBeanSemantics,
boolean hasJaxbRootElementSemantics,
boolean isAnnotatedWithNamed) {
this(declaredFields, hasInternalBeanSemantics, hasJaxbRootElementSemantics, isAnnotatedWithNamed,
new HashMap<>(),
new HashMap<>(),
new HashMap<>(),
new HashMap<>(),
new HashMap<>(),
new HashMap<>(),
new ConcurrentHashMap<>());
}

public static ClassModel INTERNAL = new ClassModel(Can.empty(), true, false, false,
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(),
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(),
Collections.emptyMap());

public static ClassModel internal() {
return INTERNAL;
}
}

private final Map<Class<?>, ClassModel> inspectedTypes = new HashMap<>();
Expand Down Expand Up @@ -349,8 +382,14 @@ private ClassModel inspectType(final Class<?> _type) {
// })
// .collect(Can.toCan());

var hasInternalBeanSemantics = _Annotations.isPresent(type, Configuration.class)
|| _Annotations.isPresent(type, BeanInternal.class);

if(hasInternalBeanSemantics) return ClassModel.internal(); //skip further inspection

var model = new ClassModel(
Can.ofArray(type.getDeclaredFields()),
hasInternalBeanSemantics,
_Annotations.isPresent(type, XmlRootElement.class),
_Annotations.isPresent(type, Named.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

Expand All @@ -54,7 +55,7 @@
* @since 2.0
*
*/
@Component
@Configuration(proxyBeanMethods = false)
@Named(CausewayModuleCoreConfig.NAMESPACE + ".CausewayBeanFactoryPostProcessor")
@Import({
AopPatch.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.causeway.applib.id.LogicalType;
import org.apache.causeway.applib.services.metamodel.BeanSort;
import org.apache.causeway.commons.collections.Can;
import org.apache.causeway.commons.internal.annotations.BeanInternal;
import org.apache.causeway.commons.internal.context._Context;
import org.apache.causeway.commons.internal.reflection._Annotations;
import org.apache.causeway.commons.internal.reflection._ClassCache;
Expand Down Expand Up @@ -129,10 +130,8 @@ public CausewayBeanMetaData classify(final @NonNull Class<?> type) {

// handle internal bean types ...

var aBeanInternal = _Annotations.synthesize(type, BeanInternal.class);
if(aBeanInternal.isPresent()) {
var logicalType = LogicalType.infer(type);
Attributes.HAS_DOMAIN_SERVICE_SEMANTICS.set(classCache, type, "true");
if(classCache.hasInternalBeanSemantics(type)) {
var logicalType = LogicalType.infer(type); //TODO should be deprecated in favor of Spring provided bean names
return CausewayBeanMetaData.injectable(BeanSort.MANAGED_BEAN_NOT_CONTRIBUTING, logicalType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import java.util.stream.Stream;

import org.apache.causeway.commons.collections.Can;
import org.apache.causeway.commons.internal.annotations.BeanInternal;

import lombok.NonNull;

/**
* Holds discovered domain types grouped by bean-sort.
*/
@BeanInternal
public class CausewayBeanTypeRegistry {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.jdo.annotations.EmbeddedOnly;

import org.apache.causeway.applib.id.LogicalType;
import org.apache.causeway.commons.internal.annotations.BeanInternal;
import org.apache.causeway.commons.internal.base._Strings;
import org.apache.causeway.commons.internal.reflection._Annotations;
import org.apache.causeway.core.config.beans.CausewayBeanMetaData;
Expand All @@ -33,6 +34,7 @@
* ServiceLoader plugin, classifies PersistenceCapable types into BeanSort.ENTITY.
* @since 2.0
*/
@BeanInternal
public class JdoBeanTypeClassifier implements CausewayBeanTypeClassifierSpi {

@Override
Expand Down

0 comments on commit d13ecfc

Please sign in to comment.