KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > classfile > impl > ClassFactory


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, 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.classfile.impl;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import edu.umd.cs.findbugs.classfile.IAnalysisCache;
26 import edu.umd.cs.findbugs.classfile.IClassFactory;
27 import edu.umd.cs.findbugs.classfile.IClassPath;
28 import edu.umd.cs.findbugs.classfile.IClassPathBuilder;
29 import edu.umd.cs.findbugs.classfile.ICodeBase;
30 import edu.umd.cs.findbugs.classfile.ICodeBaseLocator;
31 import edu.umd.cs.findbugs.classfile.IErrorLogger;
32 import edu.umd.cs.findbugs.classfile.IScannableCodeBase;
33 import edu.umd.cs.findbugs.classfile.ResourceNotFoundException;
34
35 /**
36  * Factory to create codebase/classpath/classfile objects.
37  *
38  * @author David Hovemeyer
39  */

40 public class ClassFactory implements IClassFactory {
41     private static IClassFactory theInstance = new ClassFactory();
42     
43     private ClassFactory() {
44     }
45     
46     public static IClassFactory instance() {
47         return theInstance;
48     }
49     
50     /* (non-Javadoc)
51      * @see edu.umd.cs.findbugs.classfile.impl.IClassFactory#createClassPath()
52      */

53     public IClassPath createClassPath() {
54         return new ClassPathImpl();
55     }
56
57     /* (non-Javadoc)
58      * @see edu.umd.cs.findbugs.classfile.IClassFactory#createClassPathBuilder(edu.umd.cs.findbugs.classfile.IErrorLogger)
59      */

60     public IClassPathBuilder createClassPathBuilder(IErrorLogger errorLogger) {
61         return new ClassPathBuilder(this, errorLogger);
62     }
63     
64     /* (non-Javadoc)
65      * @see edu.umd.cs.findbugs.classfile.impl.IClassFactory#createFilesystemCodeBaseLocator(java.lang.String)
66      */

67     public ICodeBaseLocator createFilesystemCodeBaseLocator(String JavaDoc pathName) {
68         // Attempt to canonicalize the pathname.
69
// It's not fatal if we can't.
70
try {
71             pathName = new File JavaDoc(pathName).getCanonicalPath();
72         } catch (IOException JavaDoc e) {
73             // Ignore
74
}
75         
76         return new FilesystemCodeBaseLocator(pathName);
77     }
78
79     /* (non-Javadoc)
80      * @see edu.umd.cs.findbugs.classfile.IClassFactory#createNestedArchiveCodeBaseLocator(edu.umd.cs.findbugs.classfile.ICodeBase, java.lang.String)
81      */

82     public ICodeBaseLocator createNestedArchiveCodeBaseLocator(ICodeBase parentCodeBase, String JavaDoc path) {
83         return new NestedZipFileCodeBaseLocator(parentCodeBase, path);
84     }
85     
86     static IScannableCodeBase createFilesystemCodeBase(FilesystemCodeBaseLocator codeBaseLocator) throws IOException JavaDoc {
87         String JavaDoc fileName = codeBaseLocator.getPathName();
88         
89         File JavaDoc file = new File JavaDoc(fileName);
90         
91         if (file.isDirectory()) {
92             return new DirectoryCodeBase(codeBaseLocator, file);
93         } else if (fileName.endsWith(".class")) {
94             return new SingleFileCodeBase(codeBaseLocator, fileName);
95         } else {
96             return new ZipFileCodeBase(codeBaseLocator, file);
97         }
98     }
99     
100     static IScannableCodeBase createNestedZipFileCodeBase(
101             NestedZipFileCodeBaseLocator codeBaseLocator)
102             throws ResourceNotFoundException, IOException JavaDoc {
103         return new NestedZipFileCodeBase(codeBaseLocator);
104     }
105     
106     /* (non-Javadoc)
107      * @see edu.umd.cs.findbugs.classfile.IClassFactory#createAnalysisCache(edu.umd.cs.findbugs.classfile.IClassPath)
108      */

109     public IAnalysisCache createAnalysisCache(IClassPath classPath, IErrorLogger errorLogger) {
110         IAnalysisCache analysisCache = new AnalysisCache(classPath, errorLogger);
111         return analysisCache;
112     }
113 }
114
Popular Tags