KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > HasMethod


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

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 JavaDoc;
27 import java.lang.reflect.Field JavaDoc;
28
29 /**
30  * test for a method
31  */

32 public class HasMethod extends ProjectComponent implements Condition {
33     private String JavaDoc classname;
34     private String JavaDoc method;
35     private String JavaDoc field;
36     private Path classpath;
37     private AntClassLoader loader;
38     private boolean ignoreSystemClasses = false;
39
40
41     /**
42      * Set the classpath to be used when searching for classes and resources.
43      *
44      * @param classpath an Ant Path object containing the search path.
45      */

46     public void setClasspath(Path classpath) {
47         createClasspath().append(classpath);
48     }
49
50     /**
51      * Classpath to be used when searching for classes and resources.
52      *
53      * @return an empty Path instance to be configured by Ant.
54      */

55     public Path createClasspath() {
56         if (this.classpath == null) {
57             this.classpath = new Path(getProject());
58         }
59         return this.classpath.createPath();
60     }
61
62     /**
63      * Set the classpath by reference.
64      *
65      * @param r a Reference to a Path instance to be used as the classpath
66      * value.
67      */

68     public void setClasspathRef(Reference r) {
69         createClasspath().setRefid(r);
70     }
71
72     /**
73      * Set the classname attribute.
74      * @param classname the name of the class to check.
75      */

76     public void setClassname(String JavaDoc classname) {
77         this.classname = classname;
78     }
79
80     /**
81      * Set the name of the method.
82      * @param method the name of the method to check.
83      */

84     public void setMethod(String JavaDoc method) {
85         this.method = method;
86     }
87
88     /**
89      * Set the name of the field.
90      * @param field the name of the field to check.
91      */

92     public void setField(String JavaDoc field) {
93         this.field = field;
94     }
95
96     /**
97      * Set whether to ignore system classes when looking for the class.
98      * @param ignoreSystemClasses a <code>boolean</code> value.
99      */

100     public void setIgnoreSystemClasses(boolean ignoreSystemClasses) {
101         this.ignoreSystemClasses = ignoreSystemClasses;
102     }
103
104     /**
105      * Check if a given class can be loaded.
106      */

107     private Class JavaDoc loadClass(String JavaDoc 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 JavaDoc se) {
117                         // class found but restricted name; this is
118
// actually the case we're looking for in JDK 1.3+,
119
// so catch the exception and return
120
return null;
121                     }
122                 } else {
123                     return null;
124                 }
125             } else if (loader != null) {
126                 return loader.loadClass(classname);
127             } else {
128                 ClassLoader JavaDoc l = this.getClass().getClassLoader();
129                 // Can return null to represent the bootstrap class loader.
130
// see API docs of Class.getClassLoader.
131
if (l != null) {
132                     return Class.forName(classname, true, l);
133                 } else {
134                     return Class.forName(classname);
135                 }
136             }
137         } catch (ClassNotFoundException JavaDoc e) {
138             throw new BuildException("class \"" + classname + "\" was not found");
139         } catch (NoClassDefFoundError JavaDoc e) {
140             throw new BuildException("Could not load dependent class \"" + e.getMessage()
141                     + "\" for class \"" + classname + "\"");
142         }
143     }
144
145
146     /** {@inheritDoc}. */
147     public boolean eval() throws BuildException {
148         if (classname == null) {
149             throw new BuildException("No classname defined");
150         }
151         Class JavaDoc 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 JavaDoc clazz) {
162         Field JavaDoc[] fields = clazz.getDeclaredFields();
163         for (int i = 0; i < fields.length; i++) {
164             Field JavaDoc fieldEntry = fields[i];
165             if (fieldEntry.getName().equals(field)) {
166                 return true;
167             }
168         }
169         return false;
170     }
171
172     private boolean isMethodFound(Class JavaDoc clazz) {
173         Method JavaDoc[] methods = clazz.getDeclaredMethods();
174         for (int i = 0; i < methods.length; i++) {
175             Method JavaDoc methodEntry = methods[i];
176             if (methodEntry.getName().equals(method)) {
177                 return true;
178             }
179         }
180         return false;
181     }
182
183 }
184
Popular Tags