001: /*
002: * FindBugs - Find Bugs in Java programs
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:
020: package edu.umd.cs.findbugs.ba.jsr305;
021:
022: import java.util.HashMap;
023:
024: import org.apache.bcel.generic.ConstantPoolGen;
025:
026: import edu.umd.cs.findbugs.ba.CFG;
027: import edu.umd.cs.findbugs.ba.DepthFirstSearch;
028: import edu.umd.cs.findbugs.ba.XFactory;
029: import edu.umd.cs.findbugs.ba.XMethod;
030: import edu.umd.cs.findbugs.ba.vna.ValueNumberDataflow;
031: import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;
032: import edu.umd.cs.findbugs.classfile.Global;
033: import edu.umd.cs.findbugs.classfile.IAnalysisCache;
034: import edu.umd.cs.findbugs.classfile.MethodDescriptor;
035:
036: /**
037: * Factory to create and cache TypeQualifierDataflow objects
038: * for a particular method.
039: *
040: * @author David Hovemeyer
041: */
042: public abstract class TypeQualifierDataflowFactory<AnalysisType extends TypeQualifierDataflowAnalysis, DataflowType extends TypeQualifierDataflow<AnalysisType>> {
043:
044: private static class DataflowResult<DataflowType> {
045: DataflowType dataflow;
046: CheckedAnalysisException checkedException;
047: RuntimeException runtimeException;
048:
049: DataflowType get() throws CheckedAnalysisException {
050: if (dataflow != null) {
051: return dataflow;
052: }
053: if (checkedException != null) {
054: throw checkedException;
055: }
056: throw runtimeException;
057: }
058: }
059:
060: private HashMap<TypeQualifierValue, DataflowResult<DataflowType>> dataflowMap;
061: private MethodDescriptor methodDescriptor;
062:
063: public TypeQualifierDataflowFactory(
064: MethodDescriptor methodDescriptor) {
065: this .methodDescriptor = methodDescriptor;
066: this .dataflowMap = new HashMap<TypeQualifierValue, DataflowResult<DataflowType>>();
067: }
068:
069: public DataflowType getDataflow(
070: TypeQualifierValue typeQualifierValue)
071: throws CheckedAnalysisException {
072: DataflowResult<DataflowType> result = dataflowMap
073: .get(typeQualifierValue);
074: if (result == null) {
075: result = compute(typeQualifierValue);
076: dataflowMap.put(typeQualifierValue, result);
077: }
078: return result.get();
079: }
080:
081: private DataflowResult<DataflowType> compute(
082: TypeQualifierValue typeQualifierValue) {
083: DataflowResult<DataflowType> result = new DataflowResult<DataflowType>();
084:
085: try {
086: IAnalysisCache analysisCache = Global.getAnalysisCache();
087:
088: DepthFirstSearch dfs = analysisCache.getMethodAnalysis(
089: DepthFirstSearch.class, methodDescriptor);
090: XMethod xmethod = XFactory.createXMethod(methodDescriptor);
091: CFG cfg = analysisCache.getMethodAnalysis(CFG.class,
092: methodDescriptor);
093: ValueNumberDataflow vnaDataflow = analysisCache
094: .getMethodAnalysis(ValueNumberDataflow.class,
095: methodDescriptor);
096: ConstantPoolGen cpg = analysisCache.getClassAnalysis(
097: ConstantPoolGen.class, methodDescriptor
098: .getClassDescriptor());
099:
100: DataflowType dataflow = getDataflow(dfs, xmethod, cfg,
101: vnaDataflow, cpg, analysisCache, methodDescriptor,
102: typeQualifierValue);
103:
104: result.dataflow = dataflow;
105: } catch (CheckedAnalysisException e) {
106: result.checkedException = e;
107: } catch (RuntimeException e) {
108: result.runtimeException = e;
109: }
110:
111: return result;
112: }
113:
114: protected abstract DataflowType getDataflow(DepthFirstSearch dfs,
115: XMethod xmethod, CFG cfg, ValueNumberDataflow vnaDataflow,
116: ConstantPoolGen cpg, IAnalysisCache analysisCache,
117: MethodDescriptor methodDescriptor,
118: TypeQualifierValue typeQualifierValue)
119: throws CheckedAnalysisException;
120:
121: }
|