001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023 002package io.cui.tools.property; 003 004import static java.util.Objects.requireNonNull; 005import java.beans.PropertyDescriptor; 006import io.cui.tools.lang.MoreObjects; 007import io.cui.tools.logging.CuiLogger; 008import io.cui.tools.reflect.MoreReflection; 009 010/** 011 * Defines the read write permissions for a given property. It is defined for properties java Bean 012 * properties. 013 * 014 * @author Oliver Wolff 015 */ 016public enum PropertyReadWrite { 017 /** 018 * The corresponding property is read only. 019 */ 020 READ_ONLY(true, false), /** 021 * The property can be read and written to. 022 */ 023 READ_WRITE(true, true), /** 024 * The property can only be written to. 025 */ 026 WRITE_ONLY(false, true), /** 027 * The property can neither be read nor written to. 028 */ 029 NONE(false, false); 030 private static final CuiLogger log = new CuiLogger(PropertyReadWrite.class); 031 private final boolean readable; 032 private final boolean writeable; 033 034 /** 035 * Resolves {@link PropertyReadWrite} for a given property with {@link MoreReflection} 036 * 037 * @param beanType to be checked, must not be null 038 * @param propertyName to be checked, must not be null 039 * @return the corresponding {@link PropertyReadWrite} for a given property 040 */ 041 public static PropertyReadWrite resolveForBean(final Class<?> beanType, final String propertyName) { 042 final var readable = MoreReflection.retrieveAccessMethod(beanType, propertyName).isPresent(); 043 final var writeable = !MoreReflection.retrieveWriteMethodCandidates(beanType, propertyName).isEmpty(); 044 if (readable && writeable) { 045 return READ_WRITE; 046 } 047 if (readable) { 048 return READ_ONLY; 049 } 050 if (writeable) { 051 return WRITE_ONLY; 052 } 053 return NONE; 054 } 055 056 /** 057 * Resolves {@link PropertyReadWrite} form the given {@link PropertyDescriptor}. If this 058 * provides unclear result it will call {@link #resolveForBean(Class, String)} 059 * 060 * @param descriptor to be read from 061 * @param beanType to be checked, must not be null 062 * @param propertyName to be checked, must not be null 063 * @return the corresponding {@link PropertyReadWrite} for a given property 064 */ 065 public static PropertyReadWrite fromPropertyDescriptor(PropertyDescriptor descriptor, final Class<?> beanType, final String propertyName) { 066 requireNonNull(descriptor); 067 if (MoreObjects.allNonNull(descriptor.getReadMethod(), descriptor.getWriteMethod())) { 068 return READ_WRITE; 069 } 070 log.debug("PropertyDescriptor \'%s\' does not describe a standard bean-structure for property \'%s\' on type \'%s\', switching to reflection", descriptor, propertyName, beanType); 071 return resolveForBean(beanType, propertyName); 072 } 073 074 @java.lang.SuppressWarnings("all") 075 @lombok.Generated 076 private PropertyReadWrite(final boolean readable, final boolean writeable) { 077 this.readable = readable; 078 this.writeable = writeable; 079 } 080 081 @java.lang.SuppressWarnings("all") 082 @lombok.Generated 083 public boolean isReadable() { 084 return this.readable; 085 } 086 087 @java.lang.SuppressWarnings("all") 088 @lombok.Generated 089 public boolean isWriteable() { 090 return this.writeable; 091 } 092}