io.microsphere.beans.BeanProperty Maven / Gradle / Ivy
The newest version!
/*
* 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 io.microsphere.beans;
import javax.annotation.Nonnull;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Objects;
import static io.microsphere.lang.function.ThrowableSupplier.execute;
import static io.microsphere.reflect.MethodUtils.invokeMethod;
import static io.microsphere.util.Assert.assertNotNull;
/**
* The class presenting the Property of Bean
*
* @author Mercy
* @since 1.0.0
*/
public class BeanProperty {
@Nonnull
private final String name;
private Object value;
@Nonnull
private final Class> beanClass;
@Nonnull
private final PropertyDescriptor descriptor;
public BeanProperty(@Nonnull String name, Class> beanClass, PropertyDescriptor descriptor) {
this.name = name;
this.beanClass = beanClass;
this.descriptor = descriptor;
}
public String getName() {
return name;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Class> getBeanClass() {
return beanClass;
}
public PropertyDescriptor getDescriptor() {
return descriptor;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BeanProperty)) return false;
BeanProperty that = (BeanProperty) o;
if (!Objects.equals(name, that.name)) return false;
if (!Objects.equals(value, that.value)) return false;
if (!Objects.equals(beanClass, that.beanClass))
return false;
return Objects.equals(descriptor, that.descriptor);
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (value != null ? value.hashCode() : 0);
result = 31 * result + (beanClass != null ? beanClass.hashCode() : 0);
result = 31 * result + (descriptor != null ? descriptor.hashCode() : 0);
return result;
}
@Override
public String toString() {
String sb = "BeanProperty{" + "name='" + name + '\'' +
", value=" + value +
", declaringClass=" + beanClass +
", descriptor=" + descriptor +
'}';
return sb;
}
/**
* Create a {@link BeanProperty} instance
*
* @param bean bean instance
* @param propertyName the name of bean property
* @return a {@link BeanProperty} instance
*/
public static BeanProperty of(@Nonnull Object bean, @Nonnull String propertyName) {
assertNotNull(bean, "The 'bean' argument must not be null");
assertNotNull(propertyName, "The 'propertyName' argument must not be null");
Class> beanClass = bean.getClass();
return execute(() -> {
PropertyDescriptor descriptor = new PropertyDescriptor(propertyName, beanClass);
BeanProperty beanProperty = new BeanProperty(propertyName, beanClass, descriptor);
Method getterMethod = descriptor.getReadMethod();
Object propertyValue = invokeMethod(bean, getterMethod);
beanProperty.value = (propertyValue);
return beanProperty;
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy