001package es.ucm.fdi.gaia.jcolibri.cbrcore; 002 003import java.lang.reflect.Field; 004 005import org.apache.logging.log4j.LogManager; 006 007import es.ucm.fdi.gaia.jcolibri.exception.AttributeAccessException; 008 009/** 010 * This class identifies an attribute of a CaseComponent (Java Bean). Attributes are part of CaseComponents and 011 * CaseComponents build a case. Note that each CaseComponent must be a Java Bean, so this class represents a 012 * field of a Java Bean (with its getXXX() and setXXX() methods). 013 * 014 * @see CaseComponent 015 * @author Juan A. Recio-GarcĂa 016 */ 017public class Attribute { 018 019 private Field field; 020 021 /** 022 * Creates an Attribute using the Field obtained with Reflection (getClass.getDeclaredField(name)). 023 * It is recommended to use the other constructor. 024 */ 025 public Attribute(Field f) 026 { 027 field = f; 028 } 029 030 /** 031 * Creates an attribute. The attribute name must be an existing field of the class and have the get/set() methods. 032 * @param attributeName Name of the field 033 * @param _class Class that the attribute belongs to. 034 */ 035 public Attribute(String attributeName, Class<?> _class) 036 { 037 try { 038 field = _class.getDeclaredField(attributeName); 039 } catch (Exception e) { 040 LogManager.getLogger().error(e + "Attribute: "+ attributeName+ " Class:" + _class.getName()); 041 } 042 } 043 044 /** 045 * Returns the name of the field. 046 */ 047 public String getName() 048 { 049 return field.getName(); 050 } 051 052 /** 053 * Returns the class that this attribute belongs to. 054 */ 055 public Class<?> getDeclaringClass() 056 { 057 return field.getDeclaringClass(); 058 } 059 060 /** 061 * Returns the type of the attribute. 062 */ 063 public Class<?> getType() 064 { 065 return field.getType(); 066 } 067 068 /** 069 * Returns the value of the attribute for a concrete object. Of course, the object must be instance of the class that this attribute belongs to. 070 * @param obj Instance to obtain the attribute from 071 * @throws AttributeAccessException 072 */ 073 public Object getValue(Object obj) throws AttributeAccessException 074 { 075 Object res = null; 076 try{ 077 res = field.get(obj); 078 return res; 079 }catch(Exception e) 080 {} 081 082 try{ 083 java.beans.PropertyDescriptor pd = new java.beans.PropertyDescriptor(field.getName(),field.getDeclaringClass()); 084 res = pd.getReadMethod().invoke(obj, (Object[])null); 085 return res; 086 }catch(Exception e) 087 {} 088 throw new AttributeAccessException("Error getting value from object: "+obj+", attribute: "+field.getName()); 089 090 } 091 092 /** 093 * Sets the value of the attribute in a concrete object. 094 * @param obj Object that defines the attribute to set. 095 * @param value Value to set. 096 * @throws AttributeAccessException 097 */ 098 public void setValue(Object obj, Object value) throws AttributeAccessException 099 { 100 try{ 101 field.set(obj, value); 102 }catch(Exception e) 103 {} 104 105 try{ 106 java.beans.PropertyDescriptor pd = new java.beans.PropertyDescriptor(field.getName(),field.getDeclaringClass()); 107 Object[] args = {value}; 108 pd.getWriteMethod().invoke(obj, args); 109 }catch(Exception e) 110 { 111 throw new AttributeAccessException("Error setting value from object: "+obj+", attribute: "+field.getName()); 112 } 113 114 } 115 116 /** 117 * Returns the hashCode of the attribute. 118 */ 119 public int hashCode() 120 { 121 return field.getName().hashCode(); 122 } 123 124 125 public boolean equals(Object o) 126 { 127 if(! (o instanceof Attribute)) 128 return false; 129 return (field.getName().equals(((Attribute)o).getName())) 130 &&(field.getDeclaringClass().equals(((Attribute)o).getDeclaringClass())); 131 } 132 133 134}