KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > detect > CheckImmutableAnnotation


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004-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.detect;
21
22 import edu.umd.cs.findbugs.*;
23 import edu.umd.cs.findbugs.ba.AnalysisContext;
24 import edu.umd.cs.findbugs.ba.ClassContext;
25 import edu.umd.cs.findbugs.ba.JCIPAnnotationDatabase;
26 import edu.umd.cs.findbugs.visitclass.PreorderVisitor;
27
28 import org.apache.bcel.classfile.*;
29
30 public class CheckImmutableAnnotation extends PreorderVisitor implements
31         Detector {
32
33     BugReporter bugReporter;
34
35     public CheckImmutableAnnotation(BugReporter bugReporter) {
36         this.bugReporter = bugReporter;
37     }
38
39     @Override JavaDoc
40     public void visitJavaClass(JavaClass obj) {
41         JCIPAnnotationDatabase jcipAnotationDatabase = AnalysisContext
42                 .currentAnalysisContext().getJCIPAnnotationDatabase();
43         if (jcipAnotationDatabase.hasClassAnnotation(obj.getClassName()
44                 .replace('/', '.'), "Immutable"))
45             super.visitJavaClass(obj);
46     }
47
48     @Override JavaDoc
49     public void visit(Field obj) {
50         if (!obj.isFinal())
51             bugReporter.reportBug(new BugInstance(this, "JCIP_FIELD_ISNT_FINAL_IN_IMMUTABLE_CLASS",
52                     NORMAL_PRIORITY).addClass(this).addVisitedField(this));
53     }
54
55
56     public void report() {
57     
58     }
59
60
61     public void visitClassContext(ClassContext classContext) {
62          classContext.getJavaClass().accept(this);
63         
64     }
65
66 }
67
Popular Tags