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