1 27 package ch.ethz.prose.query; 28 29 import java.lang.reflect.Field ; 31 import java.lang.reflect.Method ; 32 33 40 public class ClassSurrogate extends Surrogate implements java.io.Serializable { 41 42 private String className; 43 private FieldSurrogate[] fields; 44 private MethodSurrogate[] methods; 45 private int hashCode; 46 47 51 public ClassSurrogate(Class cls) 52 { 53 if (cls == null) 54 throw new IllegalArgumentException ("class parameter must not be null"); 55 56 hashCode = cls.hashCode(); 57 className = cls.getName(); 58 59 Method [] m = cls.getMethods(); 60 methods = new MethodSurrogate[m.length]; 61 for (int i=0; i < m.length; i++) 62 methods[i] = new MethodSurrogate(m[i]); 63 64 Field [] f = cls.getFields(); 65 fields = new FieldSurrogate[f.length]; 66 for (int i=0; i < f.length; i++) 67 fields[i] = new FieldSurrogate(f[i]); 68 69 70 } 71 72 73 78 public MethodSurrogate[] getMethodSurrogates() 79 { 80 return methods; 81 } 82 83 84 89 public FieldSurrogate[] getFieldSurrogates() 90 { 91 return fields; 92 } 93 94 95 98 public String getName() 99 { 100 return className; 101 } 102 103 104 110 public Object toRealInstance() throws ClassNotFoundException 111 { 112 return Class.forName(className); 113 } 114 115 116 122 public boolean equals(Object o) 123 { 124 if (!(o instanceof ClassSurrogate)) 125 return false; 126 127 ClassSurrogate other = (ClassSurrogate) o; 128 129 if (!this.getName().equals(other.getName())) 130 return false; 131 132 MethodSurrogate[] mt = this.getMethodSurrogates(); 133 MethodSurrogate[] mo = other.getMethodSurrogates(); 134 135 if (mt.length != mo.length) 136 return false; 137 138 for (int i=0; i<mt.length; i++) 139 if (!mt[i].equals(mo[i])) 140 return false; 141 142 FieldSurrogate[] ft = this.getFieldSurrogates(); 143 FieldSurrogate[] fo = other.getFieldSurrogates(); 144 145 if (ft.length != fo.length) 146 return false; 147 148 for (int i=0; i < ft.length; i++) 149 if (!ft[i].equals(fo[i])) 150 return false; 151 152 return true; 153 } 154 155 public int hashCode() 156 { 157 return hashCode; 158 } 159 160 161 164 public String toString() 165 { 166 return className; 167 } 168 } 169 170 171 | Popular Tags |