1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package crules; 25 26 import java.util.Arrays; 27 import java.util.HashMap; 28 import java.util.List; 29 import java.util.Map; 30 31 import com.sun.tools.javac.code.Kinds; 32 import com.sun.tools.javac.tree.TreeScanner; 33 34 import static com.sun.source.util.TaskEvent.Kind; 35 import static com.sun.tools.javac.code.Flags.*; 36 import static com.sun.tools.javac.tree.JCTree.JCVariableDecl; 37 38 public class MutableFieldsAnalyzer extends AbstractCodingRulesAnalyzer { 39 MutableFieldsAnalyzer()40 public MutableFieldsAnalyzer() { 41 treeVisitor = new MutableFieldsVisitor(); 42 eventKind = Kind.ANALYZE; 43 } 44 getName()45 public String getName() { 46 return "mutable_fields_analyzer"; 47 } 48 ignoreField(String className, String field)49 private boolean ignoreField(String className, String field) { 50 List<String> currentFieldsToIgnore = 51 classFieldsToIgnoreMap.get(className); 52 if (currentFieldsToIgnore != null) { 53 for (String fieldToIgnore : currentFieldsToIgnore) { 54 if (field.equals(fieldToIgnore)) { 55 return true; 56 } 57 } 58 } 59 return false; 60 } 61 62 class MutableFieldsVisitor extends TreeScanner { 63 64 @Override visitVarDef(JCVariableDecl tree)65 public void visitVarDef(JCVariableDecl tree) { 66 boolean isJavacPack = tree.sym.outermostClass().fullname.toString() 67 .contains(packageToCheck); 68 if (isJavacPack && 69 (tree.sym.flags() & SYNTHETIC) == 0 && 70 tree.sym.owner.kind == Kinds.TYP) { 71 if (!ignoreField(tree.sym.owner.flatName().toString(), 72 tree.getName().toString())) { 73 boolean enumClass = (tree.sym.owner.flags() & ENUM) != 0; 74 boolean nonFinalStaticEnumField = 75 (tree.sym.flags() & (ENUM | FINAL)) == 0; 76 boolean nonFinalStaticField = 77 (tree.sym.flags() & STATIC) != 0 && 78 (tree.sym.flags() & FINAL) == 0; 79 if (enumClass ? nonFinalStaticEnumField : nonFinalStaticField) { 80 messages.error(tree, "crules.err.var.must.be.final", tree); 81 } 82 } 83 } 84 super.visitVarDef(tree); 85 } 86 87 } 88 89 private static final String packageToCheck = "com.sun.tools.javac"; 90 91 private static final Map<String, List<String>> classFieldsToIgnoreMap = 92 new HashMap<String, List<String>>(); 93 94 static { 95 classFieldsToIgnoreMap. 96 put("com.sun.tools.javac.util.JCDiagnostic", 97 Arrays.asList("fragmentFormatter")); 98 classFieldsToIgnoreMap. 99 put("com.sun.tools.javac.util.JavacMessages", 100 Arrays.asList("defaultBundle", "defaultMessages")); 101 classFieldsToIgnoreMap. 102 put("com.sun.tools.javac.file.ZipFileIndexCache", 103 Arrays.asList("sharedInstance")); 104 classFieldsToIgnoreMap. 105 put("com.sun.tools.javac.main.JavaCompiler", 106 Arrays.asList("versionRB")); 107 classFieldsToIgnoreMap. 108 put("com.sun.tools.javac.code.Type", 109 Arrays.asList("moreInfo")); 110 classFieldsToIgnoreMap. 111 put("com.sun.tools.javac.util.SharedNameTable", 112 Arrays.asList("freelist")); 113 classFieldsToIgnoreMap. 114 put("com.sun.tools.javac.util.Log", 115 Arrays.asList("useRawMessages")); 116 } 117 118 } 119