1 22 package org.jboss.reflect.plugins.javassist; 23 24 import org.jboss.reflect.plugins.AnnotationHelper; 25 import org.jboss.reflect.spi.AnnotationValue; 26 import org.jboss.reflect.spi.ClassInfo; 27 import org.jboss.reflect.spi.FieldInfo; 28 import org.jboss.reflect.spi.TypeInfo; 29 import org.jboss.util.JBossStringBuilder; 30 31 import javassist.CtClass; 32 import javassist.CtField; 33 import javassist.Modifier; 34 import javassist.NotFoundException; 35 36 42 public class JavassistFieldInfo extends JavassistAnnotatedInfo implements FieldInfo 43 { 44 45 private static final JavassistReflectionFactory reflectionFactory = new JavassistReflectionFactory(true); 46 47 48 private CtField ctField; 49 50 51 private transient JavassistField field; 52 53 54 private transient TypeInfo fieldType; 55 56 57 protected JavassistTypeInfo typeInfo; 58 59 66 public JavassistFieldInfo(AnnotationHelper annotationHelper, JavassistTypeInfo typeInfo, CtField ctField) 67 { 68 super(annotationHelper); 69 this.typeInfo = typeInfo; 70 this.ctField = ctField; 71 } 72 73 public String getName() 74 { 75 return ctField.getName(); 76 } 77 78 public int getModifiers() 79 { 80 return ctField.getModifiers(); 81 } 82 83 public boolean isPublic() 84 { 85 return Modifier.isPublic(getModifiers()); 86 } 87 88 public boolean isStatic() 89 { 90 return Modifier.isStatic(getModifiers()); 91 } 92 93 public ClassInfo getDeclaringClass() 94 { 95 return typeInfo; 96 } 97 98 public TypeInfo getType() 99 { 100 if (fieldType != null) 101 return fieldType; 102 try 103 { 104 CtClass clazz = ctField.getType(); 105 fieldType = typeInfo.getFactory().getTypeInfo(clazz); 106 return fieldType; 107 } 108 catch (NotFoundException e) 109 { 110 throw JavassistTypeInfoFactoryImpl.raiseFieldNotFound(getName(), e); 111 } 112 } 113 114 public Object get(Object target) throws Throwable 115 { 116 if (field == null) 117 field = reflectionFactory.createField(ctField); 118 return field.get(target); 119 } 120 121 public Object set(Object target, Object value) throws Throwable 122 { 123 if (field == null) 124 field = reflectionFactory.createField(ctField); 125 field.set(target, value); 126 return null; 127 } 128 129 protected int getHashCode() 130 { 131 return getName().hashCode(); 132 } 133 134 public boolean equals(Object obj) 135 { 136 if (obj == this) 137 return true; 138 if (obj == null || obj instanceof FieldInfo == false) 139 return false; 140 141 FieldInfo other = (FieldInfo) obj; 142 if (getName().equals(other.getName()) == false) 143 return false; 144 return getDeclaringClass().equals(other.getDeclaringClass()); 145 } 146 147 public void toShortString(JBossStringBuilder buffer) 148 { 149 buffer.append(getName()); 150 } 151 152 protected void toString(JBossStringBuilder buffer) 153 { 154 buffer.append("name=").append(getName()); 155 super.toString(buffer); 156 } 157 158 public AnnotationValue[] getAnnotations() 159 { 160 return getAnnotations(ctField); 161 } 162 } 163 | Popular Tags |