01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.tool.codegen.eclipse.util;
21:
22: import java.io.File;
23: import java.net.MalformedURLException;
24: import java.net.URL;
25: import java.net.URLClassLoader;
26: import java.util.List;
27:
28: /**
29: * A utility class for reading/loading classes and
30: * extracting the information.
31: *
32: */
33: public class ClassFileReader {
34:
35: /**
36: * try whether a given class can be loaded from the given location
37: * @param className
38: * @param classPathEntries
39: * @param errorListener
40: * @return
41: */
42: public static boolean tryLoadingClass(String className,
43: String[] classPathEntries, List errorListener) {
44: //make a URL class loader from the entries
45: ClassLoader classLoader;
46:
47: if (classPathEntries.length > 0) {
48: URL[] urls = new URL[classPathEntries.length];
49:
50: try {
51: for (int i = 0; i < classPathEntries.length; i++) {
52: String classPathEntry = classPathEntries[i];
53: //this should be a file(or a URL)
54: if (classPathEntry.startsWith("http://")) {
55: urls[i] = new URL(classPathEntry);
56: } else {
57: urls[i] = new File(classPathEntry).toURL();
58: }
59: }
60: } catch (MalformedURLException e) {
61: if (errorListener != null) {
62: errorListener.add(e);
63: }
64: return false;
65: }
66:
67: classLoader = new URLClassLoader(urls);
68:
69: } else {
70: classLoader = Thread.currentThread()
71: .getContextClassLoader();
72: }
73:
74: //try to load the class with the given name
75:
76: try {
77: Class clazz = classLoader.loadClass(className);
78: clazz.getMethods();
79:
80: } catch (Throwable t) {
81: if (errorListener != null) {
82: errorListener.add(t);
83: }
84: return false;
85: }
86:
87: return true;
88:
89: }
90:
91: }
|