Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/make/tools/crules/MutableFieldsAnalyzer.java
32285 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package crules;2425import java.util.Arrays;26import java.util.HashMap;27import java.util.List;28import java.util.Map;2930import com.sun.tools.javac.code.Kinds;31import com.sun.tools.javac.tree.TreeScanner;3233import static com.sun.source.util.TaskEvent.Kind;34import static com.sun.tools.javac.code.Flags.*;35import static com.sun.tools.javac.tree.JCTree.JCVariableDecl;3637public class MutableFieldsAnalyzer extends AbstractCodingRulesAnalyzer {3839public MutableFieldsAnalyzer() {40treeVisitor = new MutableFieldsVisitor();41eventKind = Kind.ANALYZE;42}4344public String getName() {45return "mutable_fields_analyzer";46}4748private boolean ignoreField(String className, String field) {49List<String> currentFieldsToIgnore =50classFieldsToIgnoreMap.get(className);51if (currentFieldsToIgnore != null) {52for (String fieldToIgnore : currentFieldsToIgnore) {53if (field.equals(fieldToIgnore)) {54return true;55}56}57}58return false;59}6061class MutableFieldsVisitor extends TreeScanner {6263@Override64public void visitVarDef(JCVariableDecl tree) {65boolean isJavacPack = tree.sym.outermostClass().fullname.toString()66.contains(packageToCheck);67if (isJavacPack &&68(tree.sym.flags() & SYNTHETIC) == 0 &&69tree.sym.owner.kind == Kinds.TYP) {70if (!ignoreField(tree.sym.owner.flatName().toString(),71tree.getName().toString())) {72boolean enumClass = (tree.sym.owner.flags() & ENUM) != 0;73boolean nonFinalStaticEnumField =74(tree.sym.flags() & (ENUM | FINAL)) == 0;75boolean nonFinalStaticField =76(tree.sym.flags() & STATIC) != 0 &&77(tree.sym.flags() & FINAL) == 0;78if (enumClass ? nonFinalStaticEnumField : nonFinalStaticField) {79messages.error(tree, "crules.err.var.must.be.final", tree);80}81}82}83super.visitVarDef(tree);84}8586}8788private static final String packageToCheck = "com.sun.tools.javac";8990private static final Map<String, List<String>> classFieldsToIgnoreMap =91new HashMap<String, List<String>>();9293static {94classFieldsToIgnoreMap.95put("com.sun.tools.javac.util.JCDiagnostic",96Arrays.asList("fragmentFormatter"));97classFieldsToIgnoreMap.98put("com.sun.tools.javac.util.JavacMessages",99Arrays.asList("defaultBundle", "defaultMessages"));100classFieldsToIgnoreMap.101put("com.sun.tools.javac.file.ZipFileIndexCache",102Arrays.asList("sharedInstance"));103classFieldsToIgnoreMap.104put("com.sun.tools.javac.main.JavaCompiler",105Arrays.asList("versionRB"));106classFieldsToIgnoreMap.107put("com.sun.tools.javac.code.Type",108Arrays.asList("moreInfo"));109classFieldsToIgnoreMap.110put("com.sun.tools.javac.util.SharedNameTable",111Arrays.asList("freelist"));112classFieldsToIgnoreMap.113put("com.sun.tools.javac.util.Log",114Arrays.asList("useRawMessages"));115}116117}118119120