diff --git a/src/com/google/javascript/jscomp/CommandLineRunner.java b/src/com/google/javascript/jscomp/CommandLineRunner.java index e00cab2938a..d7ecd325834 100644 --- a/src/com/google/javascript/jscomp/CommandLineRunner.java +++ b/src/com/google/javascript/jscomp/CommandLineRunner.java @@ -131,9 +131,9 @@ private static class Flags { // Some clients run a few copies of the compiler through CommandLineRunner // on parallel threads (thankfully, with the same flags), // so the access to these lists should be synchronized. - private static List> guardLevels = + private static final List> guardLevels = Collections.synchronizedList(new ArrayList>()); - private static List> mixedJsSources = + private static final List> mixedJsSources = Collections.synchronizedList(new ArrayList>()); @Option( @@ -741,7 +741,7 @@ private static class Flags { private List arguments = new ArrayList<>(); private final CmdLineParser parser; - private static final Map COMPILATION_LEVEL_MAP = + private static final ImmutableMap COMPILATION_LEVEL_MAP = ImmutableMap.of( "WHITESPACE_ONLY", CompilationLevel.WHITESPACE_ONLY, @@ -940,7 +940,7 @@ private void printStringLineWrapped(String input, OutputStreamWriter outputStrea " " + input.substring(foundMatch ? endIndex + 1 : endIndex), outputStream); } - private void printShortUsageAfterErrors(PrintStream ps) { + private static void printShortUsageAfterErrors(PrintStream ps) { ps.print("Sample usage: "); ps.println("--compilation_level (-O) VAL --externs VAL --js VAL" + " --js_output_file VAL" @@ -1047,10 +1047,8 @@ private ImmutableMap splitPipeParts(Iterable input, // Our own option parser to be backwards-compatible. // It needs to be public because of the crazy reflection that args4j does. public static class BooleanOptionHandler extends OptionHandler { - private static final Set TRUES = - ImmutableSet.of("true", "on", "yes", "1"); - private static final Set FALSES = - ImmutableSet.of("false", "off", "no", "0"); + private static final ImmutableSet TRUES = ImmutableSet.of("true", "on", "yes", "1"); + private static final ImmutableSet FALSES = ImmutableSet.of("false", "off", "no", "0"); public BooleanOptionHandler( CmdLineParser parser, OptionDef option, @@ -1400,8 +1398,7 @@ private void initConfigFromFlags(String[] args, PrintStream out, PrintStream err if (flags.outputWrapperFile != null && !flags.outputWrapperFile.isEmpty()) { try { - flags.outputWrapper = Files.toString( - new File(flags.outputWrapperFile), UTF_8); + flags.outputWrapper = Files.asCharSource(new File(flags.outputWrapperFile), UTF_8).read(); } catch (Exception e) { reportError("ERROR - invalid output_wrapper_file specified."); } @@ -1422,7 +1419,7 @@ private void initConfigFromFlags(String[] args, PrintStream out, PrintStream err } if (errors) { - flags.printShortUsageAfterErrors(errorStream); + Flags.printShortUsageAfterErrors(errorStream); } else if (flags.displayHelp) { flags.printUsage(out); } else if (flags.version) { @@ -1735,7 +1732,7 @@ protected List createExterns(CompilerOptions options) throws IOExcep } } - private ImmutableList loadConformanceConfigs(List configPaths) { + private static ImmutableList loadConformanceConfigs(List configPaths) { ImmutableList.Builder configs = ImmutableList.builder(); @@ -1752,7 +1749,7 @@ private ImmutableList loadConformanceConfigs(List con private static ConformanceConfig loadConformanceConfig(String configFile) throws IOException { - String textProto = Files.toString(new File(configFile), UTF_8); + String textProto = Files.asCharSource(new File(configFile), UTF_8).read(); ConformanceConfig.Builder builder = ConformanceConfig.newBuilder(); diff --git a/src/com/google/javascript/jscomp/JSModule.java b/src/com/google/javascript/jscomp/JSModule.java index 2d627279a5a..0fa39e7a33d 100644 --- a/src/com/google/javascript/jscomp/JSModule.java +++ b/src/com/google/javascript/jscomp/JSModule.java @@ -181,8 +181,7 @@ public Set getAllDependencies() { while (!stack.isEmpty()) { JSModule module = stack.pop(); List moduleDeps = module.deps; - for (int i = 0; i < moduleDeps.size(); i++) { - JSModule dep = moduleDeps.get(i); + for (JSModule dep : moduleDeps) { if (allDeps.add(dep)) { stack.push(dep); } diff --git a/src/com/google/javascript/jscomp/JSModuleGraph.java b/src/com/google/javascript/jscomp/JSModuleGraph.java index 7a892cd9624..41a3aaa51c1 100644 --- a/src/com/google/javascript/jscomp/JSModuleGraph.java +++ b/src/com/google/javascript/jscomp/JSModuleGraph.java @@ -53,13 +53,13 @@ */ public final class JSModuleGraph { - private List modules; + private final List modules; /** - * Lists of modules at each depth. modulesByDepth.get(3) is a - * list of the modules at depth 3, for example. + * Lists of modules at each depth. modulesByDepth.get(3) is a list of the modules at + * depth 3, for example. */ - private List> modulesByDepth; + private final List> modulesByDepth; /** * dependencyMap is a cache of dependencies that makes the dependsOn function faster. Each map diff --git a/src/com/google/javascript/jscomp/JsAst.java b/src/com/google/javascript/jscomp/JsAst.java index 171c92809a6..aa07109ae3d 100644 --- a/src/com/google/javascript/jscomp/JsAst.java +++ b/src/com/google/javascript/jscomp/JsAst.java @@ -35,9 +35,9 @@ public class JsAst implements SourceAst { private static final long serialVersionUID = 1L; - private transient InputId inputId; + private final transient InputId inputId; private transient SourceFile sourceFile; - private String fileName; + private final String fileName; private Node root; private FeatureSet features; @@ -119,7 +119,7 @@ public static class ParseResult implements Serializable { private static class RecordingReporterProxy implements ErrorReporter { final ArrayList errors = new ArrayList<>(); final ArrayList warnings = new ArrayList<>(); - private ErrorReporter delegateReporter; + private final ErrorReporter delegateReporter; RecordingReporterProxy(ErrorReporter delegateReporter) { this.delegateReporter = delegateReporter; diff --git a/src/com/google/javascript/jscomp/JsMessage.java b/src/com/google/javascript/jscomp/JsMessage.java index 355f7dafc8f..cbf4842612e 100644 --- a/src/com/google/javascript/jscomp/JsMessage.java +++ b/src/com/google/javascript/jscomp/JsMessage.java @@ -277,9 +277,8 @@ public String toString() { @Override public boolean equals(Object o) { - return o == this || - o instanceof PlaceholderReference && - name.equals(((PlaceholderReference) o).name); + return o == this + || (o instanceof PlaceholderReference && name.equals(((PlaceholderReference) o).name)); } @Override @@ -317,8 +316,8 @@ private static String getExternalMessageId(String identifier) { private String desc; private boolean hidden; - private List parts = new LinkedList<>(); - private Set placeholders = new HashSet<>(); + private final List parts = new LinkedList<>(); + private final Set placeholders = new HashSet<>(); private String sourceName; diff --git a/src/com/google/javascript/jscomp/LightweightMessageFormatter.java b/src/com/google/javascript/jscomp/LightweightMessageFormatter.java index 47ed923587f..55dae7c0131 100644 --- a/src/com/google/javascript/jscomp/LightweightMessageFormatter.java +++ b/src/com/google/javascript/jscomp/LightweightMessageFormatter.java @@ -30,7 +30,7 @@ * */ public final class LightweightMessageFormatter extends AbstractMessageFormatter { - private SourceExcerpt excerpt; + private final SourceExcerpt excerpt; private static final ExcerptFormatter excerptFormatter = new LineNumberingFormatter(); private boolean includeLocation = true; diff --git a/src/com/google/javascript/jscomp/NameAnonymousFunctions.java b/src/com/google/javascript/jscomp/NameAnonymousFunctions.java index 21c0a5dfc84..dca4d26b678 100644 --- a/src/com/google/javascript/jscomp/NameAnonymousFunctions.java +++ b/src/com/google/javascript/jscomp/NameAnonymousFunctions.java @@ -17,7 +17,6 @@ package com.google.javascript.jscomp; import com.google.javascript.rhino.Node; - import java.util.logging.Logger; /** @@ -65,7 +64,7 @@ public void process(Node externs, Node root) { */ private class AnonymousFunctionNamer implements AnonymousFunctionNamingCallback.FunctionNamer { - private NodeNameExtractor nameExtractor; + private final NodeNameExtractor nameExtractor; AnonymousFunctionNamer() { this.nameExtractor = new NodeNameExtractor(DELIMITER); diff --git a/src/com/google/javascript/jscomp/NameAnonymousFunctionsMapped.java b/src/com/google/javascript/jscomp/NameAnonymousFunctionsMapped.java index d8ab3644e6d..76f89478702 100644 --- a/src/com/google/javascript/jscomp/NameAnonymousFunctionsMapped.java +++ b/src/com/google/javascript/jscomp/NameAnonymousFunctionsMapped.java @@ -42,8 +42,8 @@ */ class NameAnonymousFunctionsMapped implements CompilerPass { - private static Logger logger = Logger.getLogger( - NameAnonymousFunctionsMapped.class.getName()); + private static final Logger logger = + Logger.getLogger(NameAnonymousFunctionsMapped.class.getName()); static final char PREFIX = '$'; static final String PREFIX_STRING = "$"; diff --git a/src/com/google/javascript/jscomp/ReferenceCollectingCallback.java b/src/com/google/javascript/jscomp/ReferenceCollectingCallback.java index bb434c0bd9e..67ba371c49f 100644 --- a/src/com/google/javascript/jscomp/ReferenceCollectingCallback.java +++ b/src/com/google/javascript/jscomp/ReferenceCollectingCallback.java @@ -21,6 +21,7 @@ import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; import com.google.javascript.jscomp.NodeTraversal.ScopedCallback; import com.google.javascript.rhino.InputId; import com.google.javascript.rhino.Node; @@ -294,7 +295,7 @@ private static T pop(List list) { } private static T peek(List list) { - return list.get(list.size() - 1); + return Iterables.getLast(list); } /** @@ -592,9 +593,16 @@ public String toString() { */ public static final class Reference implements StaticRef { - private static final Set DECLARATION_PARENTS = - ImmutableSet.of(Token.VAR, Token.LET, Token.CONST, Token.PARAM_LIST, - Token.FUNCTION, Token.CLASS, Token.CATCH, Token.REST); + private static final ImmutableSet DECLARATION_PARENTS = + ImmutableSet.of( + Token.VAR, + Token.LET, + Token.CONST, + Token.PARAM_LIST, + Token.FUNCTION, + Token.CLASS, + Token.CATCH, + Token.REST); private final Node nameNode; private final BasicBlock basicBlock; diff --git a/src/com/google/javascript/jscomp/SourceFile.java b/src/com/google/javascript/jscomp/SourceFile.java index 13d9455fd4d..1373c4f44fa 100644 --- a/src/com/google/javascript/jscomp/SourceFile.java +++ b/src/com/google/javascript/jscomp/SourceFile.java @@ -42,6 +42,7 @@ import java.util.Arrays; import java.util.Enumeration; import java.util.List; +import java.util.Objects; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -588,7 +589,7 @@ public synchronized String getCode() throws IOException { if (cachedCode == null) { cachedCode = Files.toString(file, this.getCharset()); - super.setCode(cachedCode, this.getCharset() == StandardCharsets.UTF_8); + super.setCode(cachedCode, Objects.equals(this.getCharset(), StandardCharsets.UTF_8)); // Byte Order Mark can be removed by setCode cachedCode = super.getCode(); } @@ -691,7 +692,7 @@ public synchronized String getCode() throws IOException { // Must close the stream or else the cache won't be cleared. inputStream.close(); - super.setCode(cachedCode, this.getCharset() == StandardCharsets.UTF_8); + super.setCode(cachedCode, Objects.equals(this.getCharset(), StandardCharsets.UTF_8)); // Byte Order Mark can be removed by setCode cachedCode = super.getCode(); } diff --git a/src/com/google/javascript/jscomp/SourceMapInput.java b/src/com/google/javascript/jscomp/SourceMapInput.java index aa7dd45894b..d3ba8a038a5 100644 --- a/src/com/google/javascript/jscomp/SourceMapInput.java +++ b/src/com/google/javascript/jscomp/SourceMapInput.java @@ -18,7 +18,6 @@ import com.google.debugging.sourcemap.SourceMapConsumerV3; import com.google.debugging.sourcemap.SourceMapParseException; - import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; @@ -31,7 +30,7 @@ public final class SourceMapInput { private static final Logger logger = Logger.getLogger(SourceMapInput.class.getName()); - private SourceFile sourceFile; + private final SourceFile sourceFile; private volatile SourceMapConsumerV3 parsedSourceMap = null; public SourceMapInput(SourceFile sourceFile) {