1 28 29 package com.idaremedia.antx.condition; 30 31 import org.apache.tools.ant.taskdefs.condition.Condition; 32 33 import com.idaremedia.antx.AssertableProjectComponent; 34 import com.idaremedia.antx.FixtureExaminer; 35 import com.idaremedia.antx.helpers.Tk; 36 37 51 52 public class IsReference extends AssertableProjectComponent 53 implements Condition, URIable 54 { 55 59 public IsReference() 60 { 61 } 62 63 64 68 public IsReference(String refid) 69 { 70 setName(refid); 71 } 72 73 74 78 public void setName(String refId) 79 { 80 m_refId = refId; 81 } 82 83 84 87 public final String getName() 88 { 89 return m_refId; 90 } 91 92 93 94 100 public void xsetFromURI(String fragment) 101 { 102 setName(fragment); 103 } 104 105 106 107 113 public void xsetNoExist(boolean noexist) 114 { 115 m_noexist = noexist; 116 } 117 118 119 120 126 public Object getReferencedObject() 127 { 128 require_(getProject()!=null,"getRefObj- in project"); 129 String refid= Tk.resolveString(getProject(),getName()); 130 Object ref = null; 131 if (refid!=null) { 132 ref = FixtureExaminer.trueReference(getProject(),refid); 133 if (ref==FixtureExaminer.IGNORED_REFERENCE) { 134 ref = null; 135 } 136 } 137 return ref; 138 } 139 140 141 145 public void setKindOf(boolean kindOf) 146 { 147 m_kindOf = kindOf; 148 } 149 150 151 155 public final boolean isKindOf() 156 { 157 return m_kindOf; 158 } 159 160 161 165 public void setClassName(String clazzname) 166 { 167 m_requiredClass = clazzname; 168 } 169 170 171 174 public final void setClass(String clazzname) 175 { 176 setClassName(clazzname); 177 } 178 179 180 186 public final String getClassName() 187 { 188 return m_requiredClass; 189 } 190 191 192 193 199 public boolean eval() 200 { 201 verifyInProject_("eval"); 202 203 boolean result = false; 204 Object object = getReferencedObject(); 205 206 if (object!=null) { 207 if (!m_noexist) { 208 final String classname = getClassName(); 209 if (classname!=null) { 210 if (object==FixtureExaminer.PROBLEM_REFERENCE) { 211 result= false; 212 } else { 213 result= object.getClass().getName().equals(classname); 214 if (result==false && isKindOf()) { 215 result = isCompatible(object,classname); 216 } 217 } 218 } else { 219 result= true; } 221 } 222 } else if (m_noexist) { 223 result = true; 224 } 225 226 return result; 227 } 228 229 230 231 240 private boolean isCompatible(Object object, String classname) 241 { 242 boolean compatible = false; 243 ClassLoader cl = object.getClass().getClassLoader(); 244 if (cl!=null) { 245 try { 246 Class c = cl.loadClass(classname); 247 compatible = c.isInstance(object); 248 } catch(ClassNotFoundException cnfx) { 249 250 } 251 } else { 252 try { 253 Class c = Class.forName(classname); 254 compatible = c.isInstance(object); 255 }catch(ClassNotFoundException cnfx) { 256 257 } 258 } 259 return compatible; 260 } 261 262 263 private String m_refId; private boolean m_kindOf; private String m_requiredClass; private boolean m_noexist; 267 } 268 269 270 | Popular Tags |