KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > classfile > ReflectionDatabaseFactory


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;
21
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.Modifier JavaDoc;
26
27
28 /**
29  * A generic database factory that tries to create the database
30  * by (in order of preference)
31  *
32  * <ol>
33  * <li>Invoking a static <b>create</b> method </li>
34  * <li>Invoking a no-arg constructor
35  * </ol>
36  *
37  * @author David Hovemeyer
38  */

39 public class ReflectionDatabaseFactory<E> implements IDatabaseFactory<E> {
40     private Class JavaDoc<E> databaseClass;
41     
42     public ReflectionDatabaseFactory(Class JavaDoc<E> databaseClass) {
43         this.databaseClass = databaseClass;
44     }
45
46     /* (non-Javadoc)
47      * @see edu.umd.cs.findbugs.classfile.IDatabaseFactory#createDatabase()
48      */

49     public E createDatabase() throws CheckedAnalysisException {
50         E database;
51         
52         database = createUsingStaticCreateMethod();
53         if (database != null) {
54             return database;
55         }
56         
57         database = createUsingConstructor();
58         if (database != null) {
59             return database;
60         }
61
62         throw new CheckedAnalysisException(
63                 "Could not find a way to create database " + databaseClass.getName());
64     }
65
66     /**
67      * Try to create the database using a static create() method.
68      *
69      * @return the database, or null if there is no static create() method
70      * @throws CheckedAnalysisException
71      */

72     private E createUsingStaticCreateMethod() throws CheckedAnalysisException {
73         Method JavaDoc createMethod;
74         try {
75             createMethod = databaseClass.getMethod("create", new Class JavaDoc[0]);
76         } catch (NoSuchMethodException JavaDoc e) {
77             return null;
78         }
79             
80         if (!Modifier.isStatic(createMethod.getModifiers())) {
81             return null;
82         }
83
84         if (createMethod.getReturnType() != databaseClass) {
85             return null;
86         }
87
88         try {
89             return (E) createMethod.invoke(null, new Object JavaDoc[0]);
90         } catch (InvocationTargetException JavaDoc e) {
91             throw new CheckedAnalysisException("Could not create " + databaseClass.getName(), e);
92         } catch (IllegalAccessException JavaDoc e) {
93             throw new CheckedAnalysisException("Could not create " + databaseClass.getName(), e);
94         }
95     }
96
97     /**
98      * Try to create the database using a no-arg constructor.
99      *
100      * @return the database, or null if there is no no-arg constructor
101      * @throws CheckedAnalysisException
102      */

103     private E createUsingConstructor() throws CheckedAnalysisException {
104         Constructor JavaDoc<E> constructor;
105         try {
106             constructor = databaseClass.getConstructor(new Class JavaDoc[0]);
107         } catch (NoSuchMethodException JavaDoc e) {
108             return null;
109         }
110         
111         try {
112             return constructor.newInstance(new Object JavaDoc[0]);
113         } catch (InstantiationException JavaDoc e) {
114             throw new CheckedAnalysisException("Could not create " + databaseClass.getName(), e);
115         } catch (IllegalAccessException JavaDoc e) {
116             throw new CheckedAnalysisException("Could not create " + databaseClass.getName(), e);
117         } catch (InvocationTargetException JavaDoc e) {
118             throw new CheckedAnalysisException("Could not create " + databaseClass.getName(), e);
119         }
120     }
121
122     /* (non-Javadoc)
123      * @see edu.umd.cs.findbugs.classfile.IDatabaseFactory#registerWith(edu.umd.cs.findbugs.classfile.IAnalysisCache)
124      */

125     public void registerWith(IAnalysisCache analysisCache) {
126         analysisCache.registerDatabaseFactory(databaseClass, this);
127     }
128
129 }
130
Popular Tags