Skip to content

Instantly share code, notes, and snippets.

@odrotbohm
Created January 17, 2020 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odrotbohm/6fc5e49b1d95c26037a14a018c9e916f to your computer and use it in GitHub Desktop.
Save odrotbohm/6fc5e49b1d95c26037a14a018c9e916f to your computer and use it in GitHub Desktop.
/*
* Copyright 2020 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.
* You may obtain a copy of the License at
*
* https://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.springframework.data.jdk14;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;
import java.lang.reflect.RecordComponent;
import java.util.Arrays;
import org.springframework.beans.BeanInfoFactory;
import org.springframework.util.ClassUtils;
/**
* @author Oliver Drotbohm
*/
public class Jdk14BeanInfoFactory implements BeanInfoFactory {
private static final BeanInfoFactory DELEGATE;
static {
DELEGATE = ClassUtils.isPresent("java.lang.Record", Jdk14BeanInfoFactory.class.getClassLoader()) //
? RecordBeanInfoFactory.INSTANCE //
: null;
}
/*
* (non-Javadoc)
* @see org.springframework.beans.BeanInfoFactory#getBeanInfo(java.lang.Class)
*/
public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException {
return DELEGATE == null ? null : DELEGATE.getBeanInfo(beanClass);
}
static enum RecordBeanInfoFactory implements BeanInfoFactory {
INSTANCE;
/*
* (non-Javadoc)
* @see org.springframework.beans.BeanInfoFactory#getBeanInfo(java.lang.Class)
*/
public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException {
if (!beanClass.isRecord()) {
return null;
}
var descriptors = Arrays.stream(beanClass.getRecordComponents()) //
.map(RecordBeanInfoFactory::toPropertyDescriptor) //
.toArray(PropertyDescriptor[]::new);
return new RecordBeanInfo(descriptors);
}
private static PropertyDescriptor toPropertyDescriptor(RecordComponent component) {
try {
return new PropertyDescriptor(component.getName(), component.getAccessor(), null);
} catch (IntrospectionException e) {
throw new RuntimeException("Invalid record definition!", e);
}
}
private static class RecordBeanInfo extends SimpleBeanInfo {
private final PropertyDescriptor[] descriptors;
public RecordBeanInfo(PropertyDescriptor[] descriptors) {
this.descriptors = descriptors.clone();
}
/*
* (non-Javadoc)
* @see java.beans.SimpleBeanInfo#getPropertyDescriptors()
*/
@Override
public PropertyDescriptor[] getPropertyDescriptors() {
return descriptors;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment