Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
restore-keys: |
${{ runner.os }}-m2
- name: Test with Maven
run: ./mvnw clean package -B -Dmaven.test.skip=false -pl fesod-common,fesod-shaded,fesod-sheet,fesod-examples/fesod-sheet-examples
run: ./mvnw clean package -B -Dmaven.test.skip=false -pl fesod-common,fesod-shaded,fesod-beans/fesod-beans-cglib,fesod-sheet,fesod-examples/fesod-sheet-examples
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: (!cancelled())
Expand Down
53 changes: 53 additions & 0 deletions fesod-beans/fesod-beans-cglib/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.fesod</groupId>
<artifactId>fesod-beans</artifactId>
<version>${revision}</version>
</parent>

<artifactId>fesod-beans-cglib</artifactId>
<packaging>jar</packaging>
<name>Fesod Beans Cglib</name>

<properties>
<maven.test.skip>false</maven.test.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.fesod</groupId>
<artifactId>fesod-common</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>org.apache.fesod</groupId>
<artifactId>fesod-shaded</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.beans.cglib;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.fesod.shaded.cglib.core.CodeGenerationException;

/**
* Utility class for Bean property introspection.
*
* <p>
* Extends the standard JavaBean introspection ({@link Introspector}) to further support fluent-style accessors.
* </p>
* <ul>
* <li>Standard accessors: {@code getName()} / {@code setName(String)}.</li>
* <li>Fluent accessors: {@code name()} / {@code name(String)} / {@code T name(String)}.</li>
* </ul>
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
class BeanPropertyScanner {

private static final PropertyDescriptor[] EMPTY_DESCRIPTORS = new PropertyDescriptor[0];

/**
* Retrieve descriptors for all readable properties (including standard getters and fluent getters) of the target class.
*
* @param type target class
*/
public static PropertyDescriptor[] getBeanGetters(Class<?> type) {
return getBeanProperties(type, true, false);
}

/**
* Retrieve descriptors for all writeable properties (including standard setters and fluent setters) of the target class.
*
* @param type target class
*/
public static PropertyDescriptor[] getBeanSetters(Class<?> type) {
return getBeanProperties(type, false, true);
}

private static PropertyDescriptor[] getBeanProperties(Class<?> type, boolean read, boolean write) {
try {
Map<String, PropertyDescriptor> propertyMap = new LinkedHashMap<>();

BeanInfo info = Introspector.getBeanInfo(type, Object.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
propertyMap.put(pd.getName(), pd);
}

// Check for fluent-style accessors without prefix: for example, "lastName()", "lastName(String lastName)"
collectFluentAccessors(type, propertyMap);

if (propertyMap.isEmpty()) {
return EMPTY_DESCRIPTORS;
}

List<PropertyDescriptor> properties = new ArrayList<>(propertyMap.size());
for (PropertyDescriptor pd : propertyMap.values()) {
if ((read && pd.getReadMethod() != null) || (write && pd.getWriteMethod() != null)) {
properties.add(pd);
}
}

return properties.toArray(EMPTY_DESCRIPTORS);
} catch (IntrospectionException e) {
throw new CodeGenerationException(e);
}
}

private static void collectFluentAccessors(Class<?> type, Map<String, PropertyDescriptor> propertyMap)
throws IntrospectionException {
Map<String, Field> fieldMap = getAllFields(type);

for (Method method : type.getMethods()) {
if (Modifier.isStatic(method.getModifiers())
|| method.isSynthetic()
|| method.getDeclaringClass() == Object.class
|| method.getDeclaringClass() == Class.class) {
continue;
}

Field field = fieldMap.get(method.getName());
if (field == null) {
continue;
}

if (isFluentGetter(method, field)) {
PropertyDescriptor pd = propertyMap.get(method.getName());
if (pd == null) {
pd = new PropertyDescriptor(method.getName(), type, null, null);
propertyMap.put(method.getName(), pd);
}
if (pd.getReadMethod() == null) {
pd.setReadMethod(method);
}
} else if (isFluentSetter(method, field, type)) {
PropertyDescriptor pd = propertyMap.get(method.getName());
if (pd == null) {
pd = new PropertyDescriptor(method.getName(), type, null, null);
propertyMap.put(method.getName(), pd);
}
if (pd.getWriteMethod() == null) {
pd.setWriteMethod(method);
}
}
}
}

private static boolean isFluentSetter(Method method, Field field, Class<?> targetType) {
return method.getParameterCount() == 1
&& method.getParameterTypes()[0] == field.getType()
&& (method.getReturnType() == void.class
|| method.getReturnType().isAssignableFrom(targetType)
|| method.getReturnType().isAssignableFrom(method.getDeclaringClass()));
}

private static boolean isFluentGetter(Method method, Field field) {
return method.getParameterCount() == 0 && method.getReturnType() == field.getType();
}

private static Map<String, Field> getAllFields(Class<?> type) {
Map<String, Field> fields = new HashMap<>();
Class<?> current = type;
while (current != null && current != Object.class) {
for (Field field : current.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers()) && !field.isSynthetic()) {
fields.putIfAbsent(field.getName(), field);
}
}
current = current.getSuperclass();
}
return fields;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.beans.cglib;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.fesod.common.beans.BeanWrapper;
import org.apache.fesod.common.util.ValidateUtils;
import org.apache.fesod.shaded.cglib.beans.BeanMap;
import org.apache.fesod.shaded.cglib.core.DefaultNamingPolicy;

/**
* A {@link BeanWrapper} implementation backed by CGLIB's {@link BeanMap}.
*/
public final class CglibBeanWrapper implements BeanWrapper {

private static final Map<Class<?>, BeanMap> BEAN_MAP_CACHE = new ConcurrentHashMap<>();
private final BeanMap delegate;

public CglibBeanWrapper(Object bean) {
ValidateUtils.notNull(bean, "The bean instance must not be null");

this.delegate = initBeanMap(bean);
}

private BeanMap initBeanMap(Object bean) {
BeanMap beanMap = BEAN_MAP_CACHE.computeIfAbsent(bean.getClass(), clazz -> {
EnhancedBeanMapGenerator gen = new EnhancedBeanMapGenerator();
gen.setBeanClass(clazz);
gen.setContextClass(clazz);
gen.setNamingPolicy(FesodSheetNamingPolicy.INSTANCE);
return gen.create();
});

return beanMap.newInstance(bean);
}

@Override
public Object getProperty(String propertyName) {
return delegate.get(propertyName);
}

@Override
public void setProperty(String propertyName, Object value) {
delegate.put(propertyName, value);
}

@Override
public void setProperties(Map<String, Object> properties) {
delegate.putAll(properties);
}

@SuppressWarnings("unchecked")
@Override
public Set<String> getPropertyNames() {
return delegate.keySet();
}

@Override
public boolean containsProperty(String propertyName) {
return delegate.containsKey(propertyName);
}

@Override
public int getPropertySize() {
return delegate.size();
}

@Override
public Class<?> getPropertyType(String propertyName) {
return delegate.getPropertyType(propertyName);
}

@Override
public Object unwrap() {
return delegate.getBean();
}

public static class FesodSheetNamingPolicy extends DefaultNamingPolicy {
public static final FesodSheetNamingPolicy INSTANCE = new FesodSheetNamingPolicy();

@Override
protected String getTag() {
return "ByFesodCGLIB";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.beans.cglib;

import org.apache.fesod.common.beans.BeanWrapper;
import org.apache.fesod.common.beans.BeanWrapperProvider;

/**
* An SPI {@link BeanWrapperProvider} implementation backed by CGLIB.
*/
public class CglibBeanWrapperProvider implements BeanWrapperProvider {

@Override
public BeanWrapper create(Object bean) {
return new CglibBeanWrapper(bean);
}
}
Loading
Loading