/*
 * Copyright 2019 The Error Prone Authors.
 *
 * 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.common.collect.Streams.stream;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.suppliers.Supplier;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ClassTree;
import com.sun.tools.javac.util.Name;
import java.util.Objects;

/** Makes sure that you are not extending a class that has @AutoValue as an annotation. */
@BugPattern(
    summary = "Do not extend an @AutoValue/@AutoOneOf class in non-generated code.",
    severity = SeverityLevel.ERROR)
public final class ExtendsAutoValue extends BugChecker implements ClassTreeMatcher {

  private static final Supplier<ImmutableSet<Name>> AUTOS =
      VisitorState.memoize(
          s ->
              ImmutableSet.of(
                  s.getName("com.google.auto.value.AutoValue"),
                  s.getName("com.google.auto.value.AutoOneOf")));

  @Override
  public Description matchClass(ClassTree tree, VisitorState state) {
    if (tree.getExtendsClause() == null) {
      // Doesn't extend anything, can't possibly be a violation.
      return Description.NO_MATCH;
    }

    if (!ASTHelpers.annotationsAmong(
            ASTHelpers.getSymbol(tree.getExtendsClause()), AUTOS.get(state), state)
        .isEmpty()) {
      // Violation: one of its superclasses extends AutoValue.
      if (!isInGeneratedCode(state)) {
        return buildDescription(tree).build();
      }
    }

    return Description.NO_MATCH;
  }

  private static boolean isInGeneratedCode(VisitorState state) {
    // Skip generated code. Yes, I know we can do this via a flag but we should always ignore
    // generated code, so to be sure, manually check it.
    return stream(state.getPath())
        .map(ASTHelpers::getSymbol)
        .filter(Objects::nonNull)
        .anyMatch(sym -> !ASTHelpers.getGeneratedBy(sym, state).isEmpty());
  }
}

@@ -650,65 +650,8 @@
 s;%0A%0A
-import static com.google.common.collect.Streams.stream;%0A%0A
 impo
@@ -1127,34 +1127,8 @@
 ame;
-%0Aimport java.util.Objects;
 %0A%0A/*
@@ -2509,127 +2509,8 @@
 turn
- stream(state.getPath())%0A        .map(ASTHelpers::getSymbol)%0A        .filter(Objects::nonNull)%0A        .anyMatch(sym -%3E
  !AS
@@ -2537,13 +2537,8 @@
 dBy(
-sym, 
 stat
@@ -2549,17 +2549,16 @@
 sEmpty()
-)
 ;%0A  %7D%0A%7D%0A

Simplify test for generated code