001: /*
002: * Bytecode Analysis Framework
003: * Copyright (C) 2003-2007 University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package edu.umd.cs.findbugs.classfile.engine.bcel;
020:
021: import org.apache.bcel.classfile.JavaClass;
022: import org.apache.bcel.classfile.Method;
023: import org.apache.bcel.generic.ConstantPoolGen;
024: import org.apache.bcel.generic.MethodGen;
025:
026: import edu.umd.cs.findbugs.ba.AssertionMethods;
027: import edu.umd.cs.findbugs.ba.CFG;
028: import edu.umd.cs.findbugs.ba.CompactLocationNumbering;
029: import edu.umd.cs.findbugs.ba.DepthFirstSearch;
030: import edu.umd.cs.findbugs.ba.ReverseDepthFirstSearch;
031: import edu.umd.cs.findbugs.ba.npe.IsNullValueDataflow;
032: import edu.umd.cs.findbugs.ba.type.ExceptionSetFactory;
033: import edu.umd.cs.findbugs.ba.type.TypeDataflow;
034: import edu.umd.cs.findbugs.ba.vna.LoadedFieldSet;
035: import edu.umd.cs.findbugs.ba.vna.ValueNumberDataflow;
036: import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;
037: import edu.umd.cs.findbugs.classfile.ClassDescriptor;
038: import edu.umd.cs.findbugs.classfile.IAnalysisCache;
039: import edu.umd.cs.findbugs.classfile.IMethodAnalysisEngine;
040: import edu.umd.cs.findbugs.classfile.MethodDescriptor;
041:
042: /**
043: * Abstract factory class for creating analysis objects.
044: */
045: public abstract class AnalysisFactory<Analysis> implements
046: IMethodAnalysisEngine<Analysis> {
047: private String analysisName;
048: private Class<Analysis> analysisClass;
049:
050: /**
051: * Constructor.
052: *
053: * @param analysisName name of the analysis factory: for diagnostics/debugging
054: */
055: public AnalysisFactory(String analysisName,
056: Class<Analysis> analysisClass) {
057: this .analysisName = analysisName;
058: this .analysisClass = analysisClass;
059: }
060:
061: @Override
062: public String toString() {
063: return analysisName + " : " + analysisClass.getName();
064: }
065:
066: /* ----------------------------------------------------------------------
067: * IAnalysisEngine methods
068: * ---------------------------------------------------------------------- */
069:
070: /* (non-Javadoc)
071: * @see edu.umd.cs.findbugs.classfile.IAnalysisEngine#registerWith(edu.umd.cs.findbugs.classfile.IAnalysisCache)
072: */
073: public void registerWith(IAnalysisCache analysisCache) {
074: analysisCache.registerMethodAnalysisEngine(analysisClass, this );
075: }
076:
077: private static final Object NULL_ANALYSIS_RESULT = new Object();
078:
079: /* ----------------------------------------------------------------------
080: * Helper methods to get required analysis objects.
081: * ---------------------------------------------------------------------- */
082:
083: protected CFG getCFG(IAnalysisCache analysisCache,
084: MethodDescriptor methodDescriptor)
085: throws CheckedAnalysisException {
086: return analysisCache.getMethodAnalysis(CFG.class,
087: methodDescriptor);
088: }
089:
090: protected DepthFirstSearch getDepthFirstSearch(
091: IAnalysisCache analysisCache,
092: MethodDescriptor methodDescriptor)
093: throws CheckedAnalysisException {
094: return analysisCache.getMethodAnalysis(DepthFirstSearch.class,
095: methodDescriptor);
096: }
097:
098: protected ConstantPoolGen getConstantPoolGen(
099: IAnalysisCache analysisCache,
100: ClassDescriptor classDescriptor)
101: throws CheckedAnalysisException {
102: return analysisCache.getClassAnalysis(ConstantPoolGen.class,
103: classDescriptor);
104: }
105:
106: protected MethodGen getMethodGen(IAnalysisCache analysisCache,
107: MethodDescriptor methodDescriptor)
108: throws CheckedAnalysisException {
109: return analysisCache.getMethodAnalysis(MethodGen.class,
110: methodDescriptor);
111: }
112:
113: protected CompactLocationNumbering getCompactLocationNumbering(
114: IAnalysisCache analysisCache,
115: MethodDescriptor methodDescriptor)
116: throws CheckedAnalysisException {
117: return analysisCache.getMethodAnalysis(
118: CompactLocationNumbering.class, methodDescriptor);
119: }
120:
121: protected ValueNumberDataflow getValueNumberDataflow(
122: IAnalysisCache analysisCache,
123: MethodDescriptor methodDescriptor)
124: throws CheckedAnalysisException {
125: return analysisCache.getMethodAnalysis(
126: ValueNumberDataflow.class, methodDescriptor);
127: }
128:
129: protected AssertionMethods getAssertionMethods(
130: IAnalysisCache analysisCache,
131: ClassDescriptor classDescriptor)
132: throws CheckedAnalysisException {
133: return analysisCache.getClassAnalysis(AssertionMethods.class,
134: classDescriptor);
135: }
136:
137: protected JavaClass getJavaClass(IAnalysisCache analysisCache,
138: ClassDescriptor classDescriptor)
139: throws CheckedAnalysisException {
140: return analysisCache.getClassAnalysis(JavaClass.class,
141: classDescriptor);
142: }
143:
144: protected Method getMethod(IAnalysisCache analysisCache,
145: MethodDescriptor methodDescriptor)
146: throws CheckedAnalysisException {
147: return analysisCache.getMethodAnalysis(Method.class,
148: methodDescriptor);
149: }
150:
151: protected ReverseDepthFirstSearch getReverseDepthFirstSearch(
152: IAnalysisCache analysisCache,
153: MethodDescriptor methodDescriptor)
154: throws CheckedAnalysisException {
155: return analysisCache.getMethodAnalysis(
156: ReverseDepthFirstSearch.class, methodDescriptor);
157: }
158:
159: protected ExceptionSetFactory getExceptionSetFactory(
160: IAnalysisCache analysisCache,
161: MethodDescriptor methodDescriptor)
162: throws CheckedAnalysisException {
163: return analysisCache.getMethodAnalysis(
164: ExceptionSetFactory.class, methodDescriptor);
165: }
166:
167: protected IsNullValueDataflow getIsNullValueDataflow(
168: IAnalysisCache analysisCache,
169: MethodDescriptor methodDescriptor)
170: throws CheckedAnalysisException {
171: return analysisCache.getMethodAnalysis(
172: IsNullValueDataflow.class, methodDescriptor);
173: }
174:
175: protected TypeDataflow getTypeDataflow(
176: IAnalysisCache analysisCache,
177: MethodDescriptor methodDescriptor)
178: throws CheckedAnalysisException {
179: return analysisCache.getMethodAnalysis(TypeDataflow.class,
180: methodDescriptor);
181: }
182:
183: protected LoadedFieldSet getLoadedFieldSet(
184: IAnalysisCache analysisCache,
185: MethodDescriptor methodDescriptor)
186: throws CheckedAnalysisException {
187: return analysisCache.getMethodAnalysis(LoadedFieldSet.class,
188: methodDescriptor);
189: }
190: }
|