1 18 package org.apache.tools.ant.taskdefs.condition; 19 20 import org.apache.tools.ant.types.Path; 21 import org.apache.tools.ant.types.Reference; 22 import org.apache.tools.ant.AntClassLoader; 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.ProjectComponent; 25 26 import java.lang.reflect.Method ; 27 import java.lang.reflect.Field ; 28 29 32 public class HasMethod extends ProjectComponent implements Condition { 33 private String classname; 34 private String method; 35 private String field; 36 private Path classpath; 37 private AntClassLoader loader; 38 private boolean ignoreSystemClasses = false; 39 40 41 46 public void setClasspath(Path classpath) { 47 createClasspath().append(classpath); 48 } 49 50 55 public Path createClasspath() { 56 if (this.classpath == null) { 57 this.classpath = new Path(getProject()); 58 } 59 return this.classpath.createPath(); 60 } 61 62 68 public void setClasspathRef(Reference r) { 69 createClasspath().setRefid(r); 70 } 71 72 76 public void setClassname(String classname) { 77 this.classname = classname; 78 } 79 80 84 public void setMethod(String method) { 85 this.method = method; 86 } 87 88 92 public void setField(String field) { 93 this.field = field; 94 } 95 96 100 public void setIgnoreSystemClasses(boolean ignoreSystemClasses) { 101 this.ignoreSystemClasses = ignoreSystemClasses; 102 } 103 104 107 private Class loadClass(String classname) { 108 try { 109 if (ignoreSystemClasses) { 110 loader = getProject().createClassLoader(classpath); 111 loader.setParentFirst(false); 112 loader.addJavaLibraries(); 113 if (loader != null) { 114 try { 115 return loader.findClass(classname); 116 } catch (SecurityException se) { 117 return null; 121 } 122 } else { 123 return null; 124 } 125 } else if (loader != null) { 126 return loader.loadClass(classname); 127 } else { 128 ClassLoader l = this.getClass().getClassLoader(); 129 if (l != null) { 132 return Class.forName(classname, true, l); 133 } else { 134 return Class.forName(classname); 135 } 136 } 137 } catch (ClassNotFoundException e) { 138 throw new BuildException("class \"" + classname + "\" was not found"); 139 } catch (NoClassDefFoundError e) { 140 throw new BuildException("Could not load dependent class \"" + e.getMessage() 141 + "\" for class \"" + classname + "\""); 142 } 143 } 144 145 146 147 public boolean eval() throws BuildException { 148 if (classname == null) { 149 throw new BuildException("No classname defined"); 150 } 151 Class clazz = loadClass(classname); 152 if (method != null) { 153 return isMethodFound(clazz); 154 } 155 if (field != null) { 156 return isFieldFound(clazz); 157 } 158 throw new BuildException("Neither method nor field defined"); 159 } 160 161 private boolean isFieldFound(Class clazz) { 162 Field [] fields = clazz.getDeclaredFields(); 163 for (int i = 0; i < fields.length; i++) { 164 Field fieldEntry = fields[i]; 165 if (fieldEntry.getName().equals(field)) { 166 return true; 167 } 168 } 169 return false; 170 } 171 172 private boolean isMethodFound(Class clazz) { 173 Method [] methods = clazz.getDeclaredMethods(); 174 for (int i = 0; i < methods.length; i++) { 175 Method methodEntry = methods[i]; 176 if (methodEntry.getName().equals(method)) { 177 return true; 178 } 179 } 180 return false; 181 } 182 183 } 184 | Popular Tags |