make/langtools/tools/crules/AbstractCodingRulesAnalyzer.java
author lmesnik
Wed, 11 Sep 2019 16:25:12 -0700
changeset 58093 50e1d346a126
parent 49158 f62d1d1c2d9c
permissions -rw-r--r--
8230830: No required ResourceMark in src/hotspot/share/prims/jvmtiImpl.cpp:JvmtiSuspendControl::print() Reviewed-by: dholmes, cjplummer, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     1
/*
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     4
 *
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
49158
f62d1d1c2d9c 8194296: Check copyright of files in make/langtools/tools
jjg
parents: 47216
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f62d1d1c2d9c 8194296: Check copyright of files in make/langtools/tools
jjg
parents: 47216
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f62d1d1c2d9c 8194296: Check copyright of files in make/langtools/tools
jjg
parents: 47216
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    10
 *
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    15
 * accompanied this code).
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    16
 *
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    20
 *
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    23
 * questions.
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    24
 */
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    25
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    26
package crules;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    27
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    28
import java.text.MessageFormat;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    29
import java.util.Locale;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    30
import java.util.ResourceBundle;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    31
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    32
import com.sun.source.util.JavacTask;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    33
import com.sun.tools.javac.api.BasicJavacTask;
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    34
import com.sun.tools.javac.code.Symtab;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    35
import com.sun.tools.javac.model.JavacElements;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    36
import com.sun.tools.javac.model.JavacTypes;
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    37
import com.sun.tools.javac.tree.JCTree;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    38
import com.sun.tools.javac.tree.TreeScanner;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    39
import com.sun.tools.javac.util.Context;
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    40
import com.sun.tools.javac.util.JCDiagnostic;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    41
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    42
import com.sun.tools.javac.util.Log;
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    43
import com.sun.tools.javac.util.Options;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    44
import com.sun.tools.javac.util.RawDiagnosticFormatter;
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    45
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    46
import static com.sun.source.util.TaskEvent.Kind;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    47
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    48
public abstract class AbstractCodingRulesAnalyzer {
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    49
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    50
    private   final Log log;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    51
    private   final boolean rawDiagnostics;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    52
    private   final JCDiagnostic.Factory diags;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    53
    private   final Options options;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    54
    protected final Messages messages;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    55
    protected final Symtab syms;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    56
    protected final JavacElements elements;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    57
    protected final JavacTypes types;
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    58
    protected TreeScanner treeVisitor;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    59
    protected Kind eventKind;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    60
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    61
    public AbstractCodingRulesAnalyzer(JavacTask task) {
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    62
        BasicJavacTask impl = (BasicJavacTask)task;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    63
        Context context = impl.getContext();
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    64
        log = Log.instance(context);
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    65
        options = Options.instance(context);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    66
        rawDiagnostics = options.isSet("rawDiagnostics");
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    67
        diags = JCDiagnostic.Factory.instance(context);
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    68
        messages = new Messages();
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    69
        syms = Symtab.instance(context);
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    70
        elements = JavacElements.instance(context);
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25848
diff changeset
    71
        types = JavacTypes.instance(context);
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    72
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    73
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    74
    protected class Messages {
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    75
        ResourceBundle bundle;
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    76
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    77
        Messages() {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    78
            String name = getClass().getPackage().getName() + ".resources.crules";
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    79
            bundle = ResourceBundle.getBundle(name, Locale.ENGLISH);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    80
        }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    81
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    82
        public void error(JCTree tree, String code, Object... args) {
25848
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    83
            String msg;
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    84
            if (rawDiagnostics) {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    85
                RawDiagnosticFormatter f = new RawDiagnosticFormatter(options);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    86
                msg = f.formatMessage(diags.create(DiagnosticType.FRAGMENT, log.currentSource(),
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    87
                                                   tree.pos(), code, args), null);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    88
            } else {
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    89
                msg = (code == null) ? (String) args[0] : localize(code, args);
3bc09f4676a9 8043643: Add an crules analyzer avoiding string concatenation in messages of Assert checks.
jlahoda
parents: 17283
diff changeset
    90
            }
17283
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    91
            log.error(tree, "proc.messager", msg.toString());
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    92
        }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    93
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    94
        private String localize(String code, Object... args) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    95
            String msg = bundle.getString(code);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    96
            if (msg == null) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    97
                StringBuilder sb = new StringBuilder();
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    98
                sb.append("message file broken: code=").append(code);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
    99
                if (args.length > 0) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   100
                    sb.append(" arguments={0}");
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   101
                    for (int i = 1; i < args.length; i++) {
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   102
                        sb.append(", {").append(i).append("}");
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   103
                    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   104
                }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   105
                msg = sb.toString();
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   106
            }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   107
            return MessageFormat.format(msg, args);
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   108
        }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   109
    }
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   110
cddfdf252f4a 8010304: javac should detect all mutable implicit static fields in langtools using a plugin
vromero
parents:
diff changeset
   111
}