01: /*
02: * FindBugs - Find bugs in Java programs
03: * Copyright (C) 2004-2006 University of Maryland
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
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.detect;
21:
22: import org.apache.bcel.classfile.Field;
23: import org.apache.bcel.classfile.JavaClass;
24:
25: import edu.umd.cs.findbugs.BugInstance;
26: import edu.umd.cs.findbugs.BugReporter;
27: import edu.umd.cs.findbugs.Detector;
28: import edu.umd.cs.findbugs.ba.AnalysisContext;
29: import edu.umd.cs.findbugs.ba.ClassContext;
30: import edu.umd.cs.findbugs.ba.JCIPAnnotationDatabase;
31: import edu.umd.cs.findbugs.visitclass.PreorderVisitor;
32:
33: public class CheckImmutableAnnotation extends PreorderVisitor implements
34: Detector {
35:
36: BugReporter bugReporter;
37:
38: public CheckImmutableAnnotation(BugReporter bugReporter) {
39: this .bugReporter = bugReporter;
40: }
41:
42: @Override
43: public void visitJavaClass(JavaClass obj) {
44: JCIPAnnotationDatabase jcipAnotationDatabase = AnalysisContext
45: .currentAnalysisContext().getJCIPAnnotationDatabase();
46: if (jcipAnotationDatabase.hasClassAnnotation(obj.getClassName()
47: .replace('/', '.'), "Immutable"))
48: super .visitJavaClass(obj);
49: }
50:
51: @Override
52: public void visit(Field obj) {
53: if (!obj.isFinal())
54: bugReporter.reportBug(new BugInstance(this ,
55: "JCIP_FIELD_ISNT_FINAL_IN_IMMUTABLE_CLASS",
56: NORMAL_PRIORITY).addClass(this ).addVisitedField(
57: this ));
58: }
59:
60: public void report() {
61:
62: }
63:
64: public void visitClassContext(ClassContext classContext) {
65: classContext.getJavaClass().accept(this);
66:
67: }
68:
69: }
|