KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > ClassNotFoundExceptionParser


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.ba;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25
26 import edu.umd.cs.findbugs.classfile.ClassDescriptor;
27 import edu.umd.cs.findbugs.classfile.ResourceNotFoundException;
28 import edu.umd.cs.findbugs.util.ClassName;
29
30 /**
31  * Parse the detail message in a ClassNotFoundException
32  * to extract the name of the missing class.
33  * Unfortunately, this information is not directly available
34  * from the exception object. So, this class parses the
35  * detail message in several common formats (such as the
36  * format used by BCEL).
37  *
38  * @author David Hovemeyer
39  */

40 public class ClassNotFoundExceptionParser {
41     // BCEL reports missing classes in this format
42
private static final Pattern JavaDoc BCEL_MISSING_CLASS_PATTERN =
43             Pattern.compile("^.*while looking for class ([^:]*):.*$");
44
45     // edu.umd.cs.findbugs.ba.type.TypeRepository uses this format
46
private static final Pattern JavaDoc TYPE_REPOSITORY_MISSING_CLASS_PATTERN =
47             Pattern.compile("^Class ([^ ]*) cannot be resolved.*$");
48
49     private static final Pattern JavaDoc[] patternList;
50
51     static {
52         ArrayList JavaDoc<Pattern JavaDoc> list = new ArrayList JavaDoc<Pattern JavaDoc>();
53         list.add(BCEL_MISSING_CLASS_PATTERN);
54         list.add(TYPE_REPOSITORY_MISSING_CLASS_PATTERN);
55
56         patternList = list.toArray(new Pattern JavaDoc[list.size()]);
57     }
58
59     /**
60      * Get the name of the missing class from a ClassNotFoundException.
61      *
62      * @param ex the ClassNotFoundException
63      * @return the name of the missing class, or null if we
64      * couldn't figure out the class name
65      */

66     public static String JavaDoc getMissingClassName(ClassNotFoundException JavaDoc ex) {
67         // If the exception has a ResourceNotFoundException as the cause,
68
// then we have an easy answer.
69
Throwable JavaDoc cause = ex.getCause();
70         if (cause instanceof ResourceNotFoundException) {
71             String JavaDoc resourceName = ((ResourceNotFoundException) cause).getResourceName();
72             if (resourceName != null) {
73                 ClassDescriptor classDesc = ClassDescriptor.fromResourceName(resourceName);
74                 return classDesc.toDottedClassName();
75             }
76         }
77         
78         // Try the regular expression patterns to parse the class name
79
// from the exception message.
80
for (Pattern JavaDoc pattern : patternList) {
81             Matcher JavaDoc matcher = pattern.matcher(ex.getMessage());
82             if (matcher.matches())
83                 return matcher.group(1);
84         }
85         return null;
86     }
87
88 }
89
90 // vim:ts=4
91
Popular Tags