1 21 package com.db4o.query; 22 23 import java.io.*; 24 import java.lang.reflect.*; 25 26 import com.db4o.*; 27 28 103 public abstract class Predicate implements Serializable { 104 105 108 public final static String PREDICATEMETHOD_NAME="match"; 109 110 static final Class OBJECT_CLASS = Object .class; 111 112 private Class _extentType; 113 private transient Method cachedFilterMethod=null; 114 115 public Predicate() { 116 this(null); 117 } 118 119 public Predicate(Class extentType) { 120 _extentType=extentType; 121 } 122 123 Method getFilterMethod() { 126 if(cachedFilterMethod!=null) { 127 return cachedFilterMethod; 128 } 129 Method[] methods=getClass().getMethods(); 130 Method untypedMethod=null; 131 for (int methodIdx = 0; methodIdx < methods.length; methodIdx++) { 132 Method method=methods[methodIdx]; 133 if (isFilterMethod(method)) { 134 if (!OBJECT_CLASS.equals(method.getParameterTypes()[0])) { 135 cachedFilterMethod=method; 136 return method; 137 } 138 untypedMethod=method; 139 } 140 } 141 if(untypedMethod!=null) { 142 cachedFilterMethod=untypedMethod; 143 return untypedMethod; 144 } 145 throw new IllegalArgumentException ("Invalid predicate."); 146 } 147 148 private boolean isFilterMethod(Method method) { 149 if (method.getParameterTypes().length != 1) { 150 return false; 151 } 152 if (Deploy.csharp) { 153 return method.getName().equalsIgnoreCase(PREDICATEMETHOD_NAME); 154 } 155 return method.getName().equals(PREDICATEMETHOD_NAME); 156 } 157 158 161 public Class extentType() { 162 return (_extentType!=null ? _extentType : getFilterMethod().getParameterTypes()[0]); 163 } 164 165 168 public boolean appliesTo(Object candidate) { 169 try { 170 Method filterMethod=getFilterMethod(); 171 Platform4.setAccessible(filterMethod); 172 Object ret=filterMethod.invoke(this,new Object []{candidate}); 173 return ((Boolean )ret).booleanValue(); 174 } catch (Exception e) { 175 176 179 181 182 return false; 183 } 184 } 185 } 186 | Popular Tags |