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:
020: package edu.umd.cs.findbugs.ba;
021:
022: import org.apache.bcel.Constants;
023:
024: public class AssignedFieldMap implements Constants {
025: /*
026: private final Map<Method, Set<XField>> assignedFieldSetForMethodMap;
027: private final JavaClass myClass;
028:
029: public AssignedFieldMap(JavaClass jclass) {
030: this.assignedFieldSetForMethodMap = new IdentityHashMap<Method, Set<XField>>();
031: this.myClass = jclass;
032: }
033:
034: public void build() throws ClassNotFoundException {
035: // Build a set of all fields that could be assigned
036: // by methods in this class
037: Set<XField> assignableFieldSet = new HashSet<XField>();
038: scanFields(myClass, assignableFieldSet);
039: JavaClass[] superClassList = myClass.getSuperClasses();
040: if (superClassList != null) {
041: for (JavaClass aSuperClassList : superClassList) {
042: scanFields(aSuperClassList, assignableFieldSet);
043: }
044: }
045:
046: Method[] methodList = myClass.getMethods();
047: for (Method method : methodList) {
048:
049: scanMethod(method, assignableFieldSet);
050: }
051: }
052:
053: public Set<XField> getAssignedFieldSetForMethod(Method method) {
054: Set<XField> set = assignedFieldSetForMethodMap.get(method);
055: if (set == null) {
056: set = new HashSet<XField>();
057: assignedFieldSetForMethodMap.put(method, set);
058: }
059: return set;
060: }
061:
062: private void scanFields(JavaClass jclass, Set<XField> assignableFieldSet) {
063: // JavaClass myClass = classContext.getJavaClass();
064: String myClassName = myClass.getClassName();
065: String myPackageName = myClass.getPackageName();
066:
067: String superClassName = jclass.getClassName();
068: String superPackageName = jclass.getPackageName();
069:
070: Field[] fieldList = jclass.getFields();
071: for (Field field : fieldList) {
072: if (field.isStatic())
073: continue;
074: boolean assignable;
075: if (field.isPublic() || field.isProtected())
076: assignable = true;
077: else if (field.isPrivate())
078: assignable = myClassName.equals(superClassName);
079: else // package protected
080: assignable = myPackageName.equals(superPackageName);
081:
082: if (assignable) {
083: assignableFieldSet.add(new InstanceField(superClassName, field.getName(), field.getSignature(),
084: field.getAccessFlags()));
085: }
086: }
087: }
088:
089: private void scanMethod(Method method, Set<XField> assignableFieldSet) throws ClassNotFoundException {
090: //MethodGen methodGen = classContext.getMethodGen(method);
091:
092: MethodGen methodGen;
093: try {
094: methodGen= Global.getAnalysisCache().getMethodAnalysis(MethodGen.class, BCELUtil.getMethodDescriptor(myClass, method));
095: } catch (CheckedAnalysisException e) {
096: // Should not happen
097: throw new AnalysisException("Could not get MethodGen for Method", e);
098: }
099:
100: if (methodGen == null) return;
101: InstructionList il = methodGen.getInstructionList();
102: InstructionHandle handle = il.getStart();
103:
104: ConstantPoolGen cpg = methodGen.getConstantPool();
105:
106: while (handle != null) {
107: Instruction ins = handle.getInstruction();
108: short opcode = ins.getOpcode();
109: if (opcode == Constants.PUTFIELD) {
110: PUTFIELD putfield = (PUTFIELD) ins;
111:
112: XField instanceField = Hierarchy.findXField(putfield, cpg);
113: if (instanceField != null && assignableFieldSet.contains(instanceField)) {
114: Set<XField> assignedFieldSetForMethod = getAssignedFieldSetForMethod(method);
115: assignedFieldSetForMethod.add(instanceField);
116: }
117: }
118:
119: handle = handle.getNext();
120: }
121: }
122: */
123: }
124:
125: // vim:ts=4
|