/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.errorprone.bugpatterns;

import static com.google.errorprone.BugPattern.Category.JDK;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.fixes.SuggestedFixes.addModifiers;
import static com.google.errorprone.matchers.Description.NO_MATCH;

import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.MethodTree;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import javax.lang.model.element.Modifier;

/** @author cushon@google.com (Liam Miller-Cushon) */
@BugPattern(
  name = "MethodCanBeStatic",
  summary = "Private methods that do not reference the enclosing instance should be static",
  category = JDK,
  severity = SUGGESTION
)
public class MethodCanBeStatic extends BugChecker implements MethodTreeMatcher {

  @Override
  public Description matchMethod(MethodTree tree, VisitorState state) {
    MethodSymbol sym = ASTHelpers.getSymbol(tree);
    if (sym == null) {
      return NO_MATCH;
    }
    if (sym.isStatic() || sym.isConstructor() || sym.getModifiers().contains(Modifier.NATIVE)) {
      return NO_MATCH;
    }
    if (!sym.isPrivate()) {
      // Methods that override other methods, or that are overridden, can't be static.
      // We conservatively warn only for private methods.
      return NO_MATCH;
    }
    if (sym.owner.enclClass().hasOuterInstance()) {
      return NO_MATCH;
    }
    if (CanBeStaticAnalyzer.referencesOuter(tree, sym, state)) {
      return NO_MATCH;
    }
    return describeMatch(tree.getModifiers(), addModifiers(tree, state, Modifier.STATIC));
  }
}

@@ -1424,17 +1424,19 @@
 mary = %22
-P
+A p
 rivate m
@@ -1444,17 +1444,16 @@
 thod
-s
  that do
  not
@@ -1448,16 +1448,18 @@
  that do
+es
  not ref
@@ -1492,14 +1492,11 @@
 nce 
-should
+can
  be 

Weaken the wording for the MethodCanBeStatic suggestion.